mirror of
https://github.com/ae-utbm/sith.git
synced 2025-07-11 04:19:25 +00:00
Graph de famille en frontend (#820)
* Remove graphviz and use cytoscape.js instead * Frontend generated graphs * Make installation easier and faster * Better user experience * Family api and improved interface * Fix url history when using 0, improve button selection and reset reverse with reset button * Use klay layout * Add js translations and apply review comments
This commit is contained in:
committed by
GitHub
parent
bf96d8a10c
commit
f624b7c66d
53
core/api.py
53
core/api.py
@ -1,10 +1,19 @@
|
||||
from typing import Annotated
|
||||
|
||||
import annotated_types
|
||||
from django.conf import settings
|
||||
from django.http import HttpResponse
|
||||
from ninja_extra import ControllerBase, api_controller, route
|
||||
from ninja_extra.exceptions import PermissionDenied
|
||||
|
||||
from club.models import Mailing
|
||||
from core.schemas import MarkdownSchema
|
||||
from core.api_permissions import CanView
|
||||
from core.models import User
|
||||
from core.schemas import (
|
||||
FamilyGodfatherSchema,
|
||||
MarkdownSchema,
|
||||
UserFamilySchema,
|
||||
)
|
||||
from core.templatetags.renderer import markdown
|
||||
|
||||
|
||||
@ -27,3 +36,45 @@ class MailingListController(ControllerBase):
|
||||
).prefetch_related("subscriptions")
|
||||
data = "\n".join(m.fetch_format() for m in mailings)
|
||||
return data
|
||||
|
||||
|
||||
DepthValue = Annotated[int, annotated_types.Ge(0), annotated_types.Le(10)]
|
||||
DEFAULT_DEPTH = 4
|
||||
|
||||
|
||||
@api_controller("/family")
|
||||
class FamilyController(ControllerBase):
|
||||
@route.get(
|
||||
"/{user_id}",
|
||||
permissions=[CanView],
|
||||
response=UserFamilySchema,
|
||||
url_name="family_graph",
|
||||
)
|
||||
def get_family_graph(
|
||||
self,
|
||||
user_id: int,
|
||||
godfathers_depth: DepthValue = DEFAULT_DEPTH,
|
||||
godchildren_depth: DepthValue = DEFAULT_DEPTH,
|
||||
):
|
||||
user: User = self.get_object_or_exception(User, pk=user_id)
|
||||
|
||||
relations = user.get_family(godfathers_depth, godchildren_depth)
|
||||
if not relations:
|
||||
# If the user has no relations, return only the user
|
||||
# He is alone in its family, but the family exists nonetheless
|
||||
return {"users": [user], "relationships": []}
|
||||
|
||||
user_ids = {r.from_user_id for r in relations} | {
|
||||
r.to_user_id for r in relations
|
||||
}
|
||||
return {
|
||||
"users": User.objects.filter(id__in=user_ids).distinct(),
|
||||
"relationships": (
|
||||
[
|
||||
FamilyGodfatherSchema(
|
||||
godchild=r.from_user_id, godfather=r.to_user_id
|
||||
)
|
||||
for r in relations
|
||||
]
|
||||
),
|
||||
}
|
||||
|
Reference in New Issue
Block a user