Skip to content

Commit

Permalink
[FIX] provide front widget by backend
Browse files Browse the repository at this point in the history
  • Loading branch information
boot-sandre committed Sep 11, 2023
1 parent 7447e79 commit 3632596
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 24 deletions.
10 changes: 8 additions & 2 deletions apps/skii_school_core/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,18 @@ class Meta:
title = factory.Faker("text")
teacher = factory.SubFactory(TeacherAgentFactory)
start = fuzzy.FuzzyDateTime(
start_dt=datetime.now(tz=UTC) - timedelta(days=25),
start_dt=datetime.now(tz=UTC) - timedelta(hours=2),
end_dt=datetime.now(tz=UTC),
force_year=2023,
force_month=7,
force_day=13
)
stop = fuzzy.FuzzyDateTime(
start_dt=datetime.now(tz=UTC), end_dt=datetime.now(tz=UTC) + timedelta(hours=4)
start_dt=datetime.now(tz=UTC),
end_dt=datetime.now(tz=UTC) + timedelta(hours=4),
force_year=2023,
force_month=7,
force_day=13
)

@factory.post_generation
Expand Down
40 changes: 38 additions & 2 deletions apps/skii_school_core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django_countries.fields import CountryField
from djmoney.models.fields import MoneyField
from django.utils.translation import gettext_lazy as _
from ninja import Schema

from apps.skii_school_core.entities import (
RessourceEntity,
Expand All @@ -18,13 +19,31 @@
CMSUUIDEntity,
GeoCoordinateEntity,
)

from django.contrib.auth import get_user_model


User = get_user_model()


class GantStyleConfigContract(Schema):
background: str
color: str
borderRadius: str


class GanttBarConfig(Schema):
id: str
hasHandles: bool
label: str
style: GantStyleConfigContract


class GantConfigContract(Schema):
start: str
stop: str
ganttBarConfig: GanttBarConfig


##################
# AGENT ENTITIES #
##################
Expand Down Expand Up @@ -145,7 +164,24 @@ class Meta:
def __str__(self) -> str:
start_datetime = self.start.strftime(format="%Y-%m-%d %H:%M:%S")
stop_datetime = self.stop.strftime(format="%Y-%m-%d %H:%M:%S")
return f"{self.pk} [{self.state}] {str(self.title)}: {start_datetime} / {stop_datetime} "
return f"[{self.state}] {self.teacher.user.username}: {start_datetime} / {stop_datetime} "

@property
def gant_config(self) -> GantConfigContract:
return {
"start": self.start.strftime(format="%Y-%m-%d %H:%M:%S"),
"stop": self.stop.strftime(format="%Y-%m-%d %H:%M:%S"),
"ganttBarConfig": {
"id": str(self.uuid),
"hasHandles": True,
"label": self.title,
"style": {
"background": "#e09b69",
"color": "black",
"borderRadius": "20px"
}
}
}


class Location(UUIDLabelEntity, ContentEntity):
Expand Down
25 changes: 22 additions & 3 deletions apps/skii_school_core/routers/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
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.models import Event, TeacherAgent
from apps.skii_school_core.schemas import (
FormErrorsResponseContract,
EventRecordResponse,
EventListResponse,
EventContractShort,
)
from apps.skii_school_core.interfaces import AgendaInterface


UserModel = get_user_model()
Expand All @@ -26,7 +27,7 @@
422: FormErrorsResponseContract,
},
)
def location_record(request: HttpRequest, record_pk: int | str):
def event_record(request: HttpRequest, record_pk: int | str):
obj = get_object_or_404(Event, pk=record_pk)
return dict(
count=int(bool(obj)),
Expand All @@ -35,14 +36,32 @@ def location_record(request: HttpRequest, record_pk: int | str):
)


@route_event.get(
path="/event_list_by_teacher/{teacher_pk}/",
response={
200: EventListResponse,
422: FormErrorsResponseContract,
},
)
def event_list_by_teacher(request: HttpRequest, teacher_pk: int):
event_list = AgendaInterface.list_teacher_event(
agent=TeacherAgent.objects.get(pk=teacher_pk)
)
return dict(
items=list(event_list),
count=event_list.count(),
model=f"{event_list.model._meta.model_name}",
)


@route_event.get(
path="/list/",
response={
200: EventListResponse,
422: FormErrorsResponseContract,
},
)
def location_record_list(request: HttpRequest):
def event_record_list(request: HttpRequest):
qs = Event.objects.all()
return dict(
items=list(qs),
Expand Down
10 changes: 5 additions & 5 deletions apps/skii_school_core/routers/teacher.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from apps.skii_school_core.schemas import (
FormErrorsResponseContract,
StudentRecordResponse,
StudentListResponse,
TeacherListResponse,
StudentContract,
StudentContractShort,
)
Expand Down Expand Up @@ -39,15 +39,15 @@ def fetch_record(request: HttpRequest, record_pk: int):
@route_teacher.get(
path="/list/",
response={
200: StudentListResponse,
200: TeacherListResponse,
422: FormErrorsResponseContract,
},
)
def record_list(request: HttpRequest):
qs = TeacherAgent.objects.all()
agent_count = qs.count()
agent_list = TeacherAgent.objects.all()
agent_count = agent_list.count()
return 200, dict(
items=list(qs), count=agent_count, model=f"{qs.model._meta.model_name}"
items=list(agent_list), count=agent_count, model=f"{agent_list.model._meta.model_name}"
)


Expand Down
29 changes: 17 additions & 12 deletions apps/skii_school_core/schemas.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
from datetime import datetime
from uuid import UUID

from django.db.models import QuerySet
from ninja import Schema, ModelSchema
from django.contrib.auth import get_user_model
from typing import Dict, List, Any
from typing import Dict, List, Any, Iterable

from ninja.orm import create_schema

from apps.skii_school_core.models import (
StudentAgent,
TeacherAgent,
Location,
GeoCoordinate,
Event,
GantConfigContract
)

User = get_user_model()
Expand Down Expand Up @@ -94,14 +101,10 @@ class Config:
model_exclude = ["id"]


class TeacherContractShort(ModelSchema):
class TeacherContractShort(Schema):
id: int
user: UserSchemaShort

class Config:
model = TeacherAgent
model_fields = ["user"]
model_optional_fields = ["id"]


class TeacherRecordResponse(Schema):
model: str
Expand Down Expand Up @@ -164,11 +167,13 @@ class LocationListResponse(Schema):
items: List[LocationContract] = []


class EventContract(ModelSchema):
class Config:
model = Event
model_fields = "__all__"
model_exclude = ["last_modified", "created"]
class EventContract(Schema):
gant_config: GantConfigContract
start: datetime
stop: datetime
teacher: TeacherContract
students: List[StudentContract]
uuid: UUID


class EventListResponse(Schema):
Expand Down

0 comments on commit 3632596

Please sign in to comment.