Fix template loaders for django 1.11
authorMagnus Hagander <magnus@hagander.net>
Sun, 25 Mar 2018 14:52:47 +0000 (16:52 +0200)
committerMagnus Hagander <magnus@hagander.net>
Sun, 25 Mar 2018 14:52:47 +0000 (16:52 +0200)
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
django/archives/mailarchives/models.py
django/archives/settings.py
loader/sql/schema.sql

index f213caa09487c88fc5a54cb02484f9e2c952e15a..ca0b736a28e604cf47d808a8f171388654a95bd0 100644 (file)
@@ -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)
index 8fe9275c709aaf3ab29be07cdae88929802638d3..6270974dc7f5cf57d4e7f5c9a221bb7f9d9f23f9 100644 (file)
@@ -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'),)
index 8fb47b8fc3c88c8f844d15157e16cf8111206dec..1881971fc4c8a996bbe5b93ad3cf9e5e57f8c585 100644 (file)
@@ -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',
+               ],
        },
 }]
 
index 1723806bdacad8f5feba07e2dacce4d2ffe8207c..c19dac4f3077bf209340dab06a0c1de37f742f30 100644 (file)
@@ -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,