Skip to content

Commit

Permalink
Merge pull request #120 from CentreForDigitalHumanities/feature/item-…
Browse files Browse the repository at this point in the history
…gift-form

Feature/item gift form
  • Loading branch information
XanderVertegaal authored Sep 18, 2024
2 parents ec78c14 + 54317d9 commit 5499b63
Show file tree
Hide file tree
Showing 58 changed files with 1,940 additions and 176 deletions.
2 changes: 0 additions & 2 deletions backend/core/management/commands/create_dev_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
)
import random

from django.contrib.contenttypes.models import ContentType

from .fixtures import (
case_study_names,
gift_names,
Expand Down
7 changes: 7 additions & 0 deletions backend/core/types/input/DescriptionFieldInputType.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from graphene import Enum, InputObjectType
from core.models import SourceMention
from core.types.input.FieldInputType import FieldInputType


class DescriptionFieldInputType(FieldInputType, InputObjectType):
source_mention = Enum.from_enum(SourceMention)()
11 changes: 11 additions & 0 deletions backend/core/types/input/EntityDescriptionInputType.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from graphene import ID, InputObjectType, List, NonNull, String

from core.types.input.NamedInputType import NamedInputType


class EntityDescriptionInputType(NamedInputType, InputObjectType):
book = String()
chapter = String()
page = String()
designators = List(NonNull(String))
categories = List(NonNull(ID))
6 changes: 6 additions & 0 deletions backend/core/types/input/FieldInputType.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from graphene import InputObjectType, Int, String


class FieldInputType(InputObjectType):
certainty = String()
note = String()
6 changes: 6 additions & 0 deletions backend/core/types/input/NamedInputType.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from graphene import InputObjectType, String


class NamedInputType(InputObjectType):
name = String()
description = String()
10 changes: 2 additions & 8 deletions backend/event/mutations/UpdateEpisodeMutation.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
from graphene import ID, Boolean, InputObjectType, List, NonNull, ResolveInfo, String
from django.core.exceptions import ObjectDoesNotExist
from core.types.input.EntityDescriptionInputType import EntityDescriptionInputType
from event.models import Episode
from graphql_app.LettercraftMutation import LettercraftMutation

from graphql_app.types.LettercraftErrorType import LettercraftErrorType


class UpdateEpisodeInput(InputObjectType):
class UpdateEpisodeInput(EntityDescriptionInputType, InputObjectType):
id = ID(required=True)
name = String()
description = String()
book = String()
chapter = String()
page = String()
designators = List(NonNull(String))
summary = String()
categories = List(NonNull(ID))


class UpdateEpisodeMutation(LettercraftMutation):
Expand Down
13 changes: 13 additions & 0 deletions backend/graphql_app/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@
from source.queries import SourceQueries
from space.queries import SpaceQueries
from user.queries import UserQueries

from source.mutations.UpdateOrCreateSourceMutation import UpdateOrCreateSourceMutation
from event.mutations.UpdateEpisodeMutation import UpdateEpisodeMutation
from person.mutations.CreateAgentMutation import CreateAgentMutation
from person.mutations.UpdateAgentMutation import UpdateAgentMutation
from person.mutations.DeleteAgentMutation import DeleteAgentMutation
from letter.mutations.CreateLetterMutation import CreateLetterMutation
from letter.mutations.DeleteLetterMutation import DeleteLetterMutation
from letter.mutations.UpdateLetterMutation import UpdateLetterMutation
from letter.mutations.CreateGiftMutation import CreateGiftMutation
from letter.mutations.DeleteGiftMutation import DeleteGiftMutation
from letter.mutations.UpdateGiftMutation import UpdateGiftMutation
from person.mutations.CreatePersonReferenceMutation import CreatePersonReferenceMutation
from person.mutations.UpdatePersonReferenceMutation import UpdatePersonReferenceMutation
from person.mutations.DeletePersonReferenceMutation import DeletePersonReferenceMutation
Expand All @@ -38,6 +45,12 @@ class Mutation(ObjectType):
create_agent = CreateAgentMutation.Field()
update_agent = UpdateAgentMutation.Field()
delete_agent = DeleteAgentMutation.Field()
create_letter = CreateLetterMutation.Field()
update_letter = UpdateLetterMutation.Field()
delete_letter = DeleteLetterMutation.Field()
create_gift = CreateGiftMutation.Field()
update_gift = UpdateGiftMutation.Field()
delete_gift = DeleteGiftMutation.Field()
create_person_reference = CreatePersonReferenceMutation.Field()
update_person_reference = UpdatePersonReferenceMutation.Field()
delete_person_reference = DeletePersonReferenceMutation.Field()
Expand Down
45 changes: 45 additions & 0 deletions backend/letter/mutations/CreateGiftMutation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from graphene import (
ID,
Field,
InputObjectType,
List,
NonNull,
ResolveInfo,
String,
)

from letter.models import GiftDescription
from letter.types.GiftDescriptionType import GiftDescriptionType
from graphql_app.LettercraftMutation import LettercraftMutation
from graphql_app.types.LettercraftErrorType import LettercraftErrorType
from source.models import Source


class CreateGiftInput(InputObjectType):
name = String(required=True)
source = ID(required=True)


class CreateGiftMutation(LettercraftMutation):
gift = Field(GiftDescriptionType)
errors = List(NonNull(LettercraftErrorType), required=True)

django_model = GiftDescription

class Arguments:
gift_data = CreateGiftInput(required=True)

@classmethod
def mutate(cls, root: None, info: ResolveInfo, gift_data: CreateGiftInput):
try:
source = Source.objects.get(id=getattr(gift_data, "source"))
except Source.DoesNotExist:
error = LettercraftErrorType(field="source", messages=["Source not found."])
return cls(errors=[error]) # type: ignore

letter = GiftDescription.objects.create(
name=getattr(gift_data, "name"),
source=source,
)

return cls(Letter=letter, errors=[]) # type: ignore
45 changes: 45 additions & 0 deletions backend/letter/mutations/CreateLetterMutation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from graphene import (
ID,
Field,
InputObjectType,
List,
NonNull,
ResolveInfo,
String,
)

from letter.models import LetterDescription
from letter.types.LetterDescriptionType import LetterDescriptionType
from graphql_app.LettercraftMutation import LettercraftMutation
from graphql_app.types.LettercraftErrorType import LettercraftErrorType
from source.models import Source


class CreateLetterInput(InputObjectType):
name = String(required=True)
source = ID(required=True)


class CreateLetterMutation(LettercraftMutation):
letter = Field(LetterDescriptionType)
errors = List(NonNull(LettercraftErrorType), required=True)

django_model = LetterDescription

class Arguments:
letter_data = CreateLetterInput(required=True)

@classmethod
def mutate(cls, root: None, info: ResolveInfo, letter_data: CreateLetterInput):
try:
source = Source.objects.get(id=getattr(letter_data, "source"))
except Source.DoesNotExist:
error = LettercraftErrorType(field="source", messages=["Source not found."])
return cls(errors=[error]) # type: ignore

letter = LetterDescription.objects.create(
name=getattr(letter_data, "name"),
source=source,
)

return cls(Letter=letter, errors=[]) # type: ignore
26 changes: 26 additions & 0 deletions backend/letter/mutations/DeleteGiftMutation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from graphene import ID, Boolean, List, Mutation, NonNull, ResolveInfo

from letter.models import GiftDescription
from letter.types.GiftDescriptionType import GiftDescriptionType
from graphql_app.types.LettercraftErrorType import LettercraftErrorType


class DeleteGiftMutation(Mutation):
ok = Boolean(required=True)
errors = List(NonNull(LettercraftErrorType), required=True)

class Arguments:
id = ID(required=True)

@classmethod
def mutate(cls, root: None, info: ResolveInfo, id: str):
try:
gift = GiftDescriptionType.get_queryset(
GiftDescription.objects, info
).get(id=id)
except GiftDescription.DoesNotExist:
error = LettercraftErrorType(field="id", messages=["Gift not found."])
return cls(ok=False, errors=[error]) # type: ignore

gift.delete()
return cls(ok=True, errors=[]) # type: ignore
26 changes: 26 additions & 0 deletions backend/letter/mutations/DeleteLetterMutation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from graphene import ID, Boolean, List, Mutation, NonNull, ResolveInfo

from letter.models import LetterDescription
from letter.types.LetterDescriptionType import LetterDescriptionType
from graphql_app.types.LettercraftErrorType import LettercraftErrorType


class DeleteLetterMutation(Mutation):
ok = Boolean(required=True)
errors = List(NonNull(LettercraftErrorType), required=True)

class Arguments:
id = ID(required=True)

@classmethod
def mutate(cls, root: None, info: ResolveInfo, id: str):
try:
letter = LetterDescriptionType.get_queryset(
LetterDescription.objects, info
).get(id=id)
except LetterDescription.DoesNotExist:
error = LettercraftErrorType(field="id", messages=["Letter not found."])
return cls(ok=False, errors=[error]) # type: ignore

letter.delete()
return cls(ok=True, errors=[]) # type: ignore
55 changes: 55 additions & 0 deletions backend/letter/mutations/UpdateGiftMutation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from graphene import ID, Boolean, InputObjectType, List, NonNull, ResolveInfo
from django.core.exceptions import ObjectDoesNotExist
from core.types.input.DescriptionFieldInputType import DescriptionFieldInputType
from core.types.input.EntityDescriptionInputType import EntityDescriptionInputType
from letter.models import GiftDescription
from graphql_app.LettercraftMutation import LettercraftMutation

from graphql_app.types.LettercraftErrorType import LettercraftErrorType


class GiftCategorisationInput(DescriptionFieldInputType, InputObjectType):
id = ID()
category = ID(required=True)


class UpdateGiftInput(EntityDescriptionInputType, InputObjectType):
id = ID(required=True)
categorisations = List(NonNull(GiftCategorisationInput))


class UpdateGiftMutation(LettercraftMutation):
ok = Boolean(required=True)
errors = List(NonNull(LettercraftErrorType), required=True)

django_model = GiftDescription

class Arguments:
gift_data = UpdateGiftInput(required=True)

@classmethod
def mutate(cls, root: None, info: ResolveInfo, gift_data: UpdateGiftInput):
try:
retrieved_object = cls.get_or_create_object(info, gift_data)
except ObjectDoesNotExist as e:
error = LettercraftErrorType(field="id", messages=[str(e)])
return cls(ok=False, errors=[error]) # type: ignore

gift: GiftDescription = retrieved_object.object # type: ignore

try:
cls.mutate_object(gift_data, gift, info, ["categorisations"])
except ObjectDoesNotExist as field:
error = LettercraftErrorType(
field=str(field), messages=["Related object cannot be found."]
)
return cls(ok=False, errors=[error]) # type: ignore

# TODO: resolve categorisations

user = info.context.user
gift.contributors.add(user)

gift.save()

return cls(ok=True, errors=[]) # type: ignore
55 changes: 55 additions & 0 deletions backend/letter/mutations/UpdateLetterMutation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from graphene import ID, Boolean, InputObjectType, List, NonNull, ResolveInfo
from django.core.exceptions import ObjectDoesNotExist
from core.types.input.DescriptionFieldInputType import DescriptionFieldInputType
from core.types.input.EntityDescriptionInputType import EntityDescriptionInputType
from letter.models import LetterDescription
from graphql_app.LettercraftMutation import LettercraftMutation

from graphql_app.types.LettercraftErrorType import LettercraftErrorType


class LetterCategorisationInput(DescriptionFieldInputType, InputObjectType):
id = ID()
category = ID(required=True)


class UpdateLetterInput(EntityDescriptionInputType, InputObjectType):
id = ID(required=True)
categorisations = List(NonNull(LetterCategorisationInput))


class UpdateLetterMutation(LettercraftMutation):
ok = Boolean(required=True)
errors = List(NonNull(LettercraftErrorType), required=True)

django_model = LetterDescription

class Arguments:
letter_data = UpdateLetterInput(required=True)

@classmethod
def mutate(cls, root: None, info: ResolveInfo, letter_data: UpdateLetterInput):
try:
retrieved_object = cls.get_or_create_object(info, letter_data)
except ObjectDoesNotExist as e:
error = LettercraftErrorType(field="id", messages=[str(e)])
return cls(ok=False, errors=[error]) # type: ignore

letter: LetterDescription = retrieved_object.object # type: ignore

try:
cls.mutate_object(letter_data, letter, info, ["categorisations"])
except ObjectDoesNotExist as field:
error = LettercraftErrorType(
field=str(field), messages=["Related object cannot be found."]
)
return cls(ok=False, errors=[error]) # type: ignore

# TODO: resolve categorisations

user = info.context.user
letter.contributors.add(user)

letter.save()

return cls(ok=True, errors=[]) # type: ignore
Loading

0 comments on commit 5499b63

Please sign in to comment.