route result should use more schema
This commit is contained in:
parent
602093d67a
commit
f2f67a1cbb
5 changed files with 22 additions and 12 deletions
|
@ -4,7 +4,7 @@ from types import NoneType
|
|||
from typing import Annotated, Any, Literal, Union, ClassVar
|
||||
|
||||
from django.core.exceptions import FieldDoesNotExist
|
||||
from django.db.models import Model, ManyToManyField
|
||||
from django.db.models import Model
|
||||
from django.utils.functional import Promise
|
||||
from ninja import Schema
|
||||
from pydantic import Discriminator
|
||||
|
|
|
@ -651,7 +651,6 @@ class SlimLocationMixin(BaseSchema):
|
|||
groups: ClassVar[None]
|
||||
groups_by_category: ClassVar[None]
|
||||
geometry: ClassVar[None]
|
||||
point: ClassVar[None]
|
||||
|
||||
|
||||
class SlimLevelLocationSchema(SlimLocationMixin, FullLevelLocationSchema):
|
||||
|
|
|
@ -18,6 +18,8 @@ from c3nav.mapdata.api.base import api_stats_clean_location_value
|
|||
from c3nav.mapdata.models.access import AccessPermission
|
||||
from c3nav.mapdata.models.locations import Position
|
||||
from c3nav.mapdata.schemas.model_base import AnyLocationID, Coordinates3D
|
||||
from c3nav.mapdata.schemas.models import SlimLocationSchema, SpaceSchema, LevelSchema, SlimSpaceLocationSchema, \
|
||||
SlimLevelLocationSchema
|
||||
from c3nav.mapdata.utils.cache.stats import increment_cache_key
|
||||
from c3nav.mapdata.utils.locations import visible_locations_for_request
|
||||
from c3nav.routing.exceptions import LocationUnreachable, NoRouteFound, NotYetRoutable
|
||||
|
@ -128,11 +130,17 @@ class RouteItemSchema(BaseSchema):
|
|||
Annotated[None, APIField(title="null", description="no waytype (normal walking)")],
|
||||
] = APIField(None, title="waytype")
|
||||
space: Union[
|
||||
Annotated[dict, APIField(title="space", descripiton="new space that is being entered")],
|
||||
Annotated[
|
||||
SlimSpaceLocationSchema,
|
||||
APIField(title="space", descripiton="new space that is being entered")
|
||||
],
|
||||
Annotated[None, APIField(title="null", description="staying in the same space")],
|
||||
] = APIField(None, description="new space being entered")
|
||||
level: Union[
|
||||
Annotated[dict, APIField(title="level", descripiton="new level that is being entered")],
|
||||
Annotated[
|
||||
SlimLevelLocationSchema,
|
||||
APIField(title="level", descripiton="new level that is being entered")
|
||||
],
|
||||
Annotated[None, APIField(title="null", description="staying in the same level")],
|
||||
] = APIField(None, description="new level being entered")
|
||||
descriptions: list[tuple[
|
||||
|
@ -148,8 +156,8 @@ class RouteItemSchema(BaseSchema):
|
|||
|
||||
|
||||
class RouteSchema(BaseSchema):
|
||||
origin: dict # todo: improve this
|
||||
destination: dict # todo: improve this
|
||||
origin: SlimLocationSchema # todo: is this fine? works? no issues?
|
||||
destination: SlimLocationSchema # todo: is this fine? works? no issues?
|
||||
distance: float
|
||||
duration: int
|
||||
distance_str: NonEmptyStr
|
||||
|
|
|
@ -12,10 +12,11 @@ def describe_location(location, locations):
|
|||
final_location = locations.get(location.pk)
|
||||
if final_location is not None:
|
||||
location = final_location
|
||||
result = location.serialize(include_type=True, detailed=False, simple_geometry=True)
|
||||
if hasattr(location, 'serialize_position'):
|
||||
result.update(location.serialize_position())
|
||||
return result
|
||||
# todo: oh my god this needs to be improved
|
||||
from c3nav.routing.router import BaseRouterProxy
|
||||
if isinstance(location, BaseRouterProxy):
|
||||
location = location.src
|
||||
return location
|
||||
|
||||
|
||||
class Route:
|
||||
|
|
|
@ -27,6 +27,7 @@ from django.views.decorators.cache import cache_control, never_cache
|
|||
from django.views.decorators.clickjacking import xframe_options_exempt
|
||||
from django.views.decorators.http import etag
|
||||
from django.views.i18n import LANGUAGE_QUERY_PARAMETER, set_language
|
||||
from pydantic.type_adapter import TypeAdapter
|
||||
|
||||
from c3nav import __version__ as c3nav_version
|
||||
from c3nav.api.models import Secret
|
||||
|
@ -37,6 +38,7 @@ from c3nav.mapdata.models.access import AccessPermission, AccessPermissionToken
|
|||
from c3nav.mapdata.models.locations import (LocationGroup, LocationRedirect, Position, SpecificLocation,
|
||||
get_position_secret)
|
||||
from c3nav.mapdata.models.report import Report, ReportUpdate
|
||||
from c3nav.mapdata.schemas.models import SlimLocationSchema
|
||||
from c3nav.mapdata.utils.locations import (get_location_by_id_for_request, get_location_by_slug_for_request,
|
||||
levels_by_short_label_for_request)
|
||||
from c3nav.mapdata.utils.user import can_access_editor, get_user_data
|
||||
|
@ -122,9 +124,9 @@ def map_index(request, mode=None, slug=None, slug2=None, details=None, options=N
|
|||
|
||||
state = {
|
||||
'routing': routing,
|
||||
'origin': (origin.serialize(detailed=False, simple_geometry=True, geometry=False)
|
||||
'origin': (TypeAdapter(SlimLocationSchema).validate_python(origin).model_dump()
|
||||
if origin else None),
|
||||
'destination': (destination.serialize(detailed=False, simple_geometry=True, geometry=False)
|
||||
'destination': (TypeAdapter(SlimLocationSchema).validate_python(destination).model_dump()
|
||||
if destination else None),
|
||||
'sidebar': routing or destination is not None,
|
||||
'details': True if details else False,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue