Example #1
0
 def post(self):
     data = CustomerRegister.parser.parse_args()
     print('data', data['address'])
     if UserModel.find_by_username(data['username']):
         return {'message': 'username already exists'}, 400
     if AgentModel.find_by_agent_id(data['agent_id']) is None:
         return {'message': 'Invalid agent Id'}, 400
     user = UserModel(data['username'],
                      data['password'],
                      usertype='customer')
     user.save_to_db()
     agent = AgentModel.find_by_agent_id(data['agent_id'])
     d, m, y = data['dob'].split('-')
     converted_dob = datetime(int(y), int(m), int(d))
     customer = CustomerModel(customer_id=user.id,
                              agent=agent,
                              name=data['name'],
                              dob=converted_dob,
                              email=data['email'],
                              address=data['address'],
                              contact_num=data['contact_num'],
                              alternate_num=data['alternate_num'])
     print('got it', customer.json())
     customer.save_to_db()
     return {'message': 'customer Registered Successfully'}, 201
Example #2
0
 def post(self):
     data = AgentRegister.user_reg_parser.parse_args()
     if UserModel.find_by_username(data['username']):
         return {'message': 'username already exists'}, 400
     user = UserModel(data['username'], data['password'], usertype='agent')
     user.save_to_db()
     print('user', user.id)
     agent = AgentModel(user.id, data['fullname'], data['email'],
                        data['commision_percentage'])
     agent.save_to_db()
     return {'message': 'user created successfully'}, 201
Example #3
0
    def post(self):
        data = AgentRegister.registerparser.parse_args()

        if AgentModel.find_by_username(data['username']):
            return {"message": "An agent with that username already exists"}, 409

        agent = AgentModel(**data)
        try:
            agent.save_to_db()
        except:
            return {"message": "An error occured registering agent"}, 500  

        return {"message": "Agent created successfully"}, 201
Example #4
0
    def post(self):
        data = AgentLogin.loginparser.parse_args()
        agent = AgentModel.find_by_username(data['username'],data['password'])
        if agent:
            return {"message": "Login Success", "agent": agent.json() }, 200

        return {"message": "Invalid username or password !"}, 401
Example #5
0
 def get(self):
     agent_id = get_jwt_identity()
     agent = AgentModel.find_by_agent_id(agent_id)
     if agent is None:
         return {'message': 'Invalid Agent Id'}, 400
     return {'agent': agent.json()}, 200
     return 200
Example #6
0
    def put(self):
        data = AgentUpdate.updateparser.parse_args()

        agent = AgentModel.find_by_username(data['username'])
        agent.password = data['password']
        agent.phone_number = data['phone_number']
        agent.vehicle_no = data['vehicle_no']
        agent.availability = data['availability']
        agent.type = data['type']

        try:
            agent.save_to_db()
        except:
            return {"message": "An error occured updating information of  agent"}, 500  

        return {"message": "Agent information updated successfully"}, 201
Example #7
0
 def delete(self):
     data = AgentDelete.deleteparser.parse_args()
     agent = AgentModel.find_by_username(data['username'])
     if agent:
         agent.delete_from_db()
     return {'message': 'Agent deleted'}
 def get(agent_id):
     agent = AgentModel.find_by_id(agent_id)
     if agent:
         return agent.json()
     return {'message': 'Agent not found'}, 404
Example #9
0
 def get(self):
     agents = AgentModel.find_all()
     return {'agents': [agent.json() for agent in agents]}, 200