def elections_report(request): """ Creates an elections report """ q_param = request.GET.get('q', '') polls_count = voters_count = 0 percentage_voted = voters_voted_count = 0 elections = find_elections(request) report = ElectionsReport(elections) report.parse_object() elections_count = len(report.objectData) for election in report.objectData: polls_count += election['nr_polls'] voters_count += election['nr_voters'] voters_voted_count += election['nr_voters_voted'] if voters_count != 0: percentage_voted = (voters_voted_count / float(voters_count)) * 100 params = '' for key, value in list(request.GET.items()): params = params + key + '=' + value + '&' params = params[:-1] context = { 'elections_count': elections_count, 'polls_count': polls_count, 'voters_count': voters_count, 'voters_voted_count': voters_voted_count, 'percentage_voted': percentage_voted, 'elections': report.objectData, 'elections_per_page': 10, 'report_table_headers': list(REPORT_TABLE_HEADERS.items()), 'params': params, 'q': q_param } return render_template(request, "admin", context)
def elections_report(request): """ Creates an elections report """ q_param = request.GET.get('q', '') polls_count = votes_count = voters_count = 0 percentage_voted = voters_voted_count = 0 elections = find_elections(request) report = ElectionsReport(elections) report.parse_object() elections_count = len(report.objectData) for election in report.objectData: polls_count += election['nr_polls'] voters_count += election['nr_voters'] voters_voted_count += election['nr_voters_voted'] if not voters_count is 0: percentage_voted = (voters_voted_count / float(voters_count)) * 100 params = '' for key, value in request.GET.items(): params = params + key + '=' + value + '&' params = params[:-1] context = { 'elections_count': elections_count, 'polls_count': polls_count, 'voters_count': voters_count, 'voters_voted_count': voters_voted_count, 'percentage_voted': percentage_voted, 'elections': report.objectData, 'elections_per_page': 10, 'report_table_headers': REPORT_TABLE_HEADERS.items(), 'params': params, 'q': q_param } return render_template(request, "admin", context)
def get(request, *args, **kwargs): page = int(request.GET.get('page', 1)) limit = int(request.GET.get('limit', 10)) q_param = request.GET.get('q', '') default_elections_per_page = getattr(settings, 'ELECTIONS_PER_PAGE', 20) elections_per_page = request.GET.get('limit', default_elections_per_page) try: elections_per_page = int(elections_per_page) except: elections_per_page = default_elections_per_page order_by = request.GET.get('order', 'created_at') order_type = request.GET.get('order_type', 'desc') if not order_by in ELECTION_TABLE_HEADERS: order_by = 'name' elections = Election.objects.administered_by(request.admin) nr_unfiltered_elections = elections.count() if nr_unfiltered_elections == 0: return HttpResponseRedirect(reverse('election_create')) elections = elections.filter( get_filters(q_param, ELECTION_TABLE_HEADERS, ELECTION_SEARCH_FIELDS, ELECTION_BOOL_KEYS_MAP)) elections = elections.order_by(order_by) if order_type == 'desc': elections = elections.reverse() context = { 'is_superadmin': request.admin.superadmin_p, 'elections_administered': elections, 'election_table_headers': ELECTION_TABLE_HEADERS.iteritems(), 'q': q_param, 'page': page, 'elections_per_page': elections_per_page, } return render_template(request, "index", context)
def get(request, *args, **kwargs): page = int(request.GET.get('page', 1)) limit = int(request.GET.get('limit', 10)) q_param = request.GET.get('q', '') default_elections_per_page = getattr(settings, 'ELECTIONS_PER_PAGE', 20) elections_per_page = request.GET.get('limit', default_elections_per_page) try: elections_per_page = int(elections_per_page) except: elections_per_page = default_elections_per_page order_by=request.GET.get('order', 'created_at') order_type = request.GET.get('order_type', 'desc') if not order_by in ELECTION_TABLE_HEADERS: order_by = 'name' elections = Election.objects.administered_by(request.admin) nr_unfiltered_elections = elections.count() if nr_unfiltered_elections == 0: return HttpResponseRedirect(reverse('election_create')) elections = elections.filter(get_filters(q_param, ELECTION_TABLE_HEADERS, ELECTION_SEARCH_FIELDS, ELECTION_BOOL_KEYS_MAP)) elections = elections.order_by(order_by) if order_type == 'desc': elections = elections.reverse() context = { 'is_superadmin': request.admin.superadmin_p, 'elections_administered': elections, 'election_table_headers': ELECTION_TABLE_HEADERS.iteritems(), 'q': q_param, 'page': page, 'elections_per_page': elections_per_page, } return render_template(request, "index", context)