generated from boot-sandre/django-spaninja
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b176f09
commit 7447e79
Showing
4 changed files
with
133 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters