app.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import logging
  2. import os
  3. from datetime import datetime
  4. from flask import Flask, redirect, render_template, request, url_for
  5. from flask_paginate import Pagination
  6. from flask_sqlalchemy import SQLAlchemy
  7. # from sqlalchemy import Column, Integer, String
  8. # from sqlalchemy.ext.declarative import declarative_base
  9. from sqlalchemy.orm import relationship
  10. logging.basicConfig()
  11. logging.getLogger("sqlalchemy.engine").setLevel(logging.INFO)
  12. app = Flask(__name__)
  13. app.config["SQLALCHEMY_DATABASE_URI"] = os.getenv(
  14. "DB_URI", "sqlite:///news.db"
  15. )
  16. db = SQLAlchemy(app)
  17. class News(db.Model):
  18. __tablename__ = "news"
  19. nid = db.Column(db.String, primary_key=True)
  20. author = db.Column(db.Unicode)
  21. title = db.Column(db.Unicode)
  22. body = db.Column(db.Unicode)
  23. published = db.Column(db.String)
  24. last_modified = db.Column(db.String)
  25. display = db.Column(db.Unicode)
  26. comments = relationship("Comments")
  27. __mapper_args__ = {"order_by": nid}
  28. @property
  29. def canonical_url(self):
  30. return url_for(
  31. "news",
  32. year=self.published_datetime.year,
  33. month=self.published_datetime.month,
  34. nid=self.nid,
  35. )
  36. @property
  37. def published_datetime(self):
  38. return datetime.fromtimestamp(self.published)
  39. @property
  40. def published_str(self):
  41. return str(self.published_datetime)
  42. def __repr__(self):
  43. return "<News(%s)>" % (self.nid)
  44. class Comments(db.Model):
  45. __tablename__ = "comments"
  46. nid = db.Column(db.ForeignKey("news.nid"))
  47. num = db.Column(db.Integer)
  48. author = db.Column(db.Unicode)
  49. title = db.Column(db.Unicode)
  50. body = db.Column(db.Unicode)
  51. published = db.Column(db.String)
  52. # display = db.Column(db.Unicode)
  53. __table_args__ = (db.PrimaryKeyConstraint("nid", "num"),)
  54. __mapper_args__ = {"order_by": num}
  55. @property
  56. def canonical_url(self):
  57. return url_for(
  58. "news",
  59. year=self.published_datetime.year,
  60. month=self.published_datetime.month,
  61. nid=self.nid,
  62. )
  63. @property
  64. def published_datetime(self):
  65. return datetime.fromtimestamp(self.published)
  66. @property
  67. def published_str(self):
  68. return str(self.published_datetime)
  69. def __repr__(self):
  70. return "<Comments(%s:%s)>" % (self.nid, self.num)
  71. @app.route("/")
  72. def home():
  73. return """
  74. homepage: puoi fare
  75. * /search/
  76. * /search/by-month/YYYY/M
  77. * /news/ID
  78. * /news/YYYY/MM/ID.php
  79. """
  80. @app.route("/search/")
  81. def search_home():
  82. return "cerca cerca"
  83. @app.route("/search/by-month/")
  84. def search_by_month_form():
  85. return render_template("search_month.html")
  86. @app.route("/search/advanced/")
  87. def search_advanced_form():
  88. if not request.args:
  89. return render_template("search_advanced.html")
  90. def f(x):
  91. return int(request.args[x])
  92. d = datetime(year=f("fromyear"), month=f("frommonth"), day=1)
  93. ts_from = int(d.timestamp())
  94. if f("tomonth") < 12:
  95. d = datetime(year=f("toyear"), month=f("tomonth") + 1, day=1)
  96. else:
  97. d = datetime(year=f("toyear") + 1, month=1, day=1)
  98. ts_to = int(d.timestamp())
  99. news = News.query.filter(News.published >= ts_from, News.published < ts_to)
  100. if request.args["title"]:
  101. news = news.filter(News.title.ilike("%%%s%%" % request.args["title"]))
  102. if request.args["text"]:
  103. news = news.filter(News.body.ilike("%%%s%%" % request.args["text"]))
  104. if request.args["author"]:
  105. news = news.filter(
  106. News.author.ilike("%%%s%%" % request.args["author"])
  107. )
  108. if request.args["include_hidden"] == "0":
  109. news = news.filter(News.display != "f")
  110. news = news.order_by("published")
  111. page = int(request.args.get("page", "1")) - 1
  112. first = page * 50
  113. last = first + 50
  114. pagination = Pagination(
  115. page=page, total=news.count(), per_page=50, bs_version=4
  116. )
  117. news = news[first:last]
  118. return render_template(
  119. "search_results.html", results=news, pagination=pagination
  120. )
  121. @app.route("/search/by-month/", methods=["POST"])
  122. def search_by_month_form_post():
  123. return redirect(
  124. url_for(
  125. "search_by_month",
  126. year=request.form["year"],
  127. month=request.form["month"],
  128. )
  129. )
  130. @app.route("/news/<int:year>/<int:month>/")
  131. @app.route("/search/by-month/<int:year>/<int:month>")
  132. def search_by_month(year, month):
  133. page = int(request.args.get("page", "1")) - 1
  134. first = page * 50
  135. last = first + 50
  136. d = datetime(year=year, month=month, day=1)
  137. ts_from = int(d.timestamp())
  138. if month < 12:
  139. d = datetime(year=year, month=month + 1, day=1)
  140. else:
  141. d = datetime(year=year + 1, month=1, day=1)
  142. ts_to = int(d.timestamp())
  143. news = News.query.filter(
  144. News.published >= ts_from, News.published < ts_to
  145. ).order_by("published")
  146. pagination = Pagination(
  147. page=page, total=news.count(), per_page=50, bs_version=4
  148. )
  149. news = news[first:last]
  150. return render_template(
  151. "search_results.html", results=news, pagination=pagination
  152. )
  153. @app.route("/news/<int:nid>")
  154. def news_by_nid(nid):
  155. n = News.query.get(str(nid))
  156. return redirect(n.canonical_url)
  157. @app.route("/news/<year>/<month>/<int:nid>.php")
  158. def news(year, month, nid):
  159. n = News.query.get(str(nid))
  160. return render_template("news.html", n=n, comments=n.comments)