2024-06-12 10:49:45 +02:00
|
|
|
|
|
|
|
import pymysql
|
|
|
|
from jinja2 import Environment, FileSystemLoader
|
|
|
|
from flask import Flask, render_template
|
|
|
|
import os
|
|
|
|
|
|
|
|
# Configuration
|
|
|
|
HOST = "localhost"
|
|
|
|
USER = "demoUser"
|
|
|
|
PASSWORD = "demoPassword"
|
|
|
|
DATABASE = "cijliness"
|
|
|
|
CHARSET = 'utf8mb4'
|
|
|
|
|
|
|
|
# Initialize Flask app and Jinja2 template engine
|
|
|
|
|
|
|
|
|
|
|
|
app = Flask(__name__, template_folder=os.path.abspath('./templates'))
|
|
|
|
|
|
|
|
app.config['TEMPLATES_AUTO_RELOAD'] = True
|
|
|
|
app.config['SECRET_KEY'] = 'your_secret_key'
|
|
|
|
|
|
|
|
loader = FileSystemLoader('templates')
|
|
|
|
env = Environment(loader=loader)
|
|
|
|
app.jinja_env = env
|
|
|
|
|
|
|
|
def get_formatted_date(value):
|
|
|
|
"""Custom Jinja2 date filter."""
|
|
|
|
return value.strftime("%Y-%m-%d %H:%M")
|
|
|
|
|
|
|
|
env.filters['date'] = get_formatted_date
|
|
|
|
|
|
|
|
def get_articles():
|
|
|
|
"""Fetch articles from the database."""
|
|
|
|
try:
|
|
|
|
connection = pymysql.connect(
|
|
|
|
host=HOST,
|
|
|
|
user=USER,
|
|
|
|
password=PASSWORD,
|
|
|
|
db=DATABASE,
|
|
|
|
charset=CHARSET
|
|
|
|
)
|
|
|
|
|
|
|
|
cursor = connection.cursor()
|
|
|
|
|
|
|
|
query = '''
|
|
|
|
SELECT id, title, introtext,created FROM cijliness.cijl_content
|
2024-06-12 15:26:47 +02:00
|
|
|
WHERE state = 1
|
|
|
|
ORDER BY id ASC;
|
2024-06-12 10:49:45 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
cursor.execute(query)
|
|
|
|
|
|
|
|
articles = []
|
|
|
|
|
|
|
|
for row in cursor:
|
|
|
|
articles.append({
|
|
|
|
'id': row[0],
|
|
|
|
'title': row[1],
|
|
|
|
'introtext': row[2],
|
|
|
|
'created': row[3],
|
|
|
|
})
|
|
|
|
|
|
|
|
cursor.close()
|
|
|
|
connection.commit()
|
|
|
|
connection.close()
|
|
|
|
|
|
|
|
return articles
|
|
|
|
|
|
|
|
except pymysql.MySQLError as e:
|
|
|
|
print(f"Error: {e}")
|
|
|
|
return None
|
|
|
|
@app.route("/")
|
|
|
|
def index():
|
|
|
|
"""Render base.html with articles."""
|
|
|
|
articles = get_articles()
|
|
|
|
|
|
|
|
if articles is not None:
|
|
|
|
context = {'articles': articles}
|
2024-06-12 15:26:47 +02:00
|
|
|
output = render_template('base.html', **context)
|
|
|
|
with open("rendered.html", "w") as f:
|
|
|
|
f.write(output)
|
|
|
|
|
2024-06-12 10:49:45 +02:00
|
|
|
return render_template('base.html', **context)
|
|
|
|
|
|
|
|
return "Failed to fetch articles."
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2024-06-12 15:26:47 +02:00
|
|
|
app.run()
|
|
|
|
index()
|