Example #1
0
    def insert_item_from_user():
        try:
            args = parser.parse(CREATE_INV_ITEM, request)
            id_user = decode(request.headers['Authorization'],
                             SECRET_KEY,
                             algorithms='HS256')['id_user']
            lista_item_ids = [
                x.id_item for x in ListItems.get_full_item_list()
            ]
            if args['id_item'] not in lista_item_ids:
                return View.error(404, 'Item not found in')
            item = InventoryItems.insert_item_from_user(
                id_user, args['id_bin'], args['id_item'])
            prox_item = ListItems.get_item(args['id_item'])
            ListUsers.update_user_points(id_user, prox_item.points)

            result = {
                'id_user': item.id_user,
                'id_bin': item.id_bin,
                'id_item': item.id_item
            }
        except ValidationError as err:
            return View.error(400, str(err))

        return View.success(result)
Example #2
0
    def decorated(*args, **kwargs):
        # token = parser.parse(TOKEN_AUTH, request)
        token = request.headers

        if not token:
            return View.error(401, 'Missing Token')

        data = decode(token['Authorization'], SECRET_KEY, algorithms='HS256')
        if ListUsers.get_user_type(data['id_user']) in ('AD', 'LX'):
            return f(*args, **kwargs)

        return View.error(401, 'Invalid Token')
Example #3
0
    def change_user():
        try:
            args = parser.parse(CHANGE_USER, request)
            user = ListUsers.change_user(
                decode(request.headers['Authorization'],
                       SECRET_KEY,
                       algorithms='HS256')['id_user'], args)

            result = {'id_user': user.id_user}
        except ValidationError as err:
            return View.error(400, str(err))
        return View.success(result)
Example #4
0
 def delete_user():
     try:
         user = ListUsers.delete_user(
             decode(request.headers['Authorization'],
                    SECRET_KEY,
                    algorithms='HS256')['id_user'])
         if user:
             return View.success('User Deleted')
         else:
             return View.error(404, 'User Not Found')
     except ValidationError as err:
         return View.error(400, str(err))
Example #5
0
    def create_user():
        try:
            args = parser.parse(CREATE_USER, request)
            user = ListUsers.create_user(args['name'], args['email'],
                                         args['password'])
            if user == 0:
                return View.error(405, 'email already in use')

            result = {
                'id_user': user.id_user,
                'name': user.name,
                'email': user.email
            }
        except ValidationError as err:
            return View.error(400, str(err))
        return View.success(result)
Example #6
0
    def get_user():
        try:
            user = ListUsers.get_user(
                decode(request.headers['Authorization'],
                       SECRET_KEY,
                       algorithms='HS256')['id_user'])

            result = [{
                'id_user': rst.id_user,
                'name': rst.name,
                'email': rst.email,
                'points': rst.points
            } for rst in user]
        except ValidationError as err:
            return View.error(400, str(err))

        return View.success(result)
Example #7
0
    def login_user():
        try:
            args = parser.parse(LOGIN_USER, request)
            user = ListUsers.login_user(args['email'], args['password'])
            if user:
                token = encode(
                    {
                        'id_user': user.id_user,
                        'exp': datetime.utcnow() + timedelta(days=15)
                    }, SECRET_KEY)
            else:
                return View.error(401, 'Access Denied, wrong authenticators')

            result = {'token': token, 'email': user.email}
        except ValidationError as err:
            return View.error(400, str(err))

        return View.success(result)