Ejemplo n.º 1
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."}
Ejemplo n.º 2
0
 def short_json(self):
     return {
             'name': self.name, 'price': self.price, 'available': self.available, 'reserved_by': self.reserved_by,
             'year': self.year, 'car_type': self.car_type, 'vendor': self.vendor, 'model': self.model,
             'seats': self.seats, 'transmission': self.transmission, 'drive': self.drive, 'fuel': self.fuel,
             'branch': (BranchModel.find_by_id(self.branch_id)).name
             }
Ejemplo n.º 3
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()
Ejemplo n.º 4
0
    def get(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:
            return branch.json()
        return {'message': 'Branch not found.'}, 404
Ejemplo n.º 5
0
 def short_json(self):
     return {
         'name': self.name,
         'price': self.price,
         'available': self.available,
         'reserved_by': self.reserved_by,
         'year': self.year,
         'item_type': self.item_type,
         'vendor': self.vendor,
         'model': self.model,
         'branch': (BranchModel.find_by_id(self.branch_id)).name
     }
Ejemplo n.º 6
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.'}
Ejemplo n.º 7
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
Ejemplo n.º 8
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
Ejemplo n.º 9
0
    def get(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)
        is_admin = Item.is_admin()

        if item:
            if not is_admin:
                return item.short_json()
            return item.json()

        return {'message': 'Item not found.'}, 404
Ejemplo n.º 10
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.'}
Ejemplo n.º 11
0
    def get(self, branch_name="", param="", value_p=""):
        branch = BranchModel.find_by_name(branch_name)

        # /<non existent branch>/items
        # /<non existent branch>/items/<param>/<value_p>
        if not branch:
            return {'message': 'Branch not found.'}, 404

        if param == "item-type" and ItemModel.is_item_type(value_p):
            return {'items': [item.short_json() for item in ItemModel.query.filter_by(item_type=value_p, branch_id=branch.id)]}
        elif not param:
            return {'items': [item.short_json() for item in ItemModel.query.filter_by(branch_id=branch.id)]}
            # return {'items': [item.json() for item in ItemModel.query.all()]}  # list comprehension
        else:
            return {'message': 'Wrong parameters of request!'}, 400
Ejemplo n.º 12
0
    def get(self, branch_name, name):
        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)
        is_admin = Car.is_admin()

        if car:
            if not is_admin:
                return car.short_json()
            return car.json()

        return {'message': 'Car not found.'}, 404
Ejemplo n.º 13
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.'}
Ejemplo 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.'}
Ejemplo n.º 15
0
    def get(self, branch_name=""):
        is_admin = Position.is_admin()
        is_manager = Position.is_manager()

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

        # all branches
        if not branch_name:
            if not is_admin:
                return {'message': 'You are not privileged to continue!'}, 400
            return {
                'positions':
                [position.json() for position in PositionModel.query.all()]
            }

        # specific branch
        branch = BranchModel.find_by_name(branch_name)
        if not branch:
            return {'message': 'Branch not found.'}, 404

        positions_id = {
            user.position_id
            for user in UserModel.query.filter_by(branch_id=branch.id)
        }

        if not is_admin:
            if g.user.branch_id != branch.id:
                return {'message': 'You are not privileged to continue!'}, 400
            return {
                'positions': [
                    position.branch_short_json(branch.id)
                    for position in PositionModel.query.filter(
                        PositionModel.id.in_(positions_id)).all()
                ]
            }
        return {
            'positions': [
                position.branch_json(branch.id)
                for position in PositionModel.query.filter(
                    PositionModel.id.in_(positions_id)).all()
            ]
        }
Ejemplo n.º 16
0
    def get(self, branch_name="", param="", value_p=""):
        # return {'b': branch_name, 'p': param, 'v': value_p}
        branch = BranchModel.find_by_name(branch_name)

        # /<non existent branch>/cars
        # /<non existent branch>/cars/<param>/<value_p>
        if not branch:
            return {'message': 'Branch not found.'}, 404

        if param == "car-type" and CarModel.is_car_type(value_p):
            return {
                'cars': [
                    car.short_json()
                    for car in CarModel.query.filter_by(car_type=value_p,
                                                        branch_id=branch.id)
                ]
            }
        elif param == "transmission" and value_p == "automatic":
            return {
                'cars': [
                    car.short_json()
                    for car in CarModel.query.filter_by(transmission=value_p,
                                                        branch_id=branch.id)
                ]
            }
        elif param == "drive" and value_p == "4wd":
            return {
                'cars': [
                    car.short_json()
                    for car in CarModel.query.filter_by(drive=value_p,
                                                        branch_id=branch.id)
                ]
            }
        elif not param:
            return {
                'cars': [
                    car.short_json()
                    for car in CarModel.query.filter_by(branch_id=branch.id)
                ]
            }
        else:
            return {'message': 'Wrong parameters of request!'}, 400
Ejemplo n.º 17
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()
Ejemplo n.º 18
0
class Branch(Resource):
        parser = reqparse.RequestParser()
        parser.add_argument('brand_id',
                        type=int,
                        required=True,
                        help="Every branch needs a brand_id."

    def get(self, id):
        branch = BranchModel.find_by_id(id)
        if branch:
            return branch.json()
        return {'message': 'Branch not found'}, 404                   

    def post(self, id):
        if BranchModel.find_by_id(id):
            return {'message': "A branch with id '{}' already exists.".format(id)}, 400

        data = Branch.parser.parse_args()

        branch = BranchModel(id, **data)

        try:
            branch.save_to_db()
        except:
            return {"message": "An error occurred inserting the branch."}, 500

        return branch.json(), 201

    def delete(self, id):
        branch = BranchModel.find_by_id(id)
        if branch:
            branch.delete_from_db()

        return {'message': 'Branch deleted'}


class BranchList(Resource):
    def get(self):
        return {'branches': list(map(lambda x: x.json(), BranchModel.query.all()))}
Ejemplo n.º 19
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()
Ejemplo n.º 20
0
 def delete(self, id):
     branch = BranchModel.find_by_id(id)
     if branch:
Ejemplo n.º 21
0
 def post(self, id):
     if BranchModel.find_by_id(id):
         return {'message': "A branch with id '{}' already exists.".format(id)}, 400
Ejemplo n.º 22
0
 def get(self, id):
     branch = BranchModel.find_by_id(id)
     if branch: