Exemplo n.º 1
0
def find_or_create_lotgroup(parcels, address=None, **lotgroup_defaults):
    """
    Find or create a LotGroup for the given parcels. If there is only one lot
    in the parcels, just return that lot.
    """
    lots = []
    lotgroup = None
    for parcel in parcels:
        parcel_lots = find_or_create_lots(parcel, address=address)
        for lot in parcel_lots:
            if lot.group:
                lotgroup = lot.group
        lots += parcel_lots

    if len(lots) > 1:
        if lotgroup:
            lot = lotgroup
        else:
            lot = LotGroup(
                **get_lotgroup_kwargs(address=address, **lotgroup_defaults))
            lot.save()
        for member_lot in lots:
            member_lot.group = lot
            member_lot.save()
        return lot
    elif len(lots) == 1:
        lot = lots[0]
        # Intentionally don't get the address, trust the parcel's
        kwargs = get_lotgroup_kwargs(**lotgroup_defaults)
        Lot.objects.filter(pk=lot.pk).update(**kwargs)
        return lot
    return None
Exemplo n.º 2
0
def find_or_create_lotgroup(parcels, address=None, **lotgroup_defaults):
    """
    Find or create a LotGroup for the given parcels. If there is only one lot
    in the parcels, just return that lot.
    """
    lots = []
    lotgroup = None
    for parcel in parcels:
        parcel_lots = find_or_create_lots(parcel, address=address)
        for lot in parcel_lots:
            if lot.group:
                lotgroup = lot.group
        lots += parcel_lots

    if len(lots) > 1:
        if lotgroup:
            lot = lotgroup
        else:
            lot = LotGroup(**get_lotgroup_kwargs(address=address,
                                                 **lotgroup_defaults))
            lot.save()
        for member_lot in lots:
            member_lot.group = lot
            member_lot.save()
        return lot
    elif len(lots) == 1:
        lot = lots[0]
        # Intentionally don't get the address, trust the parcel's
        kwargs = get_lotgroup_kwargs(**lotgroup_defaults)
        Lot.objects.filter(pk=lot.pk).update(**kwargs)
        return lot
    return None
Exemplo n.º 3
0
    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()
Exemplo n.º 4
0
    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()
Exemplo n.º 5
0
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()
Exemplo n.º 6
0
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()