Example #1
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
Example #2
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()