app.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import os
  2. from datetime import datetime
  3. from flask import Flask, redirect, render_template, request, url_for
  4. # from sqlalchemy import Column, Integer, String
  5. # from sqlalchemy.ext.declarative import declarative_base
  6. from sqlalchemy.orm import relationship
  7. from flask_paginate import Pagination
  8. from flask_sqlalchemy import SQLAlchemy
  9. app = Flask(__name__)
  10. app.config["SQLALCHEMY_DATABASE_URI"] = os.getenv(
  11. "DB_URI", "sqlite:///news.db"
  12. )
  13. db = SQLAlchemy(app)
  14. class News(db.Model):
  15. __tablename__ = "news"
  16. nid = db.Column(db.String, primary_key=True)
  17. author = db.Column(db.Unicode)
  18. title = db.Column(db.Unicode)
  19. body = db.Column(db.Unicode)
  20. published = db.Column(db.String)
  21. last_modified = db.Column(db.String)
  22. display = db.Column(db.Unicode)
  23. comments = relationship("Comments")
  24. __mapper_args__ = {"order_by": nid}
  25. @property
  26. def canonical_url(self):
  27. return url_for(
  28. "news",
  29. year=self.published_datetime.year,
  30. month=self.published_datetime.month,
  31. nid=self.nid,
  32. )
  33. @property
  34. def published_datetime(self):
  35. return datetime.fromtimestamp(self.published)
  36. @property
  37. def published_str(self):
  38. return str(self.published_datetime)
  39. def __repr__(self):
  40. return "<News(%s)>" % (self.nid)
  41. class Comments(db.Model):
  42. __tablename__ = "comments"
  43. nid = db.Column(db.ForeignKey("news.nid"))
  44. num = db.Column(db.Integer)
  45. author = db.Column(db.Unicode)
  46. title = db.Column(db.Unicode)
  47. body = db.Column(db.Unicode)
  48. published = db.Column(db.String)
  49. # display = db.Column(db.Unicode)
  50. __table_args__ = (db.PrimaryKeyConstraint("nid", "num"),)
  51. __mapper_args__ = {"order_by": num}
  52. @property
  53. def canonical_url(self):
  54. return url_for(
  55. "news",
  56. year=self.published_datetime.year,
  57. month=self.published_datetime.month,
  58. nid=self.nid,
  59. )
  60. @property
  61. def published_datetime(self):
  62. return datetime.fromtimestamp(self.published)
  63. @property
  64. def published_str(self):
  65. return str(self.published_datetime)
  66. def __repr__(self):
  67. return "<Comments(%s:%s)>" % (self.nid, self.num)
  68. @app.route("/")
  69. def home():
  70. return """
  71. homepage: puoi fare
  72. * /search/
  73. * /search/by-month/YYYY/M
  74. * /news/ID
  75. * /news/YYYY/MM/ID.php
  76. """
  77. @app.route("/search/")
  78. def search_home():
  79. return "cerca cerca"
  80. @app.route("/search/by-month/<int:year>/<int:month>")
  81. def search_by_month(year, month):
  82. # TODO: proper pagination
  83. page = int(request.args.get("page", "1")) - 1
  84. first = page * 50
  85. last = first + 50
  86. d = datetime(year=year, month=month, day=1)
  87. ts_from = int(d.timestamp())
  88. if month < 12:
  89. d = datetime(year=year, month=month + 1, day=1)
  90. else:
  91. d = datetime(year=year + 1, month=1, day=1)
  92. ts_to = int(d.timestamp())
  93. news = News.query.filter(
  94. News.published >= ts_from, News.published < ts_to
  95. ).order_by("published")
  96. pagination = Pagination(page=page, total=news.count(), per_page=50)
  97. news = news[first:last]
  98. return render_template(
  99. "search_results.html", results=news, pagination=pagination
  100. )
  101. @app.route("/news/<int:nid>")
  102. def news_by_nid(nid):
  103. n = News.query.get(str(nid))
  104. return redirect(n.canonical_url)
  105. @app.route("/news/<year>/<month>/<int:nid>.php")
  106. def news(year, month, nid):
  107. n = News.query.get(str(nid))
  108. return render_template("news.html", n=n, comments=n.comments)