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(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: 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 candidates_view(request): politician_list = (Politician.objects.filter( statistic__id__gt=0).distinct().order_by('first_name', 'last_name')) states = State.objects.all().order_by('name') categories = (Category.objects.filter( statistic__id__gt=0).order_by('name').distinct()) category = request.GET.get('category', None) state = request.GET.get('state', None) search = request.GET.get('search', None) per_site = request.GET.get('per_page', 10) page = request.GET.get('page', 1) request.GET = request.GET.copy() stat_cookie = get_cookie(request, 'statistics', {}) if 'evaluate' in request.GET and stat_cookie == {}: request.GET.pop('evaluate') if category and category != '0': if 'evaluate' in request.GET: stats = get_cookie(request, 'statistics', {}) val = stats.get('category_%s' % category, 0) else: val = Question.objects.filter(category__id=category).aggregate( Sum('preferred_answer'))['preferred_answer__sum'] politician_list = (Politician.get_politicians_by_category( category, (int(val) * 10))) if state and state != '0': politician_list = politician_list.filter(state=state) if search: politician_list = politician_list.filter( Q(last_name__icontains=search) | Q(first_name__icontains=search) | Q(state__name__icontains=search) | Q(party__name__icontains=search) | Q(party__shortname__icontains=search)) paginator = Paginator(politician_list, per_site) try: politicians = paginator.page(page) except PageNotAnInteger: politicians = paginator.page(1) except EmptyPage: politicians = paginator.page(paginator.num_pages) # remove the page parameter from url if request.GET.get('page', None): request.GET.pop('page') return render(request, 'core/candidates/index.html', { 'politicians': politicians, 'categories': categories, 'states': states, })
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')
def candidates_view(request): politician_list = ( Politician.objects. filter(statistic__id__gt=0).distinct(). order_by('first_name', 'last_name')) states = State.objects.all().order_by('name') categories = ( Category.objects.filter(statistic__id__gt=0). order_by('name').distinct()) category = request.GET.get('category', None) state = request.GET.get('state', None) search = request.GET.get('search', None) per_site = request.GET.get('per_page', 10) page = request.GET.get('page', 1) request.GET = request.GET.copy() stat_cookie = get_cookie(request, 'statistics', {}) if 'evaluate' in request.GET and stat_cookie == {}: request.GET.pop('evaluate') if category and category != '0': if 'evaluate' in request.GET: stats = get_cookie(request, 'statistics', {}) val = stats.get('category_%s' % category, 0) else: val = Question.objects.filter( category__id=category ).aggregate(Sum('preferred_answer'))['preferred_answer__sum'] politician_list = ( Politician.get_politicians_by_category( category, (int(val) * 10))) if state and state != '0': politician_list = politician_list.filter(state=state) if search: politician_list = politician_list.filter( Q(last_name__icontains=search) | Q(first_name__icontains=search) | Q(state__name__icontains=search) | Q(party__name__icontains=search) | Q(party__shortname__icontains=search) ) paginator = Paginator(politician_list, per_site) try: politicians = paginator.page(page) except PageNotAnInteger: politicians = paginator.page(1) except EmptyPage: politicians = paginator.page(paginator.num_pages) # remove the page parameter from url if request.GET.get('page', None): request.GET.pop('page') return render( request, 'core/candidates/index.html', { 'politicians' : politicians, 'categories' : categories, 'states' : states, } )