def post(self, id=None, action=None): # special actions if action: # special actions if action == "refresh": execute_export.delay(id) return render({"id": id}) elif action == "toggle": e = Export.objects.get(id=id) e.enabled = not e.enabled e.save() return render({"id": id, "status": e.enabled}) else: restful_abort(400, error="action must be either refresh or toggle") else: # normal crud - se if we can make this DRY params = request.json params['frequency'] = string_to_timedelta(params.get('frequency', '1:00:00')) params['include_tags'] = [Tag.objects.get(name=name.strip()) for name in params['include_tags'].split(',') if name.strip()] params['exclude_tags'] = [Tag.objects.get(name=name.strip()) for name in params['exclude_tags'].split(',') if name.strip()] if not id: return render(self.objectmanager(**params).save().info()) else: self.objectmanager.objects(id=id).update(**params)
def post(self): query = request.get_json(silent=True) or {} fltr = query.get('filter', {}) params = query.get('params', {}) regex = params.pop('regex', False) if regex: fltr = {key: re.compile(value) for key, value in fltr.items()} page = params.pop('page', 1) - 1 rng = params.pop('range', 50) print "Filter:", fltr for key, value in fltr.copy().items(): if key == 'tags': if not regex: fltr['tags__name__in'] = fltr.pop('tags').split(',') else: fltr['tags__name'] = fltr.pop('tags') try: data = [] for o in self.objectmanager.objects(**fltr)[page * rng:(page + 1) * rng]: info = o.info() info['uri'] = url_for("api.{}".format(self.__class__.__name__.lower()), id=str(o.id)) data.append(info) except InvalidQueryError as e: restful_abort(400, invalid_query=str(e)) return render(data, self.template)
def get(self): args = Neighbors.parser.parse_args() field = args['field'] value = args['value'] try: if field == 'id': field = '_id' value = [ObjectId(v) for v in value] except Exception, e: restful_abort(400, reason='{} is an invalid ObjectId'.format(value))
def get(self): args = Export.parser.parse_args() output = args['output'] name = args['name'] try: return send_from_directory(g.config['EXPORTS_DIR'], 'export_{}.{}'.format(name, output), mimetype=output, ) except Exception as e: restful_abort(404, reason="The export you requested does not exist")
def post(self, id, action): if action not in ["refresh", "toggle"]: restful_abort(400, error="action must be either refresh or toggle") if action == "refresh": update_feed.delay(id) return render({"id": id}) elif action == "toggle": f = Feed.objects.get(id=id) f.enabled = not f.enabled f.save() return render({"id": id, "status": f.enabled})
def get(self): args = Export.parser.parse_args() output = args['output'] name = args['name'] try: return send_from_directory( g.config['EXPORTS_DIR'], 'export_{}.{}'.format(name, output), mimetype=output, ) except Exception as e: restful_abort(404, reason="The export you requested does not exist")
def post(self, id=None): if not id: data = request.json data['produces'] = [Tag.get_or_create(name=t.strip()) for t in request.json['produces'].split(',') if t.strip()] data['replaces'] = request.json['replaces'].split(',') return render(Tag(**data).save().info()) else: try: data = request.json data['produces'] = [Tag.get_or_create(name=t.strip()) for t in request.json['produces'].split(',') if t.strip()] data['replaces'] = request.json['replaces'].split(',') t = Tag.objects.get(id=id) t.update(**data) Observable.change_all_tags(t.name, data['name']) return render({"status": "ok"}) except TagValidationError as e: restful_abort(400, error=str(e)) except Exception as e: import traceback traceback.print_exc() restful_abort(400, error='Must specify name and produces parameters')