def load_lot_groups(self, lot_groups_file): for row in csv.DictReader(lot_groups_file): print row try: lot = Lot.objects.get(bbl=row['bbl']) parent_lot = Lot.objects.get(bbl=row['parent']) except Lot.DoesNotExist: print "Couldn't find lot, skipping" continue try: # Try to get parent lot's lotgroup, add to that lot_group = LotGroup.objects.get(lot=parent_lot) lot_group.add(lot) except Lot.DoesNotExist: # Else create a lotgroup, add both to it lot_group = LotGroup(**{ 'address_line1': parent_lot.address_line1, 'borough': parent_lot.borough, 'known_use': parent_lot.known_use, 'name': parent_lot.name, 'postal_code': parent_lot.postal_code, }) lot_group.save() lot_group.update(lots=(parent_lot, lot)) # Assume previous lot had the group's name, not its own name. # Delete since the group now has it. parent_lot.name = None parent_lot.save()
def add_steward_project(self, lot, actual_use=None): """ Add steward project to lot if it is a garden that's being imported. """ use, name, external_id = actual_use.split('|') unique_kwargs = { 'project_name': name, 'external_id': external_id, } defaults = { 'include_on_map': True, 'started_here': False, 'use': lot.known_use, } type_kwargs = { 'content_type': ContentType.objects.get_for_model(lot), 'object_id': lot.pk, } # If we don't have a name to make the project unique, add lot id if not name: unique_kwargs.update(**type_kwargs) else: defaults.update(**type_kwargs) steward_project, created = StewardProject.objects.get_or_create( defaults=defaults, **unique_kwargs ) # If the project already existed let's add this lot to the project if not created: parent_lot = steward_project.content_object try: # Try to get parent lot's lotgroup and add to that lot_group = parent_lot.lotgroup lot_group.add(lot) except Lot.DoesNotExist: # Else create a lotgroup, add both to it lot_group = LotGroup(**{ 'address_line1': parent_lot.address_line1, 'borough': parent_lot.borough, 'known_use': parent_lot.known_use, 'name': parent_lot.name, 'postal_code': parent_lot.postal_code, }) lot_group.save() lot_group.update(lots=(parent_lot, lot)) # Update the object for this project steward_project.content_object = lot_group steward_project.save() lot.steward_inclusion_opt_in = True lot.save()