/
views.py
144 lines (110 loc) · 4.77 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from django.core.paginator import Paginator
from urllib import unquote
from haystack.query import SearchQuerySet
from mail.models import *
from django.db.models import Q
import re
RESULTS_PER_PAGE = 50
def _search_string(request):
return request.GET.get('q', None)
def _search_tokens(request):
s = _search_string(request)
if s is None:
return []
# protection!
re_sanitize = re.compile(r'[^\w\d\s\'"\,\.\?\$]', re.I)
s = re_sanitize.sub('', s)
tokens = []
re_quotes = re.compile(r'\"([^\"]+)\"')
for m in re_quotes.findall(s):
tokens.append(m.replace('"','').strip())
s = s.replace('"%s"' % m, '')
for t in s.split(' '):
tokens.append(t.strip())
while '' in tokens:
tokens.remove('')
return tokens
def _highlight(text, tokens):
regexes = []
sorted_tokens = sorted(tokens, key=lambda x: len(x))
for t in sorted_tokens:
regexes.append(re.compile(r'(%s)' % t.replace(' ', r'\s+'), re.I))
for r in regexes:
text = r.sub('<span class="highlight">\\1</span>', text)
return text
def _annotate_threads(request, threads):
read_cookie = unquote(request.COOKIES.get('kagan_read','')).replace(',,', ',')
if len(read_cookie)>0:
if read_cookie[0]==',':
read_cookie = read_cookie[1:]
if read_cookie[-1]==',':
read_cookie = read_cookie[:-1]
try:
read_ids = map(lambda x: (x!='') and int(x) or 0, read_cookie.split(','))
except:
read_ids = []
threads_obj = []
for thread in threads:
threads_obj.append({'read': (thread.id in read_ids), 'obj': thread})
return threads_obj
def _annotate_emails(emails, search=[]):
r = []
for email in emails:
email.text = _highlight(email.text, search)
r.append({ 'to_html': email.to_html(), 'cc_html': email.cc_html(), 'obj': email })
return r
def index(request, search=[], threads=None):
if threads is None:
threads_count = Thread.objects.all().count()
threads = Thread.objects.all().order_by('-date')
else:
threads_count = threads.count()
if type(threads) is SearchQuerySet:
threads = map(lambda x: x.object, threads)
p = Paginator(threads, RESULTS_PER_PAGE)
page_num = 1
try:
page_num = int(request.GET.get('page', 1))
except:
pass
page = p.page(page_num)
threads = []
for thread in page.object_list:
thread.name = _highlight(thread.name, search)
threads.append(thread)
threads = _annotate_threads(request,threads)
return render_to_response('index.html', {'range': "<strong>%d</strong> - <strong>%d</strong> of <strong>%d</strong>" % (page.start_index(), page.end_index(), threads_count), 'num_pages': p.num_pages , 'next': page_num<p.num_pages and min(p.num_pages,page_num+1) or False, 'prev': page_num>1 and max(1, page_num-1) or False, 'first': '1', 'last': p.num_pages, 'current_page': page_num, 'threads': threads, 'search': " ".join(search), 'search_orig': (_search_string(request) is not None) and _search_string(request) or '', 'path': request.path}, context_instance=RequestContext(request))
def contact(request, contact_id):
try:
person = Person.objects.get(id=contact_id)
except Thread.DoesNotExist, e:
return HttpResponseRedirect(reverse('mail.views.index'))
threads = []
emails = Email.objects.filter(Q(to=person)|Q(cc=person))
for e in emails:
threads.append(e.email_thread.id)
threads = Thread.objects.filter(id__in=threads).order_by('-date')
return index(request, threads=threads)
def contacts_index(request):
return index(request)
def thread(request, thread_id):
try:
thread = Thread.objects.get(id=thread_id)
except Thread.DoesNotExist, e:
return HttpResponseRedirect(reverse('mail.views.index'))
search = _search_tokens(request)
emails = _annotate_emails(Email.objects.filter(email_thread=thread).order_by('creation_date_time'), search)
return render_to_response('thread.html', {'thread': thread, 'emails': emails }, context_instance=RequestContext(request))
def search(request):
tokens = _search_tokens(request)
if len(tokens) is None:
return HttpResponseRedirect(reverse('mail.views.index'))
sqs = SearchQuerySet().models(Thread)
for t in tokens:
sqs = sqs.filter_or(text_and_recipients=t)
sqs = sqs.order_by('-date')
if sqs.count()==0:
return render_to_response('search_empty.html', {}, context_instance=RequestContext(request))
return index(request, search=tokens, threads=sqs)