def handle_get_collection(self, request): """Return a response for a get request on the entire collection. :param request: The incoming Request object. """ resources = self._session.query(self.cls).all() self.template = self.env.get_template('resources.html') return make_response( self.template.render( resources=resources, form=self.form(), name=self.cls.__name__))
def handle_get(self, request, primary_key): """Return a response for a get request for a specific resource. :param request: The incoming Request object :param primary_key: The primary_key of the ORM model to retrieve """ resource = self._session.query(self.cls).get(primary_key) if not resource: return None self.template = self.env.get_template('resource.html') form = self.form(request.form, resource) return make_response(self.template.render( resource=resource, name=self.name, form=form))
def handle_patch(self, request, primary_key): """Return a :class:`werkzeug.Response` object after handling the PUT call. :param request: The incoming Request object. :param primary_key: The primary_key of the ORM model to retrieve """ resource = self._session.query(self.cls).get(primary_key) if resource: for field, value in request.form.items(): setattr(resource, field, value) else: resource = self.cls(request.form) error = self.validate_data(resource) if error: return None self._session.add(resource) self._session.commit() template = self.env.get_template('resource.html') return make_response(template.render(resource=resource))
def __call__(self, request): return make_response(self.template.render())
def __call__(self, *args, **kwargs): file_path = kwargs['file_path'] with open(os.path.join('static', file_path), 'r') as file_handle: response = make_response(file_handle.read()) response.headers['Content-type'] = mimetype_for_path(file_path) return response
def bad_request(message): response = make_response(json.dumps({'status': 'ERR', 'message': message})) response.status_code = 400 response.headers = {'Content-type': 'application/json'} return response