From e275db66539e7cd142d6c1b8afcd385fec2c4e46 Mon Sep 17 00:00:00 2001 From: j Date: Mon, 25 Apr 2022 17:24:42 +0100 Subject: [PATCH] contact form --- app/contact/__init__.py | 0 app/contact/admin.py | 3 ++ app/contact/apps.py | 6 ++++ app/contact/migrations/__init__.py | 0 app/contact/models.py | 3 ++ app/contact/tests.py | 3 ++ app/contact/views.py | 45 ++++++++++++++++++++++++++++ app/settings.py | 1 + app/static/css/partials/_layout.scss | 25 ++++++++++++++++ app/templates/timeline.html | 43 ++++++++++++++++++++++---- app/urls.py | 2 ++ 11 files changed, 126 insertions(+), 5 deletions(-) create mode 100644 app/contact/__init__.py create mode 100644 app/contact/admin.py create mode 100644 app/contact/apps.py create mode 100644 app/contact/migrations/__init__.py create mode 100644 app/contact/models.py create mode 100644 app/contact/tests.py create mode 100644 app/contact/views.py diff --git a/app/contact/__init__.py b/app/contact/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/contact/admin.py b/app/contact/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/app/contact/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/app/contact/apps.py b/app/contact/apps.py new file mode 100644 index 0000000..355f785 --- /dev/null +++ b/app/contact/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class ContactConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'app.contact' diff --git a/app/contact/migrations/__init__.py b/app/contact/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/contact/models.py b/app/contact/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/app/contact/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/app/contact/tests.py b/app/contact/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/app/contact/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/app/contact/views.py b/app/contact/views.py new file mode 100644 index 0000000..a97dd55 --- /dev/null +++ b/app/contact/views.py @@ -0,0 +1,45 @@ +import json + +from django.conf import settings +from django.core.mail import EmailMessage +from django.http import HttpResponse +from django.shortcuts import render, redirect, get_object_or_404 + + +def render_to_json_response(dictionary, content_type="application/json; charset=utf-8", status=200): + try: + response = json.dumps(dictionary, ensure_ascii=False) + '\n' + except TypeError: + logger.error('failed to serialize to JSON: %s', dictionary) + raise + if not isinstance(response, bytes): + response = response.encode('utf-8') + return HttpResponse(response, content_type=content_type, status=status) + + +def index(request): + + if request.method == 'POST': + try: + data = json.loads(request.body.decode()) + except: + response = {} + return render_to_json_response(response) + + for key in ('email', 'name', 'message'): + if not data.get(key): + print(key, 'msising', data) + return render_to_json_response({}) + + name = data['name'] + email = data['email'] + message = data['message'] + subject = '{} has left a message on brixton-timeline'.format(name) + from_ = settings.CONTACT_FROM_EMAIL + to = settings.CONTACT_TO_EMAIL + if not isinstance(to, list): + to = [to] + msg = EmailMessage(subject, message, from_, to, reply_to=[email]) + msg.send(fail_silently=True) + return render_to_json_response({"sent": True}) + return redirect('/') diff --git a/app/settings.py b/app/settings.py index 175e39c..d8875b3 100755 --- a/app/settings.py +++ b/app/settings.py @@ -152,6 +152,7 @@ GEOIP_PATH = BASE_DIR / 'geo' URL_PREFIX = '' + try: from .local_settings import * except ImportError: diff --git a/app/static/css/partials/_layout.scss b/app/static/css/partials/_layout.scss index 7b1888e..5424c1c 100644 --- a/app/static/css/partials/_layout.scss +++ b/app/static/css/partials/_layout.scss @@ -40,6 +40,31 @@ p { } } } + +#contact { + .content { + padding-top: 5%; + width: 640px; + height: 480px; + margin: auto; + } + input, textarea { + width: 600px; + border-radius: 4px; + border: 1px solid black; + } + input[type="submit"] { + background: none; + } + input[type="submit"]:hover { + background: red; + cursor: pointer; + } + textarea { + height: 320px + } + +} @media screen and (max-width: 799px) { #intro { margin: 16px; diff --git a/app/templates/timeline.html b/app/templates/timeline.html index f3a53e8..395cb65 100644 --- a/app/templates/timeline.html +++ b/app/templates/timeline.html @@ -42,15 +42,48 @@
-
-
-
- +
+ +
+
+
+ + {% csrf_token %} - {{ contact.body | safe }} +