Skip to content
Snippets Groups Projects
Commit 19e2816e authored by Hangzhi Yu's avatar Hangzhi Yu
Browse files

Adapt to CRUD refactoring

parent f0a822c1
No related branches found
No related tags found
1 merge request!310Resolve "Implement Vue substitution frontend"
......@@ -91,9 +91,9 @@ import {
gqlSubjects,
gqlPersons,
gqlRooms,
createAmendLesson,
patchAmendLesson,
deleteAmendLesson,
createAmendLessons,
patchAmendLessons,
deleteAmendLessons,
} from "./amendLesson.graphql";
export default {
......@@ -140,10 +140,10 @@ export default {
cancelled: this.selectedEvent.meta.cancelled,
comment: this.selectedEvent.meta.comment,
},
gqlCreateMutation: createAmendLesson,
gqlPatchMutation: patchAmendLesson,
gqlCreateMutation: createAmendLessons,
gqlPatchMutation: patchAmendLessons,
deleteEvent: false,
gqlDeleteMutation: deleteAmendLesson,
gqlDeleteMutation: deleteAmendLessons,
};
},
methods: {
......
......@@ -19,9 +19,9 @@ query gqlRooms {
}
}
mutation createAmendLesson($input: CreateLessonEventInput!) {
createAmendLesson(input: $input) {
lessonEvent {
mutation createAmendLessons($input: [BatchCreateLessonEventInput]!) {
createAmendLessons(input: $input) {
items: lessonEvents {
id
amends {
id
......@@ -46,9 +46,9 @@ mutation createAmendLesson($input: CreateLessonEventInput!) {
}
}
mutation patchAmendLesson($input: PatchLessonEventInput!, $id: ID!) {
patchAmendLesson(input: $input, id: $id) {
lessonEvent {
mutation patchAmendLessons($input: [BatchPatchLessonEventInput]!) {
patchAmendLessons(input: $input) {
items: lessonEvents {
id
subject {
id
......@@ -68,8 +68,8 @@ mutation patchAmendLesson($input: PatchLessonEventInput!, $id: ID!) {
}
}
mutation deleteAmendLesson($id: ID!) {
deleteAmendLesson(id: $id) {
ok
mutation deleteAmendLessons($ids: [ID]!) {
deleteAmendLesson(ids: $ids) {
deletionCount
}
}
......@@ -2,10 +2,9 @@ from datetime import timezone
import graphene
from graphene_django import DjangoObjectType
from graphene_django_cud.mutations import DjangoCreateMutation, DjangoPatchMutation
from graphene_django_cud.mutations import DjangoBatchCreateMutation, DjangoBatchDeleteMutation, DjangoBatchPatchMutation
from aleksis.core.models import CalendarEvent, Group, Person, Room
from aleksis.core.schema.base import DeleteMutation
from ..models import LessonEvent
from ..util.chronos_helpers import get_classes, get_rooms, get_teachers
......@@ -56,6 +55,9 @@ class LessonEventType(DjangoObjectType):
"cancelled",
"comment",
)
filter_fields = {
"id": ["exact", "lte", "gte"],
}
class DatetimeTimezoneMixin:
......@@ -77,20 +79,8 @@ class DatetimeTimezoneMixin:
value = value.replace(tzinfo=timezone.utc)
return value
@classmethod
def before_save(cls, root, info, input, obj, patch_obj=False):
# before_save has different signatures for different mutations
# This handles create & patch
# https://graphene-django-cud.readthedocs.io/en/latest/guide/other-hooks.html?highlight=before_save#before-save
if patch_obj:
obj = patch_obj
obj.timezone = obj.amends.timezone
return obj
class AmendLessonCreateMutation(DatetimeTimezoneMixin, DjangoCreateMutation):
class AmendLessonBatchCreateMutation(DatetimeTimezoneMixin, DjangoBatchCreateMutation):
class Meta:
model = LessonEvent
permissions = ("chronos.edit_substitution_rule",)
......@@ -106,17 +96,30 @@ class AmendLessonCreateMutation(DatetimeTimezoneMixin, DjangoCreateMutation):
"comment",
)
@classmethod
def before_save(cls, root, info, input, created_objects):
for obj in created_objects:
obj.timezone = obj.amends.timezone
return created_objects
class AmendLessonPatchMutation(DatetimeTimezoneMixin, DjangoPatchMutation):
class AmendLessonBatchPatchMutation(DatetimeTimezoneMixin, DjangoBatchPatchMutation):
class Meta:
model = LessonEvent
permissions = ("chronos.edit_substitution_rule",)
only_fields = ("subject", "teachers", "groups", "rooms", "cancelled", "comment")
@classmethod
def before_save(cls, root, info, input, updated_objects):
for obj in updated_objects:
obj.timezone = obj.amends.timezone
return updated_objects
class AmendLessonDeleteMutation(DeleteMutation):
klass = LessonEvent
permission_required = "chronos.edit_substitution_rule"
class AmendLessonBatchDeleteMutation(DjangoBatchDeleteMutation):
class Meta:
model = LessonEvent
permissions = ("chronos.delete_substitution_rule",)
class TimetableType(graphene.Enum):
......@@ -139,15 +142,6 @@ class TimetableObjectType(graphene.ObjectType):
return f"{root.type.value}-{root.id}"
class LessonEventType(DjangoObjectType):
class Meta:
model = LessonEvent
fields = ("id",)
filter_fields = {
"id": ["exact", "lte", "gte"],
}
class Query(graphene.ObjectType):
timetable_teachers = graphene.List(TimetablePersonType)
timetable_groups = graphene.List(TimetableGroupType)
......@@ -197,6 +191,6 @@ class Query(graphene.ObjectType):
class Mutation(graphene.ObjectType):
create_amend_lesson = AmendLessonCreateMutation.Field()
patch_amend_lesson = AmendLessonPatchMutation.Field()
delete_amend_lesson = AmendLessonDeleteMutation.Field()
create_amend_lessons = AmendLessonBatchCreateMutation.Field()
patch_amend_lessons = AmendLessonBatchPatchMutation.Field()
delete_amend_lessons = AmendLessonBatchDeleteMutation.Field()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment