Exemple #1
0
def schedule_calendar(request):
    # Requests will include a 'start' value which is a Monday
    delta = datetime.timedelta(days=1)
    MON = datetime.datetime.strptime(request.GET['start'], "%Y-%m-%d")
    TUE = MON + delta
    WED = TUE + delta
    THU = WED + delta
    FRI = THU + delta
    SAT = FRI + delta
    if request.method == 'GET':
        query = ScheduleEntry.objects.filter(identifier=request.GET['identifier'])
        events = []
        for entry in query:
            course = schedule_get_course(entry)
            for meeting_time in course.meeting_times.all():
                for day in list(meeting_time.days):
                    day = MON if day == 'M' else TUE if day == 'T' else WED if day == 'W' else THU if day == 'R' else FRI if day == 'F' else SAT
                    start = datetime.datetime.combine(day, meeting_time.start_time).isoformat()
                    end = datetime.datetime.combine(day, meeting_time.end_time).isoformat()
                    events.append({
                        'id': str(course.crn),
                        'title': course.course,
                        'start': start,
                        'end': end,
                        'url': 'https://opencourseproject.com/course/' + str(course.term.value) + '/' + str(course.crn) + '/',
                    })
        days = SafeString(json.dumps(events))
        return HttpResponse(days, 201)
    else:
        return HttpResponse('Method not allowed', 405)
Exemple #2
0
def exam_calendar(request):
    if request.method == 'GET':
        query = ScheduleEntry.objects.filter(identifier=request.GET['identifier'])
        events = []
        for entry in query:
            course = schedule_get_course(entry)
            exam = exam_for_course(course)
            if exam:
                start = datetime.datetime.combine(exam.exam_date, exam.exam_start_time).isoformat()
                end = datetime.datetime.combine(exam.exam_date, exam.exam_end_time).isoformat()
                events.append({
                    'id': str(course.crn),
                    'title': course.course,
                    'start': start,
                    'end': end,
                    'url': 'https://opencourseproject.com/course/' + str(course.term.value) + '/' + str(course.crn) + '/',
                })
        exams = SafeString(json.dumps(events))
        return HttpResponse(exams, 201)
    else:
        return HttpResponse('Method not allowed', 405)
Exemple #3
0
def schedule(request):
    if request.method == 'POST':
        form = ScheduleForm(request.POST)
        if form.is_valid():
            term = form.cleaned_data['term']
    else:
        if 'term' in request.GET:
            term = Term.objects.get(value=request.GET['term'])
        else:
            profile = Profile.objects.get(user=request.user)
            if profile.default_term:
                term = profile.default_term
            else:
                term = Term.objects.all()[0]
        form = ScheduleForm()
        form.fields['term'].initial = term
    query = ScheduleEntry.objects.filter(user=request.user, term=term)
    courses = schedule_get_courses(query)
    hash = hashlib.md5(b'%s:%s' % (str(request.user.username), str(term.name))).hexdigest()[:15]
    share_url = request.build_absolute_uri('/schedule/' + hash + '/')

    credits_min = 0
    credits_max = 0
    invalid_courses = []
    if len(query) > 0:
        term = query[0].term
        user = query[0].user
        for entry in query:
            course = schedule_get_course(entry)
            if course is None:
                invalid_courses.append(entry)
                courses.remove(course)
            else:
                value = course.hours
                credits_min += int(value[:1])
                if len(value) > 1:
                    credits_max += int(value[4:])
    if credits_max > 0:
        credits_max = credits_min + credits_max

    has_exams = False
    try:
        ExamSource.objects.get(term=term)
        has_exams = True
    except ExamSource.DoesNotExist:
        has_exams = False

    table = ScheduleTable(courses)
    print_table = SchedulePrintTable(courses)
    RequestConfig(request).configure(table)
    RequestConfig(request).configure(print_table)
    context = {
        'table': table,
        'print_table': print_table,
        'form': form,
        'term': term,
        'authenticated': True,
        'by_id': False,
        'identifier': hash,
        'share': len(query) > 0,
        'share_url': share_url,
        'credits_min': credits_min,
        'credits_max': credits_max,
        'invalid_courses': invalid_courses,
        'has_exams': has_exams,
    }
    return render(request, 'schedule/course_schedule.html', context)
def schedule(request):
    term = Term.objects.all()[0]
    profile = Profile.objects.get(user=request.user)
    if request.method == 'POST':
        form = ScheduleForm(request.user, request.POST)
        if form.is_valid():
            term = form.cleaned_data['term']
    else:
        if 'term' in request.GET:
            term = Term.objects.get(value=request.GET['term'])
        else:
            term = Term.objects.all()[0]
            if profile.default_term:
                term = profile.default_term
        form = ScheduleForm(request.user)
        form.fields['term'].initial = term

    options_form = ScheduleOptionsForm()
    options_form.fields['show_colors'].initial = profile.show_colors_schedule
    options_form.fields['show_details'].initial = profile.show_details_schedule

    query = ScheduleEntry.objects.filter(user=request.user, term=term).order_by('course_crn')
    if len(query) is 0:
        temp = ScheduleEntry.objects.filter(user=request.user)
        if len(temp) > 0:
            term = schedule_get_course(temp[0]).term
            query = ScheduleEntry.objects.filter(user=request.user, term=term).order_by('course_crn')
    courses = schedule_get_courses(query)
    hash = get_identifier(request.user, term)
    share_url = request.build_absolute_uri('/schedule/' + hash + '/')

    credits_min = 0
    credits_max = 0
    time_min = datetime.time(8, 0, 0)
    time_max = datetime.time(18, 0, 0)
    show_sat = False
    invalid_courses = []
    deleted_courses = []
    if len(query) > 0:
        term = query[0].term
        user = query[0].user
        for entry in query:
            course = schedule_get_course(entry)
            if course is None:
                invalid_courses.append(entry)
                courses.remove(course)
            else:
                if course.deleted:
                    deleted_courses.append(course)
                value = course.hours
                credits_min += int(value[:1])
                meeting_time = course.primary_meeting_time
                if meeting_time is not None:
                    if meeting_time.start_time < time_min:
                        time_min = meeting_time.start_time
                    if meeting_time.end_time > time_max:
                        time_max = meeting_time.end_time
                    if 'S' in meeting_time.days:
                        show_sat = True
                if len(value) > 1:
                    credits_max += int(value[4:])
    if credits_max > 0:
        credits_max = credits_min + credits_max
    time_min = time_min.strftime('%H:%M:%S')
    time_max = time_max.strftime('%H:%M:%S')
    has_exams = False
    try:
        source = ExamSource.objects.get(term=term)
        has_exams = source.active
    except ExamSource.DoesNotExist:
        has_exams = False

    colors = get_colors_for_courses(courses)
    print_table = SchedulePrintTable(courses)
    RequestConfig(request).configure(print_table)
    context = {
        'courses': courses,
        'course_colors': colors,
        'print_table': print_table,
        'form': form,
        'options_form': options_form,
        'term': term,
        'authenticated': True,
        'identifier': hash,
        'has_courses': len(query) > 0,
        'share_url': share_url,
        'credits_min': credits_min,
        'credits_max': credits_max,
        'time_min': time_min,
        'time_max': time_max,
        'show_sat': show_sat,
        'invalid_courses': invalid_courses,
        'deleted_courses': deleted_courses,
        'has_exams': has_exams,
    }
    return render(request, 'schedule/schedule.html', context)