Ejemplo n.º 1
0
 def save(self, vr=None):
     if vr:
         ts = vr.timeslot
         ts.start = self.cleaned_data['start_time']
         ts.end = self.cleaned_data['end_time']
         ts.short_description = ts.description = self.cleaned_data[
             'description']
         ts.save()
         vr.num_volunteers = self.cleaned_data['num_volunteers']
         vr.program = self.program
         vr.save()
     else:
         ts = Event()
         ts.program = self.program
         ts.start = self.cleaned_data['start_time']
         ts.end = self.cleaned_data['end_time']
         ts.short_description = ts.description = self.cleaned_data[
             'description']
         ts.event_type = EventType.get_from_desc('Volunteer')
         ts.save()
         vr = VolunteerRequest()
         vr.program = self.program
         vr.timeslot = ts
         vr.num_volunteers = self.cleaned_data['num_volunteers']
         vr.save()
Ejemplo n.º 2
0
 def save(self, vr=None):
     if vr:
         ts = vr.timeslot
         ts.start = self.cleaned_data['start_time']
         ts.end = self.cleaned_data['end_time']
         ts.short_description = ts.description = self.cleaned_data[
             'description']
         ts.save()
         vr.num_volunteers = self.cleaned_data['num_volunteers']
         vr.program = self.program
         vr.save()
     else:
         ts = Event()
         ts.anchor = self.program.anchor
         ts.start = self.cleaned_data['start_time']
         ts.end = self.cleaned_data['end_time']
         ts.short_description = ts.description = self.cleaned_data[
             'description']
         ts.event_type, created = EventType.objects.get_or_create(
             description='Volunteer')
         ts.save()
         vr = VolunteerRequest()
         vr.program = self.program
         vr.timeslot = ts
         vr.num_volunteers = self.cleaned_data['num_volunteers']
         vr.save()
Ejemplo n.º 3
0
    def resources_timeslot_import(self, request, tl, one, two, module, extra,
                                  prog):
        context = {}
        response = None

        controller = ResourceController(prog)

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

        import_form = TimeslotImportForm(request.POST)
        if not import_form.is_valid():
            context['import_timeslot_form'] = import_form
        else:
            past_program = import_form.cleaned_data['program']
            start_date = import_form.cleaned_data['start_date']

            if past_program == prog:
                raise ESPError(
                    "You're trying to import timeslots from a program"
                    " to itself! Try a different program instead.",
                    log=False)

            #   Figure out timeslot dates
            new_timeslots = []
            prev_timeslots = past_program.getTimeSlots().order_by('start')
            time_delta = start_date - prev_timeslots[0].start.date()
            for orig_timeslot in prev_timeslots:
                new_timeslot = Event(
                    program=self.program,
                    event_type=orig_timeslot.event_type,
                    short_description=orig_timeslot.short_description,
                    description=orig_timeslot.description,
                    priority=orig_timeslot.priority,
                    start=orig_timeslot.start + time_delta,
                    end=orig_timeslot.end + time_delta,
                )
                #   Save the new timeslot only if it doesn't duplicate an existing one
                if import_mode == 'save' and not Event.objects.filter(
                        program=new_timeslot.program,
                        start=new_timeslot.start,
                        end=new_timeslot.end).exists():
                    new_timeslot.save()
                new_timeslots.append(new_timeslot)

            #   Render a preview page showing the resources for the previous program if desired
            context['past_program'] = past_program
            context['start_date'] = start_date.strftime('%m/%d/%Y')
            context['new_timeslots'] = new_timeslots
            if import_mode == 'preview':
                context['prog'] = self.program
                response = render_to_response(
                    self.baseDir() + 'timeslot_import.html', request, context)
            else:
                extra = 'timeslot'

        return (response, context)
Ejemplo n.º 4
0
    def add_or_edit_timeslot(self, form):
        """ form is a TimeslotForm object   """
        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)
        return new_timeslot
Ejemplo n.º 5
0
    def teacher_events(self, request, tl, one, two, module, extra, prog):
        context = {}

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

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

            elif data['command'] == 'add':
                #   add/edit timeslot
                form = TimeslotForm(data)
                if form.is_valid():
                    new_timeslot = Event()

                    # decide type
                    type = "training"

                    if data.has_key(
                            'submit') and data['submit'] == "Add Interview":
                        type = "interview"

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

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

        interview_times = self.getTimes('interview').select_related(
            'anchor__userbit_qsc__user')
        training_times = self.getTimes('training').select_related(
            'anchor__userbit_qsc__user')

        for ts in list(interview_times) + list(training_times):
            ts.teachers = [
                x.user.first_name + ' ' + x.user.last_name + ' <' +
                x.user.email + '>' for x in self.bitsBySlot(ts.anchor)
            ]

        context['prog'] = prog
        context['interview_times'] = interview_times
        context['training_times'] = training_times

        return render_to_response(self.baseDir() + 'teacher_events.html',
                                  request, (prog, tl), context)
Ejemplo n.º 6
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)