project/scipycon/registration/views.py
changeset 194 808abc104b55
parent 193 ed5828a90109
child 202 b4391b3d5fcc
child 236 29ecd3dd6565
equal deleted inserted replaced
193:ed5828a90109 194:808abc104b55
   254     }))
   254     }))
   255 
   255 
   256 
   256 
   257 @login_required
   257 @login_required
   258 def regstats(request, scope,
   258 def regstats(request, scope,
   259                  template_name='registration/regstats.html'):
   259              template_name='registration/regstats.html'):
   260     """View that gives the statistics of registrants.
   260     """View that gives the statistics of registrants.
   261     """
   261     """
   262     from project.scipycon.registration.forms import RegistrationAdminSelectForm
   262 
   263     if not request.user.is_staff:
   263     if not request.user.is_staff:
   264         redirect_to = reverse('scipycon_login', kwargs={'scope': scope})
   264         redirect_to = reverse('scipycon_login', kwargs={'scope': scope})
   265 
   265 
   266     if request.method == "POST":
   266 
   267         form = RegistrationAdminSelectForm(request.POST)
   267     q = Registration.objects.all()
   268         if form.is_valid():
   268     conf_num = q.filter(conference=True).count()
   269             conference = form.cleaned_data['by_conference']
   269     tut_num = q.filter(tutorial=True).count()
   270             tutorial = form.cleaned_data['by_tutorial']
   270     sprint_num = q.filter(sprint=True).count()
   271             sprint = form.cleaned_data['by_sprint']
   271 
   272             include = form.cleaned_data['include']
       
   273 
       
   274             q = Registration.objects.all()
       
   275             q = q.filter(conference=conference)
       
   276             q = q.filter(tutorial=tutorial)
       
   277             q = q.filter(sprint=sprint)
       
   278 
       
   279             q = q.order_by('registrant__email')
       
   280 
       
   281             query = q.query
       
   282             results = list(q)
       
   283 
       
   284             if include == []:
       
   285                 # default to include all fields
       
   286                 include = [i[0] for i in IC]
       
   287             if results:
       
   288                 response = HttpResponse(mimetype='text/csv')
       
   289                 response['Content-Disposition'] = 'attachment; filename=registrations.csv'
       
   290                 output = csv.writer(response)
       
   291                 output.writerow([h for h in include])
       
   292                 for row in results:
       
   293                     conference = row.conference == True and 'yes' or 'no'
       
   294                     tutorial = row.tutorial == True and 'yes' or 'no'
       
   295                     sprint = row.sprint == True and 'yes' or 'no'
       
   296                     wrow = []
       
   297                     if 'Name' in include:
       
   298                         wrow.append(
       
   299                             row.registrant.get_full_name().encode('utf-8'))
       
   300                     if 'Email' in include:
       
   301                         wrow.append(row.registrant.email.encode('utf-8'))
       
   302                     if 'Organisation' in include:
       
   303                         wrow.append(row.organisation.encode('utf-8'))
       
   304                     if 'Conference' in include:
       
   305                         wrow.append(conference)
       
   306                     if 'Tutorial' in include:
       
   307                         wrow.append(tutorial)
       
   308                     if 'Sprint' in include:
       
   309                         wrow.append(sprint)
       
   310                     output.writerow(wrow)
       
   311                 return response
       
   312             else:
       
   313                 no_results = u'No results found for the query'
       
   314 
       
   315     else:
       
   316         form = RegistrationAdminSelectForm()
       
   317     return render_to_response(template_name, RequestContext(request,
   272     return render_to_response(template_name, RequestContext(request,
   318         locals()))
   273         {'params': {'scope': scope},
       
   274          'conf_num': conf_num, 
       
   275          'tut_num': tut_num,
       
   276          'sprint_num': sprint_num,
       
   277          }))