Skip to content

Commit

Permalink
Detect other individuals potentially mentioned in subject lines.
Browse files Browse the repository at this point in the history
  • Loading branch information
Melissa Draper committed Aug 4, 2012
1 parent f5a8f27 commit a0b3b11
Showing 1 changed file with 42 additions and 5 deletions.
47 changes: 42 additions & 5 deletions brain/views.py
Expand Up @@ -14,8 +14,9 @@
from django.conf import settings from django.conf import settings
from django.shortcuts import render_to_response from django.shortcuts import render_to_response
from django.template import Template from django.template import Template
from django.utils.html import escape
from django.db import connection
from brain.models import Person, Alias from brain.models import Person, Alias

from datetime import date, datetime, timedelta, time from datetime import date, datetime, timedelta, time
from dateutil.relativedelta import MO from dateutil.relativedelta import MO
from dateutil.rrule import rrule,DAILY from dateutil.rrule import rrule,DAILY
Expand All @@ -40,7 +41,9 @@ def parse_mbox(message):
for part in message.get_payload(): for part in message.get_payload():
parse_mbox(part) parse_mbox(part)
else: else:
return message.get_payload(decode=True) body = message.get_payload(decode=True)
return escape(body)



def mbox(date_obj, weekday): def mbox(date_obj, weekday):


Expand Down Expand Up @@ -75,7 +78,7 @@ def mbox(date_obj, weekday):
body = parse_mbox(message).replace('\n','<br />\n') body = parse_mbox(message).replace('\n','<br />\n')
subject = email.header.decode_header(message['subject']) subject = email.header.decode_header(message['subject'])
subject = subject[0][0] subject = subject[0][0]
subject = subject.replace('[Whereis] ', '') subject = escape(subject.replace('[Whereis] ', ''))
who = message['from'].split('(') who = message['from'].split('(')
who = who[0].strip() who = who[0].strip()
who = who.replace(' at ', '@') who = who.replace(' at ', '@')
Expand Down Expand Up @@ -224,6 +227,32 @@ def taghilight(body):


return body return body


def getothers(subject, sender):

subject = ' %s ' % subject
cursor = connection.cursor()
cursor.execute('with list as '
'(select a.id, a.alias, '
'(select distinct count(*) '
'as cnt from brain_alias a2 where a2.alias = a.alias) '
'as occurs from brain_alias a) '
'select alias from list '
'where occurs = 1;')
aliaslist = cursor.fetchall()
aliaslist = map(' '.join, aliaslist)

others = []
o = others.append

for alias in aliaslist:
if subject.find(alias) > 0:
matchq = Alias.objects.filter( alias = alias )
match = matchq[0].person_id
if match != sender:
o(match)

return ' '.join(others)

def singleday(year, month, day): def singleday(year, month, day):
d = date(year, month, day) d = date(year, month, day)
dateformatted = d.strftime('%b %d %Y'); dateformatted = d.strftime('%b %d %Y');
Expand All @@ -245,15 +274,23 @@ def singleday(year, month, day):
a('<tr><th colspan="2"><a class="nounder" href="/%s"><</a>&nbsp;%s&nbsp;<a class="nounder" href="/%s">></a></th></tr>' % (yesterday, d, tomorrow)) a('<tr><th colspan="2"><a class="nounder" href="/%s"><</a>&nbsp;%s&nbsp;<a class="nounder" href="/%s">></a></th></tr>' % (yesterday, d, tomorrow))


for mboxmail in mboxlist: for mboxmail in mboxlist:
avatar_url = libravatar_url(mboxmail['From']) avatar_url = libravatar_url(email = mboxmail['From'], size = 150)


try: try:
tagstring = gettags(mboxmail['From'], mboxmail['Body']) tagstring = gettags(mboxmail['From'], mboxmail['Body'])
tagstring = ' | '.join(tagstring) tagstring = ' | '.join(tagstring)
except: except:
tagstring = '' tagstring = ''


a('<tr><td class="headers"><p class="subject">%s</p><p>%s</p><img src="%s"><p>%s</p></td><td class="what">%s<br/><br/>%s</td></tr>' % (mboxmail['Subject'], mboxmail['From'], avatar_url, mboxmail['Date'], taghilight(mboxmail['Body']), tagstring)) try:
others = getothers(mboxmail['Subject'], mboxmail['From'])
except:
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>') a('</table>')


Expand Down

0 comments on commit a0b3b11

Please sign in to comment.