From c803f3d1579a77a9437f7008ab9aabbff2026624 Mon Sep 17 00:00:00 2001 From: Bruno Amaral Date: Sun, 24 Jul 2022 01:56:01 +0100 Subject: [PATCH 1/5] create a db table for custom site settings --- django/sitesettings/__init__.py | 0 django/sitesettings/admin.py | 10 ++++++++ django/sitesettings/apps.py | 6 +++++ .../sitesettings/migrations/0001_initial.py | 24 +++++++++++++++++++ django/sitesettings/migrations/__init__.py | 0 django/sitesettings/models.py | 8 +++++++ django/sitesettings/tests.py | 3 +++ django/sitesettings/views.py | 3 +++ 8 files changed, 54 insertions(+) create mode 100644 django/sitesettings/__init__.py create mode 100644 django/sitesettings/admin.py create mode 100644 django/sitesettings/apps.py create mode 100644 django/sitesettings/migrations/0001_initial.py create mode 100644 django/sitesettings/migrations/__init__.py create mode 100644 django/sitesettings/models.py create mode 100644 django/sitesettings/tests.py create mode 100644 django/sitesettings/views.py diff --git a/django/sitesettings/__init__.py b/django/sitesettings/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/django/sitesettings/admin.py b/django/sitesettings/admin.py new file mode 100644 index 00000000..9a924bcd --- /dev/null +++ b/django/sitesettings/admin.py @@ -0,0 +1,10 @@ +from django.contrib import admin + +# Register your models here. +from .models import CustomSettings + +class CustomSettingsAdmin(admin.ModelAdmin): + list_display = ['site','title',] + + +admin.site.register(CustomSettings,CustomSettingsAdmin) diff --git a/django/sitesettings/apps.py b/django/sitesettings/apps.py new file mode 100644 index 00000000..c7d108d6 --- /dev/null +++ b/django/sitesettings/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class SitesettingsConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'sitesettings' diff --git a/django/sitesettings/migrations/0001_initial.py b/django/sitesettings/migrations/0001_initial.py new file mode 100644 index 00000000..c2b54ff0 --- /dev/null +++ b/django/sitesettings/migrations/0001_initial.py @@ -0,0 +1,24 @@ +# Generated by Django 4.0.4 on 2022-07-24 00:44 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ('sites', '0002_alter_domain_unique'), + ] + + operations = [ + migrations.CreateModel( + name='CustomSettings', + fields=[ + ('setting_id', models.AutoField(primary_key=True, serialize=False)), + ('title', models.TextField(unique=True)), + ('site', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='sites.site')), + ], + ), + ] diff --git a/django/sitesettings/migrations/__init__.py b/django/sitesettings/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/django/sitesettings/models.py b/django/sitesettings/models.py new file mode 100644 index 00000000..c7df4ec5 --- /dev/null +++ b/django/sitesettings/models.py @@ -0,0 +1,8 @@ +from django.db import models +from django.contrib.sites.models import Site + +# Create your models here. +class CustomSettings(models.Model): + setting_id = models.AutoField(primary_key=True) + site = models.ForeignKey(Site, on_delete=models.PROTECT) + title = models.TextField(blank=False, null=False, unique=True) diff --git a/django/sitesettings/tests.py b/django/sitesettings/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/django/sitesettings/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/django/sitesettings/views.py b/django/sitesettings/views.py new file mode 100644 index 00000000..91ea44a2 --- /dev/null +++ b/django/sitesettings/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. From 453aa42cb5837b3f7cb1a30fde1f02e059860773 Mon Sep 17 00:00:00 2001 From: Bruno Amaral Date: Sun, 24 Jul 2022 01:57:08 +0100 Subject: [PATCH 2/5] add django.contrib.sites and customSettings to installed_apps --- django/admin/settings.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/django/admin/settings.py b/django/admin/settings.py index cd918d11..7ad8a03b 100644 --- a/django/admin/settings.py +++ b/django/admin/settings.py @@ -12,6 +12,7 @@ import os from pathlib import Path +SITE_ID = 1 # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent @@ -41,6 +42,8 @@ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + 'django.contrib.sites', + 'sitesettings', 'django_cron', 'db_maintenance', 'indexers', @@ -55,6 +58,7 @@ 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'django.middleware.gzip.GZipMiddleware', + 'django.contrib.sites.middleware.CurrentSiteMiddleware', ] ROOT_URLCONF = 'admin.urls' From 0f667bc9c1247a9524142c20343ac7c1fe3103cf Mon Sep 17 00:00:00 2001 From: Bruno Amaral Date: Sun, 24 Jul 2022 02:06:16 +0100 Subject: [PATCH 3/5] fix plural --- django/sitesettings/admin.py | 6 +++--- django/sitesettings/models.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/django/sitesettings/admin.py b/django/sitesettings/admin.py index 9a924bcd..c1747abe 100644 --- a/django/sitesettings/admin.py +++ b/django/sitesettings/admin.py @@ -1,10 +1,10 @@ from django.contrib import admin # Register your models here. -from .models import CustomSettings +from .models import CustomSetting -class CustomSettingsAdmin(admin.ModelAdmin): +class CustomSettingAdmin(admin.ModelAdmin): list_display = ['site','title',] -admin.site.register(CustomSettings,CustomSettingsAdmin) +admin.site.register(CustomSetting,CustomSettingAdmin) diff --git a/django/sitesettings/models.py b/django/sitesettings/models.py index c7df4ec5..685640af 100644 --- a/django/sitesettings/models.py +++ b/django/sitesettings/models.py @@ -2,7 +2,7 @@ from django.contrib.sites.models import Site # Create your models here. -class CustomSettings(models.Model): +class CustomSetting(models.Model): setting_id = models.AutoField(primary_key=True) site = models.ForeignKey(Site, on_delete=models.PROTECT) title = models.TextField(blank=False, null=False, unique=True) From e7ac1365f635afea34ff1246074315f9c537c111 Mon Sep 17 00:00:00 2001 From: Bruno Amaral Date: Sun, 24 Jul 2022 11:54:04 +0100 Subject: [PATCH 4/5] moves content from template system to the db --- django/sitesettings/models.py | 3 +- django/subscriptions/mercury.py | 31 ++++++++++++++----- django/templates/emails/admin_summary.html | 19 +++--------- .../templates/emails/trial_notification.html | 12 ++----- django/templates/emails/weekly_summary.html | 12 ++----- 5 files changed, 34 insertions(+), 43 deletions(-) diff --git a/django/sitesettings/models.py b/django/sitesettings/models.py index 685640af..98fc3ab7 100644 --- a/django/sitesettings/models.py +++ b/django/sitesettings/models.py @@ -5,4 +5,5 @@ class CustomSetting(models.Model): setting_id = models.AutoField(primary_key=True) site = models.ForeignKey(Site, on_delete=models.PROTECT) - title = models.TextField(blank=False, null=False, unique=True) + title = models.CharField(max_length=280,blank=False, null=False, unique=True) + email_footer = models.TextField(blank=True,null=True) diff --git a/django/subscriptions/mercury.py b/django/subscriptions/mercury.py index 8b234ba7..d93e316d 100644 --- a/django/subscriptions/mercury.py +++ b/django/subscriptions/mercury.py @@ -1,12 +1,18 @@ -from django.conf import settings -from subscriptions.models import Subscribers,Lists from django_cron import CronJobBase, Schedule -from django.template.loader import get_template -from gregory.models import Articles,Trials +from django.conf import settings from django.db.models import Q +from django.template.loader import get_template from django.utils.html import strip_tags -import requests +from gregory.models import Articles,Trials +from sitesettings.models import * +from subscriptions.models import Subscribers,Lists import datetime +import requests +from django.contrib.sites.models import Site + +## Get custom settings from DB +customsettings = CustomSetting.objects.get(site=1) +site = Site.objects.get(pk=1) list_clinical_trials = [] for email in Subscribers.objects.filter(subscriptions__list_name='Clinical Trials').values(): @@ -47,7 +53,10 @@ def do(self): summary = { "articles": articles, "trials":trials, - "admin": admin + "admin": admin, + "title": customsettings.title, + "email_footer": customsettings.email_footer, + "site": site, } admin=str(summary['admin'].email) html = get_template('emails/admin_summary.html').render(summary) @@ -77,7 +86,10 @@ def do(self): trials = Trials.objects.filter(~Q(sent_to_subscribers=True)) summary = { "articles": articles, - "trials":trials + "trials":trials, + "title": customsettings.title, + "email_footer": customsettings.email_footer, + "site": site, } html = get_template('emails/weekly_summary.html').render(summary) text= strip_tags(html) @@ -104,7 +116,10 @@ def do(self): subscribers.append(email['email']) summary = { - "trials":trials + "trials":trials, + "title": customsettings.title, + "email_footer": customsettings.email_footer, + "site": site, } html = get_template('emails/trial_notification.html').render(summary) text= strip_tags(html) diff --git a/django/templates/emails/admin_summary.html b/django/templates/emails/admin_summary.html index 11aba377..64a1223f 100644 --- a/django/templates/emails/admin_summary.html +++ b/django/templates/emails/admin_summary.html @@ -3,7 +3,7 @@ - Gregory MS update + {{ title }} update