|
| 1 | +__author__ = "Individual contributors (see AUTHORS file)" |
| 2 | +__date__ = "$DATE$" |
| 3 | +__rev__ = "$REV$" |
| 4 | +__license__ = "AGPL v.3" |
| 5 | +__copyright__ = """ |
| 6 | +This file is part of the ESP Web Site |
| 7 | +Copyright (c) 2007 by the individual contributors |
| 8 | + (see AUTHORS file) |
| 9 | +
|
| 10 | +The ESP Web Site is free software; you can redistribute it and/or |
| 11 | +modify it under the terms of the GNU Affero General Public License |
| 12 | +as published by the Free Software Foundation; either version 3 |
| 13 | +of the License, or (at your option) any later version. |
| 14 | +
|
| 15 | +This program is distributed in the hope that it will be useful, |
| 16 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | +GNU Affero General Public License for more details. |
| 19 | +
|
| 20 | +You should have received a copy of the GNU Affero General Public |
| 21 | +License along with this program; if not, write to the Free Software |
| 22 | +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 23 | +
|
| 24 | +Contact information: |
| 25 | +MIT Educational Studies Program |
| 26 | + 84 Massachusetts Ave W20-467, Cambridge, MA 02139 |
| 27 | + Phone: 617-253-4882 |
| 28 | + |
| 29 | +Learning Unlimited, Inc. |
| 30 | + 527 Franklin St, Cambridge, MA 02139 |
| 31 | + Phone: 617-379-0178 |
| 32 | + |
| 33 | +""" |
| 34 | + |
| 35 | +from datetime import datetime |
| 36 | +from urllib import quote |
| 37 | + |
| 38 | +from esp.middleware.threadlocalrequest import get_current_request |
| 39 | +from esp.program.models import Program, StudentAppResponse, StudentRegistration, RegistrationType |
| 40 | +from esp.program.models.class_ import ClassSubject |
| 41 | +from esp.program.modules.base import ProgramModuleObj |
| 42 | +from esp.program.modules.base import main_call, aux_call, needs_admin, needs_student, meets_grade |
| 43 | +from esp.web.util import render_to_response |
| 44 | +from esp.users.models import ESPUser |
| 45 | +from esp.utils.query_utils import nest_Q |
| 46 | + |
| 47 | +from django import forms |
| 48 | +from django.http import HttpResponseRedirect |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +class ClassChangeRequestModule(ProgramModuleObj): |
| 53 | + doc = """Let students enter a lottery to switch classes after the program has started.""" |
| 54 | + |
| 55 | + @classmethod |
| 56 | + def module_properties(cls): |
| 57 | + return { |
| 58 | + "admin_title": "Class Change Request", |
| 59 | + "link_title": "Class Change Request", |
| 60 | + "module_type": "learn", |
| 61 | + "required": False, |
| 62 | + } |
| 63 | + |
| 64 | + class Meta: |
| 65 | + proxy = True |
| 66 | + |
| 67 | + def isCompleted(self): |
| 68 | + return StudentRegistration.valid_objects().filter(user=get_current_request().user, |
| 69 | + relationship__name="Request").exists() |
| 70 | + |
| 71 | + @main_call |
| 72 | + @needs_student |
| 73 | + @meets_grade |
| 74 | + def classchangerequest(self, request, tl, one, two, module, extra, prog): |
| 75 | + timeslots = prog.getTimeSlots() |
| 76 | + sections = prog.sections().filter(status=10, meeting_times__isnull=False).distinct() |
| 77 | + |
| 78 | + enrollments = {} |
| 79 | + for timeslot in timeslots: |
| 80 | + try: |
| 81 | + enrollments[timeslot] = ClassSubject.objects.get(nest_Q(StudentRegistration.is_valid_qobject(), 'sections__studentregistration'), sections__studentregistration__relationship__name="Enrolled", sections__studentregistration__user=request.user, sections__meeting_times=timeslot, parent_program=prog) |
| 82 | + except ClassSubject.DoesNotExist: |
| 83 | + enrollments[timeslot] = None |
| 84 | + |
| 85 | + context = {} |
| 86 | + context['timeslots'] = timeslots |
| 87 | + context['enrollments'] = enrollments |
| 88 | + context['user'] = request.user |
| 89 | + if 'success' in request.GET: |
| 90 | + context['success'] = True |
| 91 | + else: |
| 92 | + context['success'] = False |
| 93 | + |
| 94 | + if request.user.isStudent(): |
| 95 | + sections_by_slot = dict([(timeslot,[(section, 1 == StudentRegistration.valid_objects().filter(user=context['user'], section=section, relationship__name="Request").count()) for section in sections if section.get_meeting_times()[0] == timeslot and section.parent_class.grade_min <= request.user.getGrade(prog) <= section.parent_class.grade_max and section.parent_class not in enrollments.values() and ESPUser.getRankInClass(request.user, section.parent_class) in (5,10)]) for timeslot in timeslots]) |
| 96 | + else: |
| 97 | + sections_by_slot = dict([(timeslot,[(section, False) for section in sections if section.get_meeting_times()[0] == timeslot]) for timeslot in timeslots]) |
| 98 | + |
| 99 | + fields = {} |
| 100 | + for i, timeslot in enumerate(sections_by_slot.keys()): |
| 101 | + choices = [('0', "I'm happy with my current enrollment.")] |
| 102 | + initial = '0' |
| 103 | + for section in sections_by_slot[timeslot]: |
| 104 | + choices.append((section[0].emailcode(), section[0].emailcode()+": "+section[0].title())) |
| 105 | + if section[1]: |
| 106 | + initial = section[0].emailcode() |
| 107 | + fields['timeslot_'+str(i+1)] = forms.ChoiceField(label="Timeslot "+str(i+1)+" ("+timeslot.pretty_time()+")", choices=choices, initial=initial) |
| 108 | + |
| 109 | + form = type('ClassChangeRequestForm', (forms.Form,), fields) |
| 110 | + context['form'] = form() |
| 111 | + if request.method == "POST": |
| 112 | + old_requests = StudentRegistration.valid_objects().filter(user=context['user'], section__parent_class__parent_program=prog, relationship__name="Request") |
| 113 | + for r in old_requests: |
| 114 | + r.expire() |
| 115 | + form = form(request.POST) |
| 116 | + if form.is_valid(): |
| 117 | + for value in form.cleaned_data.values(): |
| 118 | + section = None |
| 119 | + for s in sections: |
| 120 | + if s.emailcode() == value: |
| 121 | + section = s |
| 122 | + break |
| 123 | + if not section: |
| 124 | + continue |
| 125 | + r = StudentRegistration.valid_objects().get_or_create(user=context['user'], section=section, relationship=RegistrationType.objects.get_or_create(name="Request", category="student")[0])[0] |
| 126 | + r.save() |
| 127 | + |
| 128 | + return HttpResponseRedirect(request.path.rstrip('/')+'/?success') |
| 129 | + else: |
| 130 | + return render_to_response(self.baseDir() + 'classchangerequest.html', request, context) |
0 commit comments