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
1645f21
commit 6ff6dad
Showing
17 changed files
with
401 additions
and
125 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,10 +1,11 @@ | ||
from .student import sub_route as route_student | ||
from .teacher import sub_route as route_teacher | ||
|
||
from .location import LocationResourceRouter | ||
from .lesson import LessonEventRouter | ||
from .location import router as route_location | ||
from .lesson import router as route_lesson | ||
|
||
__all__ = [ | ||
LocationResourceRouter, | ||
LessonEventRouter, | ||
route_location, | ||
route_student, | ||
route_teacher, | ||
route_lesson, | ||
] |
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,19 +1,100 @@ | ||
from typing import List | ||
|
||
from django.db.models import Model | ||
from django.http import HttpRequest | ||
from django.shortcuts import get_object_or_404 | ||
from ninja import Router | ||
|
||
from skii.endpoint.routers.abstract import RestRouterProducer | ||
from apps.base.schemas import FormInvalidResponseContract | ||
from skii.endpoint.schemas.identifier import IntStrUUID4 | ||
from skii.endpoint.schemas.response import SkiiMsgContract | ||
from skii.platform.models.common import GeoCoordinate | ||
from skii.platform.models.event import LessonEvent | ||
from skii.platform.schemas.event import LessonContract, LessonSaveContract | ||
|
||
|
||
class AutomatedLessonRouter(RestRouterProducer): | ||
class Config(RestRouterProducer.Config): | ||
model: Model = LessonEvent | ||
name: str = "lesson" | ||
operation: List[str] = ["create", "read", "update", "delete", "list"] | ||
tags = ["lesson"] | ||
# Create a django ninja API router dedicated to the student | ||
router = Router(tags=["lesson"]) | ||
|
||
|
||
LessonEventRouter = AutomatedLessonRouter() | ||
RouterContract = LessonContract | ||
RouterSaveContract = LessonSaveContract | ||
RouterModel = LessonEvent | ||
RouterListContract = List[RouterContract] | ||
|
||
__all__ = [LessonEventRouter] | ||
|
||
@router.post( | ||
path="/create/", | ||
response={ | ||
200: RouterContract, | ||
422: FormInvalidResponseContract, | ||
}, | ||
) | ||
def record_create(request: HttpRequest, payload: RouterSaveContract): | ||
record_payload = payload.dict() | ||
record = RouterModel(**record_payload) | ||
record.save() | ||
record.refresh_from_db() | ||
return 200, record | ||
|
||
|
||
@router.get( | ||
path="/read/{pk}/", | ||
response={ | ||
200: RouterContract, | ||
422: FormInvalidResponseContract, | ||
}, | ||
) | ||
def record_read(request: HttpRequest, pk: IntStrUUID4): | ||
return 200, get_object_or_404(RouterModel, pk=pk) | ||
|
||
|
||
@router.post( | ||
path="/update/{pk}/", | ||
response={ | ||
200: RouterContract, | ||
422: FormInvalidResponseContract, | ||
}, | ||
) | ||
def record_update(request: HttpRequest, pk: IntStrUUID4, payload: RouterSaveContract): | ||
record_payload = payload.dict() | ||
if "coordinate" in record_payload: | ||
geo_coordinate = record_payload["coordinate"] | ||
del record_payload["coordinate"] | ||
geo_coordinate_obj, created = GeoCoordinate.objects.update_or_create( | ||
geo_coordinate, **geo_coordinate | ||
) | ||
record_payload["coordinate"] = geo_coordinate_obj | ||
record = get_object_or_404(RouterModel, pk=pk) | ||
for attr, value in record_payload.items(): | ||
setattr(record, attr, value) | ||
record.save() | ||
record.refresh_from_db() | ||
return 200, record | ||
|
||
|
||
@router.get( | ||
path="/delete/{pk}/", | ||
response={ | ||
200: SkiiMsgContract, | ||
422: FormInvalidResponseContract, | ||
}, | ||
) | ||
def record_delete(request: HttpRequest, pk: IntStrUUID4): | ||
qs = RouterModel.objects.all().filter(pk=pk) | ||
if qs.exists(): | ||
qs.delete() | ||
return 200, SkiiMsgContract(message="Record deleted") | ||
|
||
|
||
@router.get( | ||
path="/list/", | ||
response={ | ||
200: RouterListContract, | ||
422: FormInvalidResponseContract, | ||
}, | ||
) | ||
def record_list(request: HttpRequest): | ||
return 200, RouterModel.objects.all() | ||
|
||
|
||
__all__ = [router] |
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,48 +1,107 @@ | ||
from typing import List, Any | ||
from typing import List | ||
|
||
from django.db.models import Model | ||
from ninja import Schema | ||
from django.http import HttpRequest | ||
from django.shortcuts import get_object_or_404 | ||
from ninja import Router | ||
|
||
from skii.endpoint.routers.abstract import RestRouterProducer | ||
from apps.base.schemas import FormInvalidResponseContract | ||
from skii.endpoint.schemas.identifier import IntStrUUID4 | ||
from skii.endpoint.schemas.response import SkiiMsgContract | ||
from skii.platform.models.common import GeoCoordinate | ||
from skii.platform.models.resource import LocationResource | ||
from skii.platform.schemas.common import CountryContract, GeoCoordinateContract | ||
|
||
|
||
class AutomatedLocationRouter(RestRouterProducer): | ||
class Config(RestRouterProducer.Config): | ||
# Model Config | ||
model: Model = LocationResource | ||
name: str = "location" | ||
# Router config | ||
operation: List[str] = ["create", "read", "update", "delete", "list"] | ||
base_class: Schema = IntStrUUID4 | ||
tags = ["lesson"] | ||
# Introspection config | ||
depth: int = 1 | ||
save_depth: int = 0 | ||
# Fields config/tweak | ||
fields: List[str] | None = None | ||
save_fields: List[str] | None = None | ||
exclude_fields: List[str] | None = ["uuid"] | ||
save_exclude_fields: List[str] | None = ["uuid"] + ["created", "last_modified"] | ||
custom_fields: List[tuple[Any, Any, Any]] | None = [ | ||
( | ||
"country", | ||
CountryContract, | ||
CountryContract.parse_obj(dict(flag="", code="", name="")), | ||
), | ||
( | ||
"coordinate", | ||
GeoCoordinateContract, | ||
GeoCoordinateContract.parse_obj( | ||
dict(latitude=25.4536, longitude=70.4457) | ||
), | ||
), | ||
] | ||
save_custom_fields: List[tuple[Any, Any, Any]] | None = None | ||
|
||
|
||
LocationResourceRouter = AutomatedLocationRouter() | ||
|
||
__all__ = [LocationResourceRouter] | ||
from skii.platform.schemas.resource import LocationContract, LocationSaveContract | ||
|
||
|
||
# Create a django ninja API router dedicated to the student | ||
router = Router(tags=["location"]) | ||
|
||
|
||
RouterContract = LocationContract | ||
RouterSaveContract = LocationSaveContract | ||
RouterModel = LocationResource | ||
RouterListContract = List[RouterContract] | ||
|
||
|
||
@router.post( | ||
path="/create/", | ||
response={ | ||
200: RouterContract, | ||
422: FormInvalidResponseContract, | ||
}, | ||
) | ||
def record_create(request: HttpRequest, payload: RouterSaveContract): | ||
record_payload = payload.dict() | ||
if "coordinate" in record_payload: | ||
geo_coordinate = record_payload["coordinate"] | ||
del record_payload["coordinate"] | ||
geo_coordinate_obj, created = GeoCoordinate.objects.update_or_create( | ||
geo_coordinate, **geo_coordinate | ||
) | ||
record_payload["coordinate"] = geo_coordinate_obj | ||
record = RouterModel(**record_payload) | ||
record.save() | ||
record.refresh_from_db() | ||
return 200, record | ||
|
||
|
||
@router.get( | ||
path="/read/{pk}/", | ||
response={ | ||
200: RouterContract, | ||
422: FormInvalidResponseContract, | ||
}, | ||
) | ||
def record_read(request: HttpRequest, pk: IntStrUUID4): | ||
return 200, get_object_or_404(RouterModel, pk=pk) | ||
|
||
|
||
@router.post( | ||
path="/update/{pk}/", | ||
response={ | ||
200: RouterContract, | ||
422: FormInvalidResponseContract, | ||
}, | ||
) | ||
def record_update(request: HttpRequest, pk: IntStrUUID4, payload: RouterSaveContract): | ||
record_payload = payload.dict() | ||
if "coordinate" in record_payload: | ||
geo_coordinate = record_payload["coordinate"] | ||
del record_payload["coordinate"] | ||
geo_coordinate_obj, created = GeoCoordinate.objects.update_or_create( | ||
geo_coordinate, **geo_coordinate | ||
) | ||
record_payload["coordinate"] = geo_coordinate_obj | ||
record = get_object_or_404(RouterModel, pk=pk) | ||
for attr, value in record_payload.items(): | ||
setattr(record, attr, value) | ||
record.save() | ||
record.refresh_from_db() | ||
return 200, record | ||
|
||
|
||
@router.get( | ||
path="/delete/{pk}/", | ||
response={ | ||
200: SkiiMsgContract, | ||
422: FormInvalidResponseContract, | ||
}, | ||
) | ||
def record_delete(request: HttpRequest, pk: IntStrUUID4): | ||
qs = RouterModel.objects.all().filter(pk=pk) | ||
if qs.exists(): | ||
qs.delete() | ||
return 200, SkiiMsgContract(message="Record deleted") | ||
|
||
|
||
@router.get( | ||
path="/list/", | ||
response={ | ||
200: RouterListContract, | ||
422: FormInvalidResponseContract, | ||
}, | ||
) | ||
def record_list(request: HttpRequest): | ||
return 200, RouterModel.objects.all() | ||
|
||
|
||
__all__ = [router] |
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
Oops, something went wrong.