def wrapper(*args, **kwargs): response = f(*args, **kwargs) if isinstance(response, tuple): data, code, header = unpack(response) else: data = response collection = None kwargs["_external"] = True templated = True for key in data.keys(): if key in collections_to_resource_type.keys(): collection = key if key in resource_type_to_collection.keys(): collection = resource_type_to_collection[key] if collection is None: return response kwargs["uri"] = collection + '/' if "id" in kwargs.keys(): kwargs["uri"] += kwargs["id"] del kwargs["id"] templated = False else: kwargs["uri"] += '{' + collection + ".id}" if collection in ['stop_areas', 'stop_points', 'lines', 'routes', 'addresses'] and "region" in kwargs: for api in ['route_schedules', 'stop_schedules', 'arrivals', 'departures', "places_nearby"]: data['links'].append({ "href": url_for("v1." + api, **kwargs), "rel": api, "templated": templated }) if collection in ['stop_areas', 'stop_points', 'addresses']: data['links'].append({ "href": url_for("v1.journeys", **kwargs), "rel": "journeys", "templated": templated }) #for lines we add the link to the calendars if 'region' in kwargs: if collection == 'lines': data['links'].append({ "href": url_for("v1.calendars", **kwargs), "rel": "calendars", "templated": templated }) if collection in ['stop_areas', 'lines', 'networks']: data['links'].append({ "href": url_for("v1.traffic_reports", **kwargs), "rel": "disruptions", "templated": templated }) if isinstance(response, tuple): return data, code, header else: return data
def wrapper(*args, **kwargs): objects = f(*args, **kwargs) if objects[1] != 200: return objects if isinstance(objects, tuple): data, code, header = unpack(objects) else: data = objects self.get_objets(data) data = self.prepare_objetcs(objects, True) kwargs = self.prepare_kwargs(kwargs, data) uri_id = None if "id" in kwargs.keys() and\ "collection" in kwargs.keys() and \ kwargs["collection"] in data.keys(): uri_id = kwargs["id"] for obj in self.data: if obj in resource_type_to_collection.keys(): kwargs["collection"] = resource_type_to_collection[obj] else: kwargs["collection"] = obj if kwargs["collection"] in collections_to_resource_type.keys(): if not uri_id: kwargs["id"] = "{" + obj + ".id}" endpoint = "v1." + kwargs["collection"] + "." endpoint += "id" if "region" in kwargs.keys() or\ "lon" in kwargs.keys()\ else "redirect" collection = kwargs["collection"] to_pass = { k: v for k, v in kwargs.iteritems() if k != "collection" } data["links"].append( create_external_link(url=endpoint, rel=collection, _type=obj, templated=True, **to_pass)) if isinstance(objects, tuple): return data, code, header else: return data
def wrapper(*args, **kwargs): objects = f(*args, **kwargs) if objects[1] != 200: return objects if isinstance(objects, tuple): data, code, header = unpack(objects) else: data = objects self.get_objets(data) data = self.prepare_objetcs(objects, True) kwargs = self.prepare_kwargs(kwargs, data) uri_id = None if "id" in kwargs.keys() and\ "collection" in kwargs.keys() and \ kwargs["collection"] in data.keys(): uri_id = kwargs["id"] for obj in self.data: if obj in resource_type_to_collection.keys(): kwargs["collection"] = resource_type_to_collection[obj] else: kwargs["collection"] = obj if kwargs["collection"] in collections_to_resource_type.keys(): if not uri_id: kwargs["id"] = "{" + obj + ".id}" endpoint = "v1." + kwargs["collection"] + "." endpoint += "id" if "region" in kwargs.keys() or\ "lon" in kwargs.keys()\ else "redirect" del kwargs["collection"] url = url_for(endpoint, _external=True, **kwargs) data["links"].append({ "href": url, "rel": obj, "templated": True }) if isinstance(objects, tuple): return data, code, header else: return data
from collections import OrderedDict from fields import NonNullNested region_fields = { "id": fields.String(attribute="region_id"), "start_production_date": fields.String, "end_production_date": fields.String, "status": fields.String, "shape": fields.String, "error": NonNullNested({ "code": fields.String, "value": fields.String }) } regions_fields = OrderedDict([ ("regions", fields.List(fields.Nested(region_fields))) ]) collections = collections_to_resource_type.keys() class Coverage(Resource): @clean_links() @add_coverage_link() @add_collection_links(collections) @marshal_with(regions_fields) def get(self, region=None, lon=None, lat=None): return i_manager.regions(region, lon, lat), 200
def wrapper(*args, **kwargs): objects = f(*args, **kwargs) if objects[1] != 200: return objects endpoint = None pagination = None if isinstance(objects, tuple): data, code, header = unpack(objects) else: data = objects for key, value in data.iteritems(): if key == "regions": endpoint = "v1.coverage" elif key == "pagination": pagination = value elif key in collections_to_resource_type.keys(): endpoint = "v1." + key + "." endpoint += "id" if "id" in kwargs else "collection" elif key in ["journeys", "stop_schedules", "route_schedules", "departures", "arrivals", "places_nearby", "calendars"]: endpoint = "v1." + key if pagination and endpoint and "region" in kwargs: pagination = data["pagination"] if "start_page" in pagination and \ "items_on_page" in pagination and \ "items_per_page" in pagination and \ "total_result" in pagination: if "links" not in data: data["links"] = [] start_page = int(pagination["start_page"]) items_per_page = int(pagination["items_per_page"]) items_on_page = int(pagination["items_on_page"]) total_result = int(pagination["total_result"]) kwargs["_external"] = True if start_page > 0: kwargs["start_page"] = start_page - 1 data["links"].append({ "href": url_for(endpoint, **kwargs), "type": "previous", "templated": False }) nb_next_page = items_per_page * start_page nb_next_page += items_on_page if total_result > nb_next_page: kwargs["start_page"] = start_page + 1 data["links"].append({ "href": url_for(endpoint, **kwargs), "type": "next", "templated": False }) if items_per_page == 0 or total_result == 0: kwargs["start_page"] = 0 else: nb_last_page = total_result - 1 nb_last_page = nb_last_page / items_per_page kwargs["start_page"] = nb_last_page data["links"].append({ "href": url_for(endpoint, **kwargs), "type": "last", "templated": False }) del kwargs["start_page"] data["links"].append({ "href": url_for(endpoint, **kwargs), "type": "first", "templated": False }) if isinstance(objects, tuple): return data, code, header else: return data