diff options
-rw-r--r-- | lib/mongo.py | 52 | ||||
-rw-r--r-- | urls.py | 2 | ||||
-rw-r--r-- | views/pages.py | 42 |
3 files changed, 96 insertions, 0 deletions
diff --git a/lib/mongo.py b/lib/mongo.py index 2ea0148..c3f2fab 100644 --- a/lib/mongo.py +++ b/lib/mongo.py @@ -7,6 +7,58 @@ from datetime import datetime connection = pymongo.Connection('localhost', 27017) +def _build_thread(emails): + thread = {} + for email in emails: + #print email['Date'], email['From'] #, email['Message-ID'] + email = Bunch(email) + ref = [] + if 'References' in email: + ref.extend(email['References'].split()[-1:]) + elif 'In-Reply-To' in email: + ref.append(email['In-Reply-To']) + if email['Message-ID'] not in thread: + thread[email['Message-ID']] = Bunch( + {'email': email, 'child': []}) + else: + thread[email['Message-ID']].email = email + for ref in set(ref): + if ref in thread: + thread[ref].child.append(email['Message-ID']) + else: + thread[ref] = Bunch( + {'email': None, 'child': [email['Message-ID']]}) + return thread + +def _tree_to_list(tree, mailid, level, thread_list): + start = tree[mailid] + start.level = level + thread_list.append(start) + for mail in start.child: + mail = tree[mail] + thread_list = _tree_to_list(tree, mail.email['Message-ID'], + level + 1, thread_list) + return thread_list + +def get_thread_list(table, threadid): + db = connection[table] + thread = list(db.mails.find({'ThreadID': threadid}, + sort=[('Date', pymongo.ASCENDING)])) + + tree = _build_thread(thread) + thread_list = [] + if thread: + thread = _tree_to_list(tree, thread[0]['Message-ID'], 0, thread_list) + return thread + else: + return [] + +def get_thread_name(table, threadid): + db = connection[table] + thread = list(db.mails.find({'ThreadID': threadid}, + sort=[('Date', pymongo.ASCENDING)]))[0] + return thread['Subject'] + def get_emails_thread(table, start_email, thread): db = connection[table] db.mails.create_index('Date') @@ -19,6 +19,8 @@ urlpatterns = patterns('', url(r'^2/archives/(?P<mlist_fqdn>.*@.*)/(?P<year>\d{4})/(?P<month>\d\d?)$', 'views.pages.archives'), url(r'^2/archives/(?P<mlist_fqdn>.*@.*)/$', 'views.pages.archives'), url(r'^2/archives/(?P<mlist_fqdn>.*@.*)$', 'views.pages.archives'), + # The thread view + url(r'^2/thread/(?P<mlist_fqdn>.*@.*)/(?P<threadid>\d+)$', 'views.pages.thread'), # This will be the new recent page url(r'^2/list$', 'views.pages.index'), url(r'^2/list/$', 'views.pages.index'), diff --git a/views/pages.py b/views/pages.py index c48d33c..5a6e694 100644 --- a/views/pages.py +++ b/views/pages.py @@ -264,3 +264,45 @@ def search_tag(request, mlist_fqdn, tag=None): else: query_string = None return _search_results_page(request, mlist_fqdn, query_string, 'Tag search') + +def thread (request, mlist_fqdn, threadid): + ''' Displays all the email for a given thread identifier ''' + list_name = mlist_fqdn.split('@')[0] + + search_form = SearchForm(auto_id=False) + t = loader.get_template('thread.html') + threads = mongo.get_thread_list(list_name, int(threadid)) + prev_thread = mongo.get_thread_name(list_name, int(threadid) - 1) + if len(prev_thread) > 40: + prev_thread = '%s...' % prev_thread[:41] + next_thread = mongo.get_thread_name(list_name, int(threadid) + 1) + if len(next_thread) > 40: + next_thread = '%s...' % next_thread[:41] + + participants = set() + cnt = 0 + for msg in threads: + msg = Bunch(msg) + # Statistics on how many participants and threads this month + participants.add(msg.email.From) + cnt = cnt + 1 + + archives_length = mongo.get_archives_length(list_name) + + c = RequestContext(request, { + 'app_name': settings.APP_NAME, + 'list_name' : list_name, + 'list_address': mlist_fqdn, + 'search_form': search_form, + 'month': 'Thread', + 'participants': participants, + 'answers': cnt, + 'first_mail': threads[0], + 'threads': threads[1:], + 'next_thread': next_thread, + 'next_thread_id': int(threadid) + 1, + 'prev_thread': prev_thread, + 'prev_thread_id': int(threadid) - 1, + 'archives_length': archives_length, + }) + return HttpResponse(t.render(c)) |