def create(self, row): first_name = row['Nombre'].strip() last_name = row['Apellido'].strip() email = row['EMail'].strip() patron = Patron( name="{} {}".format(first_name, last_name), email=email, comments='Automatically loaded with PyCamp 2018 script', ) patron.save() category = Category.objects.get(name=row["Tipo socio"].strip()) member = Member( category=category, patron=patron, has_student_certificate=row['C.Estud'].strip() == "✓", has_subscription_letter=row['Firmó'].strip() == "✓" ) member.save() street_address, city, zip_code, province, country = split_address(row['Domicilio'].strip()) person = Person( first_name=first_name, last_name=last_name, email=email, document_number=row['DNI'].strip(), nickname=row['Nick'].strip(), nationality=row['Nacionalidad'].strip(), marital_status=row['Estado Civil'].strip(), occupation=row['Profesión'].strip(), birth_date=get_date(row['Fecha Nacimiento'].strip()), street_address=street_address, city=city, zip_code=zip_code, province=province, country=country, membership=member, comments='Automatically loaded with PyCamp 2018 script', ) person.save() return member
def handle(self, *args, **options): if(len(args) != 1): self.stdout.write('Invalid number of arguments') exit() try: csvfile = open(args[0], 'r', encoding='utf-8') except: self.stdout.write('Unable to open file: ' + args[0]) exit() dialect = csv.Sniffer().sniff(csvfile.read(1024)) csvfile.seek(0) entries = csv.reader(csvfile, dialect) columns = len(next(entries)) csvfile.seek(0) entries = csv.reader(csvfile, dialect) if(options['first_row_is_label']): labels = next(entries) else: # make list of empty labels labels = [] for col in range(columns): if(col == options['date_column']): labels.append('Opskrivnings dato') elif (col == options['email_column']): labels.append('e-mail') elif (col == options['name_column']): labels.append('Navn') else: labels.append('') datetimeobject = datetime.datetime.today() for entry in entries: email = entry[options['email_column']].lower() name = entry[options['name_column']].title().strip() if(options['date_format'] == 0): date_format = '%m/%d/%Y %H:%M:%S %z' # '3/20/2014 19:51:32' date = datetimeobject.strptime(entry[options['date_column']].lstrip() + " +0001", date_format) if(options['date_format'] == 1): date_format = '%d. %m %Y, %H:%M %z' # '12. december 2014, 17:58' months = [ ('januar' , '1'), ('februar' , '2'), ('marts' , '3'), ('april' , '4'), ('maj' , '5'), ('juni' , '6'), ('juli' , '7'), ('august' , '8'), ('september' , '9'), ('oktober' , '10'), ('november' , '11'), ('december' , '12'),] date_string = entry[options['date_column']].lstrip() for month_name, month_number in months: date_string = date_string.replace(month_name, month_number) date = datetimeobject.strptime(date_string + " +0001", date_format) # find or create the family family, created = Family.objects.get_or_create(email = email) #lookup person try: person = Person.objects.get(name=name, family=family) # if current waiting list is older, replace timestamp if(date < person.added): person.added = date person.save() except ObjectDoesNotExist: # create the person person = Person(name=name, membertype=Person.CHILD, family = family, added = date) person.save() except Person.MultipleObjectsReturned: print("family " + family.email + " has duplicate mebers named " + name + " - signup date : " + str(date) + " not recorded")
def handle(self, *args, **options): if (len(args) != 1): self.stdout.write('Invalid number of arguments') exit() try: csvfile = open(args[0], 'r', encoding='utf-8') except: self.stdout.write('Unable to open file: ' + args[0]) exit() dialect = csv.Sniffer().sniff(csvfile.read(1024)) csvfile.seek(0) entries = csv.reader(csvfile, dialect) columns = len(next(entries)) csvfile.seek(0) entries = csv.reader(csvfile, dialect) if (options['first_row_is_label']): labels = next(entries) else: # make list of empty labels labels = [] for col in range(columns): if (col == options['date_column']): labels.append('Opskrivnings dato') elif (col == options['email_column']): labels.append('e-mail') elif (col == options['name_column']): labels.append('Navn') else: labels.append('') datetimeobject = datetime.datetime.today() for entry in entries: email = entry[options['email_column']].lower() name = entry[options['name_column']].title().strip() if (options['date_format'] == 0): date_format = '%m/%d/%Y %H:%M:%S %z' # '3/20/2014 19:51:32' date = datetimeobject.strptime( entry[options['date_column']].lstrip() + " +0001", date_format) if (options['date_format'] == 1): date_format = '%d. %m %Y, %H:%M %z' # '12. december 2014, 17:58' months = [ ('januar', '1'), ('februar', '2'), ('marts', '3'), ('april', '4'), ('maj', '5'), ('juni', '6'), ('juli', '7'), ('august', '8'), ('september', '9'), ('oktober', '10'), ('november', '11'), ('december', '12'), ] date_string = entry[options['date_column']].lstrip() for month_name, month_number in months: date_string = date_string.replace(month_name, month_number) date = datetimeobject.strptime(date_string + " +0001", date_format) # find or create the family family, created = Family.objects.get_or_create(email=email) #lookup person try: person = Person.objects.get(name=name, family=family) # if current waiting list is older, replace timestamp if (date < person.added): person.added = date person.save() except ObjectDoesNotExist: # create the person person = Person(name=name, membertype=Person.CHILD, family=family, added=date) person.save() except Person.MultipleObjectsReturned: print("family " + family.email + " has duplicate mebers named " + name + " - signup date : " + str(date) + " not recorded")