Exemplo n.º 1
0
 def post(cls):
     '''Add an apartment to the list of apartments'''
     data = api.payload
     title = data['title']
     apartment = ApartmentModel.fetch_by_title(title)
     if apartment:
         return {'message':'An apartment with that title already exists'}, 400
     else:
         apartment = ApartmentModel(**data)
         apartment.create_record()
         return apartment_schema.dump(apartment), 201
Exemplo n.º 2
0
 def get(cls, id:int):
     '''Get an apartment by its id'''
     apartment = ApartmentModel.fetch_by_id(id)
     if apartment:
         return apartment_schema.dump(apartment), 200
     else:
         return {'message':'That apartment does not exists'}, 404
Exemplo n.º 3
0
    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
Exemplo n.º 4
0
 def delete(cls, id: int):
     '''Delete an apartment based on its id'''
     apartment = ApartmentModel.fetch_by_id(id)
     if apartment:
         apartment.delete_record()
         return {'message':'apartment succesfully deleted'}, 200
     else:
         return {'message':'That apartment does not exists'}
Exemplo n.º 5
0
    def get(cls):
        '''Get a list of all apartments under the manager'''
        apartments = ApartmentModel.fetch_all()
        houses = []
        if apartments:
            apartment_list = apartments_schema.dump(apartments)
            apartment_ids = []
            for apartment in apartments:
                apartment_ids.append(apartment.id)
                index = 0
                while index < len(apartment_ids) - 1:
                    apartment = ApartmentModel.fetch_by_id(apartment_ids[index])
                    if apartment:
                        for house in apartment.houses:
                            houses.append(house.house_no)
                            house_dict = {'houses':houses}
                            apartment_list[index].update(house_dict)   
                            print(apartment_list)
                    index += 1
        # return apartment_list, 200

            # houses = []
            #     for house in apartment.houses:
            #             houses.append(house.house_no)
            #             houses_dict = {'houses':houses}
            #             index = 0
            # apartment_list = apartments_schema.dump(apartments)
            # while index < len(apartment_list) - 1:
            #     for apartment in apartment_list:
            #         apartment_list[index].update(houses_dict)
            #         index += 1
            #         print(houses)
            
            
            

            return apartment_list, 200
        else:
            return {'message':'That apartment does not exists'}, 404
Exemplo n.º 6
0
 def put(cls, id: int):
     '''edit an endpoint by first querying it by its id'''
     data = api.payload
     apartment = ApartmentModel.fetch_by_id(id)
     if apartment:
         if u'title' in data:
             apartment.title = data['title']
         if u'location' in data:
             apartment.location = data['location']
         if u'lanldlord_id' in data:
             apartment.landlord_id = data['landlord_id']
         return {'updated_apartment':apartment_schema.dump(apartment)}, 200
     else:
         return {'message':'That apartment does not exist'}, 404 
Exemplo n.º 7
0
    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
Exemplo n.º 8
0
 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
Exemplo n.º 9
0
    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