123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- from flask import Flask, redirect, render_template, url_for, request, make_response, Markup
- import datetime
- from blog import Blog, Post
- from config import config, START_YEAR, SITE_TITLE
- from atom import make_feed
- app = Flask(__name__)
- blog = Blog()
- date = datetime.date.today()
- year_indicate = date.year if date.year == START_YEAR else f"{START_YEAR} - {date.year}"
- make_feed(blog.get_all())
- @app.route('/feeds')
- def get_feed():
- rss_xml = render_template('atom.xml')
- response = make_response(rss_xml)
- response.headers['Content-Type'] = 'application/rss+xml'
- return response
- @app.errorhandler(404)
- def not_found(e):
- return render_template("404.html")
- @app.route("/")
- def get_home():
- return render_template(
- 'index.html',
- title=SITE_TITLE+' - Главная',
- year=year_indicate,
- page=1,
- users=blog.authors,
- pages=blog.get_pages_count(),
- posts=blog.get_by_page(page=1)
- )
- @app.route("/page/<int:page>")
- def get_blog_page(page):
- pages = blog.get_pages_count()
- if page > len(pages):
- return render_template('404.html')
- return render_template(
- 'index.html',
- title=f'{SITE_TITLE} - page {page}',
- year=year_indicate,
- page=page,
- users=blog.authors,
- pages=pages,
- posts=blog.get_by_page(page=page)
- )
- @app.route("/post/add")
- def add_post():
- blog.add_post(Post(
- id=len(blog.get_all())+1,
- user_id=2,
- title='Another post',
- body=Markup(
- 'Hello my <span style="color:red">friends</span>. This is test post')
- ))
- make_feed(blog.get_all())
- return redirect(url_for('get_home'))
- @app.route("/author/<int:author_id>")
- def get_author(author_id):
- author = blog.authors.get_user_by_id(author_id)
- posts = blog.get_posts_by_user_id(author_id)
- return render_template(
- 'index.html',
- title=f'{SITE_TITLE} - {author.username}',
- year=year_indicate,
- page=1,
- users=blog.authors,
- pages=blog.get_pages_count(posts),
- posts=blog.get_by_page(page=1, posts=posts)
- )
- @app.route("/post/<int:post_id>")
- def get_post(post_id):
- post = blog.get_post_by_id(post_id)
- if post:
- user = blog.authors.get_user_by_id(post.user_id)
- return render_template(
- 'post.html',
- title=f'{SITE_TITLE} - {post.title}',
- year=year_indicate,
- user=user,
- post=post,
- audio=post.audio_url)
- else:
- return render_template('404.html')
- @app.route("/secret")
- def get_login():
- return render_template('login.html')
- @app.route("/profile")
- def get_profile():
- return render_template('profile.html')
- @app.route("/dashboard")
- def get_dashboard():
- return render_template('dashboard.html')
- @app.route("/username/<name>=<int:age>")
- def hello_user(name, age):
- return f"Hello {name} {age} y.o."
- if __name__ == "__main__":
- app.config.from_object(config)
- app.run(port=config.PORT)
|