예제 #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."}
예제 #2
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.'}
예제 #3
0
    def delete(self):
        data1 = Position.parser.parse_args()

        position = PositionModel.find_by_id(data1["position_id"])
        if position:
            position.delete_from_db()
            return {"message": "Position deleted"}

        return {"error": "Position does not exist"}, 500
예제 #4
0
    def is_admin():
        is_user = Dashboard.is_user()
        if not is_user:
            return False

        user = g.user
        user_position = PositionModel.find_by_id(user.position_id)

        if user_position.name != Dashboard.admin:
            return False
        return True
예제 #5
0
    def is_manager():
        is_user = Continue.is_user()
        if not is_user:
            return False

        user = g.user
        user_position = PositionModel.find_by_id(user.position_id)

        if user_position.name != Continue.manager:
            return False
        return True
예제 #6
0
def is_admin():
    is_user = is_employee()
    if not is_user:
        return False

    user = g.user
    user_position = PositionModel.find_by_id(user.position_id)

    if user_position.name != admin:
        return False
    return True
예제 #7
0
    def is_admin():
        try:
            if g.customer:
                return False
        except:
            pass

        user = g.user
        user_position = PositionModel.find_by_id(user.position_id)

        if user_position.name != Branch.admin:
            return False

        return True
예제 #8
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.'}
예제 #9
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
예제 #10
0
    def get(self, user_name):
        try:
            if g.customer:
                return {'message': 'You are not privileged to continue!'}, 400
        except:
            pass

        position = PositionModel.find_by_id(g.user.position_id)
        if position.name != 'admin':
            return {
                'message': "You are not privileged to check user details!"
            }, 400

        user = UserModel.find_by_username(user_name)
        if user:
            return user.json()

        return {'message': "User '{}' not found.".format(user_name)}, 404
예제 #11
0
    def get(self):
        is_user = False
        try:
            if g.user:
                is_user = True
        except:
            pass

        if not is_user:
            return {'message': 'You are not privileged to continue!'}, 400
        else:
            user = g.user
            position = PositionModel.find_by_id(user.position_id)

            if position.name != 'admin':
                return {
                    'message': "You are not privileged to list users accounts!"
                }, 400

            return {'users': [user.json() for user in UserModel.query.all()]}
예제 #12
0
    def put(self):
        data = Positions.parser.parse_args()

        json_data = request.get_json(force=True)
        positions = json_data["positions"]

        for position in positions:
            if position["position_no"] != position["prev_position_no"]:
                matched_position = PositionModel.find_by_id(
                    position["position_id"])

                if matched_position:
                    matched_position.position_no = position["position_no"]

                    matched_position.save_to_db()
                    return {"edited_position": matched_position.json()}, 201

                # If task doesn't exist, error out
                else:
                    return {"message": "Position does not exist"}, 404
예제 #13
0
    def post(self):
        data = Dashboard.parser.parse_args()

        users = [Dashboard.it_specialist, Dashboard.customer_service]

        print(data['role'])

        if not Dashboard.is_user() and data['role'] == Dashboard.customer:
            print('CUSTOMER DASHBOARD')
            return {'message': 'passed'}

        position = PositionModel.find_by_id(g.user.position_id)
        role = position.name

        if Dashboard.is_admin() and role == data['role']:
            return {'message': 'passed'}
        elif Dashboard.is_manager() and role == data['role']:
            return {'message': 'passed'}
        elif Dashboard.is_user() and data['role'] == Dashboard.user and role in users:
            return {'message': 'passed'}

        return {'message': 'failed'}
예제 #14
0
 def get(self):
     data = Position.parser.parse_args()
     position = PositionModel.find_by_id(data["position_id"])
     if position:
         return position.json_task()
     return {"message": "Position not found"}, 404