def handle(self, *args, **options): try: count = int(options['amount']) except: count = 1 try: with transaction.atomic(): for user in User.objects.all(): for i in range(0, count): p = Politician( user=user, first_name='Vorname', last_name='Nachname', email='*****@*****.**' ) p.save() except: print('Transaction failed') try: with open('export.csv', 'wb') as csvfile: writer = csv.writer(csvfile) for user in User.objects.all(): writer.writerow( [ user.username ] + [ x.unique_url for x in Politician.objects.filter(user=user) ] ) except: print('Export failed')
def handle(self, *args, **options): try: with open(options['json_file']) as f: data = json.load(f) for row in data: try: translation.activate(row.get('language')) politician = Politician( first_name=row.get('first_name'), last_name=row.get('last_name'), email=row.get('email'), is_member_of_parliament=row.get( 'is_member_of_parliament'), party=Party.objects.get( shortname=row.get('party')), user=User.objects.get(username=row.get('user')), ) politician.save() politician.state.add(*State.objects.filter( name__in=row.get('states'))) except: print('Politician creation failed') except Exception as e: print(str(e))
def handle(self, *args, **options): try: count = int(args[0]) except: count = 1 try: with transaction.atomic(): for user in User.objects.all(): for i in range(0, count): p = Politician( user=user, first_name='Vorname', last_name='Nachname', email='*****@*****.**' ) p.save() except: print('Transaction failed') try: with open('export.csv', 'wb') as csvfile: writer = csv.writer(csvfile) for user in User.objects.all(): writer.writerow( [ user.username ] + [ x.unique_url for x in Politician.objects.filter(user=user) ] ) except: print('Export failed')
def handle(self, *args, **options): try: rows = [] with open(options['csv_file'], 'rb') as csvfile: reader = csv.reader(csvfile) count = 0 for row in reader: row = [col.decode('utf-8') for col in row] count += 1 try: first_name = row[0] last_name = row[1] email = row[2] state = State.objects.get(name=row[3]) party = Party.objects.get(shortname=row[4]) user = User.objects.get(username=row[5]) member = bool(row[6]) p = Politician(user=user, first_name=first_name, last_name=last_name, email=email, state=state, party=party, is_member_of_parliament=member) p.save() rows.append([ first_name, last_name, email, state.name, party.shortname, user.username, p.unique_url ]) except Exception as e: print('Politician creation on line %d failed: %s' % (count, e)) with open('/tmp/export.csv', 'wb') as csvfile: writer = csv.writer(csvfile) for row in rows: writer.writerow([c.encode('utf8') for c in row]) print('Export file is located in /tmp/export.csv') except Exception as e: print(str(e)) print('Could not parse target file')
def handle(self, *args, **options): try: rows = [] with open(args[0], 'rb') as csvfile: reader = csv.reader(csvfile) count = 0 for row in reader: row = [col.decode('utf-8') for col in row] count += 1 try: first_name = row[0] last_name = row[1] email = row[2] state = State.objects.get(name=row[3]) party = Party.objects.get(shortname=row[4]) user = User.objects.get(username=row[5]) member = bool(row[6]) p = Politician( user=user, first_name=first_name, last_name=last_name, email=email, state=state, party=party, is_member_of_parliament=member ) p.save() rows.append([first_name, last_name, email, state.name, party.shortname, user.username, p.unique_url]) except Exception as e: print('Politician creation on line %d failed: %s' % (count, e)) with open('/tmp/export.csv', 'wb') as csvfile: writer = csv.writer(csvfile) for row in rows: writer.writerow([c.encode('utf8') for c in row]) print('Export file is located in /tmp/export.csv') except Exception as e: print(str(e)) print('Could not parse target file')