audio_classes.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. from flask import Flask, redirect, render_template, url_for, request, make_response, Markup
  2. import datetime
  3. from blog import Blog, Post
  4. from config import config, START_YEAR, SITE_TITLE
  5. from atom import make_feed
  6. app = Flask(__name__)
  7. blog = Blog()
  8. date = datetime.date.today()
  9. year_indicate = date.year if date.year == START_YEAR else f"{START_YEAR} - {date.year}"
  10. make_feed(blog.get_all())
  11. @app.route('/feeds')
  12. def get_feed():
  13. rss_xml = render_template('atom.xml')
  14. response = make_response(rss_xml)
  15. response.headers['Content-Type'] = 'application/rss+xml'
  16. return response
  17. @app.errorhandler(404)
  18. def not_found(e):
  19. return render_template("404.html")
  20. @app.route("/")
  21. def get_home():
  22. return render_template(
  23. 'index.html',
  24. title=SITE_TITLE+' - Главная',
  25. year=year_indicate,
  26. page=1,
  27. users=blog.authors,
  28. pages=blog.get_pages_count(),
  29. posts=blog.get_by_page(page=1)
  30. )
  31. @app.route("/page/<int:page>")
  32. def get_blog_page(page):
  33. pages = blog.get_pages_count()
  34. if page > len(pages):
  35. return render_template('404.html')
  36. return render_template(
  37. 'index.html',
  38. title=f'{SITE_TITLE} - page {page}',
  39. year=year_indicate,
  40. page=page,
  41. users=blog.authors,
  42. pages=pages,
  43. posts=blog.get_by_page(page=page)
  44. )
  45. @app.route("/post/add")
  46. def add_post():
  47. blog.add_post(Post(
  48. id=len(blog.get_all())+1,
  49. user_id=2,
  50. title='Another post',
  51. body=Markup(
  52. 'Hello my <span style="color:red">friends</span>. This is test post')
  53. ))
  54. make_feed(blog.get_all())
  55. return redirect(url_for('get_home'))
  56. @app.route("/author/<int:author_id>")
  57. def get_author(author_id):
  58. author = blog.authors.get_user_by_id(author_id)
  59. posts = blog.get_posts_by_user_id(author_id)
  60. return render_template(
  61. 'index.html',
  62. title=f'{SITE_TITLE} - {author.username}',
  63. year=year_indicate,
  64. page=1,
  65. users=blog.authors,
  66. pages=blog.get_pages_count(posts),
  67. posts=blog.get_by_page(page=1, posts=posts)
  68. )
  69. @app.route("/post/<int:post_id>")
  70. def get_post(post_id):
  71. post = blog.get_post_by_id(post_id)
  72. if post:
  73. user = blog.authors.get_user_by_id(post.user_id)
  74. return render_template(
  75. 'post.html',
  76. title=f'{SITE_TITLE} - {post.title}',
  77. year=year_indicate,
  78. user=user,
  79. post=post,
  80. audio=post.audio_url)
  81. else:
  82. return render_template('404.html')
  83. @app.route("/secret")
  84. def get_login():
  85. return render_template('login.html')
  86. @app.route("/profile")
  87. def get_profile():
  88. return render_template('profile.html')
  89. @app.route("/dashboard")
  90. def get_dashboard():
  91. return render_template('dashboard.html')
  92. @app.route("/username/<name>=<int:age>")
  93. def hello_user(name, age):
  94. return f"Hello {name} {age} y.o."
  95. if __name__ == "__main__":
  96. app.config.from_object(config)
  97. app.run(port=config.PORT)