Esempio n. 1
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()
Esempio n. 2
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
Esempio n. 3
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
Esempio n. 4
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.'}