Skip to content

Commit

Permalink
[NEW] Add event api
Browse files Browse the repository at this point in the history
  • Loading branch information
boot-sandre committed Sep 10, 2023
1 parent b176f09 commit 7447e79
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 4 deletions.
4 changes: 3 additions & 1 deletion apps/skii_school_core/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from ninja.security import django_auth
from django.utils.translation import gettext_lazy as _

from apps.skii_school_core.routers import route_student, route_teacher, route_location
from apps.skii_school_core.routers import route_student, route_teacher, route_location, route_event

# Get current package version
from packaging.version import parse as parse_version
Expand Down Expand Up @@ -54,3 +54,5 @@ def custom_validation_errors(
api_skii.add_router(prefix="student", router=route_student)
api_skii.add_router(prefix="teacher", router=route_teacher)
api_skii.add_router(prefix="location", router=route_location)
api_skii.add_router(prefix="event", router=route_event)

8 changes: 7 additions & 1 deletion apps/skii_school_core/routers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
from .student import route_student
from .teacher import route_teacher
from .location import route_location
from .event import route_event


__all__ = [route_teacher, route_student, route_location]
__all__ = [
route_teacher,
route_student,
route_location,
route_event,
]
103 changes: 103 additions & 0 deletions apps/skii_school_core/routers/event.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
from django.shortcuts import get_object_or_404
from ninja import Router
from django.http import HttpRequest
from django.contrib.auth import get_user_model

from apps.skii_school_core.models import Event
from apps.skii_school_core.schemas import (
FormErrorsResponseContract,
EventRecordResponse,
EventListResponse,
EventContractShort,
)


UserModel = get_user_model()


# Create a django ninja API router dedicated to the skii school platform
route_event = Router(tags=["skii", "event"])


@route_event.get(
path="/fetch/{record_pk}/",
response={
200: EventRecordResponse,
422: FormErrorsResponseContract,
},
)
def location_record(request: HttpRequest, record_pk: int | str):
obj = get_object_or_404(Event, pk=record_pk)
return dict(
count=int(bool(obj)),
model=f"{obj._meta.model_name}",
item=obj,
)


@route_event.get(
path="/list/",
response={
200: EventListResponse,
422: FormErrorsResponseContract,
},
)
def location_record_list(request: HttpRequest):
qs = Event.objects.all()
return dict(
items=list(qs),
count=qs.count(),
model=f"{qs.model._meta.model_name}",
)


@route_event.delete(
path="/delete/{record_id}/",
)
def record_delete(request: HttpRequest, record_id: int | str):
qs = Event.objects.all().get(pk=record_id)
qs.delete()
return dict(
message="Success",
)


@route_event.post(
path="/save/{record_id}/",
response={
200: EventRecordResponse,
422: FormErrorsResponseContract,
},
)
def record_save(
request: HttpRequest, record_id: int | str, payload: EventContractShort
):
location_payload = payload.dict()
location_obj = get_object_or_404(Event, pk=record_id)
for attr, value in location_payload.items():
setattr(location_obj, attr, value)
location_obj.save()
location_obj.refresh_from_db()
return dict(
count=int(bool(location_obj)),
model=f"{location_obj._meta.model_name}",
item=location_obj,
)


@route_event.post(
path="/create/",
response={
200: EventRecordResponse,
422: FormErrorsResponseContract,
},
)
def record_create(request: HttpRequest, payload: EventContractShort):
record_payload = payload.dict()
record_obj = Event(**record_payload)
record_obj.save()
return dict(
count=int(bool(record_obj)),
model=f"{record_obj._meta.verbose_name}",
item=record_obj,
)
22 changes: 20 additions & 2 deletions apps/skii_school_core/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,26 @@ class LocationListResponse(Schema):
items: List[LocationContract] = []


class EventContract(Schema):
class EventContract(ModelSchema):
class Config:
model = Event
model_fields = "__all__"
model_exclude = ["last_modified", "created", "country"]
model_exclude = ["last_modified", "created"]


class EventListResponse(Schema):
model: str
count: int
items: List[EventContract] = []


class EventRecordResponse(LocationRecordResponse):
item: EventContract | None


class EventContractShort(ModelSchema):
class Config:
model = Location
model_fields = "__all__"
model_exclude = ["last_modified", "created"]

0 comments on commit 7447e79

Please sign in to comment.