Example #1
0
def make_unit(apt_num, building):
    #check for existing:
    units = Unit.objects.filter(building=building).filter(number=apt_num)
    unit = None
    #check if a previous building object in the db exists
    if units.exists():
        unit = units[0]
        print "Already had Unit: %s" % unit.address
    else:
        #if not, 
        #CREATE A NEW UNIT OBJECT HERE
        unit = Unit()
        unit.building = building
        unit.number = apt_num
        # don't want to set this unless it's different:
        #unit.address = building.address + ", " + apt_num

        ## bedrooms
        ## bathrooms
        ## sqft
        ## max_occupants
        unit.save()
        print "Created new unit: %s" % unit.number

    return unit
                        #loop is fine
                        elif bmunit.number == unit:
                            matched_unit = bmunit

                    if not matched_unit:
                        #if we didn't have a matching unit already,
                        #use either an existing blank unit
                        if blank_unit:
                            if blank_count > 1:
                                raise ValueError, "More than one blank unit found. This shouldn't happen"
                            else:
                                blank_unit.number = unit
                                blank_unit.save()
                        #or create a new one
                        else:
                            nunit = Unit()
                            nunit.building = building_match
                            nunit.number = unit
                            #TODO: DON'T DO THIS!
                            #nunit.address = building_match.address + ", " + unit
                            nunit.save()

                    #at this point assume we've created a corresponding unit on
                    #the building_match object...
                    #delete the original:
                    building.delete()
                    building = building_match
                else:
                    #convert this building to be a main building with a single unit
                    print "Converting building from: %s to %s" % (building.address, street)
                    building.address = street
                        #loop is fine
                        elif bmunit.number == unit:
                            matched_unit = bmunit

                    if not matched_unit:
                        #if we didn't have a matching unit already,
                        #use either an existing blank unit
                        if blank_unit:
                            if blank_count > 1:
                                raise ValueError, "More than one blank unit found. This shouldn't happen"
                            else:
                                blank_unit.number = unit
                                blank_unit.save()
                        #or create a new one
                        else:
                            nunit = Unit()
                            nunit.building = building_match
                            nunit.number = unit
                            #TODO: DON'T DO THIS!
                            #nunit.address = building_match.address + ", " + unit
                            nunit.save()

                    #at this point assume we've created a corresponding unit on
                    #the building_match object...
                    #delete the original:
                    building.delete()
                    building = building_match
                else:
                    #convert this building to be a main building with a single unit
                    print "Converting building from: %s to %s" % (
                        building.address, street)