<commit_before>from django import forms
from django.forms import Form
from django.http import HttpResponse, HttpResponseRedirect, Http404, HttpResponseNotAllowed
from django.core.urlresolvers import reverse
from django.views import generic
from django.shortcuts import get_object_or_404, render, redirect
from django.core.paginator import Paginator, InvalidPage, EmptyPage
from django.core.serializers.json import DjangoJSONEncoder
from django.db.models import Count, Avg, Q
from django.views.generic import View, ListView
from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import login_required
from aspc.courses.models import (Section, Department, Schedule, RefreshHistory, START_DATE, END_DATE, Term, Course,
                                 Instructor, CourseReview, FeaturingQuery)
from aspc.courses.forms import SearchForm, ICalExportForm, ReviewSearchForm, ReviewForm
import json
import datetime
import vobject
import operator
from dateutil import rrule

def _get_refresh_history():
    try:
        last_full = RefreshHistory.objects.order_by(
            '-last_refresh_date'
        ).filter(
            type=RefreshHistory.FULL
        )[0]
    except IndexError:
        last_full = None

    try:
        last_reg = RefreshHistory.objects.order_by(
            '-last_refresh_date'
        ).filter(
            type=RefreshHistory.REGISTRATION
        )[0]
    except IndexError:
        last_reg = None

    return last_full, last_reg

def schedule(request):
    last_full, last_reg = _get_refresh_history()
    featuring_queries = FeaturingQuery.objects.all()

    if not request.method == "GET" or len(request.GET) == 0:
        form = SearchForm()
        return render(request, 'schedule/schedule.html', {
            'form': form,
            'last_full': last_full,
            'last_reg': last_reg,
            'featuring_queries': featuring_queries
        })
    else:
        form = SearchForm(request.GET)
        if form.is_valid():
            results_set, term = form.build_queryset_and_term()
            request.session['term_key'] = term.key
            paginator = Paginator(results_set, per_page=20, orphans=5)
            GET_data = request.GET.copy()

            try:
                page = int(request.GET.get('page', '1'))
                if GET_data.get('page', False):
                    del GET_data['page']
            except ValueError:
                page = 1

            try:
                results = paginator.page(page)
            except (EmptyPage, InvalidPage):
                results = paginator.page(paginator.num_pages)

            for course in results.object_list:
                if course.id in request.session.get('schedule_courses', []):
                    course.added = True

            return render(request, 'schedule/schedule.html', {
                'form': form,
                'results': results,
                'path': ''.join([request.path, '?', GET_data.urlencode()]),
                'last_full': last_full,
                'last_reg': last_reg,
                'featuring_queries': featuring_queries
            })
        else:
            return render(request, 'schedule/schedule.html', {
                'form': form,
                'last_full': last_full,
                'last_reg': last_reg,
                'featuring_queries': featuring_queries
            })

def load_from_session(request):
    all_events = []
    valid_courses = set()
    schedule_courses = request.session.get('schedule_courses', set())
    for course in Section.objects.filter(id__in=schedule_courses):
        all_events.append(course.json())
        valid_courses.add(course.pk)
    if schedule_courses - valid_courses:
        for invalid in (schedule_courses - valid_courses):
            request.session['schedule_courses'].remove(invalid)
        request.session.modified = True
    return HttpResponse(content=json.dumps(all_events, cls=DjangoJSONEncoder), content_type='application/json')

def clear_schedule(request):
    courses_info = []
    if request.session.get('schedule_courses', False):
        schedule_courses = request.session['schedule_courses']
        del request.session['schedule_courses']
        for course in Section.objects.filter(id__in=schedule_courses):
            courses_info.append(course.json())
    return HttpResponse(content=json.dumps(courses_info, cls=DjangoJSONEncoder), content_type='application/json')

def share_schedule(request):
    schedule_courses = Section.objects.filter(id__in=request.session.get('schedule_courses',[]))
    if request.method == "POST":
        s = Schedule()
        if request.user.is_authenticated():
            s.user = request.user
        s.save()
        for course in schedule_courses:
            s.sections.add(course)

        return render(request, 'schedule/schedule_share.html', {'saved': True, 'schedule': s, 'schedule_courses': s.sections.all(),})
    else:
        return render(request, 'schedule/schedule_share.html', {'schedule_courses': schedule_courses,})

def view_schedule(request, schedule_id):
    schedule = get_object_or_404(Schedule, pk=schedule_id)
    if request.method == "POST":
        request.session['schedule_courses'] = set([c.id for c in schedule.sections.all()])
        return HttpResponseRedirect(reverse('aspc.courses.views.schedule'))
    else:
        return render(request, 'schedule/schedule_frozen.html',{'schedule': schedule,})
<commit_msg>Add login for previous courses schedules<commit_after>from django import forms
from django.forms import Form
from django.http import HttpResponse, HttpResponseRedirect, Http404, HttpResponseNotAllowed
from django.core.urlresolvers import reverse
from django.views import generic
from django.shortcuts import get_object_or_404, render, redirect
from django.core.paginator import Paginator, InvalidPage, EmptyPage
from django.core.serializers.json import DjangoJSONEncoder
from django.db.models import Count, Avg, Q
from django.views.generic import View, ListView
from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import login_required
from aspc.courses.models import (Section, Department, Schedule, RefreshHistory, START_DATE, END_DATE, Term, Course,
                                 Instructor, CourseReview, FeaturingQuery)
from aspc.courses.forms import SearchForm, ICalExportForm, ReviewSearchForm, ReviewForm
import json
import datetime
import vobject
import operator
from dateutil import rrule

def _get_refresh_history():
    try:
        last_full = RefreshHistory.objects.order_by(
            '-last_refresh_date'
        ).filter(
            type=RefreshHistory.FULL
        )[0]
    except IndexError:
        last_full = None

    try:
        last_reg = RefreshHistory.objects.order_by(
            '-last_refresh_date'
        ).filter(
            type=RefreshHistory.REGISTRATION
        )[0]
    except IndexError:
        last_reg = None

    return last_full, last_reg

def schedule(request):
    last_full, last_reg = _get_refresh_history()
    featuring_queries = FeaturingQuery.objects.all()

    if not request.method == "GET" or len(request.GET) == 0:
        form = SearchForm()
        return render(request, 'schedule/schedule.html', {
            'form': form,
            'last_full': last_full,
            'last_reg': last_reg,
            'featuring_queries': featuring_queries
        })
    else:
        form = SearchForm(request.GET)
        if form.is_valid():
            results_set, term = form.build_queryset_and_term()
            request.session['term_key'] = term.key
            paginator = Paginator(results_set, per_page=20, orphans=5)
            GET_data = request.GET.copy()

            try:
                page = int(request.GET.get('page', '1'))
                if GET_data.get('page', False):
                    del GET_data['page']
            except ValueError:
                page = 1

            try:
                results = paginator.page(page)
            except (EmptyPage, InvalidPage):
                results = paginator.page(paginator.num_pages)

            for course in results.object_list:
                if course.id in request.session.get('schedule_courses', []):
                    course.added = True

            return render(request, 'schedule/schedule.html', {
                'form': form,
                'results': results,
                'path': ''.join([request.path, '?', GET_data.urlencode()]),
                'last_full': last_full,
                'last_reg': last_reg,
                'featuring_queries': featuring_queries
            })
        else:
            return render(request, 'schedule/schedule.html', {
                'form': form,
                'last_full': last_full,
                'last_reg': last_reg,
                'featuring_queries': featuring_queries
            })

def load_from_session(request):
    all_events = []
    valid_courses = set()
    schedule_courses = request.session.get('schedule_courses', set())
    for course in Section.objects.filter(id__in=schedule_courses):
        all_events.append(course.json())
        valid_courses.add(course.pk)
    if schedule_courses - valid_courses:
        for invalid in (schedule_courses - valid_courses):
            request.session['schedule_courses'].remove(invalid)
        request.session.modified = True
    return HttpResponse(content=json.dumps(all_events, cls=DjangoJSONEncoder), content_type='application/json')

def clear_schedule(request):
    courses_info = []
    if request.session.get('schedule_courses', False):
        schedule_courses = request.session['schedule_courses']
        del request.session['schedule_courses']
        for course in Section.objects.filter(id__in=schedule_courses):
            courses_info.append(course.json())
    return HttpResponse(content=json.dumps(courses_info, cls=DjangoJSONEncoder), content_type='application/json')

def share_schedule(request):
    schedule_courses = Section.objects.filter(id__in=request.session.get('schedule_courses',[]))
    if request.method == "POST":
        s = Schedule()
        if request.user.is_authenticated():
            s.user = request.user
        s.save()
        for course in schedule_courses:
            s.sections.add(course)

        return render(request, 'schedule/schedule_share.html', {'saved': True, 'schedule': s, 'schedule_courses': s.sections.all(),})
    else:
        return render(request, 'schedule/schedule_share.html', {'schedule_courses': schedule_courses,})

def view_schedule(request, schedule_id):
    schedule = get_object_or_404(Schedule, pk=schedule_id)
    if request.method == "POST":
        request.session['schedule_courses'] = set([c.id for c in schedule.sections.all()])
        return HttpResponseRedirect(reverse('aspc.courses.views.schedule'))
    else:
        return render(request, 'schedule/schedule_frozen.html',{'schedule': schedule,})
@login_required
{'fraction_non_alphanumeric': 0.10198687568355815, 'fraction_numerical': 0.003098796937659497, 'mean_word_length': 2.734853641933288, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '729629', 'n_tokens_mistral': 3091, 'n_tokens_neox': 3011, 'n_words': 654}