This repository has been archived by the owner on Apr 9, 2024. It is now read-only.
/
views.py
110 lines (102 loc) · 4.9 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# -*- coding: utf-8 -*-
# CALaws
from django.core.paginator import Paginator, InvalidPage, EmptyPage
from django.db.models import Q
from django.http import Http404, HttpResponseRedirect, HttpResponse
from django.shortcuts import render_to_response
from django.template import RequestContext
from laws import models
from laws.models import SectionFile, SearchForm
from utils.searchtext import searchtext_sphinx, searchtext_FTS4
from utils.utils import *
from operator import itemgetter
from lxml import html, etree
from lxml.cssselect import CSSSelector as cs
import settings
bodysel = cs('body')
def target_remove(request):
current_url = request.get_full_path()
new_url = current_url.replace('target/','')
return HttpResponseRedirect(new_url)
@render_to("code_display.html")
def target_to_section(request, codename, target_section):
if codename == 'this':
current_url = request.META['HTTP_REFERER']
codename = current_url.split('-')[1]
#For Table of Contents, there is a trailing / that needs to be removed
codename = codename.strip('/')
print request.get_full_path()
#Hack to ensure there is one, and only one './' at the end of the url
new_url = request.get_full_path().rstrip('/').rstrip('.')+'./'
new_url = new_url.replace('this',codename)
return HttpResponseRedirect(new_url)
else:
code_current = models.Code.objects.get(name = codename)
code_fn = code_current.fullname
#target_section = target_section.rsplit('.',1)[0]+'.'
target_section = target_section.rstrip('.')+'.'
section_current = models.Section.objects.filter(code = code_current, secnumber = target_section)[0]
#print section_current
sectionfile = models.SectionFile.objects.get(section = section_current)
try:
parents = sectionfile.sectionfilename.parents
parents = parents.rsplit('>>',1)[0].split('>>',1)[1].strip()
except:
pass
tree_section = html.document_fromstring(sectionfile.text)
body = etree.tostring(bodysel(tree_section)[0], pretty_print=False, method='html') # selects the body element of the document
body = body.replace('CODE-this-', 'CODE-'+code_current.name+'-')
return locals()
def codes_index(request):
searchform = SearchForm(request.POST)
response = render_to_response('codelist.html', locals(), context_instance=RequestContext(request))
return response
@render_to("code_display.html")
def code_toc(request, codename):
searchform = SearchForm(request.POST)
code_current = models.Code.objects.get(name = codename)
code_fn = code_current.fullname
code_toc = open(settings.PROJECT_PATH +'/templates/'+ 'tocs/' + codename + '_toc.html')
tree_toc = html.document_fromstring(code_toc.read())
code_toc.close()
body = etree.tostring(bodysel(tree_toc)[0], pretty_print=False, method='html') # selects the body element of the document
body = body.replace('CODE-this-', 'CODE-'+code_current.name+'-')
return locals()
def my404(request):
"""
We need this handler to populate some variables for flatpages
"""
#user = get_user_object(request)
#profile = get_profile_object(user)
response = render_to_response('404.html', locals(), context_instance=RequestContext(request))
response.status_code = 404
return response
@render_to("search.html")
def search(request):
if request.method == u'POST':
searchform = SearchForm(request.POST)
if searchform.is_valid():
query = searchform.cleaned_data['query']
page_id = searchform.cleaned_data['page']
#Sphinx search query; returns a list of dictionaries; options set for snippet excerpts
#Limited by djangosphinx to 20 results, unless defaults are changed
resultslist = searchtext_sphinx(query)
total = len(resultslist)
#queryids = searchtext_FTS4(query)
resultsids = map(itemgetter('id'), resultslist)
if len(resultsids)>0:
try:
#Can also use .in_bulk(resultsids), but it seems a bit slower
# p_main = Paginator(all_results, 5)
# main_result = p_main.page(1) #page_id)
rpassages =[ (SectionFile.objects.get(id=r), resultslist._get_passages(
instance=SectionFile.objects.get(id=r),
fields=['text'],
words=query)['text']) for r in resultsids]
except SectionFile.objects.in_bulk(resultsids)==None:
pass
else:
emptyquery = True
else:
searchform = SearchForm(initial={"page":"1"})
return locals()