コード例 #1
0
 def as_dict(self):
     representation = HALRepresentation({})
     representation.add_embed('events', [
         HALEventRepresentation(e, 'events.event').as_dict()
         for e in self.events
     ])
     if self.day and self.month and self.year:
         representation.add_links(
             get_nav_links(self.endpoint,
                           self.start,
                           self.count,
                           self.size,
                           day=self.day,
                           month=self.month,
                           year=self.year))
         representation.add_link(
             'self',
             url_for(self.endpoint,
                     day=self.day,
                     month=self.month,
                     year=self.year))
     else:
         representation.add_links(
             get_nav_links(self.endpoint, self.start, self.count,
                           self.size))
         representation.add_link('self', url_for(self.endpoint))
     return representation.as_dict()
コード例 #2
0
ファイル: representations.py プロジェクト: ox-it/moxie
    def as_dict(self):
        representation = HALRepresentation({"query": self.search, "size": self.size})
        url_kwargs = {}
        if self.search:
            url_kwargs["q"] = self.search
        if self.type:
            url_kwargs["type"] = self.type
        if self.type_exact:
            url_kwargs["type_exact"] = self.type_exact
        if self.facets:
            url_kwargs["facet"] = self.facets.keys()
        if self.other_args:
            url_kwargs.update(self.other_args)

        representation.add_link("self", url_for(self.endpoint, start=self.start, count=self.count, **url_kwargs))
        representation.add_links(get_nav_links(self.endpoint, self.start, self.count, self.size, **url_kwargs))
        representation.add_embed("pois", [HALPOIRepresentation(r, "places.poidetail").as_dict() for r in self.results])
        if self.facets:
            for field_name, facet_counts in self.facets.items():
                curie = FACET_CURIE
                friendly_name = field_name
                if field_name in FACET_RENAME:
                    curie, friendly_name = FACET_RENAME[field_name]
                for val, count in facet_counts.items():
                    kwargs = {"value": val, "count": count}
                    if field_name in FACET_BY_TYPE:
                        kwargs["title"] = find_type_name(val)
                        kwargs["name"] = val
                    url_kwargs[field_name] = val
                    representation.update_link(
                        "%s:%s" % (curie, friendly_name), url_for(self.endpoint, **url_kwargs), **kwargs
                    )
                    del url_kwargs[field_name]
        return representation.as_dict()
コード例 #3
0
 def as_dict(self):
     base = super(HALCourseRepresentation, self).as_dict()
     presentations = base.pop('presentations')
     representation = HALRepresentation(base)
     representation.add_embed('presentations', presentations)
     representation.add_link('self', url_for(self.endpoint, id=self.course.id))
     return representation.as_dict()
コード例 #4
0
 def as_dict(self):
     representation = HALRepresentation({})
     halified = []
     for notification in self.notifications:
         halified.append(HALNotificationRepresentation(notification, 'notifications.notification_details').as_dict())
     representation.add_embed('notifications', halified)
     representation.add_link('self', url_for(self.endpoint))
     return representation.as_dict()
コード例 #5
0
 def as_dict(self):
     representation = HALRepresentation(self.notification.as_dict())
     followups = [HALFollowUpRepresentation(fu, self.notification, 'notifications.followp_details').as_dict()
                  for fu in self.notification.followups]
     if followups:
         representation.add_embed('followups', followups)
     representation.add_link('self', url_for(self.endpoint, ident=self.notification.uuid))
     return representation.as_dict()
コード例 #6
0
 def as_dict(self):
     representation = HALRepresentation({})
     representation.add_embed('webcams', [
         HALWebcamRepresentation(w, 'webcams.webcam').as_dict()
         for w in self.webcams
     ])
     representation.add_link('self', url_for('webcams.webcams'))
     return representation.as_dict()
コード例 #7
0
 def as_dict(self):
     response = {
         'query': self.query,
     }
     # Need to have the '.' before 'course' to correctly pick the URL
     courses = [HALCourseRepresentation(r, '.course').as_dict() for r in self.courses]
     representation = HALRepresentation(response)
     representation.add_embed('courses', courses)
     representation.add_link('self', url_for(self.endpoint, q=self.query))
     representation.add_links(get_nav_links(self.endpoint, self.start, self.count, self.size, q=self.query))
     return representation.as_dict()
コード例 #8
0
ファイル: representations.py プロジェクト: ox-it/moxie-events
 def as_dict(self):
     representation = HALRepresentation({})
     representation.add_embed('events', [HALEventRepresentation(e, 'events.event').as_dict()
                                         for e in self.events])
     if self.day and self.month and self.year:
         representation.add_links(get_nav_links(self.endpoint, self.start, self.count, self.size,
                                                day=self.day, month=self.month, year=self.year))
         representation.add_link('self', url_for(self.endpoint, day=self.day, month=self.month, year=self.year))
     else:
         representation.add_links(get_nav_links(self.endpoint, self.start, self.count, self.size))
         representation.add_link('self', url_for(self.endpoint))
     return representation.as_dict()
コード例 #9
0
 def test_hal_json_embedded(self):
     representation = HALRepresentation({'key': 'value'})
     representation.add_embed('a', {'1': 'oo'})
     representation.add_embed('a', {'2': 'oo'})
     representation.add_embed('a', {'3': 'oo'})
     representation.add_embed('b', [{'a': 'oo'}, {'b': 'oo'}])
     representation.add_embed('b', {'c': 'oo'})
     self.assertDictContainsSubset({'_embedded': {'a': [{'1': 'oo'}, {'2': 'oo'}, {'3': 'oo'}],
                                                     'b': [{'a': 'oo'}, {'b': 'oo'}, {'c': 'oo'}]}},
                                   representation.as_dict())
コード例 #10
0
 def test_hal_json_helper_representation(self):
     representation = HALRepresentation({'a':'b'})
     representation.add_link('self', '/a/b')
     representation.add_link('list', '/list', templated=True)
     representation.update_link('child', '/child/1')
     representation.update_link('child', '/child/2')
     representation.add_curie('cu', 'http://curie.com')
     representation.add_embed('rel', [HALRepresentation({'embed': 'yes'},
         links={'self':{'href':'a'}}).as_dict()])
     self.assertDictContainsSubset({'_links': {
         'self': {'href': '/a/b'},
         'list': {'href': '/list', 'templated': True},
         'child': [{'href': '/child/1'}, {'href': '/child/2'}],
         'curie': {'href': 'http://curie.com', 'name': 'cu', 'templated': True}}},
         representation.as_dict())
     self.assertDictContainsSubset(
         {'_embedded': {'rel': [{'embed': 'yes',
         '_links': {'self': {'href': 'a'}}}]}},
         representation.as_dict())
コード例 #11
0
 def as_dict(self):
     representation = HALRepresentation({})
     representation.add_embed('webcams', [HALWebcamRepresentation(w, 'webcams.webcam').as_dict()
                                          for w in self.webcams])
     representation.add_link('self', url_for('webcams.webcams'))
     return representation.as_dict()
コード例 #12
0
ファイル: representations.py プロジェクト: ox-it/moxie
 def as_dict(self):
     representation = HALRepresentation({"count": self.count})
     representation.add_link("self", url_for(self.endpoint, ident=",".join(self.pois_ids)))
     representation.add_embed("pois", [HALPOIRepresentation(r, "places.poidetail").as_dict() for r in self.results])
     return representation.as_dict()
コード例 #13
0
ファイル: representations.py プロジェクト: ox-it/moxie
    def as_dict(self):
        base = super(HALPOIRepresentation, self).as_dict()
        representation = HALRepresentation(base)
        representation.add_link("self", url_for(self.endpoint, ident=self.poi.id))
        if self.poi.files:
            reps = [FileRepresentation(r).as_dict() for r in self.poi.files]
            representation.add_embed("files", reps)

        try:
            poi_service = POIService.from_context()
        except NoConfiguredService:
            poi_service = None
        if poi_service and self.add_parent_children_links:
            # Merging all IDs (parent and children) into one set to
            # do only one query to the service
            pois_ids = set(self.poi.children)
            pois_ids.update(self.poi.parent)
            if self.poi.primary_place:
                pois_ids.add(self.poi.primary_place)

            if pois_ids:
                pois_objects = poi_service.get_places_by_identifiers(pois_ids)
                # ease lookup by having a dict with ID as key
                if pois_objects:
                    pois = dict((poi.id, poi) for poi in pois_objects)
                else:
                    pois = {}

                def add_link(relation, method, identifier):
                    """Add a link w/ or w/o title depending if we found a POI
                    :param relation: link rel (parent or child)
                    :param method: method to apply (add_link or update if it should be an array)
                    :param identifier: ID of the POI for lookup
                    """
                    poi = pois.get(identifier, None)
                    if poi and poi.name:
                        method(
                            relation,
                            url_for(self.endpoint, ident=identifier),
                            title=poi.name,
                            type=poi.type,
                            type_name=poi.type_name,
                        )
                    else:
                        method(relation, url_for(self.endpoint, ident=identifier))

                if self.poi.parent:
                    for parent in self.poi.parent:
                        add_link("parent", representation.update_link, parent)

                if self.poi.primary_place:
                    add_link("primary_place", representation.add_link, self.poi.primary_place)

                if self.poi.children:
                    for child in self.poi.children:
                        add_link("child", representation.update_link, child)

        try:
            transport_service = TransportService.from_context()
        except NoConfiguredService:
            # Transport service not configured so no RTI information
            logger.warning("No configured Transport service", exc_info=True)
        else:
            try:
                provider = transport_service.get_provider(self.poi)
            except ProviderException:
                logger.debug("No single provider found for: %s" % self.poi.id)
            else:
                representation.add_curie("rti", RTI_CURIE)
                for rtitype, title in provider.provides.items():
                    representation.add_link(
                        "rti:%s" % rtitype, url_for("places.rti", ident=self.poi.id, rtitype=rtitype), title=title
                    )
        return representation.as_dict()