~fabrixxm/lesanaweb

12dd1d40be180ce6e406575ea46c0b4fcaba2b8c — fabrixxm 6 months ago b3d356d master
Add atom feed
3 files changed, 56 insertions(+), 4 deletions(-)

M app.py
A templates/atom.xml
M templates/base.html
M app.py => app.py +32 -4
@@ 1,7 1,7 @@
import os

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

try:


@@ 19,10 19,19 @@ class ConfigurationError(Exception):

COLLECTIONPATH = os.environ.get('LW_COLLECTION_PATH', None)
PAGESIZE = int(os.environ.get('LW_PAGE_SIZE', 20))
SITEURL = os.environ.get('LW_SITEURL', None)

if COLLECTIONPATH is None:
    raise ConfigurationError("LW_COLLECTION_PATH env var is not set")

if SITEURL is None:
    raise ConfigurationError("LW_SITEURL env var is not set")

if not SITEURL.startswith("http"):
    raise ConfigurationError("LW_SITEURL should be a valid http url")

SITEURL = SITEURL.strip("/")

if not os.path.isdir(COLLECTIONPATH) or \
   not os.path.isfile(os.path.join(COLLECTIONPATH, "settings.yaml")):
    raise ConfigurationError("'{} is not a valid collection".format(COLLECTIONPATH))


@@ 76,8 85,15 @@ def about():
        'about.html',
        settings=collection.settings,
        readme=readme,
        siteurl=SITEURL,
    )

@app.route('/atom.xml')
def atom():
    q = request.args.get('q', None)
    r = _list_search(q, 'atom.xml')
    return Response(r, mimetype='application/atom+xml')

@app.route('/search.html')
def search():
    q = request.args.get('q', ' ')


@@ 94,14 110,19 @@ def entry(eid):
        ))
    entry = entries[0]

    return render_template('entry.html', entry=entry, settings=collection.settings)
    return render_template(
        'entry.html',
        entry=entry,
        settings=collection.settings,
        siteurl=SITEURL,
    )

@app.route('/', methods=['GET'])
def index():
    return _list_search()


def _list_search(q=None):
def _list_search(q=None, template='index.html'):
    page = int(request.args.get('p', 1))
    if (page < 1):
        page = 1


@@ 114,6 135,13 @@ def _list_search(q=None):
    else:
        collection.start_search(q)
    entries = list(collection.get_search_results(pagestart, pageend))
    return render_template('index.html', q=q, page=page, entries=entries, settings=collection.settings)
    return render_template(
        template,
        q=q,
        page=page,
        entries=entries,
        settings=collection.settings,
        siteurl=SITEURL,
    )



A templates/atom.xml => templates/atom.xml +20 -0
@@ 0,0 1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title>{% if q %}Results for '{{ q }}' - {% endif %}{{ settings.name }}</title>
  <link href="{{ siteurl }}" />
  <link href="{{ siteurl }}/atom.xml{% if q %}?q={{ q|urlencode }}{% endif %}" rel="self" />
  <updated></updated>
  <id></id>

  {% for entry in entries %}
  <entry>
    <title>{{ entry }}</title>
    <link href="{{ siteurl }}{{ url_for('entry', eid=entry.eid) }}"/>
    <id>urn:uuid:{{ entry.eid }}</id>
    <updated></updated>
    <summary></summary>
  </entry>
  {% endfor %}

</feed>

M templates/base.html => templates/base.html +4 -0
@@ 4,6 4,10 @@
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, height=device-height, user-scalable=no, initial-scale=1.0"/>
        <title>{% block title %}{% endblock %}</title>
        <link rel="alternate" type="application/atom+xml" href="/atom.xml" title="Atom Feed"/>
        {% if q %}
        <link rel="alternate" type="application/atom+xml" href="/atom.xml?q={{ q|urlencode }}" title="Results for '{{ q }}'"/>
        {% endif %}
        <style>
            body {
              margin: 5% auto;