You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
86 lines
2.2 KiB
Python
86 lines
2.2 KiB
Python
4 years ago
|
import dataclasses
|
||
|
import typing as t
|
||
4 years ago
|
from typing_extensions import Literal
|
||
4 years ago
|
import msgpack
|
||
4 years ago
|
import base64
|
||
4 years ago
|
|
||
4 years ago
|
from fastapi import status, Query, Depends
|
||
4 years ago
|
from pydantic import BaseModel as PyBaseModel
|
||
4 years ago
|
|
||
4 years ago
|
from django.db.models import Model, QuerySet
|
||
4 years ago
|
from django.core.exceptions import ObjectDoesNotExist
|
||
|
|
||
4 years ago
|
from django_etebase import app_settings
|
||
4 years ago
|
from django_etebase.models import AccessLevels
|
||
4 years ago
|
from myauth.models import UserType, get_typed_user_model
|
||
4 years ago
|
|
||
4 years ago
|
from .exceptions import HttpError, HttpErrorOut
|
||
4 years ago
|
|
||
4 years ago
|
User = get_typed_user_model()
|
||
4 years ago
|
|
||
4 years ago
|
Prefetch = Literal["auto", "medium"]
|
||
4 years ago
|
PrefetchQuery = Query(default="auto")
|
||
|
|
||
|
|
||
4 years ago
|
T = t.TypeVar("T", bound=Model, covariant=True)
|
||
|
|
||
|
|
||
4 years ago
|
class BaseModel(PyBaseModel):
|
||
|
class Config:
|
||
|
json_encoders = {
|
||
|
bytes: lambda x: x,
|
||
|
}
|
||
|
|
||
|
|
||
4 years ago
|
@dataclasses.dataclass
|
||
|
class Context:
|
||
4 years ago
|
user: t.Optional[UserType]
|
||
4 years ago
|
prefetch: t.Optional[Prefetch]
|
||
|
|
||
4 years ago
|
|
||
4 years ago
|
def get_object_or_404(queryset: QuerySet[T], **kwargs) -> T:
|
||
4 years ago
|
try:
|
||
|
return queryset.get(**kwargs)
|
||
|
except ObjectDoesNotExist as e:
|
||
4 years ago
|
raise HttpError("does_not_exist", str(e), status_code=status.HTTP_404_NOT_FOUND)
|
||
4 years ago
|
|
||
|
|
||
|
def is_collection_admin(collection, user):
|
||
|
member = collection.members.filter(user=user).first()
|
||
|
return (member is not None) and (member.accessLevel == AccessLevels.ADMIN)
|
||
4 years ago
|
|
||
|
|
||
4 years ago
|
def msgpack_encode(content) -> bytes:
|
||
4 years ago
|
ret = msgpack.packb(content, use_bin_type=True)
|
||
|
assert ret is not None
|
||
|
return ret
|
||
4 years ago
|
|
||
|
|
||
4 years ago
|
def msgpack_decode(content: bytes):
|
||
4 years ago
|
return msgpack.unpackb(content, raw=False)
|
||
|
|
||
|
|
||
4 years ago
|
def b64encode(value: bytes):
|
||
4 years ago
|
return base64.urlsafe_b64encode(value).decode("ascii").strip("=")
|
||
|
|
||
|
|
||
4 years ago
|
def b64decode(data: str):
|
||
4 years ago
|
data += "=" * ((4 - len(data) % 4) % 4)
|
||
|
return base64.urlsafe_b64decode(data)
|
||
|
|
||
|
|
||
4 years ago
|
def get_user_username_email_kwargs(username: str):
|
||
|
field_name = User.EMAIL_FIELD if "@" in username else User.USERNAME_FIELD
|
||
|
return {field_name + "__iexact": username.lower()}
|
||
|
|
||
|
|
||
4 years ago
|
PERMISSIONS_READ = [Depends(x) for x in app_settings.API_PERMISSIONS_READ]
|
||
|
PERMISSIONS_READWRITE = PERMISSIONS_READ + [Depends(x) for x in app_settings.API_PERMISSIONS_WRITE]
|
||
|
|
||
|
|
||
4 years ago
|
response_model_dict = {"model": HttpErrorOut}
|
||
4 years ago
|
permission_responses: t.Dict[t.Union[int, str], t.Dict[str, t.Any]] = {
|
||
|
401: response_model_dict,
|
||
|
403: response_model_dict,
|
||
|
}
|