project/scipycon/registration/views.py
changeset 137 ec6e58c639bf
parent 126 f185cb8316cf
child 142 70fcb57d8d15
equal deleted inserted replaced
136:267d9eee024b 137:ec6e58c639bf
     1 import cStringIO as StringIO
     1 from django.contrib.auth import authenticate
     2 import csv
     2 from django.contrib.auth import login
     3 
       
     4 from django.shortcuts import render_to_response
       
     5 from django.template.loader import render_to_string
       
     6 from django.shortcuts import get_object_or_404
       
     7 from django.template import RequestContext
       
     8 from django.core.urlresolvers import reverse
       
     9 from django.http import HttpResponse
       
    10 
       
    11 from django.contrib.auth.decorators import login_required
     3 from django.contrib.auth.decorators import login_required
    12 from django.contrib.auth.forms import AuthenticationForm
     4 from django.contrib.auth.forms import AuthenticationForm
    13 from django.contrib.auth.models import User
     5 from django.contrib.auth.models import User
    14 from django.core.exceptions import ObjectDoesNotExist
     6 from django.core.exceptions import ObjectDoesNotExist
    15 
     7 from django.core.urlresolvers import reverse
    16 from project.scipycon.utils import set_message_cookie
     8 from django.shortcuts import render_to_response
       
     9 from django.template import RequestContext
       
    10 
       
    11 from project.scipycon.base.models import Event
       
    12 from project.scipycon.registration.forms import RegistrationEditForm
       
    13 from project.scipycon.registration.forms import RegistrationSubmitForm
       
    14 from project.scipycon.registration.forms import WifiForm
       
    15 from project.scipycon.registration.models import Registration
       
    16 from project.scipycon.registration.models import Wifi
       
    17 from project.scipycon.registration.utils import send_confirmation
       
    18 from project.scipycon.user.forms import RegistrantForm
    17 from project.scipycon.user.models import UserProfile
    19 from project.scipycon.user.models import UserProfile
    18 from project.scipycon.user.utils import scipycon_createregistrant
    20 from project.scipycon.user.utils import scipycon_createregistrant
    19 from project.scipycon.user.forms import RegistrantForm
    21 from project.scipycon.utils import set_message_cookie
    20 from project.scipycon.talk.models import Talk
    22 
    21 
       
    22 from project.scipycon.registration.models import Registration
       
    23 from project.scipycon.registration.forms import RegistrationSubmitForm
       
    24 from project.scipycon.registration.forms import RegistrationEditForm
       
    25 from project.scipycon.registration.forms import RegistrationAdminSelectForm
       
    26 from project.scipycon.registration.forms import WifiForm
       
    27 from project.scipycon.registration.utils import send_confirmation
       
    28 
       
    29 from .forms import IC
       
    30 
    23 
    31 REG_TOTAL = 1000
    24 REG_TOTAL = 1000
    32 
    25 
    33 
    26 
       
    27 @login_required
    34 def registrations(request, scope, 
    28 def registrations(request, scope, 
    35                   template_name='registration/registrations.html'):
    29                   template_name='registration/registrations.html'):
    36     """Simple page to count registrations"""
    30     """Simple page to count registrations"""
    37 
    31 
    38     registrations = Registration.objects.all().count()
    32     registrations = Registration.objects.all().count()
    46                       template_name='registration/edit-registration.html'):
    40                       template_name='registration/edit-registration.html'):
    47     """Allows users that submitted a registration to edit it.
    41     """Allows users that submitted a registration to edit it.
    48     """
    42     """
    49 
    43 
    50     reg = Registration.objects.get(pk=id)
    44     reg = Registration.objects.get(pk=id)
       
    45     wifi = Wifi.objects.get(user=reg.registrant)
    51 
    46 
    52     if reg.registrant != request.user:
    47     if reg.registrant != request.user:
    53         redirect_to = reverse('scipycon_account', kwargs={'scope': scope})
    48         redirect_to = reverse('scipycon_account', kwargs={'scope': scope})
    54 
    49 
    55         return set_message_cookie(
    50         return set_message_cookie(
    56             redirect_to,
    51             redirect_to,
    57             msg = u'Redirected because the registration you selected' \
    52             msg = u'Redirected because the registration you selected' \
    58                       + ' is not your own.')
    53                       + ' is not your own.')
    59 
    54 
    60     if request.method == 'POST':
    55     if request.method == 'POST':
    61         form = RegistrationEditForm(data=request.POST)
    56         registration_form = RegistrationEditForm(data=request.POST)
    62         if form.is_valid():
    57         wifi_form = WifiForm(data=request.POST)
    63             reg.organisation = form.data.get('organisation')
    58 
    64             reg.occupation = form.data.get('occupation')
    59         if registration_form.is_valid() and wifi_form.is_valid():
    65             reg.city = form.data.get('city')
    60             reg.organisation = registration_form.data.get('organisation')
    66             reg.tshirt = form.data.get('tshirt')
    61             reg.occupation = registration_form.data.get('occupation')
    67             reg.allow_contact = form.data.get('allow_contact') and True or False
    62             reg.city = registration_form.data.get('city')
    68             reg.conference = form.data.get('conference') and True or False
    63             reg.phone_num = registration_form.data.get('phone_num')
    69             reg.tutorial = form.data.get('tutorial') and True or False
    64             reg.postcode = registration_form.data.get('postcode')
    70             reg.sprint = form.data.get('sprint') and True or False
    65             #reg.tshirt = registration_form.data.get('tshirt')
       
    66             reg.allow_contact = registration_form.data.get(
       
    67                 'allow_contact') and True or False
       
    68             reg.conference = registration_form.data.get(
       
    69                 'conference') and True or False
       
    70             reg.tutorial = registration_form.data.get(
       
    71                 'tutorial') and True or False
       
    72             reg.sprint = registration_form.data.get(
       
    73                 'sprint') and True or False
    71             reg.save()
    74             reg.save()
       
    75 
       
    76             wifi = wifi_form.save(reg.registrant, reg.scope)
    72 
    77 
    73             # Saved.. redirect
    78             # Saved.. redirect
    74             redirect_to = reverse('scipycon_account', kwargs={'scope': scope})
    79             redirect_to = reverse('scipycon_account', kwargs={'scope': scope})
    75 
    80 
    76             return set_message_cookie(redirect_to,
    81             return set_message_cookie(redirect_to,
    77                 msg = u'Your changes have been saved.')
    82                 msg = u'Your changes have been saved.')
       
    83         else:
       
    84             import logging
       
    85             logging.error(registration_form.data)
       
    86             raise "Bow Bow"
    78     else:
    87     else:
    79         form = RegistrationEditForm(initial={
    88         registration_form = RegistrationEditForm(initial={
    80                                     'id' : id,
    89             'id' : id,
    81                                     'organisation' : reg.organisation,
    90             'organisation' : reg.organisation,
    82                                     'occupation' : reg.occupation,
    91             'occupation' : reg.occupation,
    83                                     'city' : reg.city,
    92             'city' : reg.city,
    84                                     'tshirt' : reg.tshirt,
    93             'phone_num': reg.phone_num,
    85                                     'conference': reg.conference,
    94             #'tshirt' : reg.tshirt,
    86                                     'tutorial': reg.tutorial,
    95             'conference': reg.conference,
    87                                     'postcode' : reg.postcode,
    96             'tutorial': reg.tutorial,
    88                                     'sprint' : reg.sprint,
    97             'postcode' : reg.postcode,
    89                                     'allow_contact' : reg.allow_contact,
    98             'sprint' : reg.sprint,
       
    99             'allow_contact' : reg.allow_contact,
       
   100             })
       
   101         wifi_form = WifiForm(initial={
       
   102             'user': wifi.user,
       
   103             'scope': wifi.scope,
       
   104             'wifi': wifi.wifi
    90             })
   105             })
    91 
   106 
    92     return render_to_response(
   107     return render_to_response(
    93         template_name, RequestContext(request, {
   108         template_name, RequestContext(request, {
    94         'params': {'scope': scope}}))
   109         'params': {'scope': scope},
       
   110         'registration': {'id': id},
       
   111         'registration_form': registration_form,
       
   112         'wifi_form': wifi_form}))
    95 
   113 
    96 def submit_registration(request, scope,
   114 def submit_registration(request, scope,
    97         template_name='registration/submit-registration.html'):
   115         template_name='registration/submit-registration.html'):
    98     """Allows user to edit registration
   116     """Allows user to edit registration
    99     """
   117     """
   100 
   118 
   101     user = request.user
   119     user = request.user
   102     reg_count = Registration.objects.all().count()
   120     reg_count = Registration.objects.all().count()
   103 
   121 
       
   122     scope_entity = Event.objects.get(scope=scope)
       
   123 
   104     if user.is_authenticated():
   124     if user.is_authenticated():
   105         try:
   125         try:
   106             profile = user.get_profile()
   126             profile = user.get_profile()
   107         except:
   127         except:
   108             profile, new = UserProfile.objects.get_or_create(user=user)
   128             profile, new = UserProfile.objects.get_or_create(
       
   129                 user=user, scope=scope_entity)
   109             if new:
   130             if new:
   110                 profile.save()
   131                 profile.save()
   111         try:
   132         try:
   112             registration = Registration.objects.get(registrant=user)
   133             registration = Registration.objects.get(registrant=user)
   113             if registration:
   134             if registration:
   128 
   149 
   129         if request.POST.get('action', None) == 'login':
   150         if request.POST.get('action', None) == 'login':
   130             login_form = AuthenticationForm(data=request.POST)
   151             login_form = AuthenticationForm(data=request.POST)
   131             if login_form.is_valid():
   152             if login_form.is_valid():
   132 
   153 
   133                 from django.contrib.auth import login
       
   134                 login(request, login_form.get_user())
   154                 login(request, login_form.get_user())
   135 
   155 
   136                 redirect_to = reverse('scipycon_submit_registration',
   156                 redirect_to = reverse('scipycon_submit_registration',
   137                                       kwargs={'scope': scope})
   157                                       kwargs={'scope': scope})
   138                 return set_message_cookie(redirect_to,
   158                 return set_message_cookie(redirect_to,
   141 
   161 
   142         newuser = None
   162         newuser = None
   143         passwd = None
   163         passwd = None
   144         if not user.is_authenticated():
   164         if not user.is_authenticated():
   145             if registrant_form.is_valid():
   165             if registrant_form.is_valid():
   146                 newuser = scipycon_createregistrant(request, registrant_form.data)
   166                 newuser = scipycon_createregistrant(
       
   167                     request, registrant_form.data)
       
   168 
   147                 # Log in user
   169                 # Log in user
   148                 passwd = User.objects.make_random_password()
   170                 passwd = User.objects.make_random_password()
   149                 newuser.set_password(passwd)
   171                 newuser.set_password(passwd)
   150                 newuser.save()
   172                 newuser.save()
   151                 from django.contrib.auth import authenticate
   173 
   152                 user = authenticate(username=newuser.username, password=passwd)
   174                 user = authenticate(username=newuser.username, password=passwd)
   153 
   175 
   154                 login(request, user)
   176                 login(request, user)
   155 
   177 
   156                 newuser = user
   178                 newuser = user
   157 
   179 
   158         else:
   180         else:
   159             newuser = user
   181             newuser = user
   160 
   182 
   161         if registration_form.is_valid() and newuser:
   183         if registration_form.is_valid() and newuser and wifi_form.is_valid():
   162             allow_contact = registration_form.data.get('allow_contact') and \
   184             allow_contact = registration_form.cleaned_data.get(
       
   185                 'allow_contact') and True or False
       
   186             conference = registration_form.cleaned_data.get(
       
   187                 'conference') and True or False
       
   188             tutorial = registration_form.cleaned_data.get('tutorial') and \
   163                 True or False
   189                 True or False
   164             conference = registration_form.data.get('conference') and \
   190             sprint = registration_form.cleaned_data.get('sprint') and \
   165                 True or False
   191                 True or False
   166             tutorial = registration_form.data.get('tutorial') and \
       
   167                 True or False
       
   168             sprint = registration_form.data.get('sprint') and \
       
   169                 True or False
       
   170 
   192 
   171             registrant = User.objects.get(pk=newuser.id)
   193             registrant = User.objects.get(pk=newuser.id)
   172 
   194 
   173             presenter = None
       
   174             talks = Talk.objects.filter(
       
   175                 speaker=registrant).filter(approved=True)
       
   176             if talks:
       
   177                 for talk in talks:
       
   178                     if talk.duration == '30':
       
   179                         presenter = True
       
   180                     elif talk.duration == '60':
       
   181                         presenter = True
       
   182             
       
   183             reg = Registration(
   195             reg = Registration(
   184                     #     slug = newuser.username,
   196                 scope=scope_entity,
   185                 registrant = registrant,
   197                 registrant = registrant,
   186                 organisation = registration_form.data.get('organisation'),
   198                 organisation = registration_form.cleaned_data.get(
   187                 occupation = registration_form.data.get('occupation'),
   199                     'organisation'),
   188                 city = registration_form.data.get('city'),
   200                 occupation = registration_form.cleaned_data.get('occupation'),
   189                 tshirt = registration_form.data.get('tshirt'),
   201                 city = registration_form.cleaned_data.get('city'),
       
   202                 #tshirt = registration_form.data.get('tshirt'),
   190                 postcode = registration_form.cleaned_data.get('postcode'),
   203                 postcode = registration_form.cleaned_data.get('postcode'),
       
   204                 phone_num = registration_form.cleaned_data.get('phone_num'),
   191                 allow_contact = allow_contact,
   205                 allow_contact = allow_contact,
   192                 conference = conference,
   206                 conference = conference,
   193                 tutorial = tutorial,
   207                 tutorial = tutorial,
   194                 sprint = sprint)
   208                 sprint = sprint)
   195             reg.save() 
   209             reg.save() 
   196 
   210 
   197             # get id and use as slug and invoice number
   211             # get id and use as slug and invoice number
   198             id = reg.id
   212             id = reg.id
   199             slug = 'SPYIN10%03d' % id
   213             slug = 'SPYIN10%05d' % id
   200             reg.slug = slug
   214             reg.slug = slug
   201             reg.save()
   215             reg.save()
   202 
   216 
   203             # additional tasks:
   217             wifi = wifi_form.save(registrant, scope_entity)
   204             if wifi_form.is_valid():
   218 
   205                 wifi = wifi_form.save(registrant)
       
   206             
       
   207             # 1. include random password if we are a new user
   219             # 1. include random password if we are a new user
   208             if passwd:
   220             if passwd:
   209                 send_confirmation(registrant, slug, password=passwd)
   221                 send_confirmation(registrant, slug, password=passwd)
   210             else:
   222             else:
   211             # 2. send user email with registration id
   223             # 2. send user email with registration id