# Path: search/views.py
# def search(request):
#     result = {}
#     q = request.GET.get('q', '')
#     q = ' '.join(group_words(q))
#     page = request.GET.get('page', '')
#     size = request.GET.get('size', '')
#     code = request.GET.get('code', '')
#     dept_required = request.GET.get('dept_required', '')
#     sortby_param = request.GET.get('sort', '')
#     reverse_param = request.GET.get('reverse', '')
#     ys = request.GET.get('ys', SEMESTER)
# 
#     page_size = size or 10
#     sortby = sortby_param or 'time_token'
#     reverse = True if reverse_param == 'true' else False
#     rev_sortby = '-' + sortby if reverse else sortby
# 
#     courses = SearchQuerySet().filter()
# 
#     if dept_required:
#         try:
#             courses = Department.objects.get(
#                 ys=ys, dept_name=dept_required).required_course.all()
#         except:
#             pass
#         if courses:
#             result['type'] = 'required'
#             page_size = courses.count()
#     else:
#         courses = courses.filter(content=AutoQuery(q))
#         if code:
#             courses = courses.filter(code__contains=code)
# 
#         if code in ['GE', 'GEC']:
#             core = request.GET.get(code.lower(), '')
#             if core:
#                 courses = courses.filter(ge__contains=core)
#     courses = courses.order_by(rev_sortby)
#     paginator = Paginator(courses, page_size)
# 
#     try:
#         courses_page = paginator.page(page)
#     except PageNotAnInteger:
#         # If page is not an integer, deliver first page.
#         courses_page = paginator.page(1)
#     except EmptyPage:
#         # If page is out of range (e.g. 9999), deliver last page of results.
#         courses_page = paginator.page(paginator.num_pages)
# 
#     course_list = [
#         {k: v for (k, v) in x.__dict__.items() if not k.startswith('_')}
#         for x in [
#             x if dept_required else x.object for x in courses_page.object_list]
#     ]
# 
#     result['total'] = courses.count()
#     result['page'] = courses_page.number
#     result['courses'] = course_list
#     result['page_size'] = page_size
# 
#     return JsonResponse(result)
# 
# @cache_page(60 * 60)
# def syllabus(request, no):
#     course = get_object_or_404(Course, no=no)
#     return render(request, 'search/syllabus.html',
#                   {'course': course, 'syllabus_path': request.path})
# 
# def hit(request, no):
#     course = get_object_or_404(Course, no=no)
#     course.hit += 1
#     course.save()
#     return HttpResponse('')
# 
# def autocomplete(request):
#     q = request.GET.get('term', '')
#     q = ' '.join(group_words(q))
#     code = request.GET.get('code', '')
#     result = []
#     courses = SearchQuerySet().filter(content=AutoQuery(q))
# 
#     if code:
#         courses = courses.filter(code__contains=code)
#     if code in ['GE', 'GEC']:
#         core = request.GET.get(code.lower(), '')
#         if core:
#             courses = courses.filter(ge__contains=core)
#     if courses.count() < 100:
#         course_list = [x.chi_title for x in courses]
#         result = [{'value': c} for c in set(course_list)]
# 
#     return JsonResponse(result, safe=False)


from django.conf.urls import url
from .views import search, syllabus, hit, autocomplete

urlpatterns = [
    url(r'^$', search),
    url(r'^syllabus/(?P<no>.+)/$',
        syllabus, name='syllabus'),
    url(r'^hit/(?P<no>.+)/$', hit, name='hit'),

# Path: search/views.py
# def search(request):
#     result = {}
#     q = request.GET.get('q', '')
#     q = ' '.join(group_words(q))
#     page = request.GET.get('page', '')
#     size = request.GET.get('size', '')
#     code = request.GET.get('code', '')
#     dept_required = request.GET.get('dept_required', '')
#     sortby_param = request.GET.get('sort', '')
#     reverse_param = request.GET.get('reverse', '')
#     ys = request.GET.get('ys', SEMESTER)
# 
#     page_size = size or 10
#     sortby = sortby_param or 'time_token'
#     reverse = True if reverse_param == 'true' else False
#     rev_sortby = '-' + sortby if reverse else sortby
# 
#     courses = SearchQuerySet().filter()
# 
#     if dept_required:
#         try:
#             courses = Department.objects.get(
#                 ys=ys, dept_name=dept_required).required_course.all()
#         except:
#             pass
#         if courses:
#             result['type'] = 'required'
#             page_size = courses.count()
#     else:
#         courses = courses.filter(content=AutoQuery(q))
#         if code:
#             courses = courses.filter(code__contains=code)
# 
#         if code in ['GE', 'GEC']:
#             core = request.GET.get(code.lower(), '')
#             if core:
#                 courses = courses.filter(ge__contains=core)
#     courses = courses.order_by(rev_sortby)
#     paginator = Paginator(courses, page_size)
# 
#     try:
#         courses_page = paginator.page(page)
#     except PageNotAnInteger:
#         # If page is not an integer, deliver first page.
#         courses_page = paginator.page(1)
#     except EmptyPage:
#         # If page is out of range (e.g. 9999), deliver last page of results.
#         courses_page = paginator.page(paginator.num_pages)
# 
#     course_list = [
#         {k: v for (k, v) in x.__dict__.items() if not k.startswith('_')}
#         for x in [
#             x if dept_required else x.object for x in courses_page.object_list]
#     ]
# 
#     result['total'] = courses.count()
#     result['page'] = courses_page.number
#     result['courses'] = course_list
#     result['page_size'] = page_size
# 
#     return JsonResponse(result)
# 
# @cache_page(60 * 60)
# def syllabus(request, no):
#     course = get_object_or_404(Course, no=no)
#     return render(request, 'search/syllabus.html',
#                   {'course': course, 'syllabus_path': request.path})
# 
# def hit(request, no):
#     course = get_object_or_404(Course, no=no)
#     course.hit += 1
#     course.save()
#     return HttpResponse('')
# 
# def autocomplete(request):
#     q = request.GET.get('term', '')
#     q = ' '.join(group_words(q))
#     code = request.GET.get('code', '')
#     result = []
#     courses = SearchQuerySet().filter(content=AutoQuery(q))
# 
#     if code:
#         courses = courses.filter(code__contains=code)
#     if code in ['GE', 'GEC']:
#         core = request.GET.get(code.lower(), '')
#         if core:
#             courses = courses.filter(ge__contains=core)
#     if courses.count() < 100:
#         course_list = [x.chi_title for x in courses]
#         result = [{'value': c} for c in set(course_list)]
# 
#     return JsonResponse(result, safe=False)


# Path: search/urls.py
from django.conf.urls import url
from .views import search, syllabus, hit, autocomplete


urlpatterns = [
    url(r'^$', search),
    url(r'^syllabus/(?P<no>.+)/$',
        syllabus, name='syllabus'),
    url(r'^hit/(?P<no>.+)/$', hit, name='hit'),

    url(r'^autocomplete/$', autocomplete, name='autocomplete'),