(feat): Initial work of admin panel
This commit is contained in:
commit
7ae9ef7478
11 changed files with 209 additions and 0 deletions
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
/.venv/
|
||||||
|
/.github/
|
||||||
|
/__pycache__/
|
||||||
|
/.env
|
||||||
8
.idea/.gitignore
generated
vendored
Normal file
8
.idea/.gitignore
generated
vendored
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
||||||
21
.idea/admin-panel.iml
generated
Normal file
21
.idea/admin-panel.iml
generated
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="PYTHON_MODULE" version="4">
|
||||||
|
<component name="Flask">
|
||||||
|
<option name="enabled" value="true" />
|
||||||
|
</component>
|
||||||
|
<component name="NewModuleRootManager">
|
||||||
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<excludeFolder url="file://$MODULE_DIR$/.venv" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="jdk" jdkName="Python 3.11 (admin-panel)" jdkType="Python SDK" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
<component name="TemplatesService">
|
||||||
|
<option name="TEMPLATE_CONFIGURATION" value="Jinja2" />
|
||||||
|
<option name="TEMPLATE_FOLDERS">
|
||||||
|
<list>
|
||||||
|
<option value="$MODULE_DIR$/templates" />
|
||||||
|
</list>
|
||||||
|
</option>
|
||||||
|
</component>
|
||||||
|
</module>
|
||||||
6
.idea/inspectionProfiles/profiles_settings.xml
generated
Normal file
6
.idea/inspectionProfiles/profiles_settings.xml
generated
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
<component name="InspectionProjectProfileManager">
|
||||||
|
<settings>
|
||||||
|
<option name="USE_PROJECT_PROFILE" value="false" />
|
||||||
|
<version value="1.0" />
|
||||||
|
</settings>
|
||||||
|
</component>
|
||||||
6
.idea/misc.xml
generated
Normal file
6
.idea/misc.xml
generated
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Black">
|
||||||
|
<option name="sdkName" value="Python 3.11 (admin-panel)" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/admin-panel.iml" filepath="$PROJECT_DIR$/.idea/admin-panel.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
12
Dockerfile
Normal file
12
Dockerfile
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
FROM python:3.11-slim
|
||||||
|
|
||||||
|
WORKDIR /admin-panel
|
||||||
|
|
||||||
|
COPY requirements.txt
|
||||||
|
RUN pip install --no-cache-dir -r requirements.txt
|
||||||
|
|
||||||
|
COPY . .
|
||||||
|
|
||||||
|
EXPOSE 5001
|
||||||
|
|
||||||
|
CMD ["python", "app.py"]
|
||||||
72
app.py
Normal file
72
app.py
Normal file
|
|
@ -0,0 +1,72 @@
|
||||||
|
import os
|
||||||
|
import sqlite3
|
||||||
|
import smtplib
|
||||||
|
from email.mime.text import MIMEText
|
||||||
|
from flask import Flask, render_template, request, redirect, url_for, flash
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
|
load_dotenv()
|
||||||
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
app.secret_key = os.getenv('SECRET_KEY')
|
||||||
|
|
||||||
|
DATABASE_URL = os.getenv('DATABASE_URL')
|
||||||
|
SMTP_SERVER = os.getenv('SMTP_SERVER')
|
||||||
|
SMTP_PORT = int(os.getenv("SMTP_PORT", 465))
|
||||||
|
SMTP_USER = os.getenv('SMTP_USER')
|
||||||
|
SMTP_PASSWORD = os.getenv('SMTP_PASSWORD')
|
||||||
|
|
||||||
|
def get_all_emails():
|
||||||
|
"""Retrieve all subscriber emails from the database"""
|
||||||
|
try:
|
||||||
|
conn = sqlite3.connect(DATABASE_URL)
|
||||||
|
cursor = conn.cursor()
|
||||||
|
cursor.execute('SELECT email FROM subscribers')
|
||||||
|
results = cursor.fetchall()
|
||||||
|
conn.close()
|
||||||
|
return [row[0] for row in results]
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error: {e}")
|
||||||
|
return []
|
||||||
|
|
||||||
|
def send_update_email(subject, body):
|
||||||
|
"""Send an update email"""
|
||||||
|
subscribers = get_all_emails()
|
||||||
|
if not subscribers:
|
||||||
|
return "No subscribers found"
|
||||||
|
try:
|
||||||
|
server = smtplib.SMTP(SMTP_SERVER, SMTP_PORT, timeout=10)
|
||||||
|
server.set_debuglevel(True)
|
||||||
|
server.login(SMTP_USER, SMTP_PASSWORD)
|
||||||
|
for email in subscribers:
|
||||||
|
msg = MIMEText(body, 'html', 'utf-8')
|
||||||
|
msg['Subject'] = subject
|
||||||
|
msg['From'] = SMTP_USER
|
||||||
|
msg['To'] = email
|
||||||
|
server.sendmail(SMTP_USER, email, msg.as_string())
|
||||||
|
print(f"Updated email for {email} has been sent.")
|
||||||
|
server.quit()
|
||||||
|
return "Email has been sent."
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Failed to send email: {e}")
|
||||||
|
return f"Failed to send email: {e}"
|
||||||
|
|
||||||
|
@app.route('/')
|
||||||
|
def index():
|
||||||
|
"""Displays all subscriber emails"""
|
||||||
|
emails = get_all_emails()
|
||||||
|
return render_template("admin_index.html", emails=emails)
|
||||||
|
|
||||||
|
@app.route('/send_update_email', methods=['GET', 'POST'])
|
||||||
|
def send_update_email():
|
||||||
|
"""Display a form to send an update email"""
|
||||||
|
if request.method == 'POST':
|
||||||
|
subject = request.form['subject']
|
||||||
|
body = request.form['body']
|
||||||
|
result_message = send_update_email(subject, body)
|
||||||
|
flash(result_message)
|
||||||
|
return redirect(url_for("send_update_email"))
|
||||||
|
return render_template("send_update.html")
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
app.run(port=5001, debug=True)
|
||||||
2
requirements.txt
Normal file
2
requirements.txt
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
flask
|
||||||
|
python-dotenv
|
||||||
33
templates/admin_index.html
Normal file
33
templates/admin_index.html
Normal file
|
|
@ -0,0 +1,33 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Admin Center - Subscribers</title>
|
||||||
|
<style>
|
||||||
|
body { font-family: Arial, sans-serif; padding: 20px; }
|
||||||
|
table { border-collapse: collapse; width: 100%; }
|
||||||
|
th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
|
||||||
|
th { background-color: #f2f2f2; }
|
||||||
|
a { margin-right: 10px; }
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Subscribers</h1>
|
||||||
|
<p><a href="{{ url_for('send_update') }}">Send Update Email</a></p>
|
||||||
|
{% if emails %}
|
||||||
|
<table>
|
||||||
|
<tr>
|
||||||
|
<th>Email Address</th>
|
||||||
|
</tr>
|
||||||
|
{% for email in emails %}
|
||||||
|
<tr>
|
||||||
|
<td>{{ email }}</td>
|
||||||
|
</tr>
|
||||||
|
{% endfor %}
|
||||||
|
</table>
|
||||||
|
{% else %}
|
||||||
|
<p>No subscribers found.</p>
|
||||||
|
{% endif %}
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
37
templates/send_update.html
Normal file
37
templates/send_update.html
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Admin Center - Send Update</title>
|
||||||
|
<style>
|
||||||
|
body { font-family: Arial, sans-serif; padding: 20px; }
|
||||||
|
form { max-width: 600px; }
|
||||||
|
label { display: block; margin-top: 15px; }
|
||||||
|
input[type="text"], textarea { width: 100%; padding: 8px; }
|
||||||
|
button { margin-top: 15px; padding: 10px 20px; }
|
||||||
|
.flash { background-color: #f8d7da; color: #721c24; padding: 10px; margin-bottom: 10px; }
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Send Update Email</h1>
|
||||||
|
{% with messages = get_flashed_messages() %}
|
||||||
|
{% if messages %}
|
||||||
|
{% for message in messages %}
|
||||||
|
<div class="flash">{{ message }}</div>
|
||||||
|
{% endfor %}
|
||||||
|
{% endif %}
|
||||||
|
{% endwith %}
|
||||||
|
|
||||||
|
<form action="{{ url_for('send_update') }}" method="POST">
|
||||||
|
<label for="subject">Subject:</label>
|
||||||
|
<input type="text" name="subject" required>
|
||||||
|
|
||||||
|
<label for="body">Body (HTML allowed):</label>
|
||||||
|
<textarea name="body" rows="10" required></textarea>
|
||||||
|
|
||||||
|
<button type="submit">Send Update</button>
|
||||||
|
</form>
|
||||||
|
<p><a href="{{ url_for('index') }}">Back to Subscribers List</a></p>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Loading…
Add table
Add a link
Reference in a new issue