コード例 #1
0
 def ajax_addclass(self, request, tl, one, two, module, extra, prog):
     """ Preregister a student for the specified class and return an updated inline schedule """
     if not request.is_ajax():
         return self.addclass(request, tl, one, two, module, extra, prog)
     try:
         success = self.addclass_logic(request, tl, one, two, module, extra,
                                       prog)
         if 'no_schedule' in request.POST:
             resp = HttpResponse(mimetype='application/json')
             simplejson.dump({'status': success}, resp)
             return resp
         if success:
             try:
                 #   Rewrite the registration button if possible.  This requires telling
                 #   the ajax_schedule view what section was added/changed.
                 extra = request.POST['section_id']
             except:
                 pass
             return self.ajax_schedule(request, tl, one, two, module, extra,
                                       prog)
     except ESPError_NoLog, inst:
         print inst
         if inst[0]:
             msg = inst[0]
             raise AjaxError(msg)
         else:
             ec = sys.exc_info()[1]
             raise AjaxError(ec[1])
コード例 #2
0
 def ajax_addclass(self, request, tl, one, two, module, extra, prog):
     """ Preregister a student for the specified class and return an updated inline schedule """
     if not request.is_ajax():
         return self.addclass(request, tl, one, two, module, extra, prog)
     try:
         success = self.addclass_logic(request, tl, one, two, module, extra,
                                       prog)
         if 'no_schedule' in request.POST:
             resp = HttpResponse(content_type='application/json')
             json.dump({'status': success}, resp)
             return resp
         if success:
             try:
                 #   Rewrite the registration button if possible.  This requires telling
                 #   the ajax_schedule view what section was added/changed.
                 extra = request.POST['section_id']
             except:
                 pass
             return self.ajax_schedule(request, tl, one, two, module, extra,
                                       prog)
     except ESPError_NoLog as inst:
         # TODO(benkraft): we shouldn't need to do this.  find a better way.
         raise AjaxError(inst)
コード例 #3
0
    def ajax_schedule(self, request, tl, one, two, module, extra, prog):
        import json as json
        from django.template.loader import render_to_string
        context = self.prepare({})
        context['prog'] = self.program
        context['one'] = one
        context['two'] = two
        context['reg_open'] = bool(
            Permission.user_has_perm(request.user, {
                'learn': 'Student',
                'teach': 'Teacher'
            }[tl] + "/Classes", prog))

        schedule_str = render_to_string('users/student_schedule_inline.html',
                                        context)
        script_str = render_to_string('users/student_schedule_inline.js',
                                      context)
        json_data = {
            'student_schedule_html': schedule_str,
            'script': script_str
        }

        #   Look at the 'extra' data and act appropriately:
        #   -   List, query set, or comma-separated ID list of class sections:
        #       Add the buttons for those class sections to the returned data.
        #   -   String 'all':
        #       Add the buttons for all of the student's class sections to the returned data
        #   -   Anything else:
        #       Don't do anything.
        #   Rewrite registration button if a particular section was named.  (It will be in extra).
        sec_ids = []
        if extra == 'all':
            # TODO(benkraft): this branch of the if was broken for 5 years and
            # nobody noticed, so we may be able to remove it entirely.
            sec_ids = self.user.getSections(self.program).values_list(
                'id', flat=True)
        elif isinstance(extra, list) or isinstance(extra, QuerySet):
            sec_ids = list(extra)
        else:
            try:
                sec_ids = [int(x) for x in extra.split(',')]
            except:
                pass

        for sec_id in sec_ids:
            try:
                section = ClassSection.objects.get(id=sec_id)
                cls = section.parent_class
                button_context = {'sec': section, 'cls': cls}
                if section in request.user.getEnrolledSections(self.program):
                    button_context['label'] = 'Registered!'
                    button_context['disabled'] = True
                addbutton_str1 = render_to_string(
                    self.baseDir() + 'addbutton_fillslot.html', button_context)
                addbutton_str2 = render_to_string(
                    self.baseDir() + 'addbutton_catalog.html', button_context)
                json_data['addbutton_fillslot_sec%d_html' %
                          sec_id] = addbutton_str1
                json_data['addbutton_catalog_sec%d_html' %
                          sec_id] = addbutton_str2
            except Exception, inst:
                raise AjaxError(
                    'Encountered an error retrieving updated buttons: %s' %
                    inst)