Пример #1
0
 def post(self):
     metrics_req_count.labels(method='POST',
                              endpoint='/refreshable',
                              status_code='200').inc()
     return {
         'refresh_token':
         create_refresh_token(identity=get_jwt_identity(),
                              user_claims=get_jwt_claims())
     }, 200
Пример #2
0
 def post(self):
     jti = get_raw_jwt()['jti']
     tokens_blacklist.setex(name=jti,
                            time=app_conf.JWT_REFRESH_TOKEN_EXPIRES,
                            value=jti)
     metrics_req_count.labels(method='POST',
                              endpoint='/logout',
                              status_code='200').inc()
     return {"message": "Successfully logged out"}, 200
Пример #3
0
 def post(self):
     user = get_user()
     if user is None:
         metrics_req_count.labels(method='POST',
                                  endpoint='/login',
                                  status_code='401').inc()
         return {"message": "Bad username or password"}, 401
     metrics_req_count.labels(method='POST',
                              endpoint='/login',
                              status_code='200').inc()
     return {
         'fresh_token': create_access_token(identity=user.name, fresh=True)
     }, 200
Пример #4
0
    def delete(self, role):
        parser = reqparse.RequestParser()
        parser.add_argument('name',
                            type=str,
                            required=True,
                            help='A user cannot have a blank name')

        data = parser.parse_args()

        if safe_str_cmp(role, 'admin'):
            user = Administrator.find_by_name(data['name'])
            if user is None:
                metrics_req_count.labels(method='DELETE',
                                         endpoint='/user',
                                         status_code='200').inc()
                return {'message': 'The user does not exist'}, 200
            elif safe_str_cmp(user.name, 'ernesto'):
                metrics_req_count.labels(method='DELETE',
                                         endpoint='/user',
                                         status_code='400').inc()
                return {'message': 'User ernesto cannot be deleted'}, 400
            else:
                user.delete_from_db()

        elif safe_str_cmp(role, 'consumer'):
            user = User.find_by_name(data['name'])
            if user is None:
                metrics_req_count.labels(method='DELETE',
                                         endpoint='/user',
                                         status_code='200').inc()
                return {'message': 'The user does not exist'}, 200
            else:
                user.delete_from_db()

        else:
            metrics_req_count.labels(method='DELETE',
                                     endpoint='/user',
                                     status_code='400').inc()
            return {'message': 'Invalid role name'}, 400

        metrics_req_count.labels(method='DELETE',
                                 endpoint='/user',
                                 status_code='200').inc()
        return {
            'message': "The user '{}' has been deleted".format(data["name"])
        }, 200
Пример #5
0
    def put(self, role):
        parser = reqparse.RequestParser()
        parser.add_argument('name',
                            type=str,
                            required=True,
                            help='A user cannot have a blank name')
        parser.add_argument('password',
                            type=str,
                            required=True,
                            help='A user cannot have a blank password')

        data = parser.parse_args()

        if safe_str_cmp(role, 'admin'):
            user = Administrator.find_by_name(data['name'])
            if user is None:
                user = Administrator(name=data['name'],
                                     password=encrypt_password(
                                         data['password']))
            else:
                user.password = encrypt_password(data['password'])

        elif safe_str_cmp(role, 'consumer'):
            user = User.find_by_name(data['name'])
            if user is None:
                user = User(name=data['name'],
                            password=encrypt_password(data['password']))
            else:
                user.password = encrypt_password(data['password'])
        else:
            metrics_req_count.labels(method='PUT',
                                     endpoint='/user',
                                     status_code='400').inc()
            return {'message': 'Invalid role name'}, 400

        user.save_to_db()
        user.refresh()
        metrics_req_count.labels(method='PUT',
                                 endpoint='/user',
                                 status_code='200').inc()
        return user.json_full(), 200
Пример #6
0
    def get(self, role):
        parser = reqparse.RequestParser()
        parser.add_argument('name',
                            type=str,
                            required=True,
                            help='A user cannot have a blank name')

        data = parser.parse_args()

        if safe_str_cmp(role, 'admin'):
            user = Administrator.find_by_name(data['name'])
            if user is None:
                metrics_req_count.labels(method='GET',
                                         endpoint='/user',
                                         status_code='200').inc()
                return {'message': 'The user does not exist'}, 200
            elif safe_str_cmp(user.name, 'ernesto'):
                metrics_req_count.labels(method='GET',
                                         endpoint='/user',
                                         status_code='400').inc()
                return {'message': 'User ernesto cannot be retrieved'}, 400

        elif safe_str_cmp(role, 'consumer'):
            user = User.find_by_name(data['name'])
            if user is None:
                metrics_req_count.labels(method='GET',
                                         endpoint='/user',
                                         status_code='200').inc()
                return {'message': 'The user does not exist'}, 200

        else:
            metrics_req_count.labels(method='GET',
                                     endpoint='/user',
                                     status_code='400').inc()
            return {'message': 'Invalid role name'}, 400

        metrics_req_count.labels(method='GET',
                                 endpoint='/user',
                                 status_code='200').inc()
        return user.json(), 200
Пример #7
0
    def get(self, category):
        claims = get_jwt_claims()
        if not claims['role']:
            metrics_req_count.labels(method='GET',
                                     endpoint='/product',
                                     status_code='401').inc()
            return {'message': 'No authorization'}, 401

        product, data = validate_category(category)
        if product is None:
            metrics_req_count.labels(method='GET',
                                     endpoint='/product',
                                     status_code='400').inc()
            return {
                'message':
                "The product '{}' does not exist".format(data["name"])
            }, 400
        metrics_req_count.labels(method='GET',
                                 endpoint='/product',
                                 status_code='200').inc()
        return product.json()
Пример #8
0
 def delete(self, category):
     product, data = validate_category(category)
     if product is None and data is None:
         metrics_req_count.labels(method='DELETE',
                                  endpoint='/product',
                                  status_code='400').inc()
         return {'message': 'Invalid category name'}, 400
     elif product is None and data is not None:
         metrics_req_count.labels(method='DELETE',
                                  endpoint='/product',
                                  status_code='400').inc()
         return {
             'message':
             "The product '{}' does not exist or has already been deleted".
             format(data["name"])
         }, 400
     else:
         product.delete_from_db()
     metrics_req_count.labels(method='DELETE',
                              endpoint='/product',
                              status_code='200').inc()
     return {
         'message': "The product '{}' has been deleted".format(data["name"])
     }, 200
Пример #9
0
    def put(self, category):
        parser = reqparse.RequestParser()
        parser.add_argument('name',
                            type=str,
                            required=True,
                            help='A product cannot have a blank name')
        parser.add_argument('price',
                            type=float,
                            required=True,
                            help='A product cannot have a blank price')
        parser.add_argument('units',
                            type=int,
                            required=False,
                            help='Set the quantity of units')

        if safe_str_cmp(category, 'Pizza'):
            parser.add_argument(
                'ingredients',
                type=list,
                required=True,
                location='json',
                help='A pizza cannot have a blank list of ingredients')
            parser.add_argument('form',
                                type=str,
                                required=True,
                                choices=('REDONDA', 'CUADRADA'),
                                help='A pizza cannot have a blank form')
            data = parser.parse_args()
            product = Pizza.find_by_name(data['name'])
            data['ingredients'] = [
                ingredient.upper() for ingredient in data['ingredients']
            ]

            if product is None:
                product = Pizza(**data)
            else:
                product.price = data['price']
                product.units = data['units']
                product.ingredients = data['ingredients']
                product.form = data['form']

        elif safe_str_cmp(category, 'Complement'):
            parser.add_argument(
                'ingredients',
                type=list,
                required=True,
                location='json',
                help='A complement cannot have a blank list of ingredients')
            parser.add_argument(
                'description',
                type=str,
                required=True,
                help='A complement cannot have a blank description')
            data = parser.parse_args()
            product = Complement.find_by_name(data['name'])
            data['ingredients'] = [
                ingredient.upper() for ingredient in data['ingredients']
            ]

            if product is None:
                product = Complement(**data)
            else:
                product.price = data['price']
                product.units = data['units']
                product.ingredients = data['ingredients']
                product.description = data['description']

        elif safe_str_cmp(category, 'Drink'):
            parser.add_argument('brand',
                                type=str,
                                required=True,
                                help='A drink cannot have a blank brand')
            parser.add_argument(
                'litres',
                type=float,
                required=True,
                help='A drink cannot have a blank number of litres')
            data = parser.parse_args()
            product = Drink.find_by_name(data['name'])

            if product is None:
                product = Drink(**data)
            else:
                product.price = data['price']
                product.units = data['units']
                product.brand = data['brand']
                product.litres = data['litres']

        elif safe_str_cmp(category, 'Sauce'):
            parser.add_argument('description',
                                type=str,
                                required=True,
                                help='A sauce cannot have a blank description')
            data = parser.parse_args()
            product = Sauce.find_by_name(data['name'])

            if product is None:
                product = Sauce(**data)
            else:
                product.price = data['price']
                product.units = data['units']
                product.description = data['description']

        elif safe_str_cmp(category, 'Package'):
            parser.add_argument('pizzas',
                                type=list,
                                required=False,
                                location='json',
                                help='A package needs a list of pizzas')
            parser.add_argument('complements',
                                type=list,
                                required=False,
                                location='json',
                                help='A package needs a list of complements')
            parser.add_argument('drinks',
                                type=list,
                                required=False,
                                location='json',
                                help='A package needs a list of drinks')
            parser.add_argument('sauces',
                                type=list,
                                required=False,
                                location='json',
                                help='A package needs a list of sauces')
            data = parser.parse_args()
            product = Package.find_by_name(data['name'])

            if product is None:
                product = Package(**data)
            else:
                product.price = data['price']
                product.units = data['units']
                if data['pizzas']:
                    product.pizzas = data['pizzas']
                if data['complements']:
                    product.pizzas = data['complements']
                if data['drinks']:
                    product.pizzas = data['drinks']
                if data['sauces']:
                    product.pizzas = data['sauces']

        else:
            metrics_req_count.labels(method='PUT',
                                     endpoint='/product',
                                     status_code='400').inc()
            return {'message': 'Invalid category name'}, 400

        product.save_to_db()
        product.refresh()
        metrics_req_count.labels(method='PUT',
                                 endpoint='/product',
                                 status_code='200').inc()
        return product.json()
Пример #10
0
 def get(self):
     metrics_req_count.labels(method='GET',
                              endpoint='/products',
                              status_code='200').inc()
     return Products.json()
Пример #11
0
def home():
    metrics_req_count.labels(method='GET', endpoint='/',
                             status_code='200').inc()  # Increment the counter
    return render_template('index.html')