diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | allotter/urls.py | 12 | ||||
-rw-r--r-- | allotter/views.py | 97 | ||||
-rw-r--r-- | settings.py | 18 | ||||
-rw-r--r-- | template/allotter/complete.html | 7 | ||||
-rw-r--r-- | template/allotter/details.html | 19 |
6 files changed, 100 insertions, 54 deletions
@@ -1,4 +1,3 @@ - allotter/migrations/ *~ *.pyc diff --git a/allotter/urls.py b/allotter/urls.py index 09383e0..6556c62 100644 --- a/allotter/urls.py +++ b/allotter/urls.py @@ -3,11 +3,9 @@ from django.conf.urls.defaults import patterns, url urlpatterns = patterns('allotter.views', url(r'^login/$', 'user_login'), url(r'^logout/$', 'user_logout'), - url(r'^apply/$', 'apply'), - url(r'^details/$', 'submit_details'), - url(r'^get_pdf/$', 'generate_pdf'), - url(r'^(?P<reg_no>\w+)/submit/$', 'submit_options'), #change into numbers - url(r'^(?P<reg_no>\w+)/complete/$', 'complete'), #change into numbers - url(r'^$', 'apply'), - #url(r'^quit/$', 'quit'), + url(r'^(?P<reg_no>\d+)/apply/$', 'apply'), + url(r'^(?P<reg_no>\d+)/details/$', 'submit_details'), + url(r'^(?P<reg_no>\d+)/get_pdf/$', 'generate_pdf'), + url(r'^(?P<reg_no>\d+)/submit/$', 'submit_options', name='submit_options'), + url(r'^(?P<reg_no>\d+)/complete/$', 'complete_allotment', name='complete_allotment'), ) diff --git a/allotter/views.py b/allotter/views.py index dcfd59f..fb819fc 100644 --- a/allotter/views.py +++ b/allotter/views.py @@ -1,12 +1,11 @@ from django.contrib.auth import login, logout, authenticate -from django.contrib.auth.decorators import login_required -from django.shortcuts import render_to_response, get_object_or_404 -from django.shortcuts import render, redirect -from django.template import RequestContext -from django.http import Http404 +from django.contrib.auth.decorators import login_required +from django.shortcuts import render, redirect, get_object_or_404 -#TODO: Remove this if possible +from django.http import Http404 +from django.http import HttpResponse from django.http import HttpResponseRedirect + from django.core.urlresolvers import reverse from django.contrib.auth.models import User @@ -16,7 +15,6 @@ from allotter.forms import UserLoginForm, UserDetailsForm from itertools import chain #Reportlab libraries -from django.http import HttpResponse from reportlab.platypus import Table, TableStyle, SimpleDocTemplate, Paragraph, Spacer from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle from reportlab.lib import colors @@ -26,16 +24,17 @@ from reportlab.lib.enums import TA_JUSTIFY import time def user_login(request): - - """Take the credentials of the user and log the user in.""" + """ + Verify the user credentials and log the user in. + """ user = request.user if user.is_authenticated(): status = user.get_profile().application.submitted #Getting the submission status if status: #If already submitted, takes to Completion Page - return HttpResponseRedirect(reverse('allotter.views.complete', args=(user.username,))) + return HttpResponseRedirect(reverse('allotter.views.complete_allotment', args=(user.username,))) else: #Otherwise to Option Choosing Page - return redirect("/allotter/details") + return HttpResponseRedirect(reverse('allotter.views.apply', args=(user.username,))) if request.method == "POST": form = UserLoginForm(request.POST) @@ -44,22 +43,20 @@ def user_login(request): login(request, user) status = user.get_profile().application.submitted #Getting the submission status if status: - return HttpResponseRedirect(reverse('allotter.views.complete', args=(user.username,))) - else: - return redirect("/allotter/details") + return HttpResponseRedirect(reverse('allotter.views.complete_allotment', args=(user.username,))) + else: + return HttpResponseRedirect(reverse('allotter.views.submit_details', args=(user.username,))) else: context = {"form": form} - return render_to_response('allotter/login.html', context, - context_instance=RequestContext(request)) + return render(request, 'allotter/login.html', context) else: form = UserLoginForm() context = {"form": form} - return render_to_response('allotter/login.html', context, - context_instance=RequestContext(request)) + return render(request, 'allotter/login.html', context) @login_required -def submit_details(request): +def submit_details(request, reg_no): """ Get the secondary email address, phone number and save it to the Profile. """ @@ -72,15 +69,12 @@ def submit_details(request): form.save() return redirect("/allotter/apply/") else: - return render_to_response('allotter/details.html', - {'form':form}, - context_instance=RequestContext(request)) + return render(request, 'allotter/details.html', {'form':form}) else: form = UserDetailsForm(request.user) context = {"form": form} - return render_to_response('allotter/details.html', context, - context_instance=RequestContext(request)) + return render(request, 'allotter/details.html', context) def get_details(user, error_message = ""): """ @@ -111,7 +105,7 @@ def get_details(user, error_message = ""): return context @login_required -def apply(request): +def apply(request, reg_no): """ Displays the application page for an authenticated user. """ @@ -120,19 +114,18 @@ def apply(request): return redirect('/allotter/login/') context = get_details(user) - ci = RequestContext(request) - return render_to_response('allotter/apply.html', context, - context_instance=ci) + return render(request, 'allotter/apply.html', context) -##Logouts the user. def user_logout(request): + ##Logouts the user. logout(request) - return redirect ('/allotter/') + return redirect ('/allotter/login/') #TODO: Extensive Testing - + +@login_required def submit_options(request, reg_no): """ Gets the Options and their preference number through the POST object and @@ -164,22 +157,28 @@ def submit_options(request, reg_no): if int(opt[0]): #ignoring the options for which None was marked options_code_list.append(opt[1]) - user_application.options_selected = options_code_list #Setting the attribute in model + user_application.options_selected = options_code_list #Saving the data in model user_application.submitted = True #Submission Status user_application.save() - return HttpResponseRedirect(reverse('allotter.views.complete', args=(reg_no,))) + return HttpResponseRedirect(reverse('allotter.views.complete_allotment', args=(reg_no,))) -def complete(request, reg_no): +def complete_allotment(request, reg_no): + """ + Passes the chosen options queryset to the Completion Page Template + """ user = get_object_or_404(User, username=reg_no) sec_email = user.get_profile().secondary_email options_chosen = get_chosen_options(user) - context = {'user': reg_no, 'email': sec_email, + context = {'username': reg_no, 'email': sec_email, 'options_chosen': options_chosen} - ci = RequestContext(request) - return render_to_response('allotter/complete.html', context, context_instance=ci) + + return render(request, 'allotter/complete.html', context) def get_chosen_options(user): + """ + Reads the options submitted by the user in the Application page + """ user_profile = user.get_profile() user_application = user_profile.application np = user_application.np @@ -189,7 +188,13 @@ def get_chosen_options(user): chosen_options.append(Option.objects.get(opt_code=int(oc))) return chosen_options -def generate_pdf(request, reg_no="1234567"): + +@login_required +def generate_pdf(request, reg_no): + """ + The Ugly code for generating the pdf using ReportLab. + """ + user = get_object_or_404(User, username=reg_no) user_profile = user.get_profile() user_application = user_profile.application @@ -217,12 +222,21 @@ def generate_pdf(request, reg_no="1234567"): elements.append(Paragraph(ptext, styles["Normal"])) elements.append(Spacer(1, 12)) - ptext = '<font size=12>Following are the options in order of preference</font>' + ptext = '<font size=12>No options were chosen.</font>' elements.append(Paragraph(ptext, styles["Normal"])) elements.append(Spacer(1, 12)) data = [] - options = get_chosen_options(user) + options = get_chosen_options(user) ##Put a check to show when the options chosen is empty + + if not(options): + doc.build(elements) + return response + + ptext = '<font size=12>Following are the options in order of preference</font>' + elements.append(Paragraph(ptext, styles["Normal"])) + elements.append(Spacer(1, 12)) + counter = 1 for opt in options: data.append([counter, opt.opt_code, opt.opt_location, opt.opt_name]) @@ -237,8 +251,7 @@ def generate_pdf(request, reg_no="1234567"): ptext = '<font size=12>%s</font>' % formatted_time elements.append(Paragraph(ptext, styles["Normal"])) elements.append(Spacer(1, 12)) - - + doc.build(elements) return response diff --git a/settings.py b/settings.py index 6d0af1a..dd71bc4 100644 --- a/settings.py +++ b/settings.py @@ -108,6 +108,7 @@ MIDDLEWARE_CLASSES = ( 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', + 'debug_toolbar.middleware.DebugToolbarMiddleware', ) ROOT_URLCONF = 'aloha.urls' @@ -132,6 +133,8 @@ INSTALLED_APPS = ( # 'django.contrib.admindocs', #Third-Party Apps 'south', + 'django_extensions', + 'debug_toolbar', 'allotter', ) @@ -162,3 +165,18 @@ LOGGING = { AUTH_PROFILE_MODULE = "allotter.Profile" LOGIN_URL = '/allotter/login' + +#For developing locally +INTERNAL_IPS = ('127.0.0.1') + +DEBUG_TOOLBAR_PANELS = ( + 'debug_toolbar.panels.version.VersionDebugPanel', + 'debug_toolbar.panels.timer.TimerDebugPanel', + 'debug_toolbar.panels.settings_vars.SettingsVarsDebugPanel', + 'debug_toolbar.panels.headers.HeaderDebugPanel', + 'debug_toolbar.panels.request_vars.RequestVarsDebugPanel', + 'debug_toolbar.panels.template.TemplateDebugPanel', + 'debug_toolbar.panels.sql.SQLDebugPanel', + 'debug_toolbar.panels.signals.SignalDebugPanel', + 'debug_toolbar.panels.logger.LoggingPanel', +) diff --git a/template/allotter/complete.html b/template/allotter/complete.html index 799d965..26ee46d 100644 --- a/template/allotter/complete.html +++ b/template/allotter/complete.html @@ -2,7 +2,6 @@ {% block title %}Options saved.{% endblock %} - {% block content %} <h2> The following options have been saved. Please verify them before logging out.</h2> @@ -41,17 +40,17 @@ An email with the list of options has been sent {{ email }} for book-keeping pur {% endif %} -<form action="/allotter/apply/" method="post"> +<form id="apply" action="/allotter/{{username}}/apply/" method="post"> {% csrf_token %} <input type="submit" name="apply" value="Edit Options" /> </form> -<form action="/allotter/get_pdf/" method="post"> +<form id ="get_pdf" action="/allotter/{{username}}/get_pdf/" method="post"> {% csrf_token %} <input type="submit" name="get_pdf" value="Generate PDF" /> </form> -<form action="/allotter/logout/" method="post"> +<form id="logout" action="/allotter/logout/" method="post"> {% csrf_token %} <input type="submit" name="logout" value="Quit Allotment" /> </form> diff --git a/template/allotter/details.html b/template/allotter/details.html new file mode 100644 index 0000000..5beeb85 --- /dev/null +++ b/template/allotter/details.html @@ -0,0 +1,19 @@ +{% extends "base.html" %} + +{% block title %}Details form {% endblock %} + +{% block content %} +Please provide the following details. +<form action="/allotter/details/" method="post"> +{% csrf_token %} + +<table> +{{ form.as_table }} +</table> + +<h2> These details cannot be changed once entered. </h2> + +<input type="submit" value="Submit" /> +</form> + +{% endblock content %} |