|
| 1 | +import typing |
| 2 | + |
| 3 | +import strawberry |
| 4 | +from asgiref.sync import sync_to_async |
| 5 | +from strawberry.file_uploads import Upload |
| 6 | + |
| 7 | +from content.models import Content |
| 8 | +from content.serializers import ( |
| 9 | + ArchiveContentSerializer, |
| 10 | + ContentSerializer, |
| 11 | + TagSerializer, |
| 12 | + UpdateContentSerializer, |
| 13 | +) |
| 14 | +from content.types import ContentType, TagType |
| 15 | +from main.graphql.context import Info |
| 16 | +from utils.strawberry.mutations import ( |
| 17 | + ModelMutation, |
| 18 | + MutationEmptyResponseType, |
| 19 | + MutationResponseType, |
| 20 | + mutation_is_not_valid, |
| 21 | + process_input_data, |
| 22 | +) |
| 23 | + |
| 24 | + |
| 25 | +@strawberry.input |
| 26 | +class FolderInput: |
| 27 | + files: typing.List[Upload] |
| 28 | + |
| 29 | + |
| 30 | +CreateContentMutation = ModelMutation("Content", ContentSerializer) |
| 31 | +UpdateMutation = ModelMutation("UpdateContent", UpdateContentSerializer) |
| 32 | +DeleteContent = ModelMutation("archive", ArchiveContentSerializer) |
| 33 | +CreateTagMutation = ModelMutation("CreateTag", TagSerializer) |
| 34 | + |
| 35 | + |
| 36 | +@strawberry.type |
| 37 | +class PrivateMutation: |
| 38 | + @strawberry.mutation |
| 39 | + async def create_content( |
| 40 | + self, |
| 41 | + data: CreateContentMutation.InputType, # type: ignore[reportInvalidTypeForm] |
| 42 | + info: Info, |
| 43 | + ) -> MutationResponseType[ContentType]: |
| 44 | + return await CreateContentMutation.handle_create_mutation(data, info, None) |
| 45 | + |
| 46 | + @strawberry.mutation |
| 47 | + def read_file(self, file: Upload) -> str: |
| 48 | + return file.read().decode("utf-8") |
| 49 | + |
| 50 | + @strawberry.mutation |
| 51 | + async def update_content_title( |
| 52 | + self, |
| 53 | + id: strawberry.ID, |
| 54 | + data: UpdateMutation.PartialInputType, # type: ignore[reportInvalidTypeForm] |
| 55 | + info: Info, |
| 56 | + ) -> MutationResponseType[ContentType]: |
| 57 | + try: |
| 58 | + instance = await Content.objects.aget(id=id) |
| 59 | + except Content.DoesNotExist: |
| 60 | + return MutationResponseType(ok=False, errors=["Content not found"]) |
| 61 | + serializer = UpdateContentSerializer( |
| 62 | + instance, data=process_input_data(data), context={"request": info.context.request}, partial=True |
| 63 | + ) |
| 64 | + if errors := mutation_is_not_valid(serializer): |
| 65 | + return MutationResponseType(ok=False, errors=errors) |
| 66 | + await sync_to_async(serializer.save)() |
| 67 | + return MutationResponseType() |
| 68 | + |
| 69 | + @strawberry.mutation |
| 70 | + async def archive_content( |
| 71 | + self, id: strawberry.ID, info: Info, data: DeleteContent.PartialInputType # type: ignore[reportInvalidTypeForm] |
| 72 | + ) -> MutationEmptyResponseType: |
| 73 | + try: |
| 74 | + instance = await Content.objects.aget(id=id) |
| 75 | + except Content.DoesNotExist: |
| 76 | + return MutationEmptyResponseType(ok=False, errors=["Content not found"]) |
| 77 | + |
| 78 | + serializer = ArchiveContentSerializer( |
| 79 | + instance, data=process_input_data(data), context={"request": info.context.request}, partial=True |
| 80 | + ) |
| 81 | + |
| 82 | + if errors := mutation_is_not_valid(serializer): |
| 83 | + return MutationEmptyResponseType(ok=False, errors=errors) |
| 84 | + |
| 85 | + await sync_to_async(serializer.save)() |
| 86 | + |
| 87 | + return MutationEmptyResponseType(ok=True) |
| 88 | + |
| 89 | + @strawberry.mutation |
| 90 | + async def create_tag( |
| 91 | + self, |
| 92 | + data: CreateTagMutation.InputType, # type: ignore[reportInvalidTypeForm] |
| 93 | + info: Info, |
| 94 | + ) -> MutationResponseType[TagType]: |
| 95 | + return await CreateTagMutation.handle_create_mutation(data, info, None) |
0 commit comments