From 9debd7c84541d5db1a354b674004be43e15dc34e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laura=20Kl=C3=BCnder?= Date: Wed, 4 Dec 2024 11:58:20 +0100 Subject: [PATCH] add missing api endpoint for LabelSettings and use new_serialize --- src/c3nav/mapdata/api/mapdata.py | 8 ++++++-- src/c3nav/mapdata/models/locations.py | 24 ++---------------------- src/c3nav/mapdata/schemas/model_base.py | 21 +++++++++++++++------ 3 files changed, 23 insertions(+), 30 deletions(-) diff --git a/src/c3nav/mapdata/api/mapdata.py b/src/c3nav/mapdata/api/mapdata.py index 9e01fd61..403802fb 100644 --- a/src/c3nav/mapdata/api/mapdata.py +++ b/src/c3nav/mapdata/api/mapdata.py @@ -15,10 +15,10 @@ from c3nav.mapdata.models import (Area, Building, Door, Hole, Level, LocationGro from c3nav.mapdata.models.access import AccessRestriction, AccessRestrictionGroup from c3nav.mapdata.models.geometry.space import (POI, Column, CrossDescription, LeaveDescription, LineObstacle, Obstacle, Ramp) -from c3nav.mapdata.models.locations import DynamicLocation +from c3nav.mapdata.models.locations import DynamicLocation, LabelSettings from c3nav.mapdata.schemas.filters import (ByCategoryFilter, ByGroupFilter, ByOnTopOfFilter, FilterSchema, LevelGeometryFilter, SpaceGeometryFilter, BySpaceFilter, ByOverlayFilter) -from c3nav.mapdata.schemas.model_base import schema_description +from c3nav.mapdata.schemas.model_base import schema_description, LabelSettingsSchema from c3nav.mapdata.schemas.models import (AccessRestrictionGroupSchema, AccessRestrictionSchema, AreaSchema, BuildingSchema, ColumnSchema, CrossDescriptionSchema, DoorSchema, DynamicLocationSchema, HoleSchema, LeaveDescriptionSchema, LevelSchema, @@ -207,6 +207,10 @@ mapdata_endpoints: dict[str, list[MapdataEndpoint]] = { model=DynamicLocation, schema=DynamicLocationSchema, ), + MapdataEndpoint( + model=LabelSettings, + schema=LabelSettingsSchema, + ), MapdataEndpoint( model=DataOverlay, schema=DataOverlaySchema, diff --git a/src/c3nav/mapdata/models/locations.py b/src/c3nav/mapdata/models/locations.py index f7236653..59601383 100644 --- a/src/c3nav/mapdata/models/locations.py +++ b/src/c3nav/mapdata/models/locations.py @@ -464,6 +464,8 @@ class LocationRedirect(LocationSlug): class LabelSettings(SerializableMixin, models.Model): + new_serialize = True + title = I18nField(_('Title'), plural_name='titles', fallback_any=True) min_zoom = models.DecimalField(_('min zoom'), max_digits=3, decimal_places=1, default=-10, validators=[MinValueValidator(Decimal('-10')), @@ -475,17 +477,6 @@ class LabelSettings(SerializableMixin, models.Model): validators=[MinValueValidator(12), MaxValueValidator(30)]) - def _serialize(self, detailed=True, **kwargs): - result = super()._serialize(detailed=detailed, **kwargs) - if detailed: - result['titles'] = self.titles - if self.min_zoom > -10: - result['min_zoom'] = self.min_zoom - if self.max_zoom < 10: - result['max_zoom'] = self.max_zoom - result['font_size'] = self.font_size - return result - class Meta: verbose_name = _('Label Settings') verbose_name_plural = _('Label Settings') @@ -529,17 +520,6 @@ class DynamicLocation(CustomLocationProxyMixin, SpecificLocation, models.Model): verbose_name_plural = _('Dynamic locations') default_related_name = 'dynamiclocations' - def _serialize(self, **kwargs): - """custom_location = self.get_custom_location() - print(custom_location) - result = {} if custom_location is None else custom_location.serialize(**kwargs) - super_result = super()._serialize(**kwargs) - super_result['subtitle'] = '%s %s, %s' % (_('(moving)'), result['title'], result['subtitle']) - result.update(super_result)""" - result = super()._serialize(**kwargs) - result['dynamic'] = True - return result - def register_change(self, force=False): pass diff --git a/src/c3nav/mapdata/schemas/model_base.py b/src/c3nav/mapdata/schemas/model_base.py index 536f80bf..c26cac2a 100644 --- a/src/c3nav/mapdata/schemas/model_base.py +++ b/src/c3nav/mapdata/schemas/model_base.py @@ -1,6 +1,6 @@ import math import re -from typing import Annotated, Optional, Union, Literal +from typing import Annotated, Optional, Union, Literal, ClassVar from pydantic import Field as APIField from pydantic import PositiveInt @@ -127,7 +127,7 @@ class LocationSchema(WithAccessRestrictionSchema, TitledSchema, LocationSlugSche } -class LabelSettingsSchema(DjangoModelSchema): # todo: add titles back in here +class LabelSettingsSchema(TitledSchema, DjangoModelSchema): # todo: add titles back in here """ Settings preset for how and when to display a label. Reusable between objects. The title describes the title of this preset, not the displayed label. @@ -149,6 +149,15 @@ class LabelSettingsSchema(DjangoModelSchema): # todo: add titles back in here ) +class EffectiveLabelSettingsSchema(LabelSettingsSchema): + """ + Settings preset for how and when to display a label. + """ + id: ClassVar[None] + title: ClassVar[None] + titles: ClassVar[None] + + class SpecificLocationSchema(LocationSchema): grid_square: Union[ Annotated[NonEmptyStr, APIField(title="grid square", description="grid square(s) that this location is in")], @@ -199,24 +208,24 @@ class SpecificLocationSchema(LocationSchema): title="label settings", description=( schema_description(LabelSettingsSchema) + - "\n\nif not set, label settings of location groups might be used" + "\n\nif not set, label settings of location groups should be used" ) ) effective_label_settings: Union[ - Annotated[LabelSettingsSchema, APIField( + Annotated[EffectiveLabelSettingsSchema, APIField( title="label settings", description="label settings to use", )], Annotated[None, APIField( title="null", - description="label settings from location group will be used, if available" + description="don't display a label" )], ] = APIField( default=None, title="label settings", description=( schema_description(LabelSettingsSchema) + - "\n\nif not set, label settings of location groups might be used" + "\n\neffective label settings to use for this location" ) ) label_override: Union[