Permalink
Browse files

Add search page

  • Loading branch information...
1 parent 010f4cb commit fe7d071cfce8d4707b579f1f406cce56e43f6528 @meldra committed Aug 7, 2012
Showing with 163 additions and 4 deletions.
  1. +1 −0 brain/models.py
  2. +81 −0 brain/templates/search.html
  3. +80 −4 brain/views.py
  4. +1 −0 urls.py
View
@@ -29,5 +29,6 @@ class Archive(models.Model):
sender = models.CharField(max_length=64)
subject = models.CharField(max_length=200)
body = models.TextField()
+ others = models.TextField()
class Meta:
unique_together = ("date", "sender")
@@ -0,0 +1,81 @@
+<!--
+# WasWhereWhen is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published
+# by the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# WasWhereWhen is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with WasWhereWhen. If not, see <http://www.gnu.org/licenses/>.
+-->
+<html>
+<head>
+{%block stylesheet %}
+ <style type="text/css">
+ table {
+ width: 100%;
+ height: 100%;
+ border:3px solid darkred;
+ }
+ th {
+ background-color:#ccc;
+ }
+ th.month {
+ background-color: darkred;
+ color: #fff;
+ }
+ th.month a {
+ color:#fff;
+ }
+ td {
+ width:14%;
+ text-align:center;
+ padding: 5px;
+ }
+ td.sun, td.sat {
+ background-color:eee;
+ }
+ td.today {
+ border:1px solid darkred;
+ }
+ td.headers {
+ border:1px solid darkred;
+ text-align: left;
+ }
+ td.what {
+ border:1px solid darkred;
+ padding-bottom: 20px;
+ text-align: left;
+ width: 70%;
+ }
+ .singleday td {
+ text-align: left;
+ vertical-align: top;
+ }
+ p.subject{
+ font-weight:bold;
+ }
+ .nounder {
+ text-decoration:none;
+ }
+ .hilight {
+ background-color: #FFE34D;
+ }
+ </style>
+{% endblock stylesheet%}
+</head>
+<body>
+<a href="https://github.com/meldra/waswherewhen"><img style="position: absolute; top: 0; right: 0; border: 0; width:85px; height:85px;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_gray_6d6d6d.png" alt="Fork me on GitHub"></a>
+<form>
+{{search.as_table}}
+<input type="submit" value="Find whereis records for this person" />
+<form>
+<br/>
+<br/>
+{{results|safe}}
+</body>
+</html>
View
@@ -12,10 +12,15 @@
# along with WasWhereWhen. If not, see <http://www.gnu.org/licenses/>.
#
import calendar, mailbox, email.utils, urllib2, simplejson, re, djutils.decorators
+from django import forms
+from django.db.models import Q
+from django.forms.extras.widgets import SelectDateWidget
+from django.forms.fields import DateField, ChoiceField
+from django.core.validators import email_re
from django.conf import settings
from django.shortcuts import render_to_response
from django.template import Template
-from django.utils.html import escape
+from django.utils.html import escape, conditional_escape as esc
from django.db import connection
from django.core import serializers
from brain.models import Person, Alias, Archive
@@ -24,10 +29,10 @@
from dateutil.rrule import rrule,DAILY
from calendar import HTMLCalendar
from itertools import groupby
-from django.utils.html import conditional_escape as esc
from cStringIO import StringIO
from gzip import GzipFile
from libravatar import libravatar_url
+from urllib import unquote
withyear = False
aliascachetimer = date.today()
@@ -105,15 +110,16 @@ def mbox(date_obj, weekday, reply=True):
who = who.replace(' at ', '@')
maildate = datetime(*email.utils.parsedate(message['date'])[:6])
bodyexpl = body.split('--')
+ others = getothers(subject, who)
try:
- archive = Archive.objects.get_or_create(date = maildate, sender = who, subject = subject, body = bodyexpl[0])
+ archive = Archive.objects.get_or_create(date = maildate, sender = who, subject = subject, body = bodyexpl[0], others = others)
except:
continue
if reply == True:
if maildate.strftime('%b %d %Y') == date_obj.strftime('%b %d %Y'):
- messages.append({'date': message['date'],'sender': who, 'subject': subject, 'body': bodyexpl[0]})
+ messages.append({'date': message['date'],'sender': who, 'subject': subject, 'body': bodyexpl[0], 'others': others})
if reply == True:
return messages
@@ -473,3 +479,73 @@ def index(request, year=0, month=0, day=0):
return render_to_response('brain/templates/index.html', locals())
+class EmailChoices(forms.Form):
+ emails = []
+
+ for person in Person.objects.all():
+ emails.append([person.individual, person.individual])
+
+ email = forms.ChoiceField(choices=emails, label='')
+
+def mboxperson(person):
+
+ mboxquery = Archive.objects.filter(Q(sender=person) | Q(others__contains=person)).order_by('-date')
+ jsondata = serializers.serialize('json', mboxquery)
+ rows = simplejson.loads(jsondata)
+ mboxlist = []
+ m = mboxlist.append
+
+ for l in rows:
+ m(l['fields'])
+
+ v = []
+ a = v.append
+ a('<table class="singleday">')
+ a('<tr><th colspan="2">%s</th></tr>' % (person))
+
+ if len(mboxlist) < 1:
+ a('<tr><th><h1>Nothing in the whereis archives for this person</h1></th></tr>')
+
+ for mboxmail in mboxlist:
+ avatar_url = libravatar_url(email = mboxmail['sender'], size = 150)
+
+ if settings.DIRECTORY_JSON:
+
+ try:
+ tagstring = gettags(mboxmail['sender'], mboxmail['body'])
+ tagstring = ' | '.join(tagstring)
+ except:
+ tagstring = ''
+
+ try:
+ others = getothers(mboxmail['subject'], mboxmail['sender'])
+ except:
+ others = ''
+ else:
+ tagstring = others = ''
+
+ if len(others) > 0:
+ others = 'Possible mentions:<br/>%s' % others
+
+ a('<tr><td class="headers"><p class="subject">%s</p><img src="%s"><p class="hilight">%s</p><p>%s</p></td><td class="what">%s<br/><br/>%s</td></tr>' % (mboxmail['subject'], avatar_url, others, mboxmail['date'], taghilight(mboxmail['body']), tagstring))
+
+ a('</table>')
+
+ return '\n'.join(v)
+
+def is_valid_email(email):
+ return True if email_re.match(email) else False
+
+def search(request):
+ search = EmailChoices(auto_id=False)
+
+ try:
+ addr = unquote(request.GET['email'])
+ valid = is_valid_email(addr)
+
+ if valid == True:
+ results = mboxperson(addr)
+ except:
+ results = ''
+
+ return render_to_response('search.html', locals())
View
@@ -22,6 +22,7 @@
# url(r'^$', 'waswherewhen.views.home', name='home'),
# url(r'^waswherewhen/', include('waswherewhen.foo.urls')),
url(r'^$', 'brain.views.index'),
+ url(r'^search$', 'brain.views.search'),
url(r'^(?P<year>\d+)/$', 'brain.views.index'),
url(r'^(?P<year>\d+)/(?P<month>\d+)/$', 'brain.views.index'),
url(r'^(?P<year>\d+)/(?P<month>\d+)/(?P<day>\d+)/$', 'brain.views.index'),

0 comments on commit fe7d071

Please sign in to comment.