Exemplo n.º 1
0
    def put(self, branch_name, name):
        is_admin = Item.is_admin()
        if not is_admin:
            return {'message': 'You are not privileged to continue!'}, 400

        branch = BranchModel.find_by_name(branch_name)
        if not branch:
            return {'message': "Branch '{}' does not exist.".format(branch_name)}, 400

        data = Item.parser.parse_args()
        error_validation = validators.item_validator(**data)
        if error_validation['error validation']:
            return error_validation

        item = ItemModel.find_by_name_in_branch(branch.id, name)
        log = LogModel("update item '{}'".format(name), g.user.username, Item.admin)

        if item is None:
            item = ItemModel(name, **data)
        else:
            item.price = data['price']
            item.year = data['year']
            item.item_type = data['item_type']
            item.vendor = data['vendor']
            item.model = data['model']

            item.branch_id = data['branch_id']

        item.save_to_db()
        log.save_to_db()

        return item.json()
Exemplo n.º 2
0
    def put(self, branch_name, name):
        branch = BranchModel.find_by_name(branch_name)
        if not branch:
            return {'message': "Branch '{}' does not exist.".format(branch_name)}, 400

        item = ItemModel.find_by_name_in_branch(branch.id, name)

        if item is None:
            return {'message': 'Item does not exist.'}

        if item.available == 0:
            return {"message": "Item is already reserved."}, 400

        item.available = 0
        is_user = Item.is_user()
        if is_user:
            position = (PositionModel.find_by_id(g.user.position_id)).name
            item.reserved_by = g.user.username
            log = LogModel("reserve item '{}'".format(name), g.user.username, position)
        else:
            item.reserved_by = g.customer.username
            log = LogModel("reserve item '{}'".format(name), g.customer.username, auth.customer)

        item.save_to_db()
        log.save_to_db()

        # return item.short_json()
        return {"message": "Item reserved."}
Exemplo n.º 3
0
    def put(self, name):
        is_admin = Position.is_admin()
        if not is_admin:
            return {'message': 'You are not privileged to continue!'}, 400

        data = Position.parser.parse_args()
        error_validation = validators.position_validator(**data)
        if error_validation['error validation']:
            return error_validation

        user = g.user

        if not user.verify_password(data['password']):
            return {
                'message':
                'You can not update a position because you have typed a wrong password!'
            }, 400

        position = PositionModel.find_by_name(name)
        log = LogModel("update position '{}'".format(name), g.user.username,
                       Position.admin)

        if position is None:
            position = PositionModel(name)
        # else:
        #     position.name = name

        position.save_to_db()
        log.save_to_db()

        return position.json()
Exemplo n.º 4
0
    def put(self, name):
        is_admin = Branch.is_admin()
        if not is_admin:
            return {'message': 'You are not privileged to continue!'}, 400

        data = Branch.parser.parse_args()
        error_validation = validators.branch_validator(**data)
        if error_validation['error validation']:
            return error_validation

        branch = BranchModel.find_by_name(name)
        log = LogModel("update branch '{}'".format(name), g.user.username, Branch.admin)

        if branch is None:
            branch = BranchModel(name, **data)
        else:
            branch.country = data['country']
            branch.city = data['city']
            branch.postal_code = data['postal_code']
            branch.street = data['street']
            branch.email = data['email']
            branch.phone = data['phone']

        branch.save_to_db()
        log.save_to_db()

        return branch.json()
Exemplo n.º 5
0
    def post(self, name):
        # begin
        is_admin = Branch.is_admin()
        if not is_admin:
            return {'message': 'You are not privileged to continue!'}, 400
        # end

        if BranchModel.find_by_name(name):
            return {'message': "A branch with name '{}' already exists.".format(name)}, 400

        data = Branch.parser.parse_args()
        error_validation = validators.branch_validator(**data)
        if error_validation['error validation']:
            return error_validation

        branch = BranchModel(name, **data)
        log = LogModel("add branch '{}'".format(name), g.user.username, Branch.admin)

        try:
            branch.save_to_db()
            log.save_to_db()
        except:
            return {'message': 'An error occurred inserting the branch.'}, 500  # Internal Server Error

        return branch.json(), 201
Exemplo n.º 6
0
    def delete(self):
        is_user = False
        try:
            if g.user:
                is_user = True
        except:
            pass

        data = CustomerDelete.parser.parse_args()
        error_validation = validators.delete_validator(**data)
        if error_validation['error validation']:
            return error_validation

        if is_user:
            user = g.user
            position = PositionModel.find_by_id(user.position_id)

            if position.name != 'admin' or not user.verify_password(
                    data['password']):
                return {
                    'message':
                    "You are not privileged to delete customer's account!"
                }, 400

            customer = CustomerModel.find_by_username(data['username'])
            if customer:
                log = LogModel("remove customer '{}'".format(data['username']),
                               g.user.username, auth.admin)
                customer.delete_from_db()
                log.save_to_db()

                return {'message': "Customer's account deleted."}

            return {
                'message':
                "Customer '{}' account does not exist.".format(
                    data['username'])
            }
        else:
            customer = g.customer

            if customer.username != data['username']:
                return {
                    'message':
                    'You can not delete your account because you have typed wrong username!'
                }, 400

            if not customer.verify_password(data['password']):
                return {
                    'message':
                    'You can not delete your account because you have typed wrong password!'
                }, 400

        log = LogModel("remove customer '{}'".format(data['username']),
                       g.customer.username, auth.customer)
        customer.delete_from_db()
        log.save_to_db()

        return {'message': 'Your account is deleted.'}
Exemplo n.º 7
0
    def post(self):
        data = Log.parser.parse_args()
        log = LogModel(**data)
        try:
            log.save_to_db()
        except:
            return {"message": "An error occurred inserting the item."}, 500

        return {"message": "Log successfully add to the db."}, 201
Exemplo n.º 8
0
def save_data_to_db():
    """function to interact with database"""
    if not UserModel.find_by_username(session['username']):
        new_user = UserModel(session['username'],
                             generate_password_hash(session['password']),
                             session['phone_number'], None, None, None, None,
                             None)
        new_user.save_to_db()

    log = LogModel(session['username'], session['ip'], session['latitude'],
                   session['longitude'], session['time'], None)
    log.save_to_db()
Exemplo n.º 9
0
    def delete(self, name):
        is_admin = Branch.is_admin()
        if not is_admin:
            return {'message': 'You are not privileged to continue!'}, 400

        branch = BranchModel.find_by_name(name)
        if branch:
            log = LogModel("remove branch '{}'".format(name), g.user.username, Branch.admin)
            branch.delete_from_db()
            log.save_to_db()

        return {'message': 'Branch deleted.'}
Exemplo n.º 10
0
    def post(self, branch_name, name):
        is_admin = Car.is_admin()
        is_manager = Car.is_manager()

        if not is_admin and not is_manager:
            return {'message': 'You are not privileged to continue!'}, 400

        branch = BranchModel.find_by_name(branch_name)
        if not branch:
            return {
                'message': "Branch '{}' does not exist.".format(branch_name)
            }, 400

        if g.user.branch_id != branch.id and not is_admin:
            return {'message': 'You are not privileged to continue!'}, 400

        data = Car.parser.parse_args()
        error_validation = validators.car_validator(**data)
        if error_validation['error validation']:
            return error_validation

        if branch.id != data['branch_id']:
            return {
                'message':
                "Branch: '{}' and id: '{}' does not suit with each other.".
                format(branch_name, data['branch_id'])
            }

        if CarModel.find_by_name_in_branch(branch.id, name):
            return {
                'message': "A car with name '{}' already exists.".format(name)
            }, 400

        car = CarModel(name, **data)
        if not is_admin:
            log = LogModel("add car '{}'".format(name), g.user.username,
                           Car.manager)
        else:
            log = LogModel("add car '{}'".format(name), g.user.username,
                           Car.admin)

        try:
            car.save_to_db()
            log.save_to_db()
        except:
            return {
                "message": "An error occurred inserting the car."
            }, 500  # Internal Server Error

        if not is_admin:
            return car.short_json(), 201
        return car.json(), 201
Exemplo n.º 11
0
    def put(self, branch_name, name):
        is_user = Car.is_user()

        branch = BranchModel.find_by_name(branch_name)
        if not branch:
            return {
                'message': "Branch '{}' does not exist.".format(branch_name)
            }, 400

        car = CarModel.find_by_name_in_branch(branch.id, name)

        if car is None:
            return {'message': 'Car does not exist.'}

        if car.available == 1:
            return {"message": "Car is not reserved yet."}, 400

        if not is_user:
            if not g.customer.username == car.reserved_by:
                return {'message': 'You are not privileged to continue!'}, 400

        # branch = BranchModel.find_by_name(branch_name)
        # if not branch:
        #     return {'message': "Branch '{}' does not exist.".format(branch_name)}, 400
        #
        # car = CarModel.find_by_name_in_branch(branch.id, name)
        #
        # if car is None:
        #     return {'message': 'Car does not exist.'}
        #
        # if car.available == 1:
        #     return {"message": "Car is not reserved yet."}, 400

        car.available = 1
        if is_user:
            position = (PositionModel.find_by_id(g.user.position_id)).name
            log = LogModel("Cancelled  car '{}' reservation".format(name),
                           g.user.username, position)
        else:
            log = LogModel("Cancelled  car '{}' reservation".format(name),
                           g.customer.username, auth.customer)

        car.reserved_by = None

        car.save_to_db()
        log.save_to_db()

        # return car.short_json()
        return {'message': 'Car reservation canceled.'}
Exemplo n.º 12
0
    def delete(self, branch_name, name):
        is_admin = Item.is_admin()
        if not is_admin:
            return {'message': 'You are not privileged to continue!'}, 400

        branch = BranchModel.find_by_name(branch_name)
        if not branch:
            return {'message': "Branch '{}' does not exist.".format(branch_name)}, 400

        item = ItemModel.find_by_name_in_branch(branch.id, name)
        if item:
            log = LogModel("remove item '{}'".format(name), g.user.username, Item.admin)
            item.delete_from_db()
            log.save_to_db()

        return {'message': 'Item deleted.'}
Exemplo n.º 13
0
    def post(self):
        try:
            user = g.user
        except:
            return {'message': "You are not privileged to continue!"}, 400

        data = UserRegister.parser.parse_args()
        error_validation = validators.user_register_validator(**data)
        if error_validation['error validation']:
            return error_validation

        position = PositionModel.find_by_id(user.position_id)

        print(position)

        if position.name != 'admin':
            return {
                'message': "You are not privileged to create user's account!"
            }, 400

        if UserModel.find_by_username(data['username']):
            return {
                "message": "A user with that username already exists."
            }, 400

        if CustomerModel.find_by_username(data['username']):
            return {
                "message": "A customer with that username already exists."
            }, 400

        user = UserModel(**data)
        # user.save_to_db()
        log = LogModel("add user '{}'".format(data['username']),
                       g.user.username, auth.admin)

        try:
            user.save_to_db()
            log.save_to_db()
        except:
            return {
                'message': 'An error occurred inserting the user.'
            }, 500  # Internal Server Error

        # return {'user': user.fake_json()}, 201
        # return {'users': [user.short_json() for user in UserModel.query.all()]}, 201
        return {"message": "User created successfully."}, 201
Exemplo n.º 14
0
    def delete(self, branch_name, name):
        is_admin = Car.is_admin()
        if not is_admin:
            return {'message': 'You are not privileged to continue!'}, 400

        branch = BranchModel.find_by_name(branch_name)
        if not branch:
            return {
                'message': "Branch '{}' does not exist.".format(branch_name)
            }, 400

        car = CarModel.find_by_name_in_branch(branch.id, name)
        if car:
            log = LogModel("remove car '{}'".format(name), g.user.username,
                           Car.admin)
            car.delete_from_db()
            log.save_to_db()

        return {'message': 'Car deleted.'}
Exemplo n.º 15
0
    def post(self, name):
        # begin
        is_admin = Position.is_admin()
        if not is_admin:
            return {'message': 'You are not privileged to continue!'}, 400
        # end

        data = Position.parser.parse_args()
        error_validation = validators.position_validator(**data)
        if error_validation['error validation']:
            return error_validation

        user = g.user  # this

        # start
        if not user.verify_password(data['password']):
            return {
                'message':
                'You can not add a new position because you have typed a wrong password!'
            }, 400
        # end

        if PositionModel.find_by_name(name):
            return {
                'message':
                "A position with name '{}' already exists.".format(name)
            }, 400

        position = PositionModel(name)
        log = LogModel("add position '{}'".format(name), g.user.username,
                       Position.admin)

        try:
            position.save_to_db()
            log.save_to_db()
        except:
            return {
                'message': 'An error occurred while creating the position.'
            }, 500

        return position.json(), 201
Exemplo n.º 16
0
    def put(self, branch_name, name):
        is_admin = Car.is_admin()
        if not is_admin:
            return {'message': 'You are not privileged to continue!'}, 400

        branch = BranchModel.find_by_name(branch_name)
        if not branch:
            return {
                'message': "Branch '{}' does not exist.".format(branch_name)
            }, 400

        data = Car.parser.parse_args()
        error_validation = validators.car_validator(**data)
        if error_validation['error validation']:
            return error_validation

        car = CarModel.find_by_name_in_branch(branch.id, name)
        log = LogModel("update car '{}'".format(name), g.user.username,
                       Car.admin)

        if car is None:
            car = CarModel(name, **data)
        else:
            car.price = data['price']
            car.year = data['year']
            car.car_type = data['car_type']
            car.vendor = data['vendor']
            car.model = data['model']
            car.colour = data['colour']
            car.seats = data['seats']
            car.transmission = data['transmission']
            car.drive = data['drive']
            car.fuel = data['fuel']
            car.engine_power = data['engine_power']

            car.branch_id = data['branch_id']

        car.save_to_db()
        log.save_to_db()

        return car.json()
Exemplo n.º 17
0
    def delete(self, name):
        is_admin = Position.is_admin()
        if not is_admin:
            return {'message': 'You are not privileged to continue!'}, 400

        data = Position.parser.parse_args()
        user = g.user

        if not user.verify_password(data['password']):
            return {
                'message':
                'You can not delete a position because you have typed a wrong password!'
            }, 400

        position = PositionModel.find_by_name(name)
        if position:
            log = LogModel("remove position '{}'".format(name),
                           g.user.username, Position.admin)
            position.delete_from_db()
            log.save_to_db()

        return {'message': 'Position deleted.'}