Пример #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 get(self, name):
        is_admin = Position.is_admin()
        if not is_admin:
            return {'message': 'You are not privileged to continue!'}, 400

        position = PositionModel.find_by_name(name)
        if position:
            return position.json()
        return {'message': 'Position not found.'}, 404
Пример #4
0
    def post(self, ticker):
        data = request.get_json()

        new_position = PositionModel(ticker=data['ticker'],
                                     amount=data['amount'],
                                     price=data['price'],
                                     date=data['date'],
                                     user_id=get_jwt_identity())

        try:
            new_position.save_to_db()

            return {
                'message':
                f'Position created: {new_position.amount} shares of {new_position.ticker} at {new_position.price} on {new_position.date}. User id: {new_position.user_id}'
            }
        except:
            return {'message': 'Something went wrong'}, 500
Пример #5
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
Пример #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_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
Пример #8
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
Пример #9
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()
Пример #10
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
Пример #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.'}
Пример #12
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
Пример #13
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
Пример #14
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
Пример #15
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()]}
Пример #16
0
    def post(self):
        data = Position.parser.parse_args()
        if PositionModel.find_by_position(data["template_id"], data["task_id"],
                                          data["position_no"]):
            return {"message": "A position with this name already exists"}, 400

        position = PositionModel(data["template_id"], data["task_id"],
                                 data["position_no"])

        try:
            position.save_to_db()
        except:
            return {"message": "An error occured editing the position"}, 500

        return position.json(), 201
Пример #17
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'}
Пример #18
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.'}
Пример #19
0
    def put(self):
        data = Position.parser.parse_args()
        if PositionModel.find_by_position(data["template_id"], data["task_id"],
                                          data["position_no"]):
            return (
                {
                    "message":
                    "This position has already been allocated to this template"
                },
                500,
            )

        position = PositionModel(data["template_id"], data["task_id"],
                                 data["position_no"])

        try:
            position.save_to_db()
            return position.json(), 201
        except:
            return {"message": "An error occured inserting the position"}, 500
Пример #20
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
Пример #21
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