From ea92af529e6dab5db320acdaad82ab56a44c2777 Mon Sep 17 00:00:00 2001 From: Magnus Hagander Date: Tue, 9 Jul 2013 18:40:55 +0200 Subject: [PATCH] Add some basic API functionality Ability to view latest messages on a list, and the contents of a thread --- django/archives/mailarchives/api.py | 65 +++++++++++++++++++++++++++++ django/archives/settings.py | 2 + django/archives/urls.py | 4 ++ 3 files changed, 71 insertions(+) create mode 100644 django/archives/mailarchives/api.py diff --git a/django/archives/mailarchives/api.py b/django/archives/mailarchives/api.py new file mode 100644 index 0000000..4f31c60 --- /dev/null +++ b/django/archives/mailarchives/api.py @@ -0,0 +1,65 @@ +from django.http import HttpResponse, HttpResponseForbidden +from django.shortcuts import get_object_or_404 +from django.conf import settings + +from views import cache +from models import Message, List + +import simplejson as json + + +@cache(hours=4) +def latest(request, listname): + if not request.META['REMOTE_ADDR'] in settings.API_CLIENTS: + return HttpResponseForbidden('Invalid host') + + # Return the latest messages on this list. + # If is not specified, return 50. Max value for is 100. + if request.GET.has_key('n'): + try: + limit = int(request.GET['n']) + except: + limit = 0 + else: + limit = 50 + if limit <= 0 or limit > 100: + limit = 50 + + list = get_object_or_404(List, listname=listname) + mlist = Message.objects.defer('bodytxt', 'cc', 'to').select_related().extra(where=["threadid IN (SELECT threadid FROM list_threads WHERE listid=%s)" % list.listid]).order_by('date')[:limit] + allyearmonths = set([(m.date.year, m.date.month) for m in mlist]) + + resp = HttpResponse(content_type='application/json') + json.dump([ + {'msgid': m.messageid, + 'date': m.date.isoformat(), + 'from': m.mailfrom, + 'subj': m.subject,} + for m in mlist], resp) + + # Make sure this expires from the varnish cache when new entries show + # up in this month. + resp['X-pglm'] = ':%s:' % (':'.join(['%s/%s/%s' % (list.listid, year, month) for year, month in allyearmonths])) + return resp + + +@cache(hours=4) +def thread(request, msgid): + if not request.META['REMOTE_ADDR'] in settings.API_CLIENTS: + return HttpResponseForbidden('Invalid host') + + # Return metadata about a single thread. A list of all the emails + # that are in the thread with their basic attributes are included. + msg = get_object_or_404(Message, messageid=msgid) + mlist = Message.objects.defer('bodytxt', 'cc', 'to').filter(threadid=msg.threadid) + + resp = HttpResponse(content_type='application/json') + json.dump([ + {'msgid': m.messageid, + 'date': m.date.isoformat(), + 'from': m.mailfrom, + 'subj': m.subject, + 'att': m.has_attachment} + for m in mlist], resp) + resp['X-pgthread'] = m.threadid + return resp diff --git a/django/archives/settings.py b/django/archives/settings.py index e468892..0a01c3b 100644 --- a/django/archives/settings.py +++ b/django/archives/settings.py @@ -152,6 +152,8 @@ FORCE_SCRIPT_NAME="" # Always override! MBOX_ARCHIVES_ROOT="/dev/null" +SEARCH_CLIENTS = ('127.0.0.1',) +API_CLIENTS = ('127.0.0.1',) try: from settings_local import * diff --git a/django/archives/urls.py b/django/archives/urls.py index 1dcea65..18809c8 100644 --- a/django/archives/urls.py +++ b/django/archives/urls.py @@ -37,6 +37,10 @@ urlpatterns = patterns('', (r'^message-id/attachment/(\d+)/.*$', 'archives.mailarchives.views.attachment'), + # API calls + (r'^list/([\w-]+)/latest.json$', 'archives.mailarchives.api.latest'), + (r'^message-id.json/(.+)$', 'archives.mailarchives.api.thread'), + # Legacy forwarding from old archives site (r'^message-id/legacy/([\w-]+)/(\d+)-(\d+)/msg(\d+).php$', 'archives.mailarchives.views.legacy'), -- 2.39.5