def school_spreadsheet_downloads(request, conference_slug, school_slug): conference = get_object_or_404(Conference, url_name=conference_slug) school = get_object_or_404(School, url_name=school_slug) if school_authenticate(request, conference, school): response = HttpResponse(mimetype='text/csv') writer = UnicodeCSVWriter(response) if 'country-committee-assignments' in request.GET: response['Content-Disposition'] = 'attachment; filename=country-committee-assignments-' + conference_slug + ".csv" committees = Committee.objects.filter(conference=conference) countries = Country.objects.filter(conference=conference) headers = ['Country'] for committee in committees: headers.append(committee.name) writer.writerow(headers) counts = DelegatePosition.objects.values('committee', 'country').annotate(count=Count('id')) count_dict = dict() for item in counts: count_dict[(item['country'], item['committee'])] = item['count'] for country in countries: row = [country.name] for committee in committees: row.append(str(count_dict.get((country.pk, committee.pk), 0))) writer.writerow(row) else: raise Http404 else: raise Http404 return response
def spreadsheet_downloads(request, conference_slug): conference = get_object_or_404(Conference, url_name=conference_slug) if secretariat_authenticate(request, conference): response = HttpResponse(mimetype='text/csv') writer = UnicodeCSVWriter(response) if 'sponsor-contacts' in request.GET: response['Content-Disposition'] = 'attachment; filename=sponsor-contacts-' + conference_slug + ".csv" writer.writerow(['School', 'First Name', 'Last Name', 'E-mail Address', 'Phone', 'Street Address','Address 2','City','State / Province / Region','ZIP / Postal Code','Country']) sponsors = FacultySponsor.objects.select_related().filter(conferences__url_name__exact=conference_slug) for sponsor in sponsors: school = sponsor.school writer.writerow([school.name, sponsor.user.first_name, sponsor.user.last_name, sponsor.user.email, sponsor.phone, school.address_line_1, school.address_line_2, school.city, school.state, school.zip, school.address_country]) elif 'delegates' in request.GET: response['Content-Disposition'] = 'attachment; filename=delegates-' + conference_slug + ".csv" writer.writerow(['School', 'Country', 'Committee', 'Title', 'First Name', 'Last Name']) delegates = Delegate.objects.select_related('position_assignment',\ 'position_assignment__country',\ 'position_assignment__school',\ 'position_assignment__committee').filter(position_assignment__country__conference__url_name__exact=conference_slug) for delegate in delegates: writer.writerow([delegate.position_assignment.school.name, delegate.position_assignment.country.name, delegate.position_assignment.committee.name, delegate.position_assignment.title, delegate.first_name, delegate.last_name]) elif 'school-country-assignments' in request.GET: response['Content-Disposition'] = 'attachment; filename=school-country-assignments-' + conference_slug + ".csv" writer.writerow(['Country', 'School']) countries = Country.objects.filter(conference=conference) assignments = DelegatePosition.objects.filter(country__in=countries).values('school__name','country__name','country__pk') assignment_dict = dict() for item in assignments: if item['school__name'] != None: assignment_dict.setdefault((item['country__name'],item['country__pk']), set()).add(item['school__name']) else: assignment_dict.setdefault((item['country__name'],item['country__pk']), set()) for country in countries: key = (country.name, country.pk) if assignment_dict.has_key(key): writer.writerow([country.name, ','.join(assignment_dict[key])]) elif 'country-committee-assignments' in request.GET: response['Content-Disposition'] = 'attachment; filename=country-committee-assignments-' + conference_slug + ".csv" committees = Committee.objects.filter(conference=conference) countries = Country.objects.filter(conference=conference) headers = ['Country'] for committee in committees: headers.append(committee.name) writer.writerow(headers) counts = DelegatePosition.objects.values('committee','country').annotate(count=Count('id')) count_dict = dict() for item in counts: count_dict[(item['country'],item['committee'])] = item['count'] for country in countries: row = [country.name] for committee in committees: row.append(str(count_dict.get((country.pk, committee.pk), 0))) writer.writerow(row) elif 'country-preferences' in request.GET: response['Content-Disposition'] = 'attachment; filename=country-preferences-' + conference_slug + ".csv" preferences = CountryPreference.objects.select_related().filter(request__conference=conference).order_by('request__school__name','last_modified') writer.writerow(['School','Rank','Country','Time Submitted']) current_school = "" rank = 1 for preference in preferences: if preference.request.school.name == current_school: rank = rank + 1 else: current_school = preference.request.school.name rank = 1 writer.writerow([current_school, str(rank), preference.country.name, preference.last_modified.strftime("%A, %d. %B %Y %I:%M%p")]) elif 'delegate-count-requests' in request.GET: response['Content-Disposition'] = 'attachment; filename=delegate-count-requests-' + conference_slug + ".csv" preferences = DelegateCountPreference.objects.select_related().filter(request__conference=conference).order_by('request__school__name','request__created') writer.writerow(['School','Total Delegates Requested','Time Submitted']) for preference in preferences: writer.writerow([preference.request.school.name, preference.delegate_count, preference.request.created.strftime("%A, %d. %B %Y %I:%M%p")]) else: raise Http404 else: raise Http404 return response