~fabrixxm/lesanaweb

114171a762f5ed1acd8d9c019d0e5c82c10e788d — fabrixxm 1 year, 1 month ago 5fe3400
[wip] Transform into a generic lesana web interface

- add a 'entry' route, to show entry details
- configure flask to load templates from collection folder, with
  fallback to generic templates
- add generic templates
- fix some variable names
3 files changed, 80 insertions(+), 118 deletions(-)

M app.py
A templates/entry.html
M templates/index.html
M app.py => app.py +44 -11
@@ 1,6 1,9 @@
import os

import jinja2
from flask import Flask
from flask import render_template, request
from flask import render_template, request, abort

from lesana.collection import Collection




@@ 12,23 15,52 @@ try:
except ImportError as e:
    print("[I] no settings module. using defaults.")

# in wsgi i percorsi relativi sono andati.
# questo torna utile poi per mettere i template nel repo del catalogo
collection = Collection(COLLECTIONPATH)

# get template from collection

templatefolder = os.path.join(
	os.path.dirname(__file__),
	"templates")
app = Flask("linkopedia", template_folder=templatefolder)
collection = Collection(COLLECTIONPATH)

app = Flask("lesanaweb")

# set template search paths
# first, search in collection templates, then use defaults.
my_loader = jinja2.ChoiceLoader([
    jinja2.FileSystemLoader(
        os.path.join(collection.basedir, "templates", "web")
    ),
    jinja2.FileSystemLoader(
        os.path.join(os.path.dirname(__file__), "templates")
    ),
])
app.jinja_loader = my_loader



@app.route('/search.html')
def search(name=None):
def search():
    page = int(request.args.get('p', 1))
    q = request.args.get('q')
    q = request.args.get('q', ' ')

    collection.start_search(q)
    data = list(collection.get_search_results(page - 1, PAGESIZE))
    return render_template('index.html', q=q, page=page, data=data)
    entries = list(collection.get_search_results(page - 1, PAGESIZE))
    return render_template('index.html', q=q, page=page, entries=entries, settings=collection.settings)


@app.route('/<eid>.html')
def entry(eid):
    entries = collection.entries_from_short_eid(eid)
    if len(entries) > 1:
        abort(500, "{} is not an unique eid".format(eid))
    if not entries:
        abort(404, "Could not find an entry with eid starting with: {}".format(
            eid
        ))
    entry = entries[0]

    return render_template('entry.html', entry=entry)


@app.route('/', methods=['GET'])


@@ 38,5 70,6 @@ def index():
    pagestart = (page - 1) * PAGESIZE
    pageend = pagestart + PAGESIZE

    data = list(collection.get_all_documents())[pagestart:pageend]
    return render_template('index.html', page=page, data=data)
    entries = list(collection.get_all_documents())[pagestart:pageend]
    return render_template('index.html', page=page, entries=entries, settings=collection.settings)


A templates/entry.html => templates/entry.html +18 -0
@@ 0,0 1,18 @@
<!DOCTYPE html>
<html>
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>{{ entry }}</title>
    </head>
    <body>
      <!-- generic item renderer -->
      <nav><a href="{{ url_for('index') }}">home</a></nav>
      <h3>{{ entry }}</h3>
      <dl>
        {% for k,v in entry.get_data().items() %}
          <dt>{{ k }}</dt>
          <dd>{{ v }}</dd>
        {% endfor %}
      </dl>
    </body>
</html>

M templates/index.html => templates/index.html +18 -107
@@ 1,117 1,28 @@
<!DOCTYPE html>
<html>
    <head>
        <meta name="referrer" content="origin">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <!-- <link rel="stylesheet" type="text/css" href="links.css"> -->
        <link rel="shortcut icon" href="favicon.ico">
        <link rel="alternate" type="application/rss+xml" title="RSS" href="/rss">

        <title>Linkopedia</title>
        <style>
         html { width: 100%; }
         body {
             width: 100%; max-width: 80ch; margin: 1em auto;
             font-family: Verdana, Geneva, sans-serif;
             font-size: 12pt;
             color: #828282;
         }

         a:link { color: #000; text-decoration: none; }
         a:hover { text-decoration: underline; }

         body > header {
             color: #000;
             background-color: #82a9d2;
             padding: 0 1em;
             line-height: 2em;
             display: flex;
             flex-direction: row;
             justify-content: space-between;
         }
         body > header > h1 {
             font-size: 1em;
             margin:0;
             display: inline-block;
         }
         body > header > h1 > a { color: #000; }

         body > section {
             padding: 1em;
             background-color: #f2f2f2;
             border-bottom: 2px solid #82a9d2;
         }
         #linkslist { margin: 0; }
         #linkslist dd {
             margin: 0.2em 0 1em 0;
         }
         #linkslist dd > footer {
             font-size: 0.8em;
             margin-top: 0.2em;
         }

         #linkslist a:link { color: #000; }

         #linkslist a.tag {
             line-height: 1.2em;
             padding: 0 0.2em;
             border: 1px solid #828282;
             background-color: #E2E2E2;
             border-radius: 3px;
             color: #828282;
             text-decoration: none;
         }
         #linkslist a.tag:hover { color: #000; }


         #nextpage { color: #828282; }

         .placeholder { text-align: center; }

         body > footer {
             margin-top: 2em;
             text-align: center;
         }

        </style>
        <title>{{ settings.name }}</title>
    </head>
    <body>
        <header>
            <h1><a href="{{ url_for('index') }}">Linkopedia</a></h1>
            <span>I Bookmark del <a href="https://gl-como.it/">GL-Como</a></span>
        </header>
        <section>
            {% if q %}<h2>Cerca: '{{ q }}'</h2>{% endif %}
            <dl id="linkslist">
                {% for entry in data %}
                {% set doc = entry.get_data() %}
                <dt class="link" id="{{ doc.eid }}">
                    <a href="{{ doc.url }}" title="{{ doc.url }}">{{ doc.title }}</a>
                </dt>
                <dd>
                    {{ doc.description }}
                    <footer>
                        {{ doc.added }}
                        {{ "-" if doc.tags|count > 0 }}
                        {% for tag in doc.tags %}
                        <a class="tag" href="{{ url_for('search') }}?q=tag:{{tag|urlencode}}">{{ tag }}</a>
                        {% endfor %}
                        </footer>
                </dd>
                {% endfor %}
            </dl>
            {% if data|count > 0 %}
                {% if q %}
                    <a id="nextpage" href="{{ url_for('search', q=q, p=page+1) }}">altro</a>
                {% else %}
                    <a id="nextpage" href="{{ url_for('index', p=page+1) }}">altro</a>
                {% endif %}
        <h1>{{ settings.name }}</h1>
        <form method="get" action="{{ url_for('search') }}"><input name="q" value="{{ q }}"><input type="submit" value="Search"></form>
        <ul>
            {% for entry in entries %}
            {% set doc = entry.get_data() %}
            <li id="{{ doc.eid }}">
                <a href="{{ url_for('entry', eid=doc.eid) }}">{{ entry }}</a>
            </li>
            {% endfor %}
        </dl>
        {% if entries|count > 0 %}
            {% if q %}
                <a href="{{ url_for('search', q=q, p=page+1) }}">more</a>
            {% else %}
                <div class="placeholder">nessun risultato</div>
                <a href="{{ url_for('index', p=page+1) }}">more</a>
            {% endif %}
        </section>
        <footer>
            <form method="get" action="{{ url_for('search') }}"><input name="q" placeholder="cerca..."></form>
        </footer>
        {% else %}
            <h4>no results</h4>
        {% endif %}
    </body>
</html>