From a0b3b114d8f7dc7b74e48318188bf7355eb18296 Mon Sep 17 00:00:00 2001 From: Melissa Draper Date: Sun, 5 Aug 2012 01:05:15 +1200 Subject: [PATCH] Detect other individuals potentially mentioned in subject lines. --- brain/views.py | 47 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 5 deletions(-) diff --git a/brain/views.py b/brain/views.py index 9c6fc8f..ad840c2 100644 --- a/brain/views.py +++ b/brain/views.py @@ -14,8 +14,9 @@ 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.db import connection from brain.models import Person, Alias - from datetime import date, datetime, timedelta, time from dateutil.relativedelta import MO from dateutil.rrule import rrule,DAILY @@ -40,7 +41,9 @@ def parse_mbox(message): for part in message.get_payload(): parse_mbox(part) else: - return message.get_payload(decode=True) + body = message.get_payload(decode=True) + return escape(body) + def mbox(date_obj, weekday): @@ -75,7 +78,7 @@ def mbox(date_obj, weekday): body = parse_mbox(message).replace('\n','
\n') subject = email.header.decode_header(message['subject']) subject = subject[0][0] - subject = subject.replace('[Whereis] ', '') + subject = escape(subject.replace('[Whereis] ', '')) who = message['from'].split('(') who = who[0].strip() who = who.replace(' at ', '@') @@ -224,6 +227,32 @@ def taghilight(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): d = date(year, month, day) dateformatted = d.strftime('%b %d %Y'); @@ -245,7 +274,7 @@ def singleday(year, month, day): a('< %s >' % (yesterday, d, tomorrow)) for mboxmail in mboxlist: - avatar_url = libravatar_url(mboxmail['From']) + avatar_url = libravatar_url(email = mboxmail['From'], size = 150) try: tagstring = gettags(mboxmail['From'], mboxmail['Body']) @@ -253,7 +282,15 @@ def singleday(year, month, day): except: tagstring = '' - a('

%s

%s

%s

%s

%s' % (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:
%s' % others + + a('

%s

%s

%s

%s

%s' % (mboxmail['Subject'], avatar_url, others, mboxmail['Date'], taghilight(mboxmail['Body']), tagstring)) a('')