Beispiel #1
0
def parse_locs_from_csv(file_path, mapping, parse_range=None):
    """
    Parse location entries from a csv file.
    An extra field called mapping can be passed in for field mapping
    parse_range is the range of rows to be parsed from the csv.
    """

    location_dicts = []
    skipped = 0

    for csv_dict in csv_to_dict(file_path, machine_name=True):  # field mapping
        m = {}
        for app_field, csv_field in mapping.items():
            if csv_field:  # skip blank option
                m[clean_field_name(app_field)] = csv_dict.get(csv_field, '')

        # Check if row should be skipped here
        m['skipped'] = False

        m['hq'] = m.get('headquarters', '').lower() == 'true'

        location_dicts.append(m)

    total = len(location_dicts)
    stats = {
        'all': total,
        'skipped': skipped,
        'added': total - skipped,
    }
    return location_dicts, stats
Beispiel #2
0
def parse_locs_from_csv(file_path, mapping, parse_range=None):
    """
    Parse location entries from a csv file.
    An extra field called mapping can be passed in for field mapping
    parse_range is the range of rows to be parsed from the csv.
    """

    location_dicts = []
    skipped = 0

    for csv_dict in csv_to_dict(file_path, machine_name=True):  # field mapping
        m = {}
        for app_field, csv_field in mapping.items():
            if csv_field:  # skip blank option
                m[clean_field_name(app_field)] = csv_dict.get(csv_field, '')

        # Check if row should be skipped here
        m['skipped'] = False

        m['hq'] = m.get('headquarters','').lower() == 'true'

        location_dicts.append(m)

    total = len(location_dicts)
    stats = {
        'all': total,
        'skipped': skipped,
        'added': total-skipped,
    }
    return location_dicts, stats
Beispiel #3
0
    def __init__(self, *args, **kwargs):

        locport = kwargs.pop('locport')
        super(ImportMapForm, self).__init__(*args, **kwargs)

        #file_path = os.path.join(settings.MEDIA_ROOT, locport.get_file().file.name)
        file_path = str(locport.get_file().file.name)
        csv = csv_to_dict(file_path)

        # choices list
        choices = csv[0].keys()
        machine_choices = [slugify(c).replace('-', '') for c in choices]
        choice_tuples = zip(machine_choices, choices)

        choice_tuples.insert(0, ('', ''))  # insert blank option; top option
        choice_tuples = sorted(choice_tuples, key=lambda c: c[0].lower())

        native_fields = [
            'Location Name',
            'Description',
            'Contact',
            'Address',
            'Address 2',
            'City',
            'State',
            'Zipcode',
            'Country',
            'Phone',
            'Fax',
            'Email',
            'Website',
            'Latitude',
            'Longitude',
            'Headquarters',
        ]

        for native_field in native_fields:
            native_field_machine = slugify(native_field).replace('-', '')

            self.fields[native_field_machine] = forms.ChoiceField(
                **{
                    'label': native_field,
                    'choices': choice_tuples,
                    'required': False,
                })

            # compare required field with choices
            # if they match; set initial
            if native_field_machine in machine_choices:
                self.fields[
                    native_field_machine].initial = native_field_machine
Beispiel #4
0
    def __init__(self, *args, **kwargs):

        locport = kwargs.pop('locport')
        super(ImportMapForm, self).__init__(*args, **kwargs)
        
        #file_path = os.path.join(settings.MEDIA_ROOT, locport.get_file().file.name)
        file_path = str(locport.get_file().file.name)
        csv = csv_to_dict(file_path)

        # choices list
        choices = csv[0].keys()
        machine_choices = [slugify(c).replace('-','') for c in choices]
        choice_tuples = zip(machine_choices, choices)

        choice_tuples.insert(0, ('',''))  # insert blank option; top option
        choice_tuples = sorted(choice_tuples, key=lambda c: c[0].lower())
        
        native_fields = [
            'Location Name',
            'Description',
            'Contact',
            'Address',
            'Address 2',
            'City',
            'State',
            'Zipcode',
            'Country',
            'Phone',
            'Fax',
            'Email',
            'Website',
            'Latitude',
            'Longitude',
            'Headquarters',
        ]

        for native_field in native_fields:
            native_field_machine = slugify(native_field).replace('-','')

            self.fields[native_field_machine] = forms.ChoiceField(**{
                'label': native_field,
                'choices': choice_tuples,
                'required': False,
            })

            # compare required field with choices
            # if they match; set initial
            if native_field_machine in machine_choices:
                self.fields[native_field_machine].initial = native_field_machine