From d2a2b3031a89676de8540cfb7725b957e6fb8aaf Mon Sep 17 00:00:00 2001
From: Seth Osman <so00647@surrey.ac.uk>
Date: Sun, 4 Dec 2022 15:07:14 +0000
Subject: [PATCH] Basic contact mailer no smtp server

---
 gymapp/settings.py                  | 13 ++++++++++++
 gymapp/urls.py                      |  4 ++--
 homeapp/templates/homeapp/home.html | 20 +++++++++++++++---
 homeapp/tests.py                    | 17 ++++++++++++++-
 homeapp/urls.py                     |  1 +
 homeapp/views.py                    | 32 +++++++++++++++++++++++++++--
 templates/base.html                 |  9 ++++++++
 7 files changed, 88 insertions(+), 8 deletions(-)

diff --git a/gymapp/settings.py b/gymapp/settings.py
index c56a586..13363a6 100644
--- a/gymapp/settings.py
+++ b/gymapp/settings.py
@@ -32,6 +32,7 @@ ALLOWED_HOSTS = []
 
 INSTALLED_APPS = [
     'homeapp.apps.HomeappConfig',
+    'sass_processor',
     'django.contrib.admin',
     'django.contrib.auth',
     'django.contrib.contenttypes',
@@ -118,7 +119,19 @@ USE_TZ = True
 
 STATIC_URL = 'static/'
 
+STATIC_ROOT = BASE_DIR / 'static'
+SASS_PROCESSOR_ROOT = STATIC_ROOT
+
+STATICFILES_FINDERS = [
+    'django.contrib.staticfiles.finders.FileSystemFinder',
+    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
+    'sass_processor.finders.CssFinder',
+]
+
 # Default primary key field type
 # https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field
 
 DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
+
+DEFAULT_FROM_EMAIL = "so00647@surrey.ac.uk"
+EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
\ No newline at end of file
diff --git a/gymapp/urls.py b/gymapp/urls.py
index 9085c28..82694f2 100644
--- a/gymapp/urls.py
+++ b/gymapp/urls.py
@@ -14,9 +14,9 @@ Including another URLconf
     2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
 """
 from django.contrib import admin
-from django.urls import include, path
+from django.urls import path, include
 
 urlpatterns = [
-    path('', include('homeapp.urls')),
     path('admin/', admin.site.urls),
+    path('', include('homeapp.urls')),
 ]
diff --git a/homeapp/templates/homeapp/home.html b/homeapp/templates/homeapp/home.html
index aea532f..e018923 100644
--- a/homeapp/templates/homeapp/home.html
+++ b/homeapp/templates/homeapp/home.html
@@ -5,7 +5,21 @@
 {% endblock title%}
 
 {% block content %}
+
 <h1>Welcome!</h1>
-<p>Rendering template
-/homeapp/home.html</p>
-{% endblock content %} 
\ No newline at end of file
+{% endblock content %} 
+
+<!--
+    <script type="text/javascript">
+    console.log('In the js')
+
+    window.onload = function(){
+        if(window.jQuery){
+            alert('jQuery is loaded');
+        }
+        else{
+            alert('jQuery is not loaded');
+        }
+    }
+    </script>
+-->
\ No newline at end of file
diff --git a/homeapp/tests.py b/homeapp/tests.py
index 7ce503c..88d33a4 100644
--- a/homeapp/tests.py
+++ b/homeapp/tests.py
@@ -1,3 +1,18 @@
 from django.test import TestCase
+from django.urls import reverse
 
-# Create your tests here.
+class HomePageTests(TestCase):
+    
+    def setUp(self):
+        return
+
+    def test_homepage(self):
+        response = self.client.get('')
+        self.assertEqual(response.status_code, 200)
+        self.assertContains(response, 'Welcome!')
+        
+    def test_contact(self):
+        response = self.client.get(reverse('contact'))
+        self.assertEqual(response.status_code, 200)
+        self.assertContains(response, 'Contact Us')
+        
\ No newline at end of file
diff --git a/homeapp/urls.py b/homeapp/urls.py
index 9ec8361..b9409b3 100644
--- a/homeapp/urls.py
+++ b/homeapp/urls.py
@@ -3,4 +3,5 @@ from . import views
 
 urlpatterns = [
     path('', views.home, name='home'),
+    path('contact', views.contact, name='contact')
 ]
diff --git a/homeapp/views.py b/homeapp/views.py
index 0d89966..755b80b 100644
--- a/homeapp/views.py
+++ b/homeapp/views.py
@@ -1,5 +1,33 @@
-from django.shortcuts import render
+from django.core.mail import send_mail, BadHeaderError
+from django.http import HttpResponse, HttpResponseRedirect
+from django.shortcuts import render, redirect
+from django.contrib import messages
+from django.urls import reverse
+from .forms import ContactForm
 
 def home(request):
     context = {}
-    return render(request, 'homeapp/home.html', context)
\ No newline at end of file
+    return render(request, 'homeapp/home.html', context)
+
+def contact(request):
+    if request.method == "GET":
+        form = ContactForm()
+    else:
+        form = ContactForm(request.POST)
+        if form.is_valid():
+            name = form.cleaned_data['name']
+            subject = form.cleaned_data['subject']
+            email = form.cleaned_data['email']
+            message = name + ':\n' + form.cleaned_data['message']
+            try:
+                send_mail(subject, message, email, ['My'])
+            except BadHeaderError():
+                messages.add_message(request, messages.ERROR, 'Message Not Sent')
+                return HttpResponse("Invalid header found.")
+            
+            messages.add_message(request, messages.SUCCESS, 'Message Sent')
+            return redirect(reverse('home'))
+        else:
+            messages.add_message(request, messages.ERROR, 'Invalid Form Data; Message Not Sent')
+        
+    return render(request, 'homeapp/contact.html', {"form": form})
diff --git a/templates/base.html b/templates/base.html
index bb3e6c4..7a26515 100644
--- a/templates/base.html
+++ b/templates/base.html
@@ -1,8 +1,15 @@
 {% load static %}
+{% load sass_tags %}
 
 <!DOCTYPE html>
 <html lang="en">
 <head>
+    <link rel="stylesheet" type="text/css" href={% sass_src 'css/base.scss' %}>
+    <script type="text/javascript" src="{% static 'js/jquery-3.6.1.js' %}"></script>
+
+    {% block app_includes %}
+    {% endblock app_includes %}
+
     {% block title %}
     {% endblock title %}
 </head>
@@ -11,6 +18,8 @@
     {% block header %}
     {% endblock header %}
 
+    {% include 'messages.html' %}
+
     {% block content%}
     {% endblock content %}
 
-- 
GitLab