示例#1
0
    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)
示例#2
0
文件: crud.py 项目: carriercomm/yeti
    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)
示例#3
0
文件: api.py 项目: Sh4d0wS4int/malcom
 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))
示例#4
0
文件: api.py 项目: br0k3ngl255/malcom
 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))
示例#5
0
文件: api.py 项目: R00tAK/malcom
 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")
示例#6
0
文件: feeds.py 项目: carriercomm/yeti
 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})
示例#7
0
 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")
示例#8
0
文件: tag.py 项目: carriercomm/yeti
 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')