Advertisement
Guest User

Untitled

a guest
Jan 25th, 2016
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.38 KB | None | 0 0
  1. from flask_restful import Resource
  2. from flask_restful import wraps
  3. from flask_restful import abort
  4. from flask_restful import reqparse
  5. from flask import Flask, request
  6. import json
  7.  
  8. from common.LabelDB import Label
  9. from common.LabelDBMongo import LabelMongo
  10.  
  11.  
  12. def check_auth(username, password):
  13.     #TODO implement real authentication
  14.     return username == 'rene' and password == '12345'
  15.  
  16. def authenticate(func):
  17.     @wraps(func)
  18.     def wrapper(*args, **kwargs):
  19.         auth = request.authorization
  20.         if not auth or not check_auth(auth.username, auth.password):
  21.             abort(401)
  22.  
  23.         return func(*args, **kwargs)
  24.  
  25.     return wrapper
  26.  
  27.  
  28. class Labels(Resource):
  29.     method_decorators = [authenticate]
  30.  
  31.     def put(self):
  32.         if request.json is None:
  33.             abort(404)
  34.         labels = request.json['labels']
  35.         if labels is None:
  36.             abort(404)
  37.  
  38.         for label in labels:
  39.             #TODO add labels to db
  40.             #TODO verify if the label is well formatted
  41.             print label
  42.  
  43.  
  44. class Label(Resource):
  45.     method_decorators = [authenticate]
  46.     def __init__(self):
  47.         self.db = LabelMongo()
  48.         self.reqparse = reqparse.RequestParser()
  49.         self.reqparse.add_argument('name', type = str, location = 'json')
  50.         self.reqparse.add_argument('user', type = str, location = 'json')
  51.         self.reqparse.add_argument('offset', type = str, location = 'json')
  52.         self.reqparse.add_argument('binary', type = str, location = 'json')
  53.         super(Label, self).__init__()
  54.  
  55.  
  56.     def get(self):
  57.         offset = request.args.get('offset')
  58.         user = request.args.get('user')
  59.         binary = request.args.get('binary')
  60.         if offset == None:
  61.             lbl = self.db.get_labels(binary=binary, user=user)
  62.         else:
  63.             lbl = self.db.get_label(offset=offset, binary=binary, user=user)
  64.         return lbl
  65.  
  66.     def post(self):
  67.         offset = request.args.get('offset')
  68.         user = request.args.get('user')
  69.         binary = request.args.get('binary')
  70.         name = request.args.get('name')
  71.         self.db.add_label(offset=offset,user=user,binary=binary,name=name)
  72.  
  73.     def delete(self):
  74.         offset = request.args.get('offset')
  75.         binary = request.args.get('binary')
  76.         user = request.args.get('user')
  77.         self.db.del_label(user=user,binary=binary,offset=offset)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement