def find_or_create_lots(row, address=None):
    raw_address = (row.get('INDIVIDUAL_PARCEL', None) or
                   row.get('ADDRESS', None) or
                   address)
    address = fix_address(raw_address)
    lots = []
    for basereg_raw in row['BaseReg'].split(','):
        basereg = to_basereg(basereg_raw.strip())
        lot = find_or_create_lot(address, basereg, row['ZIP'])
        if lot:
            lots += (lot,)
    return lots
def load(filename=settings.DATA_ROOT + '/gardens.csv',
         create_organizers=False, append_to_existing=False):
    reader = csv.DictReader(open(filename, 'r'))

    gardens = itertools.groupby(reader, lambda r: r['GARDEN NAME'])

    # Default organizer type and use for all gardens
    organizer_type = OrganizerType.objects.get(name__exact='community based organization')
    steward_project_use = Use.objects.get(name__exact='community garden')

    for garden, parcels in gardens:
        lots = []
        lotgroup_kwargs = get_lotgroup_kwargs()
        organizer_kwargs = {}
        if create_organizers:
            organizer_kwargs.update({
                'type': organizer_type,
            })
        steward_kwargs = {
            'use': steward_project_use,
            'include_on_map': True,
        }

        for parcel in parcels:
            lotgroup_kwargs.update({
                'name': (fix_address(parcel['GARDEN NAME']) or
                         lotgroup_kwargs.get('name', None)),
                'address_line1': (fix_address(parcel['ADDRESS']) or
                                  lotgroup_kwargs.get('address_line1', None)),
                'postal_code': (parcel['ZIP'] or
                                lotgroup_kwargs.get('postal_code', None)),
            })
            if create_organizers:
                organizer_kwargs.update({
                    'name': (parcel['Main Contact'] or
                            organizer_kwargs.get('name', None)),
                    'phone': (parcel['Phone Number'] or
                            organizer_kwargs.get('phone', None)),
                    'email': (parcel['Email Address'] or
                            organizer_kwargs.get('email', None)),
                })
            steward_kwargs.update({
                'name': fix_address(parcel['GARDEN NAME']) or steward_kwargs.get('name', None),
                'support_organization': (parcel['Support Organization'] or
                                         organizer_kwargs.get('support_organization', None)),
                'external_id': (parcel.get('PROJECT ID', None) or
                                steward_kwargs.get('external_id', None)),
                'date_started': (parse_datetime(parcel.get('DATESTARTED', None)) or
                                 steward_kwargs.get('date_started', None)),
            })

            lots += find_or_create_lots(parcel)

        print 'Trying to add garden "%s"' % lotgroup_kwargs['name']
        print 'lots:', lots
        if not lots:
            print 'No lots found, skipping garden "%s"' % lotgroup_kwargs['name']
            continue

        print 'lotgroup_kwargs', lotgroup_kwargs
        if create_organizers:
            print 'organizer_kwargs', organizer_kwargs
        print 'steward_kwargs', steward_kwargs

        steward_project = None
        if append_to_existing:
            # Try to find existing steward project
            try:
                steward_project = StewardProject.objects.get(
                    name=steward_kwargs['name']
                )
            except Exception:
                pass

        if steward_project:
            # Look for steward_project's LotGroup
            try:
                lot_group = steward_project.content_object.lotgroup
            except Exception:
                # Create new LotGroup
                lot_group = LotGroup(**lotgroup_kwargs)
                lot_group.save()
                # Swap the steward project's old lot with the new group
                steward_project.content_object.group = lot_group
                steward_project.content_object.save()
                steward_project.content_object = lot_group
                steward_project.save()
            for member_lot in lots:
                member_lot.group = lot_group
                member_lot.save()
        else:
            # Group Lots (if len > 1)
            if len(lots) > 1:
                lot = LotGroup(**lotgroup_kwargs)
                lot.save()
                for member_lot in lots:
                    member_lot.group = lot
                    member_lot.save()
            else:
                lot = lots[0]

            if create_organizers and organizer_kwargs['name'] and organizer_kwargs['email']:
                organizer = Organizer(content_object=lot, **organizer_kwargs)
                organizer.save()
            else:
                organizer = None

            # Create a StewardProject for the (group of) lots
            steward_project = StewardProject(
                content_object=lot,
                organizer=organizer,
                **steward_kwargs
            )
            steward_project.save()