|
@@ -1,3 +1,4 @@
|
|
|
+import logging
|
|
|
import os
|
|
|
from datetime import datetime
|
|
|
|
|
@@ -8,6 +9,10 @@ from flask_sqlalchemy import SQLAlchemy
|
|
|
# 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"
|
|
@@ -105,6 +110,47 @@ 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"])
|
|
|
+ )
|
|
|
+
|
|
|
+ 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(
|