Example #1
0
    def test_get_resource_pairs(self):
        prog = self.program
        new_res_type1 = ResourceType.get_or_create('NewResource1', program = self.program)
        new_res_type2 = ResourceType.get_or_create('NewResource2', program = self.program)
        sec = random.choice(self.cls.sections.all())

        self.add_resource_request(sec, new_res_type1, 'Yes')
        self.assertTrue(self.has_resource_pair_with_teacher(new_res_type1, 0, self.teacher))

        self.add_resource_request(sec, new_res_type2, 'ThisValueIsAwesome')
        self.assertTrue(self.has_resource_pair_with_teacher(new_res_type2, 0, self.teacher))

        self.delete_resource_request(sec, new_res_type1)
        self.assertTrue(not self.has_resource_pair_with_teacher(new_res_type1, 0, self.teacher))
Example #2
0
 def save_data(self, sec):
     sec.status = self.cleaned_data['status']
     if self.cleaned_data['reg_status']:
         sec.registration_status = self.cleaned_data['reg_status']
     sec.meeting_times.clear()
     for mi in self.cleaned_data['times']:
         ts = Event.objects.get(id=mi)
         ct = ResourceType.get_or_create('Classroom')
         sec.meeting_times.add(ts)
         cr = Resource.objects.filter(res_type__id=ct.id,
                                      event__id=ts.id,
                                      name__in=self.cleaned_data['room'])
         for c in cr:
             c.assign_to_section(sec, override=True)
     rooms = Resource.objects.filter(name__in=self.cleaned_data['room'])
     if rooms.count() > 0:
         sec.classroomassignments().delete()
         for r in rooms:
             sec.assign_room(r)
     sec.checklist_progress.clear()
     for ci in self.cleaned_data['progress']:
         cpl = ProgramCheckItem.objects.get(id=ci)
         sec.checklist_progress.add(cpl)
     for r in self.cleaned_data['resources']:
         for ts in sec.meeting_times.all():
             sec.parent_program.getFloatingResources(
                 timeslot=ts,
                 queryset=True).filter(name=r)[0].assign_to_section(sec)
     sec.max_class_capacity = self.cleaned_data['class_size']
     sec.save()
Example #3
0
    def add_or_edit_restype(self, form):
        if form.cleaned_data['id'] is not None:
            new_restype = ResourceType.objects.get(id=form.cleaned_data['id'])
        else:
            new_restype = ResourceType()

        form.save_restype(self.program, new_restype)
        return new_restype
Example #4
0
 def import_restype_formset(self, sec, resform):
     rt, created = ResourceType.get_or_create(resform.cleaned_data['name'])
     rt.choices = ['Yes']
     rt.save()
     rr = ResourceRequest()
     rr.target = sec
     rr.res_type = rt
     rr.desired_value = 'Yes'
     rr.save()
     return (rt, rr)
    def create_rooms(self, request, tl, one, two, module, extra, prog):
        """ Step 1 of the diagnostic setup process. """

        #   Show a page asking for a list of rooms and their capacities.

        if request.method == 'POST':
            #   Receive the data and create a series of rooms in the specified timeslot.
            data = request.POST
            timeslot_id = int(data.get('timeslot'))
            empty_rooms = data.get('empty_rooms')
            rooms = data.get('rooms')
            room_list = rooms.split('\n')

            timeslot = Event.objects.get(id=timeslot_id)
            classroom_rt = ResourceType.get_or_create('Classroom')

            for s in room_list:
                #   The rooms will be created without additional resources
                str_dir = s.split(',')
                if len(str_dir) == 2:
                    res = Resource()
                    res.name = str_dir[0]
                    res.res_type = classroom_rt
                    res.num_students = int(str_dir[1])
                    res.event = timeslot
                    res.save()

            #   Redirect to the diagnostic sections page (step 2) excluding the last few rooms if desired.
            empty_rooms_list = []
            for t in room_list[(len(room_list) - int(empty_rooms)):]:
                str_dir = t.split(',')
                empty_rooms_list.append(str_dir[0])
            empty_rooms_str = ','.join(empty_rooms_list)

            return HttpResponseRedirect(
                '/manage/%s/%s/diagnostic_sections?timeslot=%d&empty_rooms=%s'
                % (one, two, timeslot_id, empty_rooms_str))

        context = {'prog': prog, 'room_indices': range(0, 22)}
        return render_to_response(self.baseDir() + 'room_setup.html', request,
                                  (prog, tl), context)
 def save_data(self, sec):
     sec.status = self.cleaned_data['status']
     if self.cleaned_data['reg_status']:
         sec.registration_status = self.cleaned_data['reg_status']
     sec.meeting_times.clear()
     for mi in self.cleaned_data['times']:
         ts = Event.objects.get(id=mi)
         ct = ResourceType.get_or_create('Classroom')
         sec.meeting_times.add(ts)
         cr = Resource.objects.filter(res_type__id=ct.id, event__id=ts.id, name__in=self.cleaned_data['room'])
         for c in cr:
             c.assign_to_section(sec, override=True)
     rooms = Resource.objects.filter(name__in=self.cleaned_data['room'])
     if rooms.count() > 0:
         sec.classroomassignments().delete()
         for r in rooms:
             sec.assign_room(r)
     for r in self.cleaned_data['resources']:
         for ts in sec.meeting_times.all():
             sec.parent_program.getFloatingResources(timeslot=ts, queryset=True).filter(name=r)[0].assign_to_section(sec)
     sec.max_class_capacity = self.cleaned_data['class_size']
     sec.save()
                resource_formset = e.resource_formset
                restype_formset = e.restype_formset

        else:
            errors = {}

            resource_types = set([])
            default_restypes = Tag.getProgramTag(
                'default_restypes',
                program=self.program,
            )
            if default_restypes:
                resource_type_labels = json.loads(default_restypes)
                resource_types = resource_types.union(
                    set([
                        ResourceType.get_or_create(x, self.program)
                        for x in resource_type_labels
                    ]))

            if static_resource_requests:
                # With static resource requests, we need to display a form
                # each available type --- there's no way to add the types
                # that we didn't start out with
                # Thus, if default_restype isn't set, we display everything
                # potentially relevant
                q_program = Q(program=self.program)
                if Tag.getTag('allow_global_restypes'):
                    q_program = q_program | Q(program__isnull=True)
                resource_types = resource_types.union(
                    set(
                        ResourceType.objects.filter(q_program).order_by(
Example #8
0
def newprogram(request):
    template_prog = None
    template_prog_id = None
    if 'template_prog' in request.GET and (
            int(request.GET["template_prog"])
    ) != 0:  # if user selects None which value is 0,so we need to check for 0.
        #try:
        template_prog_id = int(request.GET["template_prog"])
        tprogram = Program.objects.get(id=template_prog_id)
        template_prog = {}
        template_prog.update(tprogram.__dict__)
        del template_prog["id"]
        template_prog["program_type"] = tprogram.program_type
        template_prog["program_modules"] = tprogram.program_modules.all(
        ).values_list("id", flat=True)
        template_prog["class_categories"] = tprogram.class_categories.all(
        ).values_list("id", flat=True)
        '''
        As Program Name should be new for each new program created then it is better to not to show old program names in input box .
        template_prog["term"] = tprogram.anchor.name
        template_prog["term_friendly"] = tprogram.anchor.friendly_name
        '''

        template_prog["admins"] = ESPUser.objects.filter(
            permission__permission_type="Administer",
            permission__program=tprogram).values_list("id", flat=True)

        # aseering 5/18/2008 -- More aggressively list everyone who was an Admin
        #template_prog["admins"] = [ x.id for x in UserBit.objects.bits_get_users(verb=GetNode("V/Administer"), qsc=tprogram.anchor, user_objs=True) ]

        student_reg_bits = list(
            Permission.objects.filter(
                permission_type__startswith='Student',
                program=template_prog_id).order_by('-start_date'))
        if len(student_reg_bits) > 0:
            newest_bit = student_reg_bits[0]
            oldest_bit = student_reg_bits[-1]

            template_prog["student_reg_start"] = oldest_bit.start_date
            template_prog["student_reg_end"] = newest_bit.end_date

        teacher_reg_bits = list(
            Permission.objects.filter(
                permission_type__startswith='Teacher',
                program=template_prog_id).order_by('-start_date'))
        if len(teacher_reg_bits) > 0:
            newest_bit = teacher_reg_bits[0]
            oldest_bit = teacher_reg_bits[-1]

            template_prog["teacher_reg_start"] = oldest_bit.start_date
            template_prog["teacher_reg_end"] = newest_bit.end_date

        pac = ProgramAccountingController(tprogram)
        line_items = pac.get_lineitemtypes(
            required_only=True).values('amount_dec')

        template_prog["base_cost"] = int(
            sum(x["amount_dec"] for x in line_items))
        template_prog["sibling_discount"] = tprogram.sibling_discount

    if 'checked' in request.GET:
        # Our form's anchor is wrong, because the form asks for the parent of the anchor that we really want.
        # Don't bother trying to fix the form; just re-set the anchor when we're done.
        context = pickle.loads(request.session['context_str'])
        pcf = ProgramCreationForm(context['prog_form_raw'])
        if pcf.is_valid():

            new_prog = pcf.save(commit=True)

            commit_program(new_prog, context['perms'], context['modules'],
                           context['cost'], context['sibling_discount'])

            # Create the default resource types now
            default_restypes = Tag.getProgramTag('default_restypes',
                                                 program=new_prog)
            if default_restypes:
                resource_type_labels = json.loads(default_restypes)
                resource_types = [
                    ResourceType.get_or_create(x, new_prog)
                    for x in resource_type_labels
                ]

            #   Force all ProgramModuleObjs and their extensions to be created now
            new_prog.getModules()

            manage_url = '/manage/' + new_prog.url + '/resources'

            if settings.USE_MAILMAN and 'mailman_moderator' in settings.DEFAULT_EMAIL_ADDRESSES.keys(
            ):
                # While we're at it, create the program's mailing list
                mailing_list_name = "%s_%s" % (new_prog.program_type,
                                               new_prog.program_instance)
                teachers_list_name = "%s-%s" % (mailing_list_name, "teachers")
                students_list_name = "%s-%s" % (mailing_list_name, "students")

                create_list(
                    students_list_name,
                    settings.DEFAULT_EMAIL_ADDRESSES['mailman_moderator'])
                create_list(
                    teachers_list_name,
                    settings.DEFAULT_EMAIL_ADDRESSES['mailman_moderator'])

                load_list_settings(teachers_list_name,
                                   "lists/program_mailman.config")
                load_list_settings(students_list_name,
                                   "lists/program_mailman.config")

                apply_list_settings(
                    teachers_list_name, {
                        'owner': [
                            settings.
                            DEFAULT_EMAIL_ADDRESSES['mailman_moderator'],
                            new_prog.director_email
                        ]
                    })
                apply_list_settings(
                    students_list_name, {
                        'owner': [
                            settings.
                            DEFAULT_EMAIL_ADDRESSES['mailman_moderator'],
                            new_prog.director_email
                        ]
                    })

                if 'archive' in settings.DEFAULT_EMAIL_ADDRESSES.keys():
                    add_list_member(students_list_name, [
                        new_prog.director_email,
                        settings.DEFAULT_EMAIL_ADDRESSES['archive']
                    ])
                    add_list_member(teachers_list_name, [
                        new_prog.director_email,
                        settings.DEFAULT_EMAIL_ADDRESSES['archive']
                    ])

            return HttpResponseRedirect(manage_url)
        else:
            raise ESPError(False), "Improper form data submitted."

    #   If the form has been submitted, process it.
    if request.method == 'POST':
        form = ProgramCreationForm(request.POST)

        if form.is_valid():
            temp_prog = form.save(commit=False)
            perms, modules = prepare_program(temp_prog, form.cleaned_data)
            #   Save the form's raw data instead of the form itself, or its clean data.
            #   Unpacking of the data happens at the next step.

            context_pickled = pickle.dumps({
                'prog_form_raw':
                form.data,
                'perms':
                perms,
                'modules':
                modules,
                'cost':
                form.cleaned_data['base_cost'],
                'sibling_discount':
                form.cleaned_data['sibling_discount']
            })
            request.session['context_str'] = context_pickled

            return render_to_response('program/newprogram_review.html',
                                      request, {
                                          'prog': temp_prog,
                                          'perms': perms,
                                          'modules': modules
                                      })

    else:
        #   Otherwise, the default view is a blank form.
        if template_prog:
            form = ProgramCreationForm(template_prog)
        else:
            form = ProgramCreationForm()

    return render_to_response(
        'program/newprogram.html', request, {
            'form': form,
            'programs': Program.objects.all().order_by('-id'),
            'template_prog_id': template_prog_id
        })
Example #9
0
    def save_classroom(self, program):
        """ Steps for saving a classroom:
        -   Find the previous list of resources
        -   Create a new list of resources
        -   Move over resource assignments
        -   Delete old resources
        """

        orig_room_number = self.cleaned_data['orig_room_number']
        if orig_room_number == "":
            orig_room_number = self.cleaned_data['room_number']

        initial_rooms = program.getClassrooms().filter(name=orig_room_number).distinct()
        initial_furnishings = {}
        for r in initial_rooms:
            initial_furnishings[r] = list(r.associated_resources())

        timeslots = Event.objects.filter(id__in=[int(id_str) for id_str in self.cleaned_data['times_available']])
        furnishings = ResourceType.objects.filter(id__in=[int(id_str) for id_str in self.cleaned_data['furnishings']])

        rooms_to_keep = list(initial_rooms.filter(event__in=timeslots))
        rooms_to_delete = list(initial_rooms.exclude(event__in=timeslots))

        new_timeslots = timeslots.exclude(id__in=[x.event_id for x in rooms_to_keep])

        #   Make up new rooms specified by the form
        for t in new_timeslots:
            #   Create room
            new_room = Resource()
            new_room.num_students = self.cleaned_data['num_students']
            new_room.event = t
            new_room.res_type = ResourceType.get_or_create('Classroom')
            new_room.name = self.cleaned_data['room_number']
            new_room.save()
            t.new_room = new_room

            for f in furnishings:
                #   Create associated resource
                new_resource = Resource()
                new_resource.event = t
                new_resource.res_type = f
                new_resource.name = f.name + ' for ' + self.cleaned_data['room_number']
                new_resource.res_group = new_room.res_group
                new_resource.save()
                f.new_resource = new_resource


        #   Delete old, no-longer-valid resources
        for rm in rooms_to_delete:
            #   Find assignments pertaining to the old room
            ra_room = rm.assignments()
            for ra in ra_room:
                if ra.resource.event in new_timeslots:
                    ra.resource = timeslots[new_timeslots.index(ra.resource.event)].new_room
                    ra.save()

            #   Delete old resources... associated resources, then the room itself
            for f in initial_furnishings[rm]:
                f.delete()
            rm.delete()

        #   Sync existing rooms
        for room in rooms_to_keep:
            room.num_students = self.cleaned_data['num_students']
            room.name = self.cleaned_data['room_number']
            room.save()

            # Add furnishings that we didn't have before
            for f in furnishings.exclude(resource__res_group=room.res_group):
                #   Create associated resource
                new_resource = Resource()
                new_resource.event = room.event
                new_resource.res_type = f
                new_resource.name = f.name + ' for ' + self.cleaned_data['room_number']
                new_resource.res_group = room.res_group
                new_resource.save()
                f.new_resource = new_resource

            # Delete furnishings that we don't have any more
            for f in Resource.objects.filter(res_group=room.res_group).exclude(id=room.id).exclude(res_type__in=furnishings):
                f.delete()
Example #10
0
 def num_classes_assigned(self):
     #   Return the number of classes assigned to classrooms in this time slot.
     from esp.resources.models import ResourceAssignment, ResourceType
     classroom = ResourceType.get_or_create('Classroom')
     return ResourceAssignment.objects.filter(
         resource__event=self, resource__res_type=classroom).count()
Example #11
0
def newprogram(request):
    template_prog = None
    template_prog_id = None
    if 'template_prog' in request.GET and (int(request.GET["template_prog"])) != 0:       # if user selects None which value is 0,so we need to check for 0.
       #try:
        template_prog_id = int(request.GET["template_prog"])
        tprogram = Program.objects.get(id=template_prog_id)
        template_prog = {}
        template_prog.update(tprogram.__dict__)
        del template_prog["id"]
        template_prog["program_type"] = tprogram.program_type
        template_prog["program_modules"] = tprogram.program_modules.all().values_list("id", flat=True)
        template_prog["class_categories"] = tprogram.class_categories.all().values_list("id", flat=True)
        '''
        As Program Name should be new for each new program created then it is better to not to show old program names in input box .
        template_prog["term"] = tprogram.program_instance()
        template_prog["term_friendly"] = tprogram.niceName()
        '''

        student_reg_bits = list(Permission.objects.filter(permission_type__startswith='Student', program=template_prog_id).order_by('-start_date'))
        if len(student_reg_bits) > 0:
            newest_bit = student_reg_bits[0]
            oldest_bit = student_reg_bits[-1]

            template_prog["student_reg_start"] = oldest_bit.start_date
            template_prog["student_reg_end"] = newest_bit.end_date

        teacher_reg_bits = list(Permission.objects.filter(permission_type__startswith='Teacher', program=template_prog_id).order_by('-start_date'))
        if len(teacher_reg_bits) > 0:
            newest_bit = teacher_reg_bits[0]
            oldest_bit = teacher_reg_bits[-1]

            template_prog["teacher_reg_start"] = oldest_bit.start_date
            template_prog["teacher_reg_end"] = newest_bit.end_date

        pac = ProgramAccountingController(tprogram)
        line_items = pac.get_lineitemtypes(required_only=True).values('amount_dec')

        template_prog["base_cost"] = int(sum(x["amount_dec"] for x in line_items))
        template_prog["sibling_discount"] = tprogram.sibling_discount

    if 'checked' in request.GET:
        # Our form's anchor is wrong, because the form asks for the parent of the anchor that we really want.
        # Don't bother trying to fix the form; just re-set the anchor when we're done.
        context = pickle.loads(request.session['context_str'])
        pcf = ProgramCreationForm(context['prog_form_raw'])
        if pcf.is_valid():

            new_prog = pcf.save(commit = True)

            commit_program(new_prog, context['perms'], context['modules'], context['cost'], context['sibling_discount'])

            # Create the default resource types now
            default_restypes = Tag.getProgramTag('default_restypes', program=new_prog)
            if default_restypes:
                resource_type_labels = json.loads(default_restypes)
                resource_types = [ResourceType.get_or_create(x, new_prog) for x in resource_type_labels]

            #   Force all ProgramModuleObjs and their extensions to be created now
            new_prog.getModules()

            manage_url = '/manage/' + new_prog.url + '/resources'

            if settings.USE_MAILMAN and 'mailman_moderator' in settings.DEFAULT_EMAIL_ADDRESSES.keys():
                # While we're at it, create the program's mailing list
                mailing_list_name = "%s_%s" % (new_prog.program_type, new_prog.program_instance)
                teachers_list_name = "%s-%s" % (mailing_list_name, "teachers")
                students_list_name = "%s-%s" % (mailing_list_name, "students")

                create_list(students_list_name, settings.DEFAULT_EMAIL_ADDRESSES['mailman_moderator'])
                create_list(teachers_list_name, settings.DEFAULT_EMAIL_ADDRESSES['mailman_moderator'])

                load_list_settings(teachers_list_name, "lists/program_mailman.config")
                load_list_settings(students_list_name, "lists/program_mailman.config")

                apply_list_settings(teachers_list_name, {'owner': [settings.DEFAULT_EMAIL_ADDRESSES['mailman_moderator'], new_prog.director_email]})
                apply_list_settings(students_list_name, {'owner': [settings.DEFAULT_EMAIL_ADDRESSES['mailman_moderator'], new_prog.director_email]})

                if 'archive' in settings.DEFAULT_EMAIL_ADDRESSES.keys():
                    add_list_members(students_list_name, [new_prog.director_email, settings.DEFAULT_EMAIL_ADDRESSES['archive']])
                    add_list_members(teachers_list_name, [new_prog.director_email, settings.DEFAULT_EMAIL_ADDRESSES['archive']])


            return HttpResponseRedirect(manage_url)
        else:
            raise ESPError("Improper form data submitted.", log=False)


    #   If the form has been submitted, process it.
    if request.method == 'POST':
        form = ProgramCreationForm(request.POST)

        if form.is_valid():
            temp_prog = form.save(commit=False)
            perms, modules = prepare_program(temp_prog, form.cleaned_data)
            #   Save the form's raw data instead of the form itself, or its clean data.
            #   Unpacking of the data happens at the next step.

            context_pickled = pickle.dumps({'prog_form_raw': form.data, 'perms': perms, 'modules': modules, 'cost': form.cleaned_data['base_cost'], 'sibling_discount': form.cleaned_data['sibling_discount']})
            request.session['context_str'] = context_pickled

            return render_to_response('program/newprogram_review.html', request, {'prog': temp_prog, 'perms':perms, 'modules': modules})

    else:
        #   Otherwise, the default view is a blank form.
        if template_prog:
            form = ProgramCreationForm(template_prog)
        else:
            form = ProgramCreationForm()

    return render_to_response('program/newprogram.html', request, {'form': form, 'programs': Program.objects.all().order_by('-id'),'template_prog_id':template_prog_id})
Example #12
0
    def resources(self, request, tl, one, two, module, extra, prog):
        context = {}

        #   Process commands.  I know the code is mostly copied between the three options, and
        #   I will try to condense it intelligently when  I get the chance.
        if extra == 'timeslot':
            if request.GET.has_key('op') and request.GET['op'] == 'edit':
                #   pre-fill form
                current_slot = Event.objects.get(id=request.GET['id'])
                context['timeslot_form'] = TimeslotForm()
                context['timeslot_form'].load_timeslot(current_slot)

            if request.GET.has_key('op') and request.GET['op'] == 'delete':
                #   show delete confirmation page
                context['prog'] = self.program
                context['timeslot'] = Event.objects.get(id=request.GET['id'])
                return render_to_response(
                    self.baseDir() + 'timeslot_delete.html', request,
                    (prog, tl), context)

            if request.method == 'POST':
                data = request.POST

                if data['command'] == 'reallyremove':
                    #   delete timeslot
                    ts = Event.objects.get(id=data['id'])
                    ts.delete()

                elif data['command'] == 'addedit':
                    #   add/edit timeslot
                    form = TimeslotForm(data)
                    if form.is_valid():
                        if form.cleaned_data['id'] is not None:
                            new_timeslot = Event.objects.get(
                                id=form.cleaned_data['id'])
                        else:
                            new_timeslot = Event()

                        form.save_timeslot(self.program, new_timeslot)
                    else:
                        context['timeslot_form'] = form

        elif extra == 'restype':
            if request.GET.has_key('op') and request.GET['op'] == 'edit':
                #   pre-fill form
                current_slot = ResourceType.objects.get(id=request.GET['id'])
                context['restype_form'] = ResourceTypeForm()
                context['restype_form'].load_restype(current_slot)

            if request.GET.has_key('op') and request.GET['op'] == 'delete':
                #   show delete confirmation page
                context['prog'] = self.program
                context['restype'] = ResourceType.objects.get(
                    id=request.GET['id'])
                return render_to_response(
                    self.baseDir() + 'restype_delete.html', request,
                    (prog, tl), context)

            if request.method == 'POST':
                data = request.POST

                if data['command'] == 'reallyremove':
                    #   delete restype
                    ts = ResourceType.objects.get(id=data['id'])
                    ts.delete()

                elif data['command'] == 'addedit':
                    #   add/edit restype
                    form = ResourceTypeForm(data)

                    if form.is_valid():
                        if form.cleaned_data['id'] is not None:
                            new_restype = ResourceType.objects.get(
                                id=form.cleaned_data['id'])
                        else:
                            new_restype = ResourceType()

                        form.save_restype(self.program, new_restype)
                    else:
                        context['restype_form'] = form

        elif extra == 'classroom':
            if request.GET.has_key('op') and request.GET['op'] == 'edit':
                #   pre-fill form
                current_room = Resource.objects.get(id=request.GET['id'])
                context['classroom_form'] = ClassroomForm(self.program)
                context['classroom_form'].load_classroom(
                    self.program, current_room)

            if request.GET.has_key('op') and request.GET['op'] == 'delete':
                #   show delete confirmation page
                context['prog'] = self.program
                context['classroom'] = Resource.objects.get(
                    id=request.GET['id'])
                resources = self.program.getClassrooms().filter(
                    name=context['classroom'].name)
                context['timeslots'] = [r.event for r in resources]
                sections = ClassSection.objects.filter(
                    resourceassignment__resource__id__in=resources.values_list(
                        'id', flat=True)).distinct()

                context['sections'] = sections
                return render_to_response(
                    self.baseDir() + 'classroom_delete.html', request,
                    (prog, tl), context)

            if request.method == 'POST':
                data = request.POST

                if data['command'] == 'reallyremove':
                    #   delete classroom and associated resources
                    target_resource = Resource.objects.get(id=data['id'])
                    rooms = prog.getClassrooms().filter(
                        name=target_resource.name)
                    for room in rooms:
                        room.associated_resources().delete()
                    rooms.delete()

                elif data['command'] == 'addedit':
                    #   add/edit restype
                    form = ClassroomForm(self.program, data)

                    if form.is_valid():
                        form.save_classroom(self.program)
                    else:
                        context['classroom_form'] = form

        elif extra == 'classroom_import':
            import_mode = 'preview'
            if 'import_confirm' in request.POST and request.POST[
                    'import_confirm'] == 'yes':
                import_mode = 'save'

            import_form = ClassroomImportForm(request.POST)
            if not import_form.is_valid():
                context['import_form'] = import_form
            else:
                #   Attempt to match timeslots for the programs
                past_program = import_form.cleaned_data['program']
                ts_old = past_program.getTimeSlots().filter(
                    event_type__description__icontains='class').order_by(
                        'start')
                ts_new = self.program.getTimeSlots().filter(
                    event_type__description__icontains='class').order_by(
                        'start')
                ts_map = {}
                for i in range(min(len(ts_old), len(ts_new))):
                    ts_map[ts_old[i].id] = ts_new[i]

                resource_list = []
                #   Iterate over the resources in the previous program
                for res in past_program.getClassrooms():
                    #   If we know what timeslot to put it in, make a copy
                    if res.event.id in ts_map:
                        new_res = Resource()
                        new_res.name = res.name
                        new_res.res_type = res.res_type
                        new_res.num_students = res.num_students
                        new_res.is_unique = res.is_unique
                        new_res.user = res.user
                        new_res.event = ts_map[res.event.id]
                        #   Check to avoid duplicating rooms (so the process is idempotent)
                        if import_mode == 'save' and not Resource.objects.filter(
                                name=new_res.name,
                                event=new_res.event).exists():
                            new_res.save()
                        #   Note: furnishings are messed up, so don't bother copying those yet.
                        resource_list.append(new_res)

                #   Render a preview page showing the resources for the previous program if desired
                context['past_program'] = past_program
                if import_mode == 'preview':
                    context['prog'] = self.program
                    result = self.program.collapsed_dict(resource_list)
                    key_list = result.keys()
                    key_list.sort()
                    context['new_rooms'] = [result[key] for key in key_list]
                    return render_to_response(
                        self.baseDir() + 'classroom_import.html', request,
                        (prog, tl), context)
                else:
                    extra = 'classroom'

        elif extra == 'equipment':
            if request.GET.has_key('op') and request.GET['op'] == 'edit':
                #   pre-fill form
                equip = Resource.objects.get(id=request.GET['id'])
                context['equipment_form'] = EquipmentForm(self.program)
                context['equipment_form'].load_equipment(self.program, equip)

            if request.GET.has_key('op') and request.GET['op'] == 'delete':
                #   show delete confirmation page
                context['prog'] = self.program
                context['equipment'] = Resource.objects.get(
                    id=request.GET['id'])
                return render_to_response(
                    self.baseDir() + 'equipment_delete.html', request,
                    (prog, tl), context)

            if request.method == 'POST':
                data = request.POST

                if data['command'] == 'reallyremove':
                    #   delete this resource for all time blocks within the program
                    rl = Resource.objects.get(
                        id=data['id']).identical_resources().filter(
                            event__anchor=self.program_anchor_cached())
                    for r in rl:
                        r.delete()

                elif data['command'] == 'addedit':
                    #   add/edit restype
                    form = EquipmentForm(self.program, data)

                    if form.is_valid():
                        form.save_equipment(self.program)
                    else:
                        context['equipment_form'] = form

    #   Group contiguous blocks of time for the program
        time_options = self.program.getTimeSlots(exclude_types=[])
        time_groups = Event.group_contiguous(list(time_options))

        #   Retrieve remaining context information
        context['timeslots'] = [{'selections': group} for group in time_groups]

        if 'timeslot_form' not in context:
            context['timeslot_form'] = TimeslotForm()

        context['resource_types'] = self.program.getResourceTypes().exclude(
            priority_default=0).order_by('priority_default')
        for c in context['resource_types']:
            if c.program is None:
                c.is_global = True

        if 'restype_form' not in context:
            context['restype_form'] = ResourceTypeForm()

        if 'classroom_form' not in context:
            context['classroom_form'] = ClassroomForm(self.program)

        if 'equipment_form' not in context:
            context['equipment_form'] = EquipmentForm(self.program)

        if 'import_form' not in context:
            context['import_form'] = ClassroomImportForm()

        context['open_section'] = extra
        context['prog'] = self.program
        context['module'] = self

        #   Display default form
        return render_to_response(self.baseDir() + 'resource_main.html',
                                  request, (prog, tl), context)
Example #13
0
def newprogram(request):
    template_prog = None
    template_prog_id = None
    if 'template_prog' in request.GET and (
            int(request.GET["template_prog"])
    ) != 0:  # if user selects None which value is 0,so we need to check for 0.
        #try:
        template_prog_id = int(request.GET["template_prog"])
        tprogram = Program.objects.get(id=template_prog_id)
        template_prog = {}
        template_prog.update(tprogram.__dict__)
        del template_prog["id"]

        template_prog["program_modules"] = tprogram.program_modules.all(
        ).values_list("id", flat=True)
        template_prog["class_categories"] = tprogram.class_categories.all(
        ).values_list("id", flat=True)
        '''
        As Program Name should be new for each new program created then it is better to not to show old program names in input box .
        template_prog["term"] = tprogram.anchor.name
        template_prog["term_friendly"] = tprogram.anchor.friendly_name
        '''
        template_prog["anchor"] = tprogram.anchor.parent.id

        # aseering 5/18/2008 -- List everyone who was granted V/Administer on the specified program
        template_prog["admins"] = ESPUser.objects.filter(
            userbit__verb=GetNode("V/Administer"),
            userbit__qsc=tprogram.anchor).values_list("id", flat=True)

        # aseering 5/18/2008 -- More aggressively list everyone who was an Admin
        #template_prog["admins"] = [ x.id for x in UserBit.objects.bits_get_users(verb=GetNode("V/Administer"), qsc=tprogram.anchor, user_objs=True) ]

        program_visible_bits = list(
            UserBit.objects.bits_get_users(
                verb=GetNode("V/Flags/Public"), qsc=tprogram.anchor).filter(
                    user__isnull=True).order_by("-startdate"))
        if len(program_visible_bits) > 0:
            newest_bit = program_visible_bits[0]
            oldest_bit = program_visible_bits[-1]

            template_prog["publish_start"] = oldest_bit.startdate
            template_prog["publish_end"] = newest_bit.enddate

        student_reg_bits = list(
            UserBit.objects.bits_get_users(
                verb=GetNode("V/Deadline/Registration/Student"),
                qsc=tprogram.anchor).filter(
                    user__isnull=True).order_by("-startdate"))
        if len(student_reg_bits) > 0:
            newest_bit = student_reg_bits[0]
            oldest_bit = student_reg_bits[-1]

            template_prog["student_reg_start"] = oldest_bit.startdate
            template_prog["student_reg_end"] = newest_bit.enddate

        teacher_reg_bits = list(
            UserBit.objects.bits_get_users(
                verb=GetNode("V/Deadline/Registration/Teacher"),
                qsc=tprogram.anchor).filter(
                    user__isnull=True).order_by("-startdate"))
        if len(teacher_reg_bits) > 0:
            newest_bit = teacher_reg_bits[0]
            oldest_bit = teacher_reg_bits[-1]

            template_prog["teacher_reg_start"] = oldest_bit.startdate
            template_prog["teacher_reg_end"] = newest_bit.enddate

        line_items = LineItemType.objects.filter(
            anchor__name="Required",
            anchor__parent__parent=tprogram.anchor).values(
                "amount", "finaid_amount")

        template_prog["base_cost"] = int(
            -sum([x["amount"] for x in line_items]))
        template_prog["finaid_cost"] = int(
            -sum([x["finaid_amount"] for x in line_items]))

    if 'checked' in request.GET:
        # Our form's anchor is wrong, because the form asks for the parent of the anchor that we really want.
        # Don't bother trying to fix the form; just re-set the anchor when we're done.
        context = pickle.loads(request.session['context_str'])
        pcf = ProgramCreationForm(context['prog_form_raw'])
        if pcf.is_valid():
            # Fix the anchor friendly name right away, otherwise in-memory caches cause (mild) issues later on
            anchor = GetNode(pcf.cleaned_data['anchor'].get_uri() + "/" +
                             pcf.cleaned_data["term"])
            anchor.friendly_name = pcf.cleaned_data['term_friendly']
            anchor.save()

            new_prog = pcf.save(
                commit=False)  # don't save, we need to fix it up:
            new_prog.anchor = anchor
            new_prog.save()
            pcf.save_m2m()

            commit_program(new_prog, context['datatrees'], context['userbits'],
                           context['modules'], context['costs'])

            # Create the default resource types now
            default_restypes = Tag.getProgramTag('default_restypes',
                                                 program=new_prog)
            if default_restypes:
                resource_type_labels = json.loads(default_restypes)
                resource_types = [
                    ResourceType.get_or_create(x, new_prog)
                    for x in resource_type_labels
                ]

            #   Force all ProgramModuleObjs and their extensions to be created now
            new_prog.getModules()

            manage_url = '/manage/' + new_prog.url() + '/resources'

            if settings.USE_MAILMAN and 'mailman_moderator' in settings.DEFAULT_EMAIL_ADDRESSES.keys(
            ):
                # While we're at it, create the program's mailing list
                mailing_list_name = "%s_%s" % (new_prog.anchor.parent.name,
                                               new_prog.anchor.name)
                teachers_list_name = "%s-%s" % (mailing_list_name, "teachers")
                students_list_name = "%s-%s" % (mailing_list_name, "students")

                create_list(
                    students_list_name,
                    settings.DEFAULT_EMAIL_ADDRESSES['mailman_moderator'])
                create_list(
                    teachers_list_name,
                    settings.DEFAULT_EMAIL_ADDRESSES['mailman_moderator'])

                load_list_settings(teachers_list_name,
                                   "lists/program_mailman.config")
                load_list_settings(students_list_name,
                                   "lists/program_mailman.config")

                apply_list_settings(
                    teachers_list_name, {
                        'owner': [
                            settings.
                            DEFAULT_EMAIL_ADDRESSES['mailman_moderator'],
                            new_prog.director_email
                        ]
                    })
                apply_list_settings(
                    students_list_name, {
                        'owner': [
                            settings.
                            DEFAULT_EMAIL_ADDRESSES['mailman_moderator'],
                            new_prog.director_email
                        ]
                    })

                if 'archive' in settings.DEFAULT_EMAIL_ADDRESSES.keys():
                    add_list_member(students_list_name, [
                        new_prog.director_email,
                        settings.DEFAULT_EMAIL_ADDRESSES['archive']
                    ])
                    add_list_member(teachers_list_name, [
                        new_prog.director_email,
                        settings.DEFAULT_EMAIL_ADDRESSES['archive']
                    ])

            return HttpResponseRedirect(manage_url)
        else:
            raise ESPError(False), "Improper form data submitted."

    #   If the form has been submitted, process it.
    if request.method == 'POST':
        form = ProgramCreationForm(request.POST)

        if form.is_valid():
            temp_prog = form.save(commit=False)
            datatrees, userbits, modules = prepare_program(
                temp_prog, form.cleaned_data)
            #   Save the form's raw data instead of the form itself, or its clean data.
            #   Unpacking of the data happens at the next step.

            context_pickled = pickle.dumps({
                'prog_form_raw':
                form.data,
                'datatrees':
                datatrees,
                'userbits':
                userbits,
                'modules':
                modules,
                'costs': (form.cleaned_data['base_cost'],
                          form.cleaned_data['finaid_cost'])
            })
            request.session['context_str'] = context_pickled

            return render_to_response(
                'program/newprogram_review.html', request,
                GetNode('Q/Programs/'), {
                    'prog': temp_prog,
                    'datatrees': datatrees,
                    'userbits': userbits,
                    'modules': modules
                })

    else:
        #   Otherwise, the default view is a blank form.
        if template_prog:
            form = ProgramCreationForm(template_prog)
        else:
            form = ProgramCreationForm()

    return render_to_response(
        'program/newprogram.html', request, GetNode('Q/Programs/'), {
            'form': form,
            'programs': Program.objects.all().order_by('-id'),
            'template_prog_id': template_prog_id
        })
    def satprep_classgen(self, request, tl, one, two, module, extra, prog):
        """ This view will generate the classes for all the users. """

        delete = False

        if not request.method == 'POST' and not request.POST.has_key(
                'newclass_create'):
            #   Show the form asking for a room number and capacity for each teacher.

            user_list = self.program.getLists()['teachers_satprepinfo']['list']
            reginfos = [
                module_ext.SATPrepTeacherModuleInfo.objects.get(program=prog,
                                                                user=u)
                for u in user_list
            ]
            reginfos.sort(key=lambda x: x.subject + x.section)
            user_list = [r.user for r in reginfos]

            context = {
                'timeslots': prog.getTimeSlots(),
                'teacher_data': zip([ESPUser(u) for u in user_list], reginfos)
            }

            return render_to_response(self.baseDir() + 'newclass_confirm.html',
                                      request, (prog, tl), context)

        #   Delete current classes if specified (currently turned off, not necessary)
        if delete:
            cur_classes = ClassSubject.objects.filter(
                parent_program=self.program)
            [cls.delete() for cls in cur_classes]

        data = request.POST

        #   Pull the timeslots from the multiselect field on the form.
        timeslots = []
        for ts_id in data.getlist('timeslot_ids'):
            ts = Event.objects.get(id=ts_id)
            timeslots.append(ts)

        #   Create classrooms based on the form input.
        for key in data:
            key_dir = key.split('_')
            if len(key_dir) == 2 and key_dir[0] == 'room' and len(
                    data[key]) > 0:
                #   Extract a room number and capacity from POST data.
                room_num = data.get(key)
                cap_key = 'capacity_' + key_dir[1]
                room_capacity = int(data.get(cap_key))
                reginfo = module_ext.SATPrepTeacherModuleInfo.objects.get(
                    id=int(key_dir[1]))
                user = reginfo.user

                #   Initialize a class subject.
                newclass = ClassSubject()
                newclass.parent_program = self.program
                newclass.class_info = '%s: Section %s (%s)' % (
                    reginfo.get_subject_display(), reginfo.section,
                    reginfo.get_section_display())
                newclass.grade_min = 9
                newclass.grade_max = 12

                newclass.class_size_min = 0
                newclass.class_size_max = room_capacity

                newclass.category = ClassCategories.objects.get(
                    category='SATPrep')
                newclass.anchor = self.program.classes_node()

                newclass.save()

                nodestring = 'SAT' + str(newclass.id)
                newclass.anchor = self.program.classes_node().tree_create(
                    [nodestring])
                newclass.anchor.friendly_name = 'SAT Prep %s - %s' % (
                    reginfo.get_subject_display(),
                    reginfo.get_section_display())
                newclass.anchor.save()
                newclass.anchor.tree_create(['TeacherEmail'])
                newclass.save()

                newclass.makeTeacher(user)
                newclass.accept()

                #   Create a section of the class for each timeslot.
                #   The sections are all held in the same room by default.  This can be changed
                #   in the scheduling module later.
                for ts in timeslots:
                    new_room, created = Resource.objects.get_or_create(
                        name=room_num,
                        res_type=ResourceType.get_or_create('Classroom'),
                        event=ts)
                    new_room.num_students = room_capacity
                    new_room.save()
                    sec = newclass.add_section(
                        duration=(ts.duration().seconds / 3600.0))
                    sec.meeting_times.add(ts)
                    sec.assign_room(new_room)
                    sec.status = 10
                    sec.save()

        #dummy_anchor.delete()
        return HttpResponseRedirect('/manage/%s/schedule_options' %
                                    self.program.getUrlBase())