<commit_before>from django.core.paginator import Paginator, InvalidPage, EmptyPage
from django.core.serializers.json import DjangoJSONEncoder
from aspc.coursesearch.models import Course, Department, Meeting, Schedule
from aspc.coursesearch.forms import SearchForm
import re, json
from django.http import HttpResponse, HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.views.generic import list_detail
from django.shortcuts import get_object_or_404, render
from django.conf import settings
import shlex, subprocess

def search(request):
    if request.method == "GET":
        if len(request.GET) > 0:
            form = SearchForm(request.GET)
            if form.is_valid():
                results_set = form.build_queryset()
                paginator = Paginator(results_set, per_page=20, orphans=10)
                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)
                
                return render(request, 'coursesearch/search.html', {'form': form, 'results': results, 'path': ''.join([request.path, '?', GET_data.urlencode()]),})
            else:
                return render(request, 'coursesearch/search.html', {'form': form,})
        else:
            form = SearchForm()
            return render(request, 'coursesearch/search.html', {'form': form,})

def schedule(request):
    if request.method == "GET":
        if len(request.GET) > 0:
            form = SearchForm(request.GET)
            if form.is_valid():
                results_set = form.build_queryset()
                paginator = Paginator(results_set, per_page=10, 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, 'coursesearch/schedule.html', {'form': form, 'results': results, 'path': ''.join([request.path, '?', GET_data.urlencode()]),})
            else:
                return render(request, 'coursesearch/schedule.html', {'form': form,})
        else:
            form = SearchForm()
            return render(request, 'coursesearch/schedule.html', {'form': form,})

def load_from_session(request):
    all_events = []
    for course_id in request.session.get('schedule_courses', []):
        all_events.append(Course.objects.get(pk=course_id).json())
    return HttpResponse(content=json.dumps(all_events, cls=DjangoJSONEncoder), mimetype='application/json')<commit_msg>Added check for deleted courses in load_from_session to fix error<commit_after>from django.core.paginator import Paginator, InvalidPage, EmptyPage
from django.core.serializers.json import DjangoJSONEncoder
from aspc.coursesearch.models import Course, Department, Meeting, Schedule
from aspc.coursesearch.forms import SearchForm
import re, json
from django.http import HttpResponse, HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.views.generic import list_detail
from django.shortcuts import get_object_or_404, render
from django.conf import settings
import shlex, subprocess

def search(request):
    if request.method == "GET":
        if len(request.GET) > 0:
            form = SearchForm(request.GET)
            if form.is_valid():
                results_set = form.build_queryset()
                paginator = Paginator(results_set, per_page=20, orphans=10)
                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)
                
                return render(request, 'coursesearch/search.html', {'form': form, 'results': results, 'path': ''.join([request.path, '?', GET_data.urlencode()]),})
            else:
                return render(request, 'coursesearch/search.html', {'form': form,})
        else:
            form = SearchForm()
            return render(request, 'coursesearch/search.html', {'form': form,})

def schedule(request):
    if request.method == "GET":
        if len(request.GET) > 0:
            form = SearchForm(request.GET)
            if form.is_valid():
                results_set = form.build_queryset()
                paginator = Paginator(results_set, per_page=10, 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, 'coursesearch/schedule.html', {'form': form, 'results': results, 'path': ''.join([request.path, '?', GET_data.urlencode()]),})
            else:
                return render(request, 'coursesearch/schedule.html', {'form': form,})
        else:
            form = SearchForm()
            return render(request, 'coursesearch/schedule.html', {'form': form,})

def load_from_session(request):
    all_events = []
    valid_courses = set()
    schedule_courses = request.session.get('schedule_courses', set())
    for course in Course.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), mimetype='application/json')
{'fraction_non_alphanumeric': 0.10791169614699027, 'fraction_numerical': 0.0043877690936514465, 'mean_word_length': 1.9328508242862887, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4480858', 'n_tokens_mistral': 1962, 'n_tokens_neox': 1868, 'n_words': 427}