commit 3f053fe421591950b0beae868caeefafe6954532
parent 8204e054c0654b889eeb4488187f12b645260917
Author: William Lindholm <william_lindholm@outlook.com>
Date: Tue, 31 Oct 2023 23:26:52 +0100
Refactored code.
Diffstat:
7 files changed, 113 insertions(+), 75 deletions(-)
diff --git a/WebAPI/__init__.py b/WebAPI/__init__.py
@@ -1,61 +1,7 @@
-from flask import Flask
-from flask_restx import Resource, Api, fields
+from flask import Blueprint
+from flask_restx import Api
-from WebAPI.mailer import Mailer
-from WebAPI.dbrepository import DatabaseRepository
+blueprint = Blueprint('api', __name__)
+api = Api(blueprint)
-app = Flask(__name__)
-api = Api(app)
-
-mailer = Mailer()
-db_repo = DatabaseRepository()
-
-contact_fields = api.model('Contact', {
- 'email': fields.String(required=True),
- 'name': fields.String(required=True),
- 'message_content': fields.String(required=True)
-})
-
-
-@api.route('/contact')
-class contact(Resource):
- @api.expect(contact_fields)
- def post(self):
- data = api.payload
- email = data['email']
- name = data['name']
- message_content = data['message_content']
-
- formatted_subject = mailer.format_subject(email)
- formatted_message = mailer.format_message(name, email, message_content)
-
- try:
- mailer.send_notification(formatted_subject, formatted_message)
- except Exception as e:
- return {"message": f"An unknown error occurred: {str(e)}"}, 500
-
- db_repo.save_message(name, email, formatted_subject, message_content) # Save message to database
-
- return {"message": "Message sent and saved successfully"}, 200
-
-
-@api.route('/messages')
-class Messages(Resource):
- def get(self):
- messages = db_repo.fetch_messages()
- formatted_messages = [
- {
- "id": message[0],
- "name": message[1],
- "email": message[2],
- "subject": message[3],
- "message_content": message[4],
- "timestamp": message[5]
- }
- for message in messages
- ]
- return formatted_messages, 200
-
-
-if __name__ == '__main__':
- app.run(debug=True)
-\ No newline at end of file
+from . import routes
diff --git a/WebAPI/mailer.py b/WebAPI/mailer.py
@@ -1,9 +1,14 @@
import smtplib
import json
+from pathlib import Path
class Mailer:
def __init__(self):
- with open('settings.json', 'r') as f:
+ current_dir = Path(__file__).parent
+
+ settings_path = current_dir / '..' / 'settings.json'
+
+ with open(settings_path, 'r') as f:
settings = json.load(f)
self.notification_recipient = settings['recipient_email']
@@ -18,10 +23,7 @@ class Mailer:
server.sendmail(self.email, recipient, f"Subject: {subject}\n\n{message}")
def send_notification(self, subject, message):
- with open('settings.json', 'r') as f:
- settings = json.load(f)
- recipient = settings['recipient_email']
- self.send(subject, message, recipient)
+ self.send(subject, message, self.notification_recipient)
def send_confirmation(self, recipient):
with open('confirmation.html', 'r', encoding='utf-8') as file:
diff --git a/WebAPI/routes.py b/WebAPI/routes.py
@@ -0,0 +1,54 @@
+from . import api
+from flask_restx import Resource, fields
+
+from .dbrepository import DatabaseRepository
+from .mailer import Mailer
+
+mailer = Mailer()
+db_repo = DatabaseRepository()
+
+contact_fields = api.model('Contact', {
+ 'email': fields.String(required=True),
+ 'name': fields.String(required=True),
+ 'message_content': fields.String(required=True)
+})
+
+
+@api.route('/contact')
+class contact(Resource):
+ @api.expect(contact_fields)
+ def post(self):
+ data = api.payload
+ email = data['email']
+ name = data['name']
+ message_content = data['message_content']
+
+ formatted_subject = mailer.format_subject(email)
+ formatted_message = mailer.format_message(name, email, message_content)
+
+ try:
+ mailer.send_notification(formatted_subject, formatted_message)
+ except Exception as e:
+ return {"message": f"An unknown error occurred: {str(e)}"}, 500
+
+ db_repo.save_message(name, email, formatted_subject, message_content) # Save message to database
+
+ return {"message": "Message sent and saved successfully"}, 200
+
+
+@api.route('/messages')
+class Messages(Resource):
+ def get(self):
+ messages = db_repo.fetch_messages()
+ formatted_messages = [
+ {
+ "id": message[0],
+ "name": message[1],
+ "email": message[2],
+ "subject": message[3],
+ "message_content": message[4],
+ "timestamp": message[5]
+ }
+ for message in messages
+ ]
+ return formatted_messages, 200
+\ No newline at end of file
diff --git a/WebInterface/__init__.py b/WebInterface/__init__.py
@@ -0,0 +1,3 @@
+from flask import Blueprint
+
+web_interface = Blueprint('web_interface', __name__)
+\ No newline at end of file
diff --git a/WebInterface/routes.py b/WebInterface/routes.py
@@ -0,0 +1,6 @@
+from flask import render_template
+from . import web_interface
+
+@web_interface.route('/home')
+def home():
+ return render_template('home.html')
+\ No newline at end of file
diff --git a/WebInterface/template.html b/WebInterface/template.html
@@ -0,0 +1,28 @@
+<!doctype html>
+<html lang="en">
+<head>
+ <meta charset="utf-8">
+ <meta name="viewport" content="width=device-width, initial-scale=1">
+
+ <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet">
+
+ <title>Messages</title>
+</head>
+<body>
+
+<div class="container">
+ <h1 class="mt-5">Messages</h1>
+ <ul class="list-group mt-3">
+ {% for message in messages %}
+ <li class="list-group-item">
+ <strong>{{ message.subject }}</strong> from {{ message.name }} ({{ message.email }}) at {{ message.timestamp }}:
+ <p>{{ message.message_content }}</p>
+ </li>
+ {% endfor %}
+ </ul>
+</div>
+
+<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"></script>
+
+</body>
+</html>
+\ No newline at end of file
diff --git a/main.py b/main.py
@@ -1,15 +1,10 @@
from flask import Flask
-from flask_restx import Resource, Api
+from WebAPI import blueprint as api_blueprint
+from WebInterface import web_interface
app = Flask(__name__)
-api = Api(app)
-
-
-@api.route('/hello')
-class HelloWorld(Resource):
- def get(self):
- return {'hello': 'world'}
-
+app.register_blueprint(api_blueprint, url_prefix='/api')
+app.register_blueprint(web_interface)
if __name__ == '__main__':
- app.run(debug=True)
+ app.run(debug=True)
+\ No newline at end of file