2020-12-27 16:54:06 +00:00
|
|
|
import dataclasses
|
|
|
|
import typing as t
|
|
|
|
|
|
|
|
from fastapi import status, Query
|
2020-12-27 16:34:23 +00:00
|
|
|
|
|
|
|
from django.db.models import QuerySet
|
|
|
|
from django.core.exceptions import ObjectDoesNotExist
|
2020-12-27 16:54:06 +00:00
|
|
|
from django.contrib.auth import get_user_model
|
2020-12-27 16:34:23 +00:00
|
|
|
|
2020-12-27 19:08:00 +00:00
|
|
|
from django_etebase.models import AccessLevels
|
|
|
|
|
2020-12-27 16:34:23 +00:00
|
|
|
from .exceptions import ValidationError
|
|
|
|
|
2020-12-27 16:54:06 +00:00
|
|
|
User = get_user_model()
|
|
|
|
|
|
|
|
Prefetch = t.Literal["auto", "medium"]
|
|
|
|
PrefetchQuery = Query(default="auto")
|
|
|
|
|
|
|
|
|
|
|
|
@dataclasses.dataclass
|
|
|
|
class Context:
|
|
|
|
user: t.Optional[User]
|
|
|
|
prefetch: t.Optional[Prefetch]
|
|
|
|
|
2020-12-27 16:34:23 +00:00
|
|
|
|
|
|
|
def get_object_or_404(queryset: QuerySet, **kwargs):
|
|
|
|
try:
|
|
|
|
return queryset.get(**kwargs)
|
|
|
|
except ObjectDoesNotExist as e:
|
|
|
|
raise ValidationError("does_not_exist", str(e), status_code=status.HTTP_404_NOT_FOUND)
|
2020-12-27 19:08:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def is_collection_admin(collection, user):
|
|
|
|
member = collection.members.filter(user=user).first()
|
|
|
|
return (member is not None) and (member.accessLevel == AccessLevels.ADMIN)
|