diff options
author | Primal Pappachan | 2012-03-19 00:02:04 +0530 |
---|---|---|
committer | Primal Pappachan | 2012-03-19 00:02:04 +0530 |
commit | 6bf4281d493fe800451cb818d09686b7e6ce767e (patch) | |
tree | d1fc0ad16f8311128e76467e3cbbfccf0d85f37f | |
parent | 2c97aecefd592649e88f5ace6ed39ef98f93089d (diff) | |
download | aloha-6bf4281d493fe800451cb818d09686b7e6ce767e.tar.gz aloha-6bf4281d493fe800451cb818d09686b7e6ce767e.tar.bz2 aloha-6bf4281d493fe800451cb818d09686b7e6ce767e.zip |
Initial working version of option choosing and saving in database
-rw-r--r-- | allotter/urls.py | 6 | ||||
-rw-r--r-- | allotter/views.py | 120 | ||||
-rw-r--r-- | template/allotter/apply.html | 13 |
3 files changed, 103 insertions, 36 deletions
diff --git a/allotter/urls.py b/allotter/urls.py index 6238e3c..611fb88 100644 --- a/allotter/urls.py +++ b/allotter/urls.py @@ -2,8 +2,10 @@ 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'^apply/$', 'apply'), - #url(r'^submit/$', 'submit'), + url(r'^(?P<reg_no>\w+)/submit/$', 'submit'), #change into numbers + url(r'^(?P<reg_no>\w+)/complete/$', 'complete'), #change into numbers + url(r'^$', 'apply'), #url(r'^quit/$', 'quit'), ) diff --git a/allotter/views.py b/allotter/views.py index abfec2e..d4f1a0a 100644 --- a/allotter/views.py +++ b/allotter/views.py @@ -4,7 +4,11 @@ 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 +#TODO: Remove this if possible +from django.http import HttpResponseRedirect +from django.core.urlresolvers import reverse +from django.contrib.auth.models import User from allotter.models import Profile, Option, Exam from allotter.forms import UserLoginForm @@ -32,50 +36,106 @@ def user_login(request): return render_to_response('allotter/login.html', context, context_instance=RequestContext(request)) - -@login_required -def apply(request): - user = request.user - if not(user.is_authenticated()): - return redirect('/allotter/login/') + + +def get_options(user): + user_profile = user.get_profile() + user_application = user_profile.application + np = user_application.np #Number of Papers + first_paper = user_application.first_paper #First Paper Name + options_available_first = Option.objects.filter(exam__exam_name=first_paper).distinct() #Options for First paper + if(np == 2): + second_paper = user_application.second_paper + options_available_second = Option.objects.filter(exam__exam_name=second_paper).distinct() + + options_available = list(set(options_available_first + options_available_second)) + + +## Retrieves the user details from the database and passes it for generation +## of Application Template + +##TODO: Display a single list of options for students who have taken two exams. + +def get_details(user, error_message = ""): user_profile = user.get_profile() user_application = user_profile.application np = user_application.np #Number of Papers - first_paper = user_application.first_paper - options_available_first = Option.objects.filter(exam__exam_name=first_paper).distinct() - oafl = len(options_available_first) - if np == 2: + first_paper = user_application.first_paper #First Paper Name + options_available_first = Option.objects.filter(exam__exam_name=first_paper).distinct() #Options for First paper + oafl = len(options_available_first) + if np == 2: #If written two exams second_paper = user_application.second_paper options_available_second = Option.objects.filter(exam__exam_name=second_paper).distinct() oasl = len(options_available_second) context = {'user': user, 'first_paper': first_paper, - 'options_available_first' : options_available_first,'np' : np, - 'oafl': oafl, 'oasl': oasl, 'second_paper': second_paper, - 'options_available_second' : options_available_second} - ci = RequestContext(request) - return render_to_response('allotter/apply.html', context, - context_instance=ci) - - #form = ApplicationForm(logged_user=user) - context = {'user': user, 'first_paper': first_paper, + 'options_available_first' : options_available_first, + 'second_paper': second_paper, + 'options_available_second' : options_available_second, + 'np' : np,'oafl': oafl, 'oasl': oasl, + 'error_message': error_message} + else: #If written only one exam + context = {'user': user, 'first_paper': first_paper, 'options_available_first' : options_available_first, - 'oafl': oafl, 'np' : np} - ci = RequestContext(request) + 'oafl': oafl, 'np' : np, 'error_message' : error_message} + return context + +@login_required +def apply(request): + user = request.user + if not(user.is_authenticated()): + return redirect('/allotter/login/') + + context = get_details(user) + ci = RequestContext(request) + return render_to_response('allotter/apply.html', context, - context_instance=ci) - - -""" -def submit(request): - pass + context_instance=ci) + +##Logouts the user. -def quit(request): - pass - def user_logout(request): logout(request) return redirect ('/allotter/') + +#TODO: Extensive Testing + +def submit(request, reg_no): + user = get_object_or_404(User, username=reg_no) + user_profile = user.get_profile() + user_application = user_profile.application + first_paper = user_application.first_paper #First Paper Name + options_available_first = Option.objects.filter(exam__exam_name=first_paper).distinct() #Options for First paper + options_list = "" + for option in options_available_first: + + #TODO: Dealing with none option, Dealing with all nones + selected_option = Option.objects.get(pk=request.POST[option.opt_name]) + #except (KeyError, Option.DoesNotExist): + # Redisplay the application form with error message. + # error_message = "You didn't select a choice." + # context = get_detail(user, error_message) + # return render_to_response('allotter/apply.html', context, + # context_instance=RequestContext(request)) + #else: + + options_list += selected_option.opt_name + + user_application.options_selected += options_list + user_application.save() + return HttpResponseRedirect(reverse('allotter.views.complete', args=(reg_no,))) + return redirect('/allotter/complete/') + +#User Application + +def complete(request, reg_no): + context = {'user': reg_no} + ci = RequestContext(request) + return render_to_response('allotter/complete.html', context) + +"""def quit(request): + pass + """ """def user_register(request): diff --git a/template/allotter/apply.html b/template/allotter/apply.html index ca03726..c38837a 100644 --- a/template/allotter/apply.html +++ b/template/allotter/apply.html @@ -39,21 +39,24 @@ options are available to you. Please rank your choices.</h4> Listing the options for first test paper. {% endcomment %} -<form action="/allotter/save" method="post"> +{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %} + +<form action="/allotter/{{user.username}}/submit/" method="post"> {% csrf_token %} {% for option in options_available_first %} <p>{{ option.opt_name }}</p> {% for i in oafl|get_range %} - <input name="option" type="radio" value="{{i}}" />{{i}}<br/> + <input name="{{option.opt_name}}" type="radio" value="{{option.id}}" /><br/> {% endfor %} - <input name="option" type="radio" value="None" />None<br/> + <input name="{{option.opt_name}}" type="radio" value="None" />None<br/> {% endfor %} {% comment %} Listing the options for second test paper if it exists. {% endcomment %} +{% comment %} {% if np == 2 %} {% for option in options_available_second %} @@ -62,10 +65,12 @@ Listing the options for second test paper if it exists. <input name="option" type="radio" value="{{i}}" />{{i}}<br/> {% endfor %} <input name="option" type="radio" value="None" />None<br/> -{% endfor %} +{% endfor %} {% endif %} +{% endcomment %} + <input type="submit" name="save" value="Save" /> </form> |