示例#1
0
class TouristicContentSerializer(PicturesSerializerMixin,
                                 PublishableSerializerMixin,
                                 ZoningSerializerMixin,
                                 TranslatedModelSerializer):
    themes = ThemeSerializer(many=True)
    category = TouristicContentCategorySerializer()
    type1 = TouristicContentTypeSerializer(many=True)
    type2 = TouristicContentTypeSerializer(many=True)
    source = RecordSourceSerializer()
    portal = TargetPortalSerializer()
    reservation_system = rest_serializers.Field(
        source='reservation_system.name')

    # Nearby
    touristic_contents = CloseTouristicContentSerializer(
        many=True, source='published_touristic_contents')
    touristic_events = CloseTouristicEventSerializer(
        many=True, source='published_touristic_events')
    treks = trekking_serializers.CloseTrekSerializer(many=True,
                                                     source='published_treks')
    pois = trekking_serializers.ClosePOISerializer(many=True,
                                                   source='published_pois')

    class Meta:
        model = tourism_models.TouristicContent
        geo_field = 'geom'
        fields = ('id', 'description', 'description_teaser', 'category',
                  'themes', 'contact', 'email', 'website', 'practical_info',
                  'type1', 'type2', 'touristic_contents', 'touristic_events',
                  'treks', 'pois', 'source', 'portal', 'approved',
                  'reservation_id', 'reservation_system',) + \
            ZoningSerializerMixin.Meta.fields + \
            PublishableSerializerMixin.Meta.fields + \
            PicturesSerializerMixin.Meta.fields
示例#2
0
class TouristicContentSerializer(PicturesSerializerMixin, PublishableSerializerMixin,
                                 ZoningSerializerMixin, TranslatedModelSerializer):
    themes = ThemeSerializer(many=True)
    category = TouristicContentCategorySerializer()
    type1 = TouristicContentTypeSerializer(many=True)
    type2 = TouristicContentTypeSerializer(many=True)
    source = RecordSourceSerializer(many=True)
    portal = TargetPortalSerializer(many=True)
    reservation_system = rest_serializers.ReadOnlyField(source='reservation_system.name', default="")

    # Nearby
    touristic_contents = CloseTouristicContentSerializer(many=True, source='published_touristic_contents')
    touristic_events = CloseTouristicEventSerializer(many=True, source='published_touristic_events')
    treks = trekking_serializers.CloseTrekSerializer(many=True, source='published_treks')
    pois = trekking_serializers.ClosePOISerializer(many=True, source='published_pois')

    def __init__(self, instance=None, *args, **kwargs):
        super(TouristicContentSerializer, self).__init__(instance, *args, **kwargs)
        if 'geotrek.diving' in settings.INSTALLED_APPS:

            from geotrek.diving.serializers import CloseDiveSerializer

            self.fields['dives'] = CloseDiveSerializer(many=True, source='published_dives')

    class Meta:
        model = tourism_models.TouristicContent
        fields = ('id', 'description', 'description_teaser', 'category',
                  'themes', 'contact', 'email', 'website', 'practical_info',
                  'type1', 'type2', 'touristic_contents', 'touristic_events',
                  'treks', 'pois', 'source', 'portal', 'approved',
                  'reservation_id', 'reservation_system',) + \
            ZoningSerializerMixin.Meta.fields + \
            PublishableSerializerMixin.Meta.fields + \
            PicturesSerializerMixin.Meta.fields
示例#3
0
class DiveSerializer(PicturesSerializerMixin, PublishableSerializerMixin,
                     TranslatedModelSerializer):
    themes = ThemeSerializer(many=True)
    practice = PracticeSerializer()
    difficulty = DifficultySerializer()
    levels = LevelSerializer(many=True)
    source = RecordSourceSerializer(many=True)
    portal = TargetPortalSerializer(many=True)
    category = rest_serializers.SerializerMethodField()
    dives = CloseDiveSerializer(many=True, source='published_dives')
    treks = trekking_serializers.CloseTrekSerializer(many=True,
                                                     source='published_treks')
    pois = trekking_serializers.ClosePOISerializer(many=True,
                                                   source='published_pois')

    def __init__(self, instance=None, *args, **kwargs):
        super(DiveSerializer, self).__init__(instance, *args, **kwargs)
        if 'geotrek.tourism' in settings.INSTALLED_APPS:

            from geotrek.tourism import serializers as tourism_serializers

            self.fields[
                'touristic_contents'] = tourism_serializers.CloseTouristicContentSerializer(
                    many=True, source='published_touristic_contents')
            self.fields[
                'touristic_events'] = tourism_serializers.CloseTouristicEventSerializer(
                    many=True, source='published_touristic_events')

    class Meta:
        model = diving_models.Dive
        geo_field = 'geom'
        fields = (
            'id', 'practice', 'description_teaser', 'description', 'advice',
            'difficulty', 'levels', 'themes', 'owner', 'depth', 'facilities',
            'departure', 'disabled_sport', 'category', 'source', 'portal',
            'eid', 'dives', 'treks', 'pois'
        ) + PublishableSerializerMixin.Meta.fields + PicturesSerializerMixin.Meta.fields

    def get_category(self, obj):
        if settings.SPLIT_DIVES_CATEGORIES_BY_PRACTICE and obj.practice:
            data = {
                'id': obj.prefixed_category_id,
                'label': obj.practice.name,
                'pictogram': obj.practice.get_pictogram_url(),
                'slug': obj.practice.slug,
            }
        else:
            data = {
                'id': obj.category_id_prefix,
                'label': _("Dive"),
                'pictogram': '/static/diving/dive.svg',
                # Translators: This is a slug (without space, accent or special char)
                'slug': _('dive'),
            }
        if settings.SPLIT_DIVES_CATEGORIES_BY_PRACTICE:
            data['order'] = obj.practice and obj.practice.order
        else:
            data['order'] = settings.DIVE_CATEGORY_ORDER
        return data
class TouristicEventSerializer(PicturesSerializerMixin,
                               PublishableSerializerMixin,
                               ZoningSerializerMixin,
                               TranslatedModelSerializer):
    themes = ThemeSerializer(many=True)
    type = TouristicEventTypeSerializer()
    source = RecordSourceSerializer(many=True)
    portal = TargetPortalSerializer(many=True)

    # Nearby
    touristic_contents = CloseTouristicContentSerializer(
        many=True, source='published_touristic_contents')
    touristic_events = CloseTouristicEventSerializer(
        many=True, source='published_touristic_events')
    treks = trekking_serializers.CloseTrekSerializer(many=True,
                                                     source='published_treks')
    pois = trekking_serializers.ClosePOISerializer(many=True,
                                                   source='published_pois')

    # For consistency with touristic contents
    type1 = TouristicEventTypeSerializer(many=True)
    category = rest_serializers.SerializerMethodField()

    def __init__(self, instance=None, *args, **kwargs):
        super(TouristicEventSerializer, self).__init__(instance, *args,
                                                       **kwargs)
        if 'geotrek.diving' in settings.INSTALLED_APPS:
            from geotrek.diving.serializers import CloseDiveSerializer

            self.fields['dives'] = CloseDiveSerializer(
                many=True, source='published_dives')

    class Meta:
        model = tourism_models.TouristicEvent
        geo_field = 'geom'
        fields = ('id', 'description_teaser', 'description', 'themes',
                  'begin_date', 'end_date', 'duration', 'meeting_point',
                  'meeting_time', 'contact', 'email', 'website',
                  'organizer', 'speaker', 'type', 'accessibility',
                  'participant_number', 'booking', 'target_audience',
                  'practical_info', 'touristic_contents', 'touristic_events',
                  'treks', 'pois', 'type1', 'category', 'source', 'portal', 'approved') + \
            ZoningSerializerMixin.Meta.fields + \
            PublishableSerializerMixin.Meta.fields + \
            PicturesSerializerMixin.Meta.fields

    def get_category(self, obj):
        return {
            'id': obj.category_id_prefix,
            'order': settings.TOURISTIC_EVENT_CATEGORY_ORDER,
            'label': obj._meta.verbose_name_plural,
            'type1_label': obj._meta.get_field('type').verbose_name,
            'pictogram': '/static/tourism/touristicevent.svg',
            'slug': _('touristic-event'),
        }
示例#5
0
class SiteSerializer(PublishableSerializerMixin, ZoningSerializerMixin,
                     TranslatedModelSerializer):
    practice = PracticeSerializer()
    structure = StructureSerializer()
    labels = LabelSerializer(many=True)
    themes = ThemeSerializer(many=True)
    portal = TargetPortalSerializer(many=True)
    source = RecordSourceSerializer(many=True)
    information_desks = InformationDeskSerializer(many=True)
    web_links = WebLinkSerializer(many=True)
    type = SiteTypeSerializer()
    children = ReadOnlyField(source='published_children')

    class Meta:
        model = Site
        fields = ('id', 'structure', 'name', 'practice', 'description', 'description_teaser',
                  'ambiance', 'advice', 'period', 'labels', 'themes', 'portal', 'source',
                  'information_desks', 'web_links', 'type', 'parent', 'children', 'eid',
                  'orientation', 'wind') + \
            ZoningSerializerMixin.Meta.fields + \
            PublishableSerializerMixin.Meta.fields
class TrekSerializer(PublishableSerializerMixin, PicturesSerializerMixin,
                     AltimetrySerializerMixin, ZoningSerializerMixin,
                     TranslatedModelSerializer):
    difficulty = DifficultyLevelSerializer()
    route = RouteSerializer()
    networks = NetworkSerializer(many=True)
    themes = ThemeSerializer(many=True)
    practice = PracticeSerializer()
    usages = PracticeSerializer(many=True)  # Rando v1 compat
    accessibilities = AccessibilitySerializer(many=True)
    web_links = WebLinkSerializer(many=True)
    relationships = TrekRelationshipSerializer(
        many=True, source='published_relationships')
    treks = CloseTrekSerializer(many=True, source='published_treks')
    source = RecordSourceSerializer(many=True)
    portal = TargetPortalSerializer(many=True)
    children = rest_serializers.ReadOnlyField(source='children_id')
    parents = rest_serializers.ReadOnlyField(source='parents_id')
    previous = rest_serializers.ReadOnlyField(source='previous_id')
    next = rest_serializers.ReadOnlyField(source='next_id')

    # Idea: use rest-framework-gis
    parking_location = rest_serializers.SerializerMethodField()
    points_reference = rest_serializers.SerializerMethodField()

    gpx = rest_serializers.SerializerMethodField('get_gpx_url')
    kml = rest_serializers.SerializerMethodField('get_kml_url')
    structure = StructureSerializer()

    # For consistency with touristic contents
    type2 = TypeSerializer(source='accessibilities', many=True)
    category = rest_serializers.SerializerMethodField()

    # Method called to retrieve relevant pictures based on settings
    pictures = rest_serializers.SerializerMethodField()

    def __init__(self, instance=None, *args, **kwargs):
        # duplicate each trek for each one of its accessibilities
        if instance and hasattr(
                instance, '__iter__'
        ) and settings.SPLIT_TREKS_CATEGORIES_BY_ACCESSIBILITY:
            treks = []
            for trek in instance:
                treks.append(trek)
                for accessibility in trek.accessibilities.all():
                    clone = copy.copy(trek)
                    clone.accessibility = accessibility
                    treks.append(clone)
            instance = treks

        super(TrekSerializer, self).__init__(instance, *args, **kwargs)

        from geotrek.tourism import serializers as tourism_serializers

        if settings.SPLIT_TREKS_CATEGORIES_BY_PRACTICE:
            del self.fields['practice']
        if settings.SPLIT_TREKS_CATEGORIES_BY_ACCESSIBILITY:
            del self.fields['type2']

        self.fields[
            'information_desks'] = tourism_serializers.InformationDeskSerializer(
                many=True)
        self.fields[
            'touristic_contents'] = tourism_serializers.CloseTouristicContentSerializer(
                many=True, source='published_touristic_contents')
        self.fields[
            'touristic_events'] = tourism_serializers.CloseTouristicEventSerializer(
                many=True, source='published_touristic_events')

    class Meta:
        model = trekking_models.Trek
        id_field = 'id'  # By default on this model it's topo_object = OneToOneField(parent_link=True)
        geo_field = 'geom'
        fields = ('id', 'departure', 'arrival', 'duration', 'duration_pretty',
                  'description', 'description_teaser', 'networks', 'advice',
                  'ambiance', 'difficulty', 'information_desks', 'themes',
                  'practice', 'accessibilities', 'usages', 'access', 'route',
                  'public_transport', 'advised_parking', 'web_links', 'is_park_centered',
                  'disabled_infrastructure', 'parking_location', 'relationships',
                  'points_reference', 'gpx', 'kml', 'source', 'portal',
                  'type2', 'category', 'structure', 'treks', 'children', 'parents',
                  'previous', 'next') + \
            AltimetrySerializerMixin.Meta.fields + \
            ZoningSerializerMixin.Meta.fields + \
            PublishableSerializerMixin.Meta.fields + \
            PicturesSerializerMixin.Meta.fields

    def get_pictures(self, obj):
        pictures_list = []
        pictures_list.extend(obj.serializable_pictures)
        if settings.TREK_WITH_POIS_PICTURES:
            for poi in obj.published_pois:
                pictures_list.extend(poi.serializable_pictures)
        return pictures_list

    def get_parking_location(self, obj):
        if not obj.parking_location:
            return None
        return obj.parking_location.transform(settings.API_SRID,
                                              clone=True).coords

    def get_points_reference(self, obj):
        if not obj.points_reference:
            return None
        geojson = obj.points_reference.transform(settings.API_SRID,
                                                 clone=True).geojson
        return json.loads(geojson)

    def get_gpx_url(self, obj):
        return reverse('trekking:trek_gpx_detail',
                       kwargs={
                           'lang': get_language(),
                           'pk': obj.pk,
                           'slug': obj.slug
                       })

    def get_kml_url(self, obj):
        return reverse('trekking:trek_kml_detail',
                       kwargs={
                           'lang': get_language(),
                           'pk': obj.pk,
                           'slug': obj.slug
                       })

    def get_category(self, obj):
        if settings.SPLIT_TREKS_CATEGORIES_BY_ITINERANCY and obj.children.exists(
        ):
            data = {
                'id': 'I',
                'label': _(u"Itinerancy"),
                'pictogram': '/static/trekking/itinerancy.svg',
                # Translators: This is a slug (without space, accent or special char)
                'slug': _('itinerancy'),
            }
        elif settings.SPLIT_TREKS_CATEGORIES_BY_PRACTICE and obj.practice:
            data = {
                'id': obj.prefixed_category_id,
                'label': obj.practice.name,
                'pictogram': obj.practice.get_pictogram_url(),
                'slug': obj.practice.slug,
            }
        else:
            data = {
                'id': obj.category_id_prefix,
                'label': _(u"Hike"),
                'pictogram': '/static/trekking/trek.svg',
                # Translators: This is a slug (without space, accent or special char)
                'slug': _('trek'),
            }
        if settings.SPLIT_TREKS_CATEGORIES_BY_ITINERANCY and obj.children.exists(
        ):
            data['order'] = settings.ITINERANCY_CATEGORY_ORDER
        elif settings.SPLIT_TREKS_CATEGORIES_BY_PRACTICE:
            data['order'] = obj.practice and obj.practice.order
        else:
            data['order'] = settings.TREK_CATEGORY_ORDER
        if not settings.SPLIT_TREKS_CATEGORIES_BY_ACCESSIBILITY:
            data['type2_label'] = obj._meta.get_field(
                'accessibilities').verbose_name
        return data