Browse Source

ricerca avanzata

boyska 3 years ago
parent
commit
60a2b65e43
3 changed files with 66 additions and 11 deletions
  1. 46 0
      app.py
  2. 3 2
      templates/search_month.html
  3. 17 9
      templates/search_results.html

+ 46 - 0
app.py

@@ -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(

+ 3 - 2
templates/search_month.html

@@ -5,8 +5,9 @@
     <div class="form-group">
         <label for="year">Anno</label>
         <select  class="form-control" name="year">
-            <option>2000</option>
-            <option>2001</option>
+            {% for i in range(2000, 2008) %}
+            <option>{{i}}</option>
+            {% endfor %}
         </select>
     </div>
     <div class="form-group">

+ 17 - 9
templates/search_results.html

@@ -3,21 +3,29 @@
 <style>
 .post-hidden { font-size: 70%; }
 .post-hidden a { color: gray; }
-.result {
-    list-style: none;
-}
 .result .author { font-style: italic;  font-size: 85%;}
 </style>
-        <ul>
+<table class="table table-striped">
+    <thead>
+        <th>ID</th>
+        <th>Data</th>
+        <th>Titolo</th>
+        <th>Autore</th>
+    </thead>
+    <tbody>
             {% for r in results %}
-            <li class="result post-display--{{r.display}} {% if r.display == 'f' %}post-hidden{%endif%}"
+            <tr class="result post-display--{{r.display}} {% if r.display == 'f' %}post-hidden{%endif%}"
                 >
+                <td>
                 <a href="{{url_for('news_by_nid', nid=r.nid)}}">{{r.nid}}</a>
-                - <time>{{r.published_datetime.strftime('%d/%m/%Y')}}</time>
-                - {{r.title}} by <span class="author">{{r.author}}</span>
-            </li>
+                </td>
+                <td> <time>{{r.published_datetime.strftime('%d/%m/%Y')}}</time> </td>
+                <td> {{r.title}} </td>
+                <td class="author">{{r.author}}</td>
+            </tr>
             {% endfor %}
-        </ul>
+    </tbody>
+        </table>
         {{pagination.links}}
 {% endblock content %}