コード例 #1
0
ファイル: utils.py プロジェクト: BakethemPie/tendenci
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
コード例 #2
0
ファイル: forms.py プロジェクト: BakethemPie/tendenci
    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)
        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
コード例 #3
0
ファイル: utils.py プロジェクト: BakethemPie/tendenci
def parse_mems_from_csv(file_path, mapping, parse_range=None):
    """
    Parse membership entries from a csv file.
    An extra field called columns can be passed in for field mapping
    parse_range is the range of rows to be parsed from the csv.
    Entries without a Membership Type will be marked as skipped.
    Entries that are already in the database will be marked as skipped.
    """

    print 'mapping', mapping.keys()

    membership_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
                # membership['username'] = '******'
                m[clean_field_name(app_field)] = csv_dict.get(csv_field, '')

        username = m['username']
        try:
            user = User.objects.get(username=username)
        except User.DoesNotExist:

            username = clean_username(username)

            try:
                user = User.objects.get(username=username)
            except User.DoesNotExist:
                user = None
        
        # update full name and email
        if user:
            first_name = user.first_name
            last_name = user.last_name
            email = user.email
        else:
            first_name = m.get('firstname')
            last_name = m.get('lastname')
            email = m.get('email')
        
        if user:
            m['fullname'] = user.get_full_name()
        else:
            if first_name or last_name:
                m['fullname'] = "%s %s" % (first_name, last_name)
        m['email'] = email

        # skip importing a record if
        # membership type does not exist
        # membership record already exists

        #check if should be skipped or not
        m['skipped'] = False
        try:
            membership_type = MembershipType.objects.get(name = m['membershiptype'])
        except:
            # no memtype
            membership_type = None
            m['skipped'] = True
            skipped = skipped + 1
        
        if membership_type and user:
            # already exists
            mem_type_exists = Membership.objects.filter(user=user, membership_type=membership_type).exists()
            if mem_type_exists:
                m['skipped'] = True
                skipped = skipped + 1
        
        # detect if renewal
        m['renewal'] = bool(m.get('renewdate'))

        #update the dates
        try:
            join_dt = dt_parse(m['joindate'])
        except:
            join_dt = None
        try:
            renew_dt = dt_parse(m['renewdate'])
        except:
            renew_dt = None
        
        # tendenci 4 null date: 1951-01-01
        tendenci4_null_date = datetime(1951,1,1,0,0,0)
        if join_dt and join_dt <= tendenci4_null_date:
            join_dt = None
        if renew_dt and renew_dt <= tendenci4_null_date:
            renew_dt = None
        
        subscribe_dt = join_dt or datetime.now()
        
        try:
            expire_dt = dt_parse(m['expiredate'])
        except:
            if membership_type:
                expire_dt = membership_type.get_expiration_dt(join_dt=join_dt, renew_dt=renew_dt, renewal=m.get('renewal'))
            else:
                expire_dt = None
        
        m['joindt'] = join_dt
        m['renewdt'] = renew_dt
        m['expiredt'] = expire_dt
        m['subscribedt'] = subscribe_dt
        
        membership_dicts.append(m)
    
    total = len(membership_dicts)
    stats = {
        'all': total,
        'skipped': skipped,
        'added': total-skipped,
        }
    return membership_dicts, stats
コード例 #4
0
ファイル: forms.py プロジェクト: BakethemPie/tendenci
    def __init__(self, *args, **kwargs):
        """
        Dynamically create fields using the membership
        application chosen.  The choices provided to these
        dynamic fields are the csv import columns.
        """
        step_numeral, step_name = kwargs.pop('step', (None, None))
        app = kwargs.pop('app', '')
        file_path = kwargs.pop('file_path', '')

        super(CSVForm, self).__init__(*args, **kwargs)

        if step_numeral == 1:
            """
            Basic Form: Application & File Uploader
            """
            
            self.fields['app'] = forms.ModelChoiceField(
                label='Application', queryset=App.objects.all())

            self.fields['csv'] = forms.FileField(label="CSV File")
        
        if step_numeral == 2:
            """
            Basic Form + Mapping Fields
            """

            # file to make field-mapping form
            csv = csv_to_dict(file_path)

            # choices list
            choices = csv[0].keys()

            # make tuples; sort tuples (case-insensitive)
            choice_tuples = [(c,c) for c in csv[0].keys()]

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

            app_fields = AppField.objects.filter(app=app)

            native_fields = [
                'User Name',
                'Membership Type',
                'Corp. Membership Name',
                'Member Number',
                'Payment Method',
                'Join Date',
                'Renew Date',
                'Expire Date',
                'Owner',
                'Creator',
                'Status',
                'Status Detail',
            ]

            for native_field in native_fields:
                self.fields[slugify(native_field)] = ChoiceField(**{
                    'label': native_field,
                    'choices': choice_tuples,
                    'required': False,
                })

                # compare required field with choices
                # if they match; set initial
                if native_field in choices:
                    self.fields[slugify(native_field)].initial = native_field

            self.fields['user-name'].required = True
            self.fields['membership-type'].required = True

            for app_field in app_fields:
                for csv_row in csv:

                    if slugify(app_field.label) == 'membership-type':
                        continue  # skip membership type

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

                    # compare label with choices
                    # if label matches choice; set initial
                    if app_field.label in choices:
                        self.fields[app_field.label].initial = app_field.label
コード例 #5
0
ファイル: forms.py プロジェクト: BakethemPie/tendenci
    def __init__(self, *args, **kwargs):
        memport = kwargs.pop('memport')
        super(ImportMapForm, self).__init__(*args, **kwargs)
        
        app = memport.app
        file_path = os.path.join(settings.MEDIA_ROOT, memport.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())

        app_fields = AppField.objects.filter(app=app)
        
        native_fields = [
            'User Name',
            'Membership Type',
            'Corp. Membership Name',
            'Member Number',
            'Payment Method',
            'Join Date',
            'Renew Date',
            'Expire Date',
            'Owner',
            'Creator',
            'Status',
            'Status Detail',
        ]

        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

        self.fields['username'].required = True
        self.fields['membershiptype'].required = True

        for app_field in app_fields:
            for csv_row in csv:
            
                app_field_machine = slugify(app_field.label).replace('-','')

                if slugify(app_field_machine) == 'membershiptype':
                    continue  # skip membership type

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

                # compare label with choices
                # if label matches choice; set initial
                if app_field_machine in machine_choices:
                    self.fields[app_field_machine].initial = app_field_machine