From e052a2a44bc08995374dd971fda4db97b1bd9acf Mon Sep 17 00:00:00 2001 From: Benjamin Bach Date: Sun, 29 Aug 2021 00:21:52 +0200 Subject: [PATCH 1/2] Creates an initial newsletter sign-up form --- djangodenmark/apps/newsletter/__init__.py | 0 .../apps/newsletter/migrations/__init__.py | 0 .../newsletter/templates/consent/base.html | 15 +++++++++ .../apps/newsletter/templates/signup.html | 27 ++++++++++++++++ .../apps/newsletter/templatetags/__init__.py | 0 .../apps/newsletter/templatetags/demotags.py | 9 ++++++ djangodenmark/apps/newsletter/urls.py | 21 +++++++++++++ djangodenmark/apps/newsletter/views.py | 31 +++++++++++++++++++ djangodenmark/settings/base.py | 1 + djangodenmark/urls.py | 4 +++ 10 files changed, 108 insertions(+) create mode 100644 djangodenmark/apps/newsletter/__init__.py create mode 100644 djangodenmark/apps/newsletter/migrations/__init__.py create mode 100644 djangodenmark/apps/newsletter/templates/consent/base.html create mode 100644 djangodenmark/apps/newsletter/templates/signup.html create mode 100644 djangodenmark/apps/newsletter/templatetags/__init__.py create mode 100644 djangodenmark/apps/newsletter/templatetags/demotags.py create mode 100644 djangodenmark/apps/newsletter/urls.py create mode 100644 djangodenmark/apps/newsletter/views.py diff --git a/djangodenmark/apps/newsletter/__init__.py b/djangodenmark/apps/newsletter/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/djangodenmark/apps/newsletter/migrations/__init__.py b/djangodenmark/apps/newsletter/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/djangodenmark/apps/newsletter/templates/consent/base.html b/djangodenmark/apps/newsletter/templates/consent/base.html new file mode 100644 index 0000000..a0f9a05 --- /dev/null +++ b/djangodenmark/apps/newsletter/templates/consent/base.html @@ -0,0 +1,15 @@ +{% extends "base.html" %} + + +{% comment %} +This part of the demo shows how you can overwrite consent/base.html in order to +have your own main template apply to django-consent's built-in templates. + +Simply place the consent_content block in the appropriate location. +{% endcomment %} + +{% block demo_content %} + {% block consent_content %} + + {% endblock %} +{% endblock %} diff --git a/djangodenmark/apps/newsletter/templates/signup.html b/djangodenmark/apps/newsletter/templates/signup.html new file mode 100644 index 0000000..ecf335d --- /dev/null +++ b/djangodenmark/apps/newsletter/templates/signup.html @@ -0,0 +1,27 @@ +{% extends "base.html" %} + +{% block content %} + +

Sign up for {{ consent_source.source_name }}

+ +

+The below form is an example of inheriting from ConsentCreateView which +uses the EmailConsentForm. In your own application, you can make +similar patterns and it's encouraged that you extend one of these two classes. +

+ +
+ + {% csrf_token %} + + + {{ form.as_table }} +
+ +

+ +

+ +
+ +{% endblock %} diff --git a/djangodenmark/apps/newsletter/templatetags/__init__.py b/djangodenmark/apps/newsletter/templatetags/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/djangodenmark/apps/newsletter/templatetags/demotags.py b/djangodenmark/apps/newsletter/templatetags/demotags.py new file mode 100644 index 0000000..f4e58a6 --- /dev/null +++ b/djangodenmark/apps/newsletter/templatetags/demotags.py @@ -0,0 +1,9 @@ +from django import template +from django_consent import models + +register = template.Library() + + +@register.simple_tag +def consent_sources(): + return models.ConsentSource.objects.all() diff --git a/djangodenmark/apps/newsletter/urls.py b/djangodenmark/apps/newsletter/urls.py new file mode 100644 index 0000000..e2b9d47 --- /dev/null +++ b/djangodenmark/apps/newsletter/urls.py @@ -0,0 +1,21 @@ +from django.urls import path + +from . import views + + +app_name = "newsletter" + + +urlpatterns = [ + path("", views.Index.as_view(), name="index"), + path( + "signup//", + views.ConsentCreateView.as_view(), + name="signup", + ), + path( + "signup//confirmation/", + views.ConsentConfirmationSentView.as_view(), + name="signup_confirmation", + ), +] diff --git a/djangodenmark/apps/newsletter/views.py b/djangodenmark/apps/newsletter/views.py new file mode 100644 index 0000000..e275256 --- /dev/null +++ b/djangodenmark/apps/newsletter/views.py @@ -0,0 +1,31 @@ +from django.urls.base import reverse +from django.views.generic.base import RedirectView +from django_consent import models +from django_consent.views import ConsentConfirmationSentView +from django_consent.views import ConsentCreateView + + +class Index(RedirectView): + + permanent = False + + def get_redirect_url(self, *args, **kwargs): + source = models.ConsentSource.objects.all().first() + if not source: + source = models.ConsentSource.objects.create( + source_name="Auto-created default newsletter" + ) + return reverse("newsletter:signup", kwargs={"source_id": source.id}) + + +class ConsentCreateView(ConsentCreateView): + template_name = "signup.html" + + def get_success_url(self): + return reverse( + "demo:signup_confirmation", kwargs={"source_id": self.consent_source.id} + ) + + +class ConsentConfirmationSentView(ConsentConfirmationSentView): + pass diff --git a/djangodenmark/settings/base.py b/djangodenmark/settings/base.py index dadb458..3f0fc3c 100644 --- a/djangodenmark/settings/base.py +++ b/djangodenmark/settings/base.py @@ -24,6 +24,7 @@ "djangodenmark.apps.companies", "widget_tweaks", "djangodenmark.apps.jobposts", + "djangodenmark.apps.newsletter", ] MIDDLEWARE = [ diff --git a/djangodenmark/urls.py b/djangodenmark/urls.py index 4d8edf7..5eed541 100644 --- a/djangodenmark/urls.py +++ b/djangodenmark/urls.py @@ -18,10 +18,12 @@ from django.contrib import admin from django.urls import include from django.urls import path +from django_consent import urls as consent_urls from .apps.companies import urls as company_urls from .apps.events import urls as event_urls from .apps.jobposts import urls as jobpost_urls +from .apps.newsletter import urls as newsletter_urls urlpatterns = [ path("admin/", admin.site.urls), @@ -30,6 +32,8 @@ path("accounts/", include("django.contrib.auth.urls")), path("companies/", include(company_urls)), path("jobs/", include(jobpost_urls)), + path("consent/", include(consent_urls)), + path("newsletter/", include(newsletter_urls)), ] if settings.DEBUG: From 77afd77c6d80004765c69f84df388f0e09859969 Mon Sep 17 00:00:00 2001 From: Benjamin Bach Date: Sun, 29 Aug 2021 00:32:28 +0200 Subject: [PATCH 2/2] Fixes for confirmation page --- djangodenmark/apps/newsletter/templates/consent/base.html | 2 +- djangodenmark/apps/newsletter/views.py | 3 ++- djangodenmark/settings/base.py | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/djangodenmark/apps/newsletter/templates/consent/base.html b/djangodenmark/apps/newsletter/templates/consent/base.html index a0f9a05..3ca2745 100644 --- a/djangodenmark/apps/newsletter/templates/consent/base.html +++ b/djangodenmark/apps/newsletter/templates/consent/base.html @@ -8,7 +8,7 @@ Simply place the consent_content block in the appropriate location. {% endcomment %} -{% block demo_content %} +{% block content %} {% block consent_content %} {% endblock %} diff --git a/djangodenmark/apps/newsletter/views.py b/djangodenmark/apps/newsletter/views.py index e275256..d542e1b 100644 --- a/djangodenmark/apps/newsletter/views.py +++ b/djangodenmark/apps/newsletter/views.py @@ -23,7 +23,8 @@ class ConsentCreateView(ConsentCreateView): def get_success_url(self): return reverse( - "demo:signup_confirmation", kwargs={"source_id": self.consent_source.id} + "newsletter:signup_confirmation", + kwargs={"source_id": self.consent_source.id}, ) diff --git a/djangodenmark/settings/base.py b/djangodenmark/settings/base.py index 3f0fc3c..bf73723 100644 --- a/djangodenmark/settings/base.py +++ b/djangodenmark/settings/base.py @@ -18,13 +18,13 @@ "django.contrib.messages", "django.contrib.staticfiles", "djangodenmark.apps.memberships", + "djangodenmark.apps.newsletter", "django_consent", "djangodenmark.apps.events", "djangodenmark.apps.accounts", "djangodenmark.apps.companies", "widget_tweaks", "djangodenmark.apps.jobposts", - "djangodenmark.apps.newsletter", ] MIDDLEWARE = [