project/kiwipycon/registration/views.py
changeset 1 fda1c66b25f9
equal deleted inserted replaced
0:9a77edda77b7 1:fda1c66b25f9
       
     1 # -*- coding: utf-8 -*-
       
     2 from __future__ import absolute_import
       
     3 
       
     4 import cStringIO as StringIO
       
     5 import csv
       
     6 
       
     7 # django
       
     8 from django.conf import settings
       
     9 from django.shortcuts import render_to_response
       
    10 from django.template.loader import render_to_string
       
    11 from django.shortcuts import get_object_or_404
       
    12 from django.template import RequestContext
       
    13 from django.core.urlresolvers import reverse
       
    14 from django.http import HttpResponse
       
    15 
       
    16 from django.contrib.auth.decorators import login_required
       
    17 from django.contrib.auth.forms import AuthenticationForm
       
    18 from django.contrib.auth.models import User
       
    19 from django.core.exceptions import ObjectDoesNotExist
       
    20 
       
    21 #kiwipycon
       
    22 from project.kiwipycon.utils import set_message_cookie
       
    23 from project.kiwipycon.utils import slugify
       
    24 from project.kiwipycon.user.models import UserProfile
       
    25 from project.kiwipycon.user.utils import kiwipycon_createregistrant
       
    26 from project.kiwipycon.user.forms import RegistrantForm
       
    27 from project.kiwipycon.sponsor.models import Sponsor
       
    28 from project.kiwipycon.talk.models import Talk
       
    29 
       
    30 from .models import Registration
       
    31 from .models import Wifi
       
    32 from .forms import RegistrationSubmitForm
       
    33 from .forms import RegistrationEditForm
       
    34 from .forms import RegistrationAdminSelectForm
       
    35 from .forms import WifiForm
       
    36 from .utils import send_confirmation
       
    37 
       
    38 from .forms import IC
       
    39 
       
    40 REG_TOTAL = 1000
       
    41 
       
    42 @login_required
       
    43 def download_csv(request,
       
    44         template_name = 'registration/download-csv.html'):
       
    45     """
       
    46     """
       
    47     if not request.user.is_staff:
       
    48         redirect_to = reverse('kiwipycon_login')
       
    49     if request.method == "POST":
       
    50         form = RegistrationAdminSelectForm(request.POST)
       
    51         if form.is_valid():
       
    52             conference = form.cleaned_data['by_conference']
       
    53             tutorial = form.cleaned_data['by_tutorial']
       
    54             sprint = form.cleaned_data['by_sprint']
       
    55             amount = form.cleaned_data['by_amount']
       
    56             tshirt = form.cleaned_data['by_tshirt']
       
    57             order_by = form.cleaned_data['order_by']
       
    58             include = form.cleaned_data['include']
       
    59             q = Registration.objects.all()
       
    60             if conference == 'conference':
       
    61                 q = q.filter(conference=True)
       
    62             elif conference == 'no conference':
       
    63                 q = q.filter(conference=False)
       
    64             elif tutorial == 'tutorial':
       
    65                 q = q.filter(tutorial=True)
       
    66             elif tutorial == 'no tutorial':
       
    67                 q = q.filter(tutorial=False)
       
    68             if sprint == 'sprint':
       
    69                 q = q.filter(sprint=True)
       
    70             if sprint == 'no sprint':
       
    71                 q = q.filter(sprint=False)
       
    72             elif tshirt != 'all':
       
    73                 q = q.filter(tshirt=tshirt)
       
    74             q = q.order_by('registrant__email')
       
    75             query = q.query
       
    76             results = list(q)
       
    77             if include == []:
       
    78                 # default to include all fields
       
    79                 include = [i[0] for i in IC]
       
    80             if results:
       
    81                 response = HttpResponse(mimetype='text/csv')
       
    82                 response['Content-Disposition'] = 'attachment; filename=registrations.csv'
       
    83                 output = csv.writer(response)
       
    84                 output.writerow([h for h in include])
       
    85                 for row in results:
       
    86                     conference = row.conference == True and 'yes' or 'no'
       
    87                     tutorial = row.tutorial == True and 'yes' or 'no'
       
    88                     sprint = row.sprint == True and 'yes' or 'no'
       
    89                     wrow = []
       
    90                     if 'Name' in include:
       
    91                         wrow.append(
       
    92                             row.registrant.get_full_name().encode('utf-8'))
       
    93                     if 'Email' in include:
       
    94                         wrow.append(row.registrant.email.encode('utf-8'))
       
    95                     if 'Organisation' in include:
       
    96                         wrow.append(row.organisation.encode('utf-8'))
       
    97                     if 'Conference' in include:
       
    98                         wrow.append(conference)
       
    99                     if 'Tutorial' in include:
       
   100                         wrow.append(tutorial)
       
   101                     if 'Sprint' in include:
       
   102                         wrow.append(sprint)
       
   103                     if 'T-size' in include:
       
   104                         wrow.append(row.tshirt)
       
   105                     output.writerow(wrow)
       
   106                 return response
       
   107             else:
       
   108                 no_results = u'No results found for the query'
       
   109 
       
   110     else:
       
   111         form = RegistrationAdminSelectForm()
       
   112     return render_to_response(template_name, RequestContext(request,
       
   113         locals()))
       
   114 
       
   115 # NOT REQUIRED FOR SciPy.in
       
   116 @login_required
       
   117 def invoice(request, template_name='registration/invoice.html'):
       
   118     user = request.user
       
   119     registration = get_object_or_404(Registration, registrant=user)
       
   120     if registration.sponsor:
       
   121         redirect_to = reverse('kiwipycon_account')
       
   122         return set_message_cookie(redirect_to,
       
   123                 msg = u'You are a sponsored guest, no payment required.')
       
   124     return render_to_response(template_name, RequestContext(request,
       
   125         {'registration' : registration, 'user': user}))
       
   126 
       
   127 @login_required
       
   128 def pdf_invoice(request, template_name='registration/invoice.html'):
       
   129     user = request.user
       
   130     registration = get_object_or_404(Registration, registrant=user)
       
   131     if registration.sponsor:
       
   132         redirect_to = reverse('kiwipycon_account')
       
   133         return set_message_cookie(redirect_to,
       
   134                 msg = u'You are a sponsored guest, no payment required.')
       
   135     content = render_to_string(template_name,
       
   136         {'registration' : registration, 'user': user})
       
   137     result = StringIO.StringIO()
       
   138     import ho.pisa
       
   139     pdf = ho.pisa.pisaDocument(StringIO.StringIO(content.encode("UTF-8")),result)
       
   140     if not pdf.err:
       
   141         return HttpResponse(result.getvalue(), mimetype='application/pdf')
       
   142     return HttpResponse("Gremlins ate your invoice, please try html" \
       
   143         " version")
       
   144 
       
   145 
       
   146 def registrations(request,
       
   147         template_name='registration/registrations.html'):
       
   148     """Simple page to count registrations"""
       
   149     #registrations = Registration.objects.filter(payment=True).count()
       
   150     registrations = Registration.objects.all().count()
       
   151     return render_to_response(template_name, RequestContext(request,
       
   152         {
       
   153         'over_reg' : registrations >= REG_TOTAL and True or False,
       
   154             'registrations' : registrations}))
       
   155 
       
   156 @login_required
       
   157 def edit_registration(request, id,
       
   158         template_name='registration/edit-registration.html'):
       
   159     '''Allows users that submitted a registration to edit it.
       
   160     '''
       
   161     reg = Registration.objects.get(pk=id)
       
   162 
       
   163     if reg.registrant != request.user:
       
   164         redirect_to = reverse('kiwipycon_account')
       
   165         return set_message_cookie(redirect_to,
       
   166                 msg = u'Redirected because the registration you selected' \
       
   167                       + ' is not your own.')
       
   168 
       
   169     if request.method == 'POST':
       
   170         form = RegistrationEditForm(data=request.POST)
       
   171         if form.is_valid():
       
   172             reg.organisation = form.data.get('organisation')
       
   173             reg.occupation = form.data.get('occupation')
       
   174             reg.city = form.data.get('city')
       
   175             reg.tshirt = form.data.get('tshirt')
       
   176             reg.allow_contact = form.data.get('allow_contact') and True or False
       
   177             reg.conference = form.data.get('conference') and True or False
       
   178             reg.tutorial = form.data.get('tutorial') and True or False
       
   179             reg.sprint = form.data.get('sprint') and True or False
       
   180             reg.save()
       
   181             # Saved.. redirect
       
   182             redirect_to = reverse('kiwipycon_account')
       
   183             return set_message_cookie(redirect_to,
       
   184                     msg = u'Your changes have been saved.')
       
   185     else:
       
   186         form = RegistrationEditForm(initial={
       
   187                                     'id' : id,
       
   188                                     'organisation' : reg.organisation,
       
   189                                     'occupation' : reg.occupation,
       
   190                                     'city' : reg.city,
       
   191                                     'tshirt' : reg.tshirt,
       
   192                                     'conference': reg.conference,
       
   193                                     'tutorial': reg.tutorial,
       
   194                                     'postcode' : reg.postcode,
       
   195                                     'sprint' : reg.sprint,
       
   196                                     'allow_contact' : reg.allow_contact,
       
   197             })
       
   198 
       
   199     return render_to_response(template_name, RequestContext(request, locals()))
       
   200 
       
   201 def submit_registration(request,
       
   202         template_name='registration/submit-registration.html'):
       
   203     '''Allows user to edit registration
       
   204     '''
       
   205     user = request.user
       
   206     reg_count = Registration.objects.all().count()
       
   207     if user.is_authenticated():
       
   208         try:
       
   209             profile = user.get_profile()
       
   210         except:
       
   211             profile, new = UserProfile.objects.get_or_create(user=user)
       
   212             if new:
       
   213                 profile.save()
       
   214         try:
       
   215             registration = Registration.objects.get(registrant=user)
       
   216             if registration:
       
   217                 redirect_to = reverse('kiwipycon_account')
       
   218                 return set_message_cookie(redirect_to,
       
   219                         msg = u'You have already been registered.')
       
   220 
       
   221         except ObjectDoesNotExist:
       
   222             pass
       
   223 
       
   224     message = None
       
   225 
       
   226     if request.method == 'POST':
       
   227         registration_form = RegistrationSubmitForm(data=request.POST)
       
   228         registrant_form = RegistrantForm(data=request.POST)
       
   229         wifi_form = WifiForm(data=request.POST)
       
   230 
       
   231         if request.POST.get('action', None) == 'login':
       
   232             login_form = AuthenticationForm(data=request.POST)
       
   233             if login_form.is_valid():
       
   234 
       
   235                 from django.contrib.auth import login
       
   236                 login(request, login_form.get_user())
       
   237 
       
   238                 redirect_to = reverse('kiwipycon_submit_registration')
       
   239                 return set_message_cookie(redirect_to,
       
   240                         msg = u'You have been logged in please continue' + \
       
   241                                'with registration.')
       
   242 
       
   243         newuser = None
       
   244         passwd = None
       
   245         if not user.is_authenticated():
       
   246             if registrant_form.is_valid():
       
   247                 newuser = kiwipycon_createregistrant(request, registrant_form.data)
       
   248                 # Log in user
       
   249                 passwd = User.objects.make_random_password()
       
   250                 newuser.set_password(passwd)
       
   251                 newuser.save()
       
   252                 from django.contrib.auth import authenticate
       
   253                 user = authenticate(username=newuser.username, password=passwd)
       
   254 
       
   255                 from django.contrib.auth import login
       
   256                 login(request, user)
       
   257 
       
   258                 newuser = user
       
   259 
       
   260         else:
       
   261             newuser = user
       
   262 
       
   263         if registration_form.is_valid() and newuser:
       
   264             allow_contact = registration_form.data.get('allow_contact') and \
       
   265                                         True or False
       
   266             conference = registration_form.data.get('conference') and \
       
   267                                         True or False
       
   268             tutorial = registration_form.data.get('tutorial') and \
       
   269                                         True or False
       
   270             sprint = registration_form.data.get('sprint') and \
       
   271                                         True or False
       
   272 
       
   273             registrant = User.objects.get(pk=newuser.id)
       
   274 
       
   275             presenter = None
       
   276             talks = Talk.objects.filter(
       
   277                     speaker=registrant).filter(approved=True)
       
   278             if talks:
       
   279                 for talk in talks:
       
   280                     if talk.duration == '30':
       
   281                         presenter = True
       
   282                     elif talk.duration == '60':
       
   283                         presenter = True
       
   284             
       
   285             reg = Registration(
       
   286                     #     slug = newuser.username,
       
   287                 registrant = registrant,
       
   288                 organisation = registration_form.data.get('organisation'),
       
   289                 occupation = registration_form.data.get('occupation'),
       
   290                 city = registration_form.data.get('city'),
       
   291                 tshirt = registration_form.data.get('tshirt'),
       
   292                 postcode = registration_form.cleaned_data.get('postcode'),
       
   293                 allow_contact = allow_contact,
       
   294                 conference = conference,
       
   295                 tutorial = tutorial,
       
   296                 sprint = sprint)
       
   297             reg.save() 
       
   298 
       
   299             # get id and use as slug and invoice number
       
   300             id = reg.id
       
   301             slug = 'KPC09%03d' % id
       
   302             reg.slug = slug
       
   303             reg.save()
       
   304 
       
   305             # additional tasks:
       
   306             if wifi_form.is_valid():
       
   307                 wifi = wifi_form.save(registrant)
       
   308             
       
   309             # 1. include random password if we are a new user
       
   310             if passwd:
       
   311                 send_confirmation(registrant, slug, password=passwd)
       
   312             else:
       
   313             # 2. send user email with registration id
       
   314                 send_confirmation(registrant, slug)
       
   315 
       
   316             redirect_to = reverse('kiwipycon_registrations')
       
   317             return set_message_cookie(redirect_to,
       
   318                     msg = u'Thank you, your registration has been submitted '\
       
   319                            'and an email has been sent with payment details.')
       
   320 
       
   321     else:
       
   322         registration_form = RegistrationSubmitForm()
       
   323         registrant_form = RegistrantForm()
       
   324         wifi_form = WifiForm()
       
   325 
       
   326     login_form = AuthenticationForm()
       
   327 
       
   328 
       
   329     return render_to_response(template_name, RequestContext(request, {
       
   330         'registration_form': registration_form,
       
   331         'registrant_form' : registrant_form,
       
   332         'over_reg' : reg_count >= REG_TOTAL and True or False,
       
   333         'wifi_form' : wifi_form,
       
   334         'message' : message,
       
   335         'login_form' : login_form
       
   336     }))