123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- 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
- from users import Users
- app = Flask(__name__)
- blog = Blog()
- users = Users()
- 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=users,
- 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=users,
- 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("/post/<int:post_id>")
- def get_post(post_id):
- post = blog.get_post_by_id(post_id)
- if post:
- username = users.get_user_by_id(post.user_id).username
- return render_template(
- 'post.html',
- title=f'{SITE_TITLE} - {post.title}',
- year=year_indicate,
- username=username,
- post=post)
- 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()
|