Ejemplo n.º 1
0
    def content(self):
        content = self.reply.json()
        if content is None:
            return super(POIsInputFile, self).content()

        features = []
        for feature in content['features']:
            properties = feature['properties']

            # Geotrek < 0.23
            if 'serializable_type' in properties:
                poitype_value = properties.pop('serializable_type')
                thumbnail_values = properties.pop('serializable_thumbnail')
                pictures_values = properties.pop('serializable_pictures')
            else:
                poitype_value = properties.pop('type')
                thumbnail_values = properties.pop('thumbnail')
                pictures_values = properties.pop('pictures')

            properties['type'] = reroot(poitype_value, attr='pictogram')
            properties['thumbnail'] = reroot(thumbnail_values)
            properties['pictures'] = reroot(pictures_values, attr='url')
            feature['properties'] = properties
            features.append(feature)
        content['features'] = features
        return self.serialize_json(content)
Ejemplo n.º 2
0
    def content(self):
        content = self.reply.json()
        if content is None:
            return super(POIsInputFile, self).content()

        features = []
        for feature in content['features']:
            properties = feature['properties']

            # Geotrek < 0.23
            if 'serializable_type' in properties:
                poitype_value = properties.pop('serializable_type')
                thumbnail_values = properties.pop('serializable_thumbnail')
                pictures_values = properties.pop('serializable_pictures')
            else:
                poitype_value = properties.pop('type')
                thumbnail_values = properties.pop('thumbnail')
                pictures_values = properties.pop('pictures')

            properties['type'] = reroot(poitype_value, attr='pictogram')
            properties['thumbnail'] = reroot(thumbnail_values)
            properties['pictures'] = reroot(pictures_values, attr='url')
            feature['properties'] = properties
            features.append(feature)
        content['features'] = features
        return self.serialize_json(content)
Ejemplo n.º 3
0
    def content(self):
        content = self.reply.json()
        if content is None:
            return super(TrekInputFile, self).content()

        content['thumbnail'] = reroot(content['thumbnail'])
        content['pictures'] = reroot(content['pictures'], attr='url')
        content['themes'] = reroot(content['themes'], attr='pictogram')
        content['usages'] = reroot(content['usages'], attr='pictogram')

        content['difficulty'] = reroot(content['difficulty'], attr='pictogram')
        content['route'] = reroot(content['route'], attr='pictogram')
        content['networks'] = reroot(content['networks'], attr='pictogram')

        wl = []
        for w in content['web_links']:
            if w.get('category'):  # Safety for uncategorized links
                w['category'] = reroot(w['category'], attr='pictogram')
            wl.append(w)
        content['web_links'] = wl

        # Remove unpublished treks from related
        content['relationships'] = [r for r in content['relationships'] if r['published']]
        # For presentation purposes
        content['relationships_departure'] = [r for r in content['relationships'] if r['has_common_departure']]
        content['relationships_edge'] = [r for r in content['relationships'] if r['has_common_edge']]
        content['relationships_circuit'] = [r for r in content['relationships'] if r['is_circuit_step']]
        return self.serialize_json(content)
Ejemplo n.º 4
0
 def test_reroot_of_a_list(self):
     rerooted = reroot([
         '/geotrek/media/api/poi.geojson',
         '/geotrek/media/api/layer.geojson'
     ])
     self.assertEqual(
         rerooted, ['/media/api/poi.geojson', '/media/api/layer.geojson'])
Ejemplo n.º 5
0
 def test_reroot_of_a_dict_entry_of_a_list(self):
     objs = [
         dict(path='/geotrek/media/api/poi.geojson'),
         dict(path='/geotrek/media/api/layer.geojson')
     ]
     objs = reroot(objs, attr='path')
     self.assertEqual(objs, [{
         'path': '/media/api/poi.geojson'
     }, {
         'path': '/media/api/layer.geojson'
     }])
Ejemplo n.º 6
0
    def content(self):
        content = self.reply.json()
        if content is None:
            return super(InformationDeskInputFile, self).content()

        features = []
        for feature in content['features']:
            properties = feature['properties']
            properties['photo_url'] = reroot(properties['photo_url'])
            properties['html'] = render_to_string(
                'trekking/_information_desk.html', {'desk': properties})
            feature['properties'] = properties
            features.append(feature)
        content['features'] = features
        return self.serialize_json(content)
Ejemplo n.º 7
0
    def content(self):
        content = self.reply.json()
        if content is None:
            return super(InformationDeskInputFile, self).content()

        features = []
        for feature in content['features']:
            properties = feature['properties']
            properties['photo_url'] = reroot(properties['photo_url'])
            properties['html'] = render_to_string('trekking/_information_desk.html',
                                                  {'desk': properties})
            feature['properties'] = properties
            features.append(feature)
        content['features'] = features
        return self.serialize_json(content)
Ejemplo n.º 8
0
    def content(self):
        content = self.reply.json()
        if content is None:
            return super(AttachmentInputFile, self).content()

        # Filter attachments, mainly by mimetype
        filtered = []
        for attachment in content:
            # Images come through pictures list already
            if attachment['is_image']:
                continue
            attachment['url'] = reroot(attachment['url'])

            # Mimetype check (either by first part or complete)
            main, category = attachment['mimetype']
            mimetype = '%s/%s' % (main, category)
            allowed = settings.FILELIST_MIMETYPES

            if main in allowed or mimetype in allowed:
                filtered.append(attachment)

        return self.serialize_json(filtered)
Ejemplo n.º 9
0
    def content(self):
        content = self.reply.json()
        if content is None:
            return super(AttachmentInputFile, self).content()

        # Filter attachments, mainly by mimetype
        filtered = []
        for attachment in content:
            # Images come through pictures list already
            if attachment['is_image']:
                continue
            attachment['url'] = reroot(attachment['url'])

            # Mimetype check (either by first part or complete)
            main, category = attachment['mimetype']
            mimetype = '%s/%s' % (main, category)
            allowed = settings.FILELIST_MIMETYPES

            if main in allowed or mimetype in allowed:
                filtered.append(attachment)

        return self.serialize_json(filtered)
Ejemplo n.º 10
0
    def content(self):
        content = self.reply.json()
        if content is None:
            return super(TrekInputFile, self).content()

        content['thumbnail'] = reroot(content['thumbnail'])
        content['pictures'] = reroot(content['pictures'], attr='url')
        content['themes'] = reroot(content['themes'], attr='pictogram')
        content['usages'] = reroot(content['usages'], attr='pictogram')

        content['difficulty'] = reroot(content['difficulty'], attr='pictogram')
        content['route'] = reroot(content['route'], attr='pictogram')
        content['networks'] = reroot(content['networks'], attr='pictogram')

        wl = []
        for w in content['web_links']:
            if w.get('category'):  # Safety for uncategorized links
                w['category'] = reroot(w['category'], attr='pictogram')
            wl.append(w)
        content['web_links'] = wl

        # Remove unpublished treks from related
        content['relationships'] = [
            r for r in content['relationships'] if r['published']
        ]
        # For presentation purposes
        content['relationships_departure'] = [
            r for r in content['relationships'] if r['has_common_departure']
        ]
        content['relationships_edge'] = [
            r for r in content['relationships'] if r['has_common_edge']
        ]
        content['relationships_circuit'] = [
            r for r in content['relationships'] if r['is_circuit_step']
        ]
        return self.serialize_json(content)
Ejemplo n.º 11
0
 def test_reroot_does_not_alter_if_not_starting_with_media(self):
     notrerooted = reroot('/geotrek/api/poi.geojson')
     self.assertEqual(notrerooted, '/geotrek/api/poi.geojson')
Ejemplo n.º 12
0
 def test_reroot_of_a_dict_entry_of_a_list(self):
     objs = [dict(path='/geotrek/media/api/poi.geojson'),
             dict(path='/geotrek/media/api/layer.geojson')]
     objs = reroot(objs, attr='path')
     self.assertEqual(objs, [{'path': '/media/api/poi.geojson'},
                             {'path': '/media/api/layer.geojson'}])
Ejemplo n.º 13
0
 def test_reroot_of_a_dict_entry(self):
     obj = dict(url='/geotrek/media/api/poi.geojson')
     obj = reroot(obj, attr='url')
     self.assertEqual(obj['url'], '/media/api/poi.geojson')
Ejemplo n.º 14
0
 def test_reroot_of_a_list(self):
     rerooted = reroot(['/geotrek/media/api/poi.geojson',
                        '/geotrek/media/api/layer.geojson'])
     self.assertEqual(rerooted, ['/media/api/poi.geojson',
                                 '/media/api/layer.geojson'])
Ejemplo n.º 15
0
 def test_reroot_of_simple_value(self):
     rerooted = reroot('/geotrek/media/api/poi.geojson')
     self.assertEqual(rerooted, '/media/api/poi.geojson')
Ejemplo n.º 16
0
 def test_reroot_of_simple_value(self):
     rerooted = reroot('/geotrek/media/api/poi.geojson')
     self.assertEqual(rerooted, '/media/api/poi.geojson')
Ejemplo n.º 17
0
 def test_reroot_of_a_dict_entry(self):
     obj = dict(url='/geotrek/media/api/poi.geojson')
     obj = reroot(obj, attr='url')
     self.assertEqual(obj['url'], '/media/api/poi.geojson')
Ejemplo n.º 18
0
    def content(self):
        content = self.reply.json()
        if content is None:
            return super(TrekListInputFile, self).content()

        features = []
        for feature in content['features']:
            properties = feature['properties']
            pk = properties.get('pk', -1)

            # Ignore treks that are not published
            if not properties.get('published', False):
                logger.debug('Trek %s is not published.' % pk)
                continue

            # Ignore treks that are not linestring
            if feature['geometry']['type'].lower() != 'linestring':
                msg = 'Trek %s was ignored (not linestring).' % pk
                cprint(msg, 'red', attrs=['bold'], file=self.stderr)
                continue

            # Fill with detail properties
            detailpath = models.Trek.detailpath.format(pk=pk)
            detailfile = TrekInputFile(detailpath, **self.initkwargs)
            detailfile.pull()
            detail = json.loads(detailfile.content())
            properties.update(detail)

            # Remove rooturl from relative URLs
            relative_props = ['altimetric_profile', 'elevation_area_url', 'gpx', 'kml',
                'map_image_url', 'printable', 'poi_layer', 'information_desk_layer']
            for k in relative_props:
                properties[k] = properties[k].replace(self.client.rooturl, '') if properties.get(k) else properties.get(k)

            # Reroot information desks photos
            if 'information_desks' in properties:
                properties['information_desks'] = reroot(properties['information_desks'], attr='photo_url')
            else:
                properties['information_desks'] = [properties['information_desk']]

            # Download attachments list file
            url = reroot(properties['filelist_url'])
            destination = models.AttachmentFile.filepath.format(trek__pk=pk)
            # Store destination as new official url (e.g. for Geotrek mobile)
            properties['filelist_url'] = destination
            f = AttachmentInputFile(url, store=destination, **self.initkwargs)
            f.pull()

            # Add POIs information in list, useful for textual search
            f = POIsInputFile(models.POIs.filepath.format(trek__pk=pk), **self.initkwargs)
            f.pull()
            poiscontent = json.loads(f.content())
            poisprops = [poi['properties'] for poi in poiscontent['features']]
            properties['pois'] = [{'name': poiprop['name'],
                                   'description': poiprop['description'],
                                   'type': poiprop['type']['label']}
                                  for poiprop in poisprops]
            feature['properties'] = properties
            features.append(feature)

        content['features'] = features
        return self.serialize_json(content)
Ejemplo n.º 19
0
    def content(self):
        content = self.reply.json()
        if content is None:
            return super(TrekListInputFile, self).content()

        features = []
        for feature in content['features']:
            properties = feature['properties']
            pk = properties.get('pk', -1)

            # Ignore treks that are not published
            if not properties.get('published', False):
                logger.debug('Trek %s is not published.' % pk)
                continue

            # Ignore treks that are not linestring
            if feature['geometry']['type'].lower() != 'linestring':
                msg = 'Trek %s was ignored (not linestring).' % pk
                cprint(msg, 'red', attrs=['bold'], file=self.stderr)
                continue

            # Fill with detail properties
            detailpath = models.Trek.detailpath.format(pk=pk)
            detailfile = TrekInputFile(detailpath, **self.initkwargs)
            detailfile.pull()
            detail = json.loads(detailfile.content())
            properties.update(detail)

            # Remove rooturl from relative URLs
            relative_props = [
                'altimetric_profile', 'elevation_area_url', 'gpx', 'kml',
                'map_image_url', 'printable', 'poi_layer',
                'information_desk_layer'
            ]
            for k in relative_props:
                properties[k] = properties[k].replace(
                    self.client.rooturl,
                    '') if properties.get(k) else properties.get(k)

            # Reroot information desks photos
            if 'information_desks' in properties:
                properties['information_desks'] = reroot(
                    properties['information_desks'], attr='photo_url')
            else:
                properties['information_desks'] = [
                    properties['information_desk']
                ]

            # Download attachments list file
            url = reroot(properties['filelist_url'])
            destination = models.AttachmentFile.filepath.format(trek__pk=pk)
            # Store destination as new official url (e.g. for Geotrek mobile)
            properties['filelist_url'] = destination
            f = AttachmentInputFile(url, store=destination, **self.initkwargs)
            f.pull()

            # Add POIs information in list, useful for textual search
            f = POIsInputFile(models.POIs.filepath.format(trek__pk=pk),
                              **self.initkwargs)
            f.pull()
            poiscontent = json.loads(f.content())
            poisprops = [poi['properties'] for poi in poiscontent['features']]
            properties['pois'] = [{
                'name': poiprop['name'],
                'description': poiprop['description'],
                'type': poiprop['type']['label']
            } for poiprop in poisprops]
            feature['properties'] = properties
            features.append(feature)

        content['features'] = features
        return self.serialize_json(content)
Ejemplo n.º 20
0
 def test_reroot_does_not_alter_if_not_starting_with_media(self):
     notrerooted = reroot('/geotrek/api/poi.geojson')
     self.assertEqual(notrerooted, '/geotrek/api/poi.geojson')