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 resources_classroom_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 = ClassroomImportForm(request.POST) if not import_form.is_valid(): context['import_form'] = import_form else: past_program = import_form.cleaned_data['program'] complete_availability = import_form.cleaned_data['complete_availability'] resource_list = [] if complete_availability: # Make classrooms available at all of the new program's timeslots for resource in past_program.groupedClassrooms(): for timeslot in self.program.getTimeSlots(): new_res = Resource( name = resource.name, res_type = resource.res_type, num_students = resource.num_students, is_unique = resource.is_unique, user = resource.user, event = timeslot ) if import_mode == 'save' and not Resource.objects.filter(name=new_res.name, event=new_res.event).exists(): new_res.save() resource_list.append(new_res) else: # Attempt to match timeslots for the programs 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] # 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 context['complete_availability'] = complete_availability 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] response = render_to_response(self.baseDir()+'classroom_import.html', request, context) else: extra = 'classroom' return (response, context)
def resources_classroom_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 = ClassroomImportForm(request.POST) if not import_form.is_valid(): context['import_form'] = import_form else: past_program = import_form.cleaned_data['program'] complete_availability = import_form.cleaned_data[ 'complete_availability'] resource_list = [] if complete_availability: # Make classrooms available at all of the new program's timeslots for resource in past_program.groupedClassrooms(): for timeslot in self.program.getTimeSlots(): new_res = Resource(name=resource.name, res_type=resource.res_type, num_students=resource.num_students, is_unique=resource.is_unique, user=resource.user, event=timeslot) if import_mode == 'save' and not Resource.objects.filter( name=new_res.name, event=new_res.event).exists(): new_res.save() resource_list.append(new_res) else: # Attempt to match timeslots for the programs 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] # 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 context['complete_availability'] = complete_availability 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] response = render_to_response( self.baseDir() + 'classroom_import.html', request, context) else: extra = 'classroom' return (response, context)
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()
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)