def post(self):
     """
     Creates a new customer.
     """
     if not (0 <= request.json["pin"] <= 99999):
         raise UnprocessableEntity("The PIN must be of 5 digits")
     new_customer = Customer(
         customer_mail_address=request.json['mail_address'],
         customer_first_name=request.json['first_name'],
         customer_last_name=request.json['last_name'],
         customer_pin_hash=str(request.json['pin']))
     print(new_customer.customer_pin_hash)
     db.session.add(new_customer)
     try:
         db.session.commit()
     except OperationalError:
         db.session.remove()
         raise InternalServerError(
             description='Customer table does not exists.')
     except IntegrityError:
         db.session.remove()
         raise Conflict(description=new_customer.__repr__() +
                        ' already exists')
     current_app.logger.info(new_customer.__repr__() +
                             ' added to database.')
     return {'message': 'Resource created'}, 201
Example #2
0
def register_admin_customer():
    admin = Customer(customer_mail_address='*****@*****.**',
                     customer_first_name='admin',
                     customer_last_name='admin',
                     customer_pin_hash='12345')
    admin.customer_is_admin = True
    db.session.add(admin)
    try:
        db.session.commit()
    except IntegrityError:
        db.session.remove()
        raise Conflict(description=admin.__repr__() + ' already exists')
    response = {
        'mail_address': admin.customer_mail_address,
        'pin': 12345,
        'message': 'Login to get an admin token'
    }
    return response, 200