def post(cls): """Add a tenant to a certain apartment""" data = api.payload email = data["email"] tenant = TenantModel.fetch_by_email(email) if tenant: return {"message": "A tenant by that email already exists"}, 400 else: tenant = TenantModel(**data) tenant.create_record() return tenant_schema.dump(tenant), 201
def get(cls): """Get a list of all tenants in a certain apartment""" tenants = TenantModel.fetch_all() if tenants: return tenants_schema.dump(tenants), 200 else: return {"message": "There are no tenants in this apartment"}, 404
def put(cls, id:int): """Edit a house by first querying it by its id""" data = api.payload house = HouseModel.fetch_by_id(id) if house: if u"number_of_bedrooms" in data: house.number_of_bedrooms = data["number_of_bedrooms"] if u"rent" in data: house.rent = data["rent"] if u"house_no" in data: house.house_no = data["house_no"] if u"tenant_id" in data: house.tenant_id = data["tenant_id"] house.add_record() house_dict = house_schema.dump(house) #Get Tenant name tenant_name = TenantModel.fetch_by_id(house.tenant_id).full_name tenant_dict = {'Tenant': tenant_name} house_dict.update(tenant_dict) #Get Block Name block_name = ApartmentModel.fetch_by_id(house.apartment_id).title block = {'Block':block_name} house_dict.update(block) return ( { "message": "House successfully updated", "updated_house": house_dict, }, 200, ) else: return {"message": "The house you are trying to edit does not exists"}, 404
def delete(cls, id: int): """delete a tenant by their id""" tenant = TenantModel.fetch_by_id(id) if tenant: tenant.delete_record() return {"message": "Tenant deleted successfully"}, 200 else: return {"message": "That tenant does not exists"}, 404
def get(cls, id: int): """Get a tenant by their id""" tenant = TenantModel.fetch_by_id(id) if tenant: houses = tenant.house tenant_dict = tenant_schema.dump(tenant) house = [house.house_no for house in houses] tenant_dict.update({"house": house[0]}) return tenant_dict, 200 else: return {"message": "That tenant does not exists"}, 404
def put(cls, id: int): """Edit tenant's details after querying them by their id""" data = api.payload tenant = TenantModel.fetch_by_id(id) if tenant: if u"full_name" in data: tenant.full_name = data["full_name"] if u"phone_number" in data: tenant.phone_number = data["phone_number"] if u"phone_number" in data: tenant.phone_number = data["phone_number"] return {"message": "Tenant details updated succesfully"}, 200 else: return {"message": "That could not be found"}
def get(cls, id:int): """Get a house based on its id""" house = HouseModel.fetch_by_id(id) if house: #GET TENANT NAME tenant = TenantModel.fetch_by_id(house.tenant_id) house_dict = house_schema.dump(house) tenant_dict = {"tenant": tenant.full_name} house_dict.update(tenant_dict) #GET BLOCK NAME block = ApartmentModel.fetch_by_id(house.apartment_id).title block_dict = {'Block': block} house_dict.update(block_dict) return house_dict, 200 else: return {"message": "That house does not exist"}, 404
def post(cls): """Add a house to the list of houses in an apartment""" data = api.payload house_no = data["house_no"] house = HouseModel.fetch_by_house_no(house_no) if house: return {"message": "THat house already exists in the system"}, 400 else: house = HouseModel(**data) house.add_record() tenant = TenantModel.fetch_by_id(house.tenant_id) if tenant: tenant_dict = {'tenant':tenant.full_name} house_dict = house_schema.dump(house) apartment = ApartmentModel.fetch_by_id(house.apartment_id) apartment_dict = {'Block':apartment.title} house_dict.update(tenant_dict) house_dict.update(apartment_dict) return house_dict, 201 else: house.delete_record() return {'message':'Please add a valid tenant'}, 400
def get(cls): """Get a list of all houses in an apartment""" houses = HouseModel.fetch_all() if houses: house_list = houses_schema.dump(houses) index = 0 while index < len(house_list) - 1: #GET TENANT NAME tenant_id = house_list[index]['tenant_id'] tenant_name = TenantModel.fetch_by_id(tenant_id).full_name tenant_dict = {'Tenant' :tenant_name} house_dict_list = houses_schema.dump(houses) for each_dict in house_dict_list: each_dict.update(tenant_dict) #GET APARTMENT NAME AS BLOCK apartment_id = house_list[index]['apartment_id'] apartment_name = ApartmentModel.fetch_by_id(apartment_id).title apartment_dict = {'Block': apartment_name} # index += 1 index += 1 # tenant_id += 1 # apartment_id += 1 # house_dict_list = houses_schema.dump(houses) # print(each_dict) # each_dict.update(apartment_dict) return house_dict_list, 200 else: return {"message": "THere are no houses to display at the moment"}, 404