From 4d159ca8b4efb0b1cf0893bca63d6e9489a9095f Mon Sep 17 00:00:00 2001 From: Magnus Hagander Date: Sun, 25 Mar 2018 16:52:47 +0200 Subject: [PATCH] Fix template loaders for django 1.11 Seems django 1.11 automatically enables caching template loader, which of course breaks the ability to make any changes to the pages of a website without restarting it. And there is no way to turn it off other than to explicitly configure individual loders (the logic to turn it on in non-debug configurations is hardcoded and cannot be changed). --- django/archives/mailarchives/api.py | 25 +++++++++++++++++- django/archives/mailarchives/models.py | 15 +++++++++++ django/archives/settings.py | 5 +++- loader/sql/schema.sql | 35 ++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 2 deletions(-) diff --git a/django/archives/mailarchives/api.py b/django/archives/mailarchives/api.py index f213caa..ca0b736 100644 --- a/django/archives/mailarchives/api.py +++ b/django/archives/mailarchives/api.py @@ -3,7 +3,7 @@ from django.shortcuts import get_object_or_404 from django.conf import settings from views import cache -from models import Message, List +from models import Message, List, ApiClient, ThreadSubscription import json @@ -103,3 +103,26 @@ def thread(request, msgid): for m in mlist], resp) resp['X-pgthread'] = m.threadid return resp + +def thread_subscribe(request, msgid): + if not settings.PUBLIC_ARCHIVES: + return HttpResponseForbidden('No API access on private archives for now') + + if not request.META['REMOTE_ADDR'] in settings.API_CLIENTS: + return HttpResponseForbidden('Invalid host') + + if not request.META.has_key('HTTP_X_APIKEY'): + return HttpResponseForbidden('No API key') + + if request.method != 'PUT': + return HttpResponseForbidden('Invalid HTTP verb') + + apiclient = get_object_or_404(ApiClient, apikey=request.META['HTTP_X_APIKEY']) + msg = get_object_or_404(Message, messageid=msgid) + + (obj, created) = ThreadSubscription.objects.get_or_create(apiclient=apiclient, + threadid=msg.threadid) + if created: + return HttpResponse(status=201) + else: + return HttpResponse(status=200) diff --git a/django/archives/mailarchives/models.py b/django/archives/mailarchives/models.py index 8fe9275..6270974 100644 --- a/django/archives/mailarchives/models.py +++ b/django/archives/mailarchives/models.py @@ -114,3 +114,18 @@ class ListSubscriber(models.Model): class Meta: unique_together = (('list', 'username'), ) db_table = 'listsubscribers' + +class ApiClient(models.Model): + apikey = models.CharField(max_length=100, null=False, blank=False) + postback = models.URLField(max_length=500, null=False, blank=False) + + class Meta: + db_table = 'apiclients' + +class ThreadSubscription(models.Model): + apiclient = models.ForeignKey(ApiClient, null=False, blank=False) + threadid = models.IntegerField(null=False, blank=False) + + class Meta: + db_table = 'threadsubscriptions' + unique_together = (('apiclient', 'threadid'),) diff --git a/django/archives/settings.py b/django/archives/settings.py index 8fb47b8..1881971 100644 --- a/django/archives/settings.py +++ b/django/archives/settings.py @@ -97,12 +97,15 @@ ROOT_URLCONF = 'archives.urls' TEMPLATES = [{ 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.request', 'django.contrib.messages.context_processors.messages', ], + 'loaders': [ + 'django.template.loaders.filesystem.Loader', + 'django.template.loaders.app_directories.Loader', + ], }, }] diff --git a/loader/sql/schema.sql b/loader/sql/schema.sql index 1723806..c19dac4 100644 --- a/loader/sql/schema.sql +++ b/loader/sql/schema.sql @@ -79,6 +79,24 @@ CREATE TABLE attachments( ); CREATE INDEX idx_attachments_msg ON attachments(message); +CREATE TABLE apiclients( + id SERIAL NOT NULL PRIMARY KEY, + apikey varchar(100) NOT NULL, + postback varchar(500) NOT NULL +); + +CREATE TABLE threadsubscriptions( + id SERIAL NOT NULL PRIMARY KEY, + apiclient_id integer NOT NULL REFERENCES apiclients(id), + threadid integer NOT NULL +); + +CREATE TABLE threadnotifications( + apiclient_id integer NOT NULL REFERENCES apiclients(id), + threadid integer NOT NULL, + CONSTRAINT threadnotifications_pkey PRIMARY KEY (apiclient_id, threadid) +); + CREATE TABLE loaderrors( id SERIAL NOT NULL PRIMARY KEY, listid int NOT NULL, @@ -126,6 +144,23 @@ CREATE TRIGGER messages_fti_trigger FOR EACH ROW EXECUTE PROCEDURE messages_fti_trigger_func(); CREATE INDEX messages_fti_idx ON messages USING gin(fti); +CREATE OR REPLACE FUNCTION messages_notify_threads_trg_func() RETURNS trigger AS $$ +BEGIN + INSERT INTO threadnotifications (apiclient_id, threadid) + SELECT apiclient_id, threadid + FROM threadsubscriptions + WHERE threadsubscriptions.threadid=NEW.threadid + ON CONFLICT DO NOTHING; + IF FOUND THEN + NOTIFY thread_updated; + END IF; + RETURN NEW; +END +$$ LANGUAGE 'plpgsql'; +CREATE TRIGGER messages_notify_trigger + AFTER INSERT ON messages + FOR EACH ROW EXECUTE PROCEDURE messages_notify_threads_trg_func(); + CREATE TABLE legacymap( listid int not null, year int not null, -- 2.30.2