Browse Source

mostra commenti

boyska 3 years ago
parent
commit
dbf0111d98
3 changed files with 93 additions and 22 deletions
  1. 76 21
      app.py
  2. 4 0
      templates/base.html
  3. 13 1
      templates/news.html

+ 76 - 21
app.py

@@ -1,19 +1,20 @@
 import sqlite3
 from datetime import datetime
 
-from  flask import Flask, render_template, redirect, url_for, request
-from  flask_sqlalchemy import SQLAlchemy
-from  flask_paginate import Pagination
+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
 
 app = Flask(__name__)
-app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///news.db'
+app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///news.db"
 db = SQLAlchemy(app)
 
 
 class News(db.Model):
-    __tablename__ = 'news'
+    __tablename__ = "news"
 
     nid = db.Column(db.String, primary_key=True)
     author = db.Column(db.Unicode)
@@ -22,10 +23,52 @@ class News(db.Model):
     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)
+        return url_for(
+            "news",
+            year=self.published_datetime.year,
+            month=self.published_datetime.month,
+            nid=self.nid,
+        )
 
     @property
     def published_datetime(self):
@@ -36,44 +79,56 @@ class News(db.Model):
         return str(self.published_datetime)
 
     def __repr__(self):
-       return "<News(%s)>" % (self.nid)
+        return "<Comments(%s:%s)>" % (self.nid, self.num)
 
-@app.route('/')
+
+@app.route("/")
 def home():
-    return 'homepage'
+    return """
+    homepage: puoi fare
+     * /search/
+     * /search/by-month/YYYY/M
+     * /news/ID
+     * /news/YYYY/MM/ID.php
+    """
+
 
-@app.route('/search/')
+@app.route("/search/")
 def search_home():
-    return 'cerca cerca'
+    return "cerca cerca"
+
 
-@app.route('/search/by-month/<int:year>/<int:month>')
+@app.route("/search/by-month/<int:year>/<int:month>")
 def search_by_month(year, month):
     # TODO: proper pagination
-    page = int(request.args.get('page', '1')) - 1
+    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)
+        d = datetime(year=year, month=month + 1, day=1)
     else:
-        d = datetime(year=year+1, month=1, day=1)
+        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')
+    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)
     news = news[first:last]
 
-    return render_template('search_results.html', results=news, pagination=pagination)
+    return render_template("search_results.html", results=news, pagination=pagination)
 
-@app.route('/news/<int:nid>')
+
+@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')
+
+@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)
-
+    return render_template("news.html", n=n, comments=n.comments)

+ 4 - 0
templates/base.html

@@ -5,6 +5,10 @@
     display: inline;
     padding: 0 0.5em;
 }
+.article-comment {
+    margin: 1em;
+    border: 1px solid blue;
+}
 </style>
     </body>
     <div>

+ 13 - 1
templates/news.html

@@ -1,10 +1,22 @@
+{% extends "base.html" %}
+{% block content %}
 <article>
     <header> <h1>{{n.title}}</h1>
         <div>
-        <time> {{n.published_datetime.strftime("%d %b %Y")}}</time>
+        <time> {{n.published_datetime.strftime("%d %b %Y - %H:%M")}}</time>
         </div>
     </header>
     <div class="article-body">
         {{n.body|safe}}
     </div>
+    <div class="article-comments">
+        {% for comm in comments %}
+        <div class="article-comment">
+            <div class="author">{{comm.author}}</div>
+            <div class="published"><time>{{comm.published_datetime.strftime("%d %b %Y - %H:%M")}}</time></div>
+            {{comm.body|safe}}
+        </div>
+        {% endfor %}
+    </div>
 </article>
+{% endblock content %}