def one(self, resource, *args, **kwargs): """ The principal GET by ID handler for the RouteBuilder. All GET requests that are structured like so: `HTTP GET http://localhost:5000/<prefix>/<route-model>/<uuid>` (Where <your-blueprint> represents a particular resource mapping). (Where <uuid> represents an database instance ID). This will use the RouteBuilder DAO and then fetch data for the assigned model. In this case, selecting only one, by UUID. :return: response object with application/json content-type preset. :rtype: Type[JsonResponse] """ if isinstance(resource, str) and resource.endswith('/'): resource = resource.split('/').pop(0) query = QueryBuffer(self.model) resp = query.one(self.key, resource) if not resp: return self.exception(HTTPNotFound()) return self.response(HTTPSuccess(self.json(query.data, **query.queryargs.__dict__)))
def one_child_resource(self, resource, field, *args, **kwargs): query = QueryBuffer(self.model, auto=False).one(self.key, resource) instance = query.one(self.key, resource).data path = field.split('/') prop = None if not instance: return self.exception(HTTPNotFound()) if len(path) > 1: child = None data = query.data endpoint = path.pop() for idx, item in enumerate(path): if isinstance(data, int) or isinstance(data, str): raise HTTPBadRequest(msg='Cannot query this data type') if isinstance(data, list): child = data[int(item)] else: child = getattr(instance, item) data = getattr(child, endpoint) prop = endpoint else: prop = field data = getattr(instance, field) return HTTPSuccess({prop: data}, path=field.split('/'))
def one(self, resource, *args, **kwargs): """ The principal GET by ID handler for the RouteBuilder. All GET requests that are structured like so: `HTTP GET http://localhost:5000/<prefix>/<route-model>/<uuid>` (Where <your-blueprint> represents a particular resource mapping). (Where <uuid> represents an database instance ID). This will use the RouteBuilder DAO and then fetch data for the assigned model. In this case, selecting only one, by UUID. :return: response object with application/json content-type preset. :rtype: Type[JsonResponse] """ if resource == self.model.__tablename__: return self.get(*args, **kwargs) query = QueryBuffer(self.model) data = query.get(resource, self.key) if not data: return HTTPNotFound().pack() return HTTPSuccess( serialize(self.model, data, rels=query.queryargs.rels))
def head(self, resource): query = QueryBuffer(self.model) resp = query.one(self.key, resource) if not resp: return self.exception(HTTPNotFound()) return HTTPSuccess()
def post_child_resource(self, modelid, resource, *args, **kwargs): instance = QueryBuffer(self.model).one(self.key, modelid).data child = getattr(self.model.__mapper__.relationships, resource).entity.entity(**self._payload()) getattr(instance, resource).append(child) dao = self.dao.save(instance) return HTTPUpdated()
def one_child_resource(self, modelid, resource, *args, **kwargs): query = QueryBuffer(self.model, auto=False).one(self.key, modelid) modeltree = resource.split('/') if len(modeltree): data = query.data for item in modeltree: if isinstance(data, int) or isinstance(data, str): raise HTTPBadRequest(msg='Cannot query this data type') if isinstance(data, list): data = data[int(item)] else: data = getattr(data, item) if resource in columns(self.model, strformat=True): return HTTPSuccess({resource: getattr(query.data, resource)}) child = getattr(self.model, resource).comparator.entity.class_ return HTTPSuccess( self.json(QueryBuffer(child, auto=False).query.join(self.model.articles).all(), **query.queryargs.__dict__), __parent=self.json(query.data, **query.queryargs.__dict__) )
def one_child_resource(self, resource, field, *args, **kwargs): query = QueryBuffer(self.model, auto=False).one(self.key, resource) modeltree = field.split('/') if len(modeltree): data = query.data for item in modeltree: if isinstance(data, list): data = data[int(item)] else: data = getattr(data, item) if resource in columns(self.model, strformat=True): return HTTPSuccess({resource: getattr(query.data, resource)}) child = getattr(self.model, resource).comparator.entity.class_ return HTTPSuccess(serialize(child, getattr(query.data, resource)))
def get(self, *args, **kwargs): """ The principal GET handler for the RouteBuilder. All GET requests that are structured like so: `HTTP GET http://localhost:5000/<prefix>/<route-model>` (Where your-blueprint represents a particular resource mapping). Will be routed to this function. This will use the RouteBuilder DAO and then fetch data for the assigned model. In this case, select all. :return: response object with application/json content-type preset. :rtype: HTTPSuccess """ query = QueryBuffer(self.model).all() return self.response(HTTPSuccess(self.json(query.data, **query.queryargs.__dict__)))
def put(self, modelid, *args, **kwargs): query = QueryBuffer(self.model).one(self.key, modelid) instance = query.data self.dao.update(instance, self._payload()) return HTTPUpdated(iserialize(instance))
def querystring(self): self.query = QueryBuffer(self.model, auto=True) return self.query