etesync-server/etebase_fastapi/member.py

105 lines
3.2 KiB
Python
Raw Normal View History

2020-12-27 19:32:48 +00:00
import typing as t
from django.contrib.auth import get_user_model
2020-12-27 19:47:30 +00:00
from django.db import transaction
2020-12-27 19:32:48 +00:00
from django.db.models import QuerySet
2020-12-28 07:04:45 +00:00
from fastapi import APIRouter, Depends, status
2020-12-27 19:32:48 +00:00
from django_etebase import models
from .authentication import get_authenticated_user
from .msgpack import MsgpackRoute
2020-12-28 08:00:35 +00:00
from .utils import get_object_or_404, BaseModel, permission_responses
2020-12-27 19:32:48 +00:00
from .stoken_handler import filter_by_stoken_and_limit
2020-12-28 07:04:45 +00:00
from .collection import get_collection, verify_collection_admin
2020-12-27 19:32:48 +00:00
User = get_user_model()
2020-12-28 08:00:35 +00:00
member_router = APIRouter(route_class=MsgpackRoute, responses=permission_responses)
2020-12-27 19:32:48 +00:00
default_queryset: QuerySet = models.CollectionMember.objects.all()
def get_queryset(collection: models.Collection = Depends(get_collection)) -> QuerySet:
return default_queryset.filter(collection=collection)
def get_member(username: str, queryset: QuerySet = Depends(get_queryset)) -> QuerySet:
return get_object_or_404(queryset, user__username__iexact=username)
2020-12-27 19:32:48 +00:00
2020-12-27 19:47:30 +00:00
class CollectionMemberModifyAccessLevelIn(BaseModel):
accessLevel: models.AccessLevels
2020-12-27 19:32:48 +00:00
class CollectionMemberOut(BaseModel):
username: str
accessLevel: models.AccessLevels
class Config:
orm_mode = True
@classmethod
def from_orm(cls: t.Type["CollectionMemberOut"], obj: models.CollectionMember) -> "CollectionMemberOut":
return cls(username=obj.user.username, accessLevel=obj.accessLevel)
class MemberListResponse(BaseModel):
data: t.List[CollectionMemberOut]
iterator: t.Optional[str]
done: bool
2020-12-28 07:11:35 +00:00
@member_router.get("/member/", response_model=MemberListResponse, dependencies=[Depends(verify_collection_admin)])
2020-12-27 19:32:48 +00:00
def member_list(
iterator: t.Optional[str] = None,
limit: int = 50,
queryset: QuerySet = Depends(get_queryset),
2020-12-27 19:32:48 +00:00
):
queryset = queryset.order_by("id")
result, new_stoken_obj, done = filter_by_stoken_and_limit(
iterator, limit, queryset, models.CollectionMember.stoken_annotation
)
new_stoken = new_stoken_obj and new_stoken_obj.uid
return MemberListResponse(
2020-12-27 19:32:48 +00:00
data=[CollectionMemberOut.from_orm(item) for item in result],
iterator=new_stoken,
done=done,
)
2020-12-28 07:04:45 +00:00
@member_router.delete(
2020-12-28 07:11:35 +00:00
"/member/{username}/",
2020-12-27 20:56:23 +00:00
status_code=status.HTTP_204_NO_CONTENT,
dependencies=[Depends(verify_collection_admin)],
)
2020-12-27 19:32:48 +00:00
def member_delete(
obj: models.CollectionMember = Depends(get_member),
2020-12-27 19:32:48 +00:00
):
obj.revoke()
2020-12-28 07:04:45 +00:00
@member_router.patch(
2020-12-28 07:11:35 +00:00
"/member/{username}/",
2020-12-27 20:56:23 +00:00
status_code=status.HTTP_204_NO_CONTENT,
dependencies=[Depends(verify_collection_admin)],
)
2020-12-27 19:47:30 +00:00
def member_patch(
data: CollectionMemberModifyAccessLevelIn,
instance: models.CollectionMember = Depends(get_member),
2020-12-27 19:47:30 +00:00
):
with transaction.atomic():
# We only allow updating accessLevel
if instance.accessLevel != data.accessLevel:
instance.stoken = models.Stoken.objects.create()
instance.accessLevel = data.accessLevel
instance.save()
2020-12-28 07:04:45 +00:00
@member_router.post(
2020-12-28 07:11:35 +00:00
"/member/leave/",
2020-12-27 20:56:23 +00:00
status_code=status.HTTP_204_NO_CONTENT,
)
def member_leave(user: User = Depends(get_authenticated_user), collection: models.Collection = Depends(get_collection)):
2020-12-27 19:32:48 +00:00
obj = get_object_or_404(collection.members, user=user)
obj.revoke()