Example #1
0
    def post(self):
        """
        create a new resource set
        :return: JSON {message:"", status_code:""}
        """
        args = self.reqparse.parse_args()
        parts = args['Authorization'].split()
        user_id = DBHelper.get_user_id(parts[1])

        data = request.get_json()

        if not 'rs_id' or not 'categories' in data:
            raise CustomError('Invalid parameter, <rs_id> and <categories> required', status_code=400)

        if type(data['categories']) is not list:
            raise CustomError('the type of "categories" is a list', status_code=400)

        if DBHelper.check_rs_id(data['rs_id']) is not None:
            raise CustomError(('{0} has been created').format(data['rs_id']), status_code=409)

        DBHelper.set_resource_set(user_id, data['rs_id'], data['categories'])

        # app_log.info(('new resource set {0} created').format(data['rs_id']), extra={'sender': 'DataSource'})

        return {
            'message': 'Created successfully',
            'status_code': 201
        }, 201
Example #2
0
    def post(self):
        """
        create new data
        :return:
        """
        args = self.reqparse.parse_args()
        parts = args['Authorization'].split()
        user_id = DBHelper.get_user_id(parts[1])

        sample = request.get_json()

        if 'label' not in sample:
            raise CustomError('Parameter <label> missing', status_code=400)
        if 'data' not in sample:
            raise CustomError('Parameter <data> missing', status_code=400)

        label = sample['label']
        data = sample['data']

        lb = DBHelper.get_label_by_name(label)
        if lb is None:
            raise CustomError('Label Not Found', status_code=404)
        label_id = lb.id

        for d in data:
            d.update({
                'user_id': user_id,
                'label_id': label_id
            })
            DBHelper.set_data(d)

        return {
                   'message': 'Upload successfully!',
                   'status_code': 201
               }, 201
Example #3
0
def register_resource_set(ext_id):
    """
    register resource set to DataOperator
    :param ext_id: String, unique id
    :return: Boolean
    """
    try:
        user_id = DBHelper.get_user_id(ext_id)
        if user_id is False:
            return False

        rs = DBHelper.get_resource_set(user_id)

        for i in rs:
            data = json.dumps(i)
            req = urllib2.Request('http://46.101.30.187:8080/api/protection/resourceSets', data)
            req.add_header('Content-Type', 'application/json')

            f = urllib2.urlopen(req).read()
            obj = json.loads(f)

            con = obj['content'][0]

            DBHelper.set_rs_id({'name': con['name'], 'rs_id': con['rs_id']})
        return True
    except Exception as e:
        print(str(e))
        return None
Example #4
0
def signup(obj):
    """
    Register an account
    :param obj: Json, {username:'', password:'', email:''}
    :return: Object, account/None
    """
    try:

        info = DBHelper.set_info(obj['email'])

        ext_id = hashlib.md5(obj['username'] + '@' + app.config['APP_NAME']).hexdigest()

        obj = {
            'username': obj['username'],
            'password': obj['password'],
            'user_info_id': info,
            'status': 1,
            'ext_id': ext_id
        }

        account = DBHelper.set_user(obj)

        if account is not None:
            return account
        else:
            return None

    except Exception as e:
        print(str(e))
        return None
Example #5
0
    def post(self):
        """
        Store consent receipt
        :return:
        """
        sample = request.get_json()
        if 'consentReceipt' not in sample:
            return CustomError('No consentReceipt in payload', status_code=400)

        receipt = sample['consentReceipt']
        DBHelper.set_receipt(receipt)
        return {'message': 'Accepted!'}, 201
Example #6
0
    def put(self):
        """
        Update the status of consent receipt
        :return:
        """
        sample = request.get_json()
        if 'receipt_id' not in sample:
            return CustomError('No receipt_id in payload', status_code=400)
        if 'authorization_status' not in sample:
            return CustomError('No authorization_status in payload', status_code=400)

        DBHelper.update_receipt(sample)
        return {'message': 'updated!'}, 200
Example #7
0
 def get(self):
     """
     get all categories of this DataSource
     :return: Object
     """
     ca = DBHelper.get_categories()
     return ca, 200
Example #8
0
 def get(self):
     """
     create units from this specific DataSource
     :return: Object
     """
     ca = DBHelper.get_units()
     return ca, 200
Example #9
0
 def get(self):
     """
     create labels of this specific DataSource
     :return: Object
     """
     lbs = DBHelper.get_labels()
     return lbs,201
Example #10
0
    def post(self):
        """
        register a new account for user
        :return:
        """

        # if not request.json:
        #     raise InvalidUsage('Payload is not JSON', status_code=400)

        obj = request.get_json()
        # obj = {'username':'******', 'password':'******', 'email':'3'}

        if 'username' not in obj:
            raise CustomError('Parameter <username> missing', status_code=400)
        if 'password' not in obj:
            raise CustomError('Parameter <password> missing', status_code=400)
        if 'email' not in obj:
            raise CustomError('Parameter <email> missing', status_code=400)

        info = DBHelper.set_info(obj['email'])

        if DBHelper.check_username(obj['username']) is False:
            raise CustomError('register failed, username has existed!', status_code=409)

        ext_id = hashlib.md5(obj['username'] + '@' + app.config['APP_NAME']).hexdigest()

        obj = {
            'username': obj['username'],
            'password': obj['password'],
            'user_info_id': info,
            'status': 1,
            'ext_id': ext_id
        }

        account = DBHelper.set_user(obj)

        # app_log.info(('new user {0} registered').format(obj['username']), extra={'sender': 'DataSource'})

        return {
            'message': 'register successfully',
            'ext_id': account.ext_id,
            'status_code': 201
        }, 201
Example #11
0
    def post(self):
        """
        create a new category for this specific DataSource
        :return: JSON {message:"", status_code:""}
        """

        sample = request.get_json()

        if "category" not in sample:
            raise CustomError("Parameter <category> missing", status_code=400)

        if DBHelper.get_category_by_name(sample["category"]) is not None:
            raise CustomError(("{0} has been created").format(sample["category"]), status_code=409)

        if "desc" in sample:
            DBHelper.set_category(sample["category"], sample["desc"])
        else:
            DBHelper.set_category(sample["category"])

        return {"message": "Created successfully!", "status_code": 201}, 201
Example #12
0
 def get(self):
     """
     Get contract
     :return: Object, services contract template
     """
     re = DBHelper.get_categories()
     ty = []
     for r in re:
         ty.append(r.name)
     Contract_Template['data_type'] = ty
     return Contract_Template, 200
Example #13
0
    def post(self):
        """
        create a new units
        :return: Json {message:"", status_code:""}
        """

        sample = request.get_json()

        if "units" not in sample:
            raise CustomError("Parameter <units> missing", status_code=400)

        if DBHelper.get_units_by_name(sample["units"]) is not None:
            raise CustomError(("{0} has been created").format(sample["units"]), status_code=409)

        if "desc" in sample:
            DBHelper.set_units(sample["units"], sample["desc"])
        else:
            DBHelper.set_units(sample["units"])

        # app_log.info(('new unit {0} created').format(sample['units']), extra={'sender': 'DataSource'})
        return {"message": "Created successfully!", "status_code": 201}, 201
Example #14
0
    def get(self):
        """
        get a specific user's information
        :return: Json
        """
        args = self.reqparse.parse_args()
        parts = args['Authorization'].split()
        info = DBHelper.get_info(parts[1])

        if info:
            return info
        else:
            raise CustomError('Invalid ext_id', status_code=400)
Example #15
0
def validate_token(auth):
    """
    Inspect ext_id
    :param auth: the content of HTTP Authorization header
    :return: Boolean true/false
    """
    parts = auth.split()

    if len(parts) != 2:
        raise CustomError('Wrong context of Authorization Header')

    if parts[0].lower() != 'bearer':
        raise CustomError('Unsupported authorization type')

    if DBHelper.check_token(parts[1]):
        return True
    else:
        return False
Example #16
0
    def post(self):

        obj = request.get_json()
        # obj = {'username':'******', 'password':'******'}
        if 'username' not in obj:
            raise CustomError('Parameter <username> missing', status_code=400)
        if 'password' not in obj:
            raise CustomError('Parameter <password> missing', status_code=400)

        ext_id = DBHelper.get_ext_id(obj)

        if ext_id is None:
            raise CustomError('Invalid username or password!', status_code=400)

        return {
            'message': 'login successfully!',
            'ext_id': ext_id,
            'status_code': 200
        }, 200
Example #17
0
    def post(self):
        """
        create a new label for this specific DataSource
        :return: JSON {message:"", status_code:""}
        """
        sample = request.get_json()

        if 'label' not in sample:
            raise CustomError('Parameter <label> missing', status_code=400)
        if 'units' not in sample:
            raise CustomError('Parameter <units> missing', status_code=400)
        if 'category' not in sample:
            raise CustomError('Parameter <category> missing', status_code=400)

        label = sample['label']
        units = sample['units']
        category = sample['category']

        if DBHelper.get_label_by_name(label) is not None:
            raise CustomError(('{0} has been created').format(label), status_code=409)

        if units is None:
            u_id = None
        else:
            un = DBHelper.get_units_by_name(units)
            if un is None:
                raise NotFound(payload={'detail': ('{0} Not Found').format(units)})
            u_id = un.id

        ca = DBHelper.get_category_by_name(category)
        if ca is None:
            raise NotFound(payload={'detail': ('{0} Not Found').format(category)})

        if 'desc' in sample:
            DBHelper.set_label(label, u_id, ca.id, sample['desc'])
        else:
            DBHelper.set_label(label, u_id, ca.id)
        return {
            'message': 'Upload successfully!',
            'status_code': 201
        }, 201