123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- import logging
- import os
- from datetime import datetime
- from flask import Flask, redirect, render_template, request, url_for
- from flask_paginate import Pagination
- from flask_sqlalchemy import SQLAlchemy
- # from sqlalchemy import Column, Integer, String
- # from sqlalchemy.ext.declarative import declarative_base
- from sqlalchemy.orm import relationship
- logging.basicConfig()
- logging.getLogger("sqlalchemy.engine").setLevel(logging.INFO)
- app = Flask(__name__)
- app.config["SQLALCHEMY_DATABASE_URI"] = os.getenv(
- "DB_URI", "sqlite:///news.db"
- )
- db = SQLAlchemy(app)
- class News(db.Model):
- __tablename__ = "news"
- nid = db.Column(db.String, primary_key=True)
- author = db.Column(db.Unicode)
- title = db.Column(db.Unicode)
- body = db.Column(db.Unicode)
- published = db.Column(db.String)
- last_modified = db.Column(db.String)
- display = db.Column(db.Unicode)
- comments = relationship("Comments")
- __mapper_args__ = {"order_by": nid}
- @property
- def canonical_url(self):
- return url_for(
- "news",
- year=self.published_datetime.year,
- month=self.published_datetime.month,
- nid=self.nid,
- )
- @property
- def published_datetime(self):
- return datetime.fromtimestamp(self.published)
- @property
- def published_str(self):
- return str(self.published_datetime)
- def __repr__(self):
- return "<News(%s)>" % (self.nid)
- class Comments(db.Model):
- __tablename__ = "comments"
- nid = db.Column(db.ForeignKey("news.nid"))
- num = db.Column(db.Integer)
- author = db.Column(db.Unicode)
- title = db.Column(db.Unicode)
- body = db.Column(db.Unicode)
- published = db.Column(db.String)
- # display = db.Column(db.Unicode)
- __table_args__ = (db.PrimaryKeyConstraint("nid", "num"),)
- __mapper_args__ = {"order_by": num}
- @property
- def canonical_url(self):
- return url_for(
- "news",
- year=self.published_datetime.year,
- month=self.published_datetime.month,
- nid=self.nid,
- )
- @property
- def published_datetime(self):
- return datetime.fromtimestamp(self.published)
- @property
- def published_str(self):
- return str(self.published_datetime)
- def __repr__(self):
- return "<Comments(%s:%s)>" % (self.nid, self.num)
- @app.route("/")
- def home():
- return render_template("home.html")
- @app.route("/search/")
- def search_home():
- return "cerca cerca"
- @app.route("/search/by-month/")
- def search_by_month_form():
- return render_template("search_month.html")
- @app.route("/search/advanced/")
- def search_advanced_form():
- if not request.args:
- return render_template("search_advanced.html")
- def f(x):
- return int(request.args[x])
- d = datetime(year=f("fromyear"), month=f("frommonth"), day=1)
- ts_from = int(d.timestamp())
- if f("tomonth") < 12:
- d = datetime(year=f("toyear"), month=f("tomonth") + 1, day=1)
- else:
- d = datetime(year=f("toyear") + 1, month=1, day=1)
- ts_to = int(d.timestamp())
- news = News.query.filter(News.published >= ts_from, News.published < ts_to)
- if request.args["title"]:
- news = news.filter(News.title.ilike("%%%s%%" % request.args["title"]))
- if request.args["text"]:
- news = news.filter(News.body.ilike("%%%s%%" % request.args["text"]))
- if request.args["author"]:
- news = news.filter(
- News.author.ilike("%%%s%%" % request.args["author"])
- )
- if request.args["include_hidden"] == "0":
- news = news.filter(News.display != "f")
- news = news.order_by("published")
- page = int(request.args.get("page", "1")) - 1
- first = page * 50
- last = first + 50
- pagination = Pagination(
- page=page, total=news.count(), per_page=50, bs_version=4
- )
- news = news[first:last]
- return render_template(
- "search_results.html", results=news, pagination=pagination
- )
- @app.route("/search/by-month/", methods=["POST"])
- def search_by_month_form_post():
- return redirect(
- url_for(
- "search_by_month",
- year=request.form["year"],
- month=request.form["month"],
- )
- )
- @app.route("/news/<int:year>/<int:month>/")
- @app.route("/search/by-month/<int:year>/<int:month>")
- def search_by_month(year, month):
- page = int(request.args.get("page", "1")) - 1
- first = page * 50
- last = first + 50
- d = datetime(year=year, month=month, day=1)
- ts_from = int(d.timestamp())
- if month < 12:
- d = datetime(year=year, month=month + 1, day=1)
- else:
- d = datetime(year=year + 1, month=1, day=1)
- ts_to = int(d.timestamp())
- news = News.query.filter(
- News.published >= ts_from, News.published < ts_to
- ).order_by("published")
- pagination = Pagination(
- page=page, total=news.count(), per_page=50, bs_version=4
- )
- news = news[first:last]
- return render_template(
- "search_results.html", results=news, pagination=pagination
- )
- @app.route("/news/<int:nid>")
- def news_by_nid(nid):
- n = News.query.get(str(nid))
- return redirect(n.canonical_url)
- @app.route("/news/<year>/<month>/<int:nid>.php")
- def news(year, month, nid):
- n = News.query.get(str(nid))
- return render_template("news.html", n=n, comments=n.comments)
|