def as_dict(self, human_readable=False): agend = {} for i in range(0, 7): agend[i] = [] # If temporary list, return empty dict if self.blocks == [0]: return agend # We're going to make a dict with seven entries. Values will be dicts for # each day of the week. the subdicts will map start timecodes to tuples # with format (start time, end time) prev_blk = start_blk = self.blocks[0] # for each block in that course for n in xrange(1, len(self.blocks) + 1): # if block continues from previous, or past last block if n == len(self.blocks) or self.blocks[n] != prev_blk + 5: # record the previous session dow = start_blk / 1000 if human_readable: start_time = decode_time(start_blk % 1000) end_time = decode_time((prev_blk + 5) % 1000) else: start_time = (start_blk / 10.0) % 100 end_time = ((prev_blk + 5) / 10.0) % 100 info = (start_time, end_time) agend[dow].append(info) # and start a new one if n != len(self.blocks): start_blk = prev_blk = self.blocks[n] else: prev_blk = self.blocks[n] return agend
def agenda(request, student_id): selected_student = get_object_or_404(Student, netID=student_id) selected_identity = (selected_student.netID, selected_student.first_name, selected_student.last_name) user_student = Student.objects.get(netID=request.user) user_identity = (user_student.netID, user_student.first_name, user_student.last_name, user_student.pk) # Is the user looking at his own agenda? own_agenda = (selected_student == user_student) ss_courses = selected_student.course_set.all() # ss_blocks = selected_student.blocks().sort() agend = {} for i in range(0, 7): agend[i] = {} # We're going to make a dict with five entries. Values will be dicts for # each day of the week. the subdicts will map start timecodes to tuples # with format (Course, start time, end time) # for each course the student is taking for c in ss_courses: prev_blk = 0 first_blk = c.blocks[0] # and for each block in that course for blk in c.blocks: # see if the block is beginning of the session # if not, go to the next block if blk == prev_blk + 5: prev_blk = blk continue # but if so, record the previous session dow = first_blk / 1000 start_time = decode_time(first_blk % 1000) end_time = decode_time((prev_blk+5) % 1000) info = (c, start_time, end_time) agend[dow][first_blk] = info # and start a new one first_blk = blk prev_blk = blk else: # Handle the last session dow = int(str(first_blk)[0]) start_time = decode_time(first_blk % 1000) end_time = decode_time((prev_blk+5) % 1000) info = (c, start_time, end_time) agend[dow][first_blk] = info # now order all of the dictionaries for i in range(1,6): od = collections.OrderedDict(sorted(agend[i].items())) agend[i] = od dow = {} dow[0] = "Sunday" dow[1] = "Monday" dow[2] = "Tuesday" dow[3] = "Wednesday" dow[4] = "Thursday" dow[5] = "Friday" dow[6] = "Saturday" # Prepare the privacy option form if user is looking at own agenda if own_agenda: privacy_form = AgendaPrivacyForm(instance=user_student) agenda_visibility = True else: privacy_form = None agenda_visibility = selected_student.agenda_visibility # Preare the search for friend form friend_form = FriendAgendaForm() context = { 'agenda' : agend, 'dow' : dow, 'own_agenda' : own_agenda, 'user_identity' : user_identity, 'selected_identity' : selected_identity, 'privacy_form' : privacy_form, 'agenda_visibility' : agenda_visibility, 'friend_form' : friend_form, } if request.method == 'POST' and own_agenda: privacy_form = AgendaPrivacyForm(request.POST) if privacy_form.is_valid(): a_v = privacy_form.cleaned_data['agenda_visibility'] user_student.agenda_visibility = a_v user_student.save() # ugly hack to get forms to refresh request.method = 'GET' return agenda(request, user_student.netID) else: raise RuntimeError("Form was not valid") return render(request, 'wintersession/agenda.html', context)
def agenda(request, student_id): selected_student = get_object_or_404(Student, netID=student_id) selected_identity = (selected_student.netID, selected_student.first_name, selected_student.last_name) user_student = Student.objects.get(netID=request.user) user_identity = (user_student.netID, user_student.first_name, user_student.last_name, user_student.pk) # Is the user looking at his own agenda? own_agenda = (selected_student == user_student) ss_courses = selected_student.course_set.all() # ss_blocks = selected_student.blocks().sort() agend = {} for i in range(1, 6): agend[i] = {} # We're going to make a dict with five entries. Values will be dicts for # each day of the week. the subdicts will map start timecodes to tuples # with format (Course, start time, end time) # for each course the student is taking for c in ss_courses: prev_blk = 0 first_blk = c.blocks[0] # and for each block in that course for blk in c.blocks: # see if the block is beginning of the session # if not, go to the next block if blk == prev_blk + 5: prev_blk = blk continue # but if so, record the previous session dow = int(str(first_blk)[0]) start_time = decode_time(first_blk % 1000) end_time = decode_time((prev_blk + 5) % 1000) info = (c, start_time, end_time) agend[dow][first_blk] = info # and start a new one first_blk = blk prev_blk = blk else: # Handle the last session dow = int(str(first_blk)[0]) start_time = decode_time(first_blk % 1000) end_time = decode_time((prev_blk + 5) % 1000) info = (c, start_time, end_time) agend[dow][first_blk] = info # now order all of the dictionaries for i in range(1, 6): od = collections.OrderedDict(sorted(agend[i].items())) agend[i] = od dow = {} dow[1] = "Monday" dow[2] = "Tuesday" dow[3] = "Wednesday" dow[4] = "Thursday" dow[5] = "Friday" # Prepare the privacy option form if user is looking at own agenda if own_agenda: privacy_form = AgendaPrivacyForm(instance=user_student) agenda_visibility = True else: privacy_form = None agenda_visibility = selected_student.agenda_visibility # Preare the search for friend form friend_form = FriendAgendaForm() context = { 'agenda': agend, 'dow': dow, 'own_agenda': own_agenda, 'user_identity': user_identity, 'selected_identity': selected_identity, 'privacy_form': privacy_form, 'agenda_visibility': agenda_visibility, 'friend_form': friend_form, } if request.method == 'POST' and own_agenda: privacy_form = AgendaPrivacyForm(request.POST) if privacy_form.is_valid(): a_v = privacy_form.cleaned_data['agenda_visibility'] user_student.agenda_visibility = a_v user_student.save() # ugly hack to get forms to refresh request.method = 'GET' return agenda(request, user_student.netID) else: raise RuntimeError("Form was not valid") return render(request, 'wintersession/agenda.html', context)