1
0
Fork 0
mirror of https://github.com/EDeev/web-dev.git synced 2026-10-08 04:59:37 +03:00
web-dev/labs/lab-1/app/app.py
Egor Deev 69ced80d88 Режим отладки и SECRET_KEY из окружения, зависимости lab-4/5 закреплены
- app.run(debug=True) во всех лабораторных и экзамене заменён на FLASK_DEBUG=1 из
  окружения: отладчик Werkzeug позволяет выполнять код на сервере;
- secret_key — из SECRET_KEY, прежние значения остались только как запасные;
- lab-4/5: Flask-SQLAlchemy 2.5 + SQLAlchemy 1.4 (со свежим SQLAlchemy 2 не импортировались);
- неиспользуемые импорты и переменные удалены (ruff).
2026-10-02 21:27:00 +00:00

52 lines
1.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
import random
from flask import Flask, render_template
from faker import Faker
fake = Faker('ru_RU')
app = Flask(__name__)
application = app
images_ids = ['Pic1', 'Pic2', 'Pic3', 'Pic4', 'Pic5']
def generate_comments(replies=True):
comments = []
for i in range(random.randint(1, 3)):
comment = { 'author': fake.name(), 'text': fake.text() }
if replies:
comment['replies'] = generate_comments(replies=False)
comments.append(comment)
return comments
def generate_post(i):
return {
'title': fake.company(),
'text': fake.paragraph(nb_sentences=100),
'author': fake.name(),
'date': fake.date_time_between(start_date='-2y', end_date='now'),
'image_id': f'{images_ids[i]}.jpg',
'comments': generate_comments()
}
posts_list = sorted([generate_post(i) for i in range(5)], key=lambda p: p['date'], reverse=True)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/posts')
def posts():
return render_template('posts.html', title='Посты', posts=posts_list)
@app.route('/posts/<int:index>')
def post(index):
p = posts_list[index]
return render_template('post.html', title=p['title'], post=p)
@app.route('/about')
def about():
return render_template('about.html', title='Об авторе')
if __name__ == '__main__':
app.run(debug=os.environ.get('FLASK_DEBUG') == '1')