From bcaa342c3ce29202e11252d2d1f9c901dfd8b277 Mon Sep 17 00:00:00 2001 From: corbreedy Date: Thu, 21 Oct 2021 21:55:22 +0200 Subject: [PATCH] email +env --- .gitignore | 1 + hpst/admin.py | 10 +++++++++- hpst/forms.py | 19 +++++++++++++++---- hpst/models.py | 9 ++++++++- hpst/templates/email.html | 28 ++++++++++++++++++++++++++++ hpst/templates/index.html | 33 +++++++++++++++++++++++++++------ hpst/views.py | 37 +++++++++++++++++++++++++++---------- requirements.txt | 2 ++ sinnestau/settings.py | 19 +++++++++++++++---- 9 files changed, 132 insertions(+), 26 deletions(-) create mode 100644 hpst/templates/email.html diff --git a/.gitignore b/.gitignore index fb26ad1..e1e3fa1 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,7 @@ sinnestau/migrations/ hpst/migrations/ venv3/ +.env #Emacs files *~ diff --git a/hpst/admin.py b/hpst/admin.py index 0a94471..0d877aa 100644 --- a/hpst/admin.py +++ b/hpst/admin.py @@ -1,7 +1,8 @@ from django.contrib import admin # Register your models here. -from .models import Termin +from hpst.models import Termin +from hpst.models import Kontakt class TerminAdmin(admin.ModelAdmin): list_display = ['titel', 'date'] @@ -9,3 +10,10 @@ class TerminAdmin(admin.ModelAdmin): actions = [] admin.site.register(Termin,TerminAdmin) + +class KontaktAdmin(admin.ModelAdmin): + list_display = ['subject','email', 'datum'] + ordering = ['datum'] + actions = [] + +admin.site.register(Kontakt,KontaktAdmin) diff --git a/hpst/forms.py b/hpst/forms.py index 063a3f5..658fab4 100644 --- a/hpst/forms.py +++ b/hpst/forms.py @@ -1,7 +1,18 @@ # sendemail/forms.py from django import forms -class ContactForm(forms.Form): - from_email = forms.EmailField(required=True) - subject = forms.CharField(required=True) - message = forms.CharField(widget=forms.Textarea, required=True) +class KontaktForm(forms.Form): + subject = forms.CharField(widget=forms.HiddenInput()) + from_email = forms.EmailField(required=True, + widget= forms.EmailInput + (attrs={'class':'w3-input w3-border', + 'placeholder':'Email'})) + name = forms.CharField(max_length=200, + required=True, + widget= forms.TextInput + (attrs={'class':'w3-input w3-border', + 'placeholder':'Name'})) + message = forms.CharField(required=True, + widget=forms.Textarea + (attrs={'class':'w3-input w3-border', + 'placeholder':'Mitteilung'})) diff --git a/hpst/models.py b/hpst/models.py index 609ece6..6fc0cf5 100644 --- a/hpst/models.py +++ b/hpst/models.py @@ -1,6 +1,6 @@ from django.db import models from django.contrib.auth.models import User - +from django.utils import timezone #Sinnestau.de models class Termin(models.Model): @@ -19,3 +19,10 @@ class Termin(models.Model): text = models.TextField() date = models.DateField() termintyp = models.CharField(max_length=1, choices=TERMIN_SET, default='S') + +class Kontakt(models.Model): + name = models.CharField(max_length=200,blank=True, null=True) + email = models.CharField(max_length=250) + subject= models.CharField(max_length=255) + text = models.TextField() + datum = models.DateTimeField(default=timezone.now) diff --git a/hpst/templates/email.html b/hpst/templates/email.html new file mode 100644 index 0000000..d87c9ea --- /dev/null +++ b/hpst/templates/email.html @@ -0,0 +1,28 @@ +{% extends "st_base.html" %} +{% load static %} +{% load st_extratags %} + +{% block javascript %} +{% endblock %} + +{% block style %} +{% endblock %} + +{% block content %} + + + +
+

Kontaktaufnahme

+

{{msg.titel}}

+

{{msg.text}}

+
+

Message!

+

{{ message }}

+
+ + +
+ + +{% endblock %} diff --git a/hpst/templates/index.html b/hpst/templates/index.html index b617667..594f1be 100644 --- a/hpst/templates/index.html +++ b/hpst/templates/index.html @@ -235,18 +235,39 @@ Kronach / Fischbach
Email: info@sinnes-tau.de

Komme auf eine Tasse vorbei, oder hinterlasse mir eine Nachricht:

-
- {% csrf_token %} - {{ form.as_p }} + {% if form.errors %} + {% for field in form %} + {% for error in field.errors %} +
+

Eingabefehler!

+

{{ error|escape }}

+
+ + {% endfor %} + {% endfor %} + {% for error in form.non_field_errors %} +
+

Eingabefehler!

+

{{ error|escape }}

+
+ {% endfor %} + {% endif %} + + + + {% csrf_token %} + {% for hidden_field in form.hidden_fields %} + {{ hidden_field }} + {% endfor %}
- + {{ form.name }}
- + {{ form.from_email }}
- + {{ form.message }} diff --git a/hpst/views.py b/hpst/views.py index 689b3d7..39777bd 100644 --- a/hpst/views.py +++ b/hpst/views.py @@ -8,17 +8,19 @@ import datetime import pytz from hpst.models import Termin -from hpst.forms import ContactForm +from hpst.models import Kontakt +from hpst.forms import KontaktForm +#Email +from django.core.mail import BadHeaderError,send_mail -# Create your views here. + +# Hauptseite def index(request): termine=Termin.objects.filter(date__gt=timezone.now()).order_by( 'date') context ={ "termine":termine, - "data":"Gfg is the best", - "list":[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] + "form":KontaktForm(initial={'subject': 'Kontaktformular - Index - sinnestau.de',}) } - # return response with template and context return render(request, "index.html", context) def wildnispaedagogik(request): @@ -107,19 +109,34 @@ def impressum(request): def contactView(request): if request.method == 'GET': - form = ContactForm() + form = KontaktForm() else: - form = ContactForm(request.POST) + print("havepost") + r_message="" + form = KontaktForm(request.POST) if form.is_valid(): + print("form valid") subject = form.cleaned_data['subject'] from_email = form.cleaned_data['from_email'] message = form.cleaned_data['message'] + name = form.cleaned_data['name'] + text=from_email+'\n.........................\n'+message + newkon=Kontakt.objects.create( + name=name, + subject=subject, + email=from_email, + text=text + ) + r_message="Vielen Dank! Wir haben Ihre Email erhalten!" try: - send_mail(subject, message, from_email, ['tanja@kuntner.de']) + send_mail(subject, text, 'formular@sinnes-tau.de', ['tanja@kuntner.de']) except BadHeaderError: return HttpResponse('Invalid header found.') - return redirect('success') - return render(request, "email.html", {'form': form}) + #return redirect('success') + else: + print(form.errors) + r_message="Form invalid" + return render(request, "email.html", {'form': form,'message':r_message}) def successView(request): return HttpResponse('Success! Thank you for your message.') diff --git a/requirements.txt b/requirements.txt index 161f658..f75729c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,5 @@ django~=3.2.7 wheel mysqlclient uwsgi +python-decouple +dj_database_url diff --git a/sinnestau/settings.py b/sinnestau/settings.py index 4dfbc13..675a1c2 100644 --- a/sinnestau/settings.py +++ b/sinnestau/settings.py @@ -11,7 +11,9 @@ https://docs.djangoproject.com/en/3.2/ref/settings/ """ from pathlib import Path - +#Pw etc +from decouple import config +from dj_database_url import parse as db_url # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent @@ -21,10 +23,10 @@ BASE_DIR = Path(__file__).resolve().parent.parent # See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = 'django-insecure-4)p&qhcr@7(az19%7&aqoy49amjf#h+eupul=u28=kd)w@=rp@' +SECRET_KEY = config('SECRET_KEY') # SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True +DEBUG = config('DEBUG') ALLOWED_HOSTS = ['sinnes-tau.de','sinnes-tau.kuntner.de','localhost', '127.0.0.1','192.168.178.25','kuntner.de' ] @@ -78,8 +80,11 @@ WSGI_APPLICATION = 'sinnestau.wsgi.application' DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', + 'NAME': config('DATABASE_NAME'), + 'USER': config('DATABASE_USERNAME'), + 'PASSWORD': config('DATABASE_PASSWORT'), + 'HOST': config('DATABASE_HOST'), 'OPTIONS': { - 'read_default_file': '/etc/www/myst.cnf', 'init_command': "SET sql_mode='STRICT_TRANS_TABLES'", 'charset': 'utf8mb4' }, @@ -135,3 +140,9 @@ STATIC_ROOT = str(BASE_DIR.joinpath('build/static')) DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' +EMAIL_HOST = config('EMAIL_HOST') +EMAIL_PORT = 587 +EMAIL_HOST_USER = config('EMAIL_HOST_USER') +EMAIL_HOST_PASSWORD = config('EMAIL_HOST_PASSWORD'); +EMAIL_USE_TLS = True +