app.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 render_template("home.html")
  74. @app.route("/search/")
  75. def search_home():
  76. return "cerca cerca"
  77. @app.route("/search/by-month/")
  78. def search_by_month_form():
  79. return render_template("search_month.html")
  80. @app.route("/search/advanced/")
  81. def search_advanced_form():
  82. if not request.args:
  83. return render_template("search_advanced.html")
  84. def f(x):
  85. return int(request.args[x])
  86. d = datetime(year=f("fromyear"), month=f("frommonth"), day=1)
  87. ts_from = int(d.timestamp())
  88. if f("tomonth") < 12:
  89. d = datetime(year=f("toyear"), month=f("tomonth") + 1, day=1)
  90. else:
  91. d = datetime(year=f("toyear") + 1, month=1, day=1)
  92. ts_to = int(d.timestamp())
  93. news = News.query.filter(News.published >= ts_from, News.published < ts_to)
  94. if request.args["title"]:
  95. news = news.filter(News.title.ilike("%%%s%%" % request.args["title"]))
  96. if request.args["text"]:
  97. news = news.filter(News.body.ilike("%%%s%%" % request.args["text"]))
  98. if request.args["author"]:
  99. news = news.filter(
  100. News.author.ilike("%%%s%%" % request.args["author"])
  101. )
  102. if request.args["include_hidden"] == "0":
  103. news = news.filter(News.display != "f")
  104. news = news.order_by("published")
  105. page = int(request.args.get("page", "1")) - 1
  106. first = page * 50
  107. last = first + 50
  108. pagination = Pagination(
  109. page=page, total=news.count(), per_page=50, bs_version=4
  110. )
  111. news = news[first:last]
  112. return render_template(
  113. "search_results.html", results=news, pagination=pagination
  114. )
  115. @app.route("/search/by-month/", methods=["POST"])
  116. def search_by_month_form_post():
  117. return redirect(
  118. url_for(
  119. "search_by_month",
  120. year=request.form["year"],
  121. month=request.form["month"],
  122. )
  123. )
  124. @app.route("/news/<int:year>/<int:month>/")
  125. @app.route("/search/by-month/<int:year>/<int:month>")
  126. def search_by_month(year, month):
  127. page = int(request.args.get("page", "1")) - 1
  128. first = page * 50
  129. last = first + 50
  130. d = datetime(year=year, month=month, day=1)
  131. ts_from = int(d.timestamp())
  132. if month < 12:
  133. d = datetime(year=year, month=month + 1, day=1)
  134. else:
  135. d = datetime(year=year + 1, month=1, day=1)
  136. ts_to = int(d.timestamp())
  137. news = News.query.filter(
  138. News.published >= ts_from, News.published < ts_to
  139. ).order_by("published")
  140. pagination = Pagination(
  141. page=page, total=news.count(), per_page=50, bs_version=4
  142. )
  143. news = news[first:last]
  144. return render_template(
  145. "search_results.html", results=news, pagination=pagination
  146. )
  147. @app.route("/news/<int:nid>")
  148. def news_by_nid(nid):
  149. n = News.query.get(str(nid))
  150. return redirect(n.canonical_url)
  151. @app.route("/news/<year>/<month>/<int:nid>.php")
  152. def news(year, month, nid):
  153. n = News.query.get(str(nid))
  154. return render_template("news.html", n=n, comments=n.comments)