def course(request, offset): course_id = int(offset) try: course = Course.objects.get(pk=course_id) except Course.DoesNotExist: return {'error_code': 'CourseNotFound'}, 404 response = { 'id': course_id, 'orig_id': course.original_id, 'name': unify_brackets(course.name), 'credit': float_nullable(course.credit), 'teacher': listify_str(course.teacher), 'ta': listify_str(course.ta), 'semester_id': course.semester_id, 'lessons': [{ 'day': lesson.day, 'start': lesson.start, 'end': lesson.end, 'location': lesson.location, 'weekset_id': lesson.weekset_id, 'weeks': lesson.weeks, 'weeks_display': lesson.weeks_display, } for lesson in course.lesson_set.all()] } return response
def course_list(request): after = request.GET.get('after', None) user_profile = request.user.get_profile() course_statuses = (user_profile.coursestatus_set.all(). select_related('course').prefetch_related('course__lesson_set')) if after: try: course_statuses = course_statuses.filter(time__gte=after) except ValidationError: return {'error_code': 'SyntaxError'}, 400 response = [{ 'id': course_status.course_id, 'status': CourseStatus.STATUS_CHOICES_DICT[course_status.status], 'orig_id': course_status.course.original_id, 'name': unify_brackets(course_status.course.name), 'credit': float_nullable(course_status.course.credit), 'teacher': listify_str(course_status.course.teacher), 'ta': listify_str(course_status.course.ta), 'semester_id': course_status.course.semester_id, 'lessons': [{ 'day': lesson.day, 'start': lesson.start, 'end': lesson.end, 'location': lesson.location, 'weekset_id': lesson.weekset_id, 'weeks': lesson.weeks, 'weeks_display': lesson.weeks_display, } for lesson in course_status.course.lesson_set.all()] } for course_status in course_statuses] return response
def all(request): semester_id = request.GET.get('semester_id', None) if not semester_id: return {'error_code': 'SyntaxError'}, 400 try: semester = Semester.objects.get(pk=semester_id) except Semester.DoesNotExist: return {'error_code': 'SemesterNotFound'}, 404 cache_name = 'semester_{0}_courses'.format(semester.pk) response = cache.get(cache_name) if not response: response = [{ 'id': course.id, 'orig_id': course.original_id, 'name': unify_brackets(course.name), 'credit': float_nullable(course.credit), 'teacher': listify_str(course.teacher), 'ta': listify_str(course.ta), 'semester_id': course.semester_id, 'lessons': [{ 'day': lesson.day, 'start': lesson.start, 'end': lesson.end, 'location': lesson.location, 'weekset_id': lesson.weekset_id, 'weeks': lesson.weeks, 'weeks_display': lesson.weeks_display, } for lesson in course.lesson_set.all()], } for course in semester.course_set.all().prefetch_related( 'lesson_set')] # timeout: 6 months cache.set(cache_name, bz2.compress(cPickle.dumps(response), 9), 15552000) else: response = cPickle.loads(bz2.decompress(response)) return response
def course_list(request): after = request.GET.get('after', None) user_profile = request.user.get_profile() course_statuses = (user_profile.coursestatus_set.all().select_related( 'course').prefetch_related('course__lesson_set')) if after: try: course_statuses = course_statuses.filter(time__gte=after) except ValidationError: return {'error_code': 'SyntaxError'}, 400 response = [{ 'id': course_status.course_id, 'status': CourseStatus.STATUS_CHOICES_DICT[course_status.status], 'orig_id': course_status.course.original_id, 'name': unify_brackets(course_status.course.name), 'credit': float_nullable(course_status.course.credit), 'teacher': listify_str(course_status.course.teacher), 'ta': listify_str(course_status.course.ta), 'semester_id': course_status.course.semester_id, 'lessons': [{ 'day': lesson.day, 'start': lesson.start, 'end': lesson.end, 'location': lesson.location, 'weekset_id': lesson.weekset_id, 'weeks': lesson.weeks, 'weeks_display': lesson.weeks_display, } for lesson in course_status.course.lesson_set.all()] } for course_status in course_statuses] return response
def gen_ical(request): cal = Calendar() cal['version'] = '2.0' cal['prodid'] = '-//Prototype//Nanbeige//ZH' # *mandatory elements* where the prodid can be changed, see RFC 5445 user_profile = request.user.get_profile() # get current semester semesters = user_profile.campus.university.semester_set.all() today = date.today() for s in semesters: if today > s.week_start and today < s.week_end: semester = s break course_statuses = user_profile.coursestatus_set.filter( course__semester=semester).exclude(status=CourseStatus.CANCEL) university = user_profile.campus.university for course_status in course_statuses: for lesson in course_status.course.lesson_set.all(): if lesson.weekset == None: weekgroup = listify_int(lesson.weeks) else: weekgroup = listify_int(lesson.weekset.weeks) for recur in weekgroup: event = Event() event.add('summary', unify_brackets(course_status.course.name)) offset = timedelta(days=lesson.day - 1 + 7 * (int(recur) - 1)) classdate = semester.week_start + offset start = university.scheduleunit_set.get( number=lesson.start).start end = university.scheduleunit_set.get(number=lesson.end).end event.add('dtstart', datetime.combine(classdate, start)) event.add('dtend', datetime.combine(classdate, end)) event.add('location', lesson.location) event.add('description', u'教师:' + course_status.course.teacher) event['uid'] = str( uuid1()) + '@Nanbeige' # change after formal name...? cal.add_component(event) return HttpResponse(cal.to_ical(), mimetype="text/calendar")
def gen_ical(request): cal = Calendar() cal['version'] = '2.0' cal['prodid'] = '-//Prototype//Nanbeige//ZH' # *mandatory elements* where the prodid can be changed, see RFC 5445 user_profile = request.user.get_profile() # get current semester semesters = user_profile.campus.university.semester_set.all() today = date.today() for s in semesters: if today > s.week_start and today < s.week_end: semester = s break course_statuses = user_profile.coursestatus_set.filter(course__semester=semester).exclude(status=CourseStatus.CANCEL) university = user_profile.campus.university for course_status in course_statuses: for lesson in course_status.course.lesson_set.all(): if lesson.weekset == None: weekgroup = listify_int(lesson.weeks) else: weekgroup = listify_int(lesson.weekset.weeks) for recur in weekgroup: event = Event() event.add('summary', unify_brackets(course_status.course.name)) offset = timedelta(days = lesson.day - 1 + 7 * (int(recur) - 1)) classdate = semester.week_start + offset start = university.scheduleunit_set.get(number=lesson.start).start end = university.scheduleunit_set.get(number=lesson.end).end event.add('dtstart', datetime.combine(classdate, start)) event.add('dtend', datetime.combine(classdate, end)) event.add('location', lesson.location) event.add('description', u'教师:' + course_status.course.teacher) event['uid'] = str(uuid1()) + '@Nanbeige' # change after formal name...? cal.add_component(event) return HttpResponse(cal.to_ical(), mimetype="text/calendar")
def all(request): semester_id = request.GET.get('semester_id', None) if not semester_id: return {'error_code': 'SyntaxError'}, 400 try: semester = Semester.objects.get(pk=semester_id) except Semester.DoesNotExist: return {'error_code': 'SemesterNotFound'}, 404 cache_name = 'semester_{0}_courses'.format(semester.pk) response = cache.get(cache_name) if not response: response = [{ 'id': course.id, 'orig_id': course.original_id, 'name': unify_brackets(course.name), 'credit': float_nullable(course.credit), 'teacher': listify_str(course.teacher), 'ta': listify_str(course.ta), 'semester_id': course.semester_id, 'lessons': [{ 'day': lesson.day, 'start': lesson.start, 'end': lesson.end, 'location': lesson.location, 'weekset_id': lesson.weekset_id, 'weeks': lesson.weeks, 'weeks_display': lesson.weeks_display, } for lesson in course.lesson_set.all()], } for course in semester.course_set.all().prefetch_related('lesson_set')] # timeout: 6 months cache.set(cache_name, bz2.compress(cPickle.dumps(response), 9), 15552000) else: response = cPickle.loads(bz2.decompress(response)) return response