Permalink
Browse files

django messages in scheduler. bug fix: trying to schedule without the…

… schedule attributes loaded (lazy loading) fails. now it fetches manually if not there
  • Loading branch information...
1 parent 591814c commit b3f88192935f73ac1bf9f2175f16dbcc0b3def03 Kevin Ngo committed Jun 18, 2011
Showing with 65 additions and 21 deletions.
  1. +10 −8 course/views.py
  2. +12 −0 scheduler/templates/scheduler.html
  3. +43 −13 scheduler/views.py
View
18 course/views.py
@@ -1,6 +1,5 @@
from django.shortcuts import render_to_response
from django.http import Http404, HttpResponseRedirect
-from django.core.urlresolvers import reverse
from django.contrib import messages
from django.template import RequestContext, Template
import simplejson as json
@@ -29,7 +28,6 @@ def index(request):
return HttpResponseRedirect('/course/' + department + number + '/')
-
def show(request, department, number, status=''):
""" shows information about single course and can register from here """
course = department + number
@@ -46,23 +44,27 @@ def show(request, department, number, status=''):
# user sumbitted crns to register for courses, use urls to redirect to register view
crn_list_reg = request.POST.getlist('choose')
if len(crn_list_reg) is 1:
- success = register(request, course, crn_list_reg[0])
+ success = register(request, crn_list_reg[0])
else:
- success = register(request, course, crn_list_reg[0], crn_list_reg[1])
+ success = register(request, crn_list_reg[0], crn_list_reg[1])
# display confirmation or error message if register success/fail
- if not success:
- messages.error(request, "Failed to register for " + course )
- else:
+ if success:
messages.success(request, "Successfully registered for " + course)
+ else:
+ messages.error(request, "Failed to register for " + course )
return render_to_response('course/show.html', {'courses':courses}, context_instance=RequestContext(request))
-def register(request, course, crn1, crn2=""):
+def register(request, crn1, crn2=""):
""" register for course """
regclass = request.session['regclass']
+ if not regclass.schedule:
+ regclass.get_current_schedule()
+ if not regclass.next_schedule:
+ regclass.get_next_schedule()
return regclass.add_class(crn1, crn2)
View
12 scheduler/templates/scheduler.html
@@ -7,6 +7,17 @@
<br/>
{% comment %}
+Display confirmation or error messages on registration request
+{% endcomment %}
+{% if messages %}
+<ul class="messages">
+ {% for message in messages %}
+ <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
+ {% endfor %}
+</ul>
+{% endif %}
+
+{% comment %}
Form to choose which classes to schedule
{% endcomment %}
<form action="." method="post">
@@ -53,6 +64,7 @@
<h3>
{% ifequal combination|length classes_possible %}
{% endifequal %}
+ {% comment %} REGISTER FORM {% endcomment %}
<form action='.' method='POST'>
{% for course in combination %}
<input type="hidden" name="course{{forloop.counter}}" value={{course.department|upper}}{{course.number}} />
View
56 scheduler/views.py
@@ -1,15 +1,24 @@
-import reglib
+from django.shortcuts import render_to_response
+from django.http import Http404, HttpResponseRedirect
+from django.contrib import messages
+from django.template import RequestContext, Template
import simplejson as json
+import reglib
-from django.http import Http404, HttpResponseRedirect
-from django.shortcuts import render_to_response
def index(request):
if request.method == 'GET':
return render_to_response('scheduler.html')
+ regclass = request.session['regclass']
+
# if user chose to register a list of courses
try:
+ if not regclass.schedule:
+ regclass.get_current_schedule()
+ if not regclass.next_schedule:
+ regclass.get_next_schedule()
+
# create a dictionary of courses from the POST request
courses = []
# the post keys will be course1, course2....crn1, crn2....
@@ -20,13 +29,22 @@ def index(request):
crn = request.POST[crn_key]
courses.append({'course':course, 'crn':crn})
- register(courses)
- return render_to_response('scheduler.html')
+ # register
+ courses_copy = courses[:] # copy this guy because he's gonna get popped to hell
+ success_list = register(regclass, courses)
+
+ # display confimation or error message if register success/fail
+ # for each course
+ for (course, success) in zip(courses_copy, success_list):
+ if success:
+ messages.success(request, "Successfully registered for " + course['course'])
+ else:
+ messages.error(request, "Failed to register for " + course['course'])
+
except:
pass
-
+
# get list of courses and create combinations
- regclass = request.session['regclass']
try:
classes = request.POST['classes'].split(', ')
term_year = request.POST['term'] + request.POST['year']
@@ -35,16 +53,17 @@ def index(request):
classes_possible = results['classes_possible']
combinations_json = json.dumps(combinations)
except:
- return render_to_response('scheduler.html')
+ return render_to_response('scheduler.html', context_instance=RequestContext(request))
return render_to_response('scheduler.html', {
'combinations':combinations,
'json':combinations_json,
'range':range(24),
- 'classes_possible':classes_possible}
+ 'classes_possible':classes_possible},
+ context_instance=RequestContext(request)
)
-def register(courses):
+def register(regclass, courses):
""" register for all courses in a combination at once
@parameters courses is a list of course dictionaries with crn and course title
puts the crns in an appropriate list (with lecture/lab-rec logic) and hands it to add_class """
@@ -62,11 +81,22 @@ def register(courses):
crn_list.append([ course_pop['crn'], course['crn'] ])
courses.remove( {'course': course['course'], 'crn': course['crn']} )
if not lab_rec_pair_flag:
- crn_list.append(course_pop['crn'])
+ crn_list.append(str(course_pop['crn']))
lab_rec_pair_flag = 0
- regclass = request.session['regclass']
- regclass.add_classes(crn_list)
+ success_list = regclass.add_classes(crn_list)
+
+ # Ungroups lecture/lab-rec pairs in the true/false return list
+ return_list = []
+ for index, crn in enumerate(crn_list):
+ if success_list[index]:
+ return_list.append(True)
+ else:
+ return_list.append(False)
+
+ return return_list
+
+

0 comments on commit b3f8819

Please sign in to comment.