Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/metafeeds: Support for canteen meta feeds and index feed #2

Merged
merged 19 commits into from
Oct 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
da909fc
feature/metafeeds: Restructured API, splitted menu and meta feed
kaifabian Oct 14, 2018
c526a68
feature/metafeeds: add field 'key' to Canteen
kaifabian Oct 14, 2018
12f1ae6
feature/metafeeds: added helper function for reverse urls
kaifabian Oct 14, 2018
380bc3e
feature/metafeeds: added menu feed url to meta feed
kaifabian Oct 14, 2018
24e2d6b
feature/metafeeds: added missing test
kaifabian Oct 14, 2018
341dd8a
feature/metafeeds: added feed index page
kaifabian Oct 14, 2018
7e4ceee
whitespace fix
kaifabian Oct 14, 2018
1c4f6fc
feature/metafeeds: added alias urls
kaifabian Oct 14, 2018
69675ef
feature/metafeeds: moved meta feed default url
kaifabian Oct 14, 2018
80cb737
feature/metafeeds: moved index generation to views
kaifabian Oct 21, 2018
c5e6ee3
feature/metafeeds: inlined builder instance creation
kaifabian Oct 21, 2018
fc2a6f6
feature/metafeeds: removed unused 'menu' parameter
kaifabian Oct 21, 2018
4055f62
feature/metafeeds: replaced 'reverse' module with Flask's url_for
kaifabian Oct 21, 2018
54fb7ca
Merge branch 'master' into feature/metafeeds
kaifabian Oct 21, 2018
4132b09
feature/metafeeds: removed unused import
kaifabian Oct 21, 2018
90d3faa
feature/metafeeds: removed unused function parameters
kaifabian Oct 21, 2018
74bf7a4
feature/metafeeds: moved menu feed url generation to views.py
kaifabian Oct 21, 2018
301853f
feature/metafeeds: cleanup debug prints
kaifabian Oct 21, 2018
5d262aa
Merge branch 'feature/metafeeds' of github.com:kaifabian/om-parser-st…
kaifabian Oct 21, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion stw_potsdam/canteen.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

from collections import namedtuple

Canteen = namedtuple('Canteen', ('name', 'street', 'city', 'id', 'chash'))
Canteen = namedtuple('Canteen', ('key', 'name', 'street', 'city', 'id', 'chash'))
3 changes: 2 additions & 1 deletion stw_potsdam/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ def _get_config(filename):

def _parse_canteen(config, canteen_name):
get = partial(config.get, canteen_name)
return Canteen(name=get('name'),
return Canteen(key=canteen_name,
name=get('name'),
street=get('street'),
city=get('city'),
id=get('id'),
Expand Down
21 changes: 13 additions & 8 deletions stw_potsdam/feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from pyopenmensa.feed import LazyBuilder


PRICE_ROLE_MAPPING = {
'student': 'preis_s',
'other': 'preis_g',
Expand Down Expand Up @@ -48,18 +49,22 @@ def _process_day(builder, day):
roles=None)


def _create_builder(canteen):
def render_menu(menu):
builder = LazyBuilder()

for day in _active_days(menu):
_process_day(builder, day)

return builder.toXMLFeed()


def render_meta(canteen, menu_feed_url):
builder = LazyBuilder()

builder.name = canteen.name
builder.address = canteen.street
builder.city = canteen.city
return builder


def render(canteen, menu):
builder = _create_builder(canteen)

for day in _active_days(menu):
_process_day(builder, day)
builder.define(name="full", priority="0", url=menu_feed_url, source=None, dayOfWeek="*", dayOfMonth="*", hour="8-18", minute="0", retry="30 1")

return builder.toXMLFeed()
55 changes: 49 additions & 6 deletions stw_potsdam/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# -*- encoding: utf-8 -*-

from flask import Flask, make_response
import os
import urlparse

from flask import Flask, jsonify, make_response, url_for
from werkzeug.contrib.cache import SimpleCache

import feed
Expand All @@ -10,6 +13,16 @@
CACHE_TIMEOUT = 45 * 60

app = Flask(__name__)
app.url_map.strict_slashes = False

if 'BASE_URL' in os.environ:
base_url = urlparse.urlparse(os.environ.get('BASE_URL'))
if base_url.scheme:
app.config['PREFERRED_URL_SCHEME'] = base_url.scheme
if base_url.netloc:
app.config['SERVER_NAME'] = base_url.netloc
if base_url.path:
app.config['APPLICATION_ROOT'] = base_url.path

cache = SimpleCache()

Expand Down Expand Up @@ -37,23 +50,53 @@ def get_menu(canteen, params):
return menu


def canteen_feed_xml(canteen, menu):
xml = feed.render(canteen, menu)
def _canteen_feed_xml(xml):
response = make_response(xml)
response.mimetype = 'text/xml'
return response


@app.route('/canteen/<canteen_name>')
def canteen_feed(canteen_name):
def canteen_menu_feed_xml(menu):
xml = feed.render_menu(menu)
return _canteen_feed_xml(xml)


def canteen_meta_feed_xml(canteen):
menu_feed_url = url_for('canteen_menu_feed', canteen_name=canteen.key, _external=True)
xml = feed.render_meta(canteen, menu_feed_url)
return _canteen_feed_xml(xml)


@app.route('/canteens/<canteen_name>')
@app.route('/canteens/<canteen_name>/meta')
f4lco marked this conversation as resolved.
Show resolved Hide resolved
def canteen_meta_feed(canteen_name):
config = read_canteen_config()

if canteen_name not in config:
return canteen_not_found(config, canteen_name)

canteen = config[canteen_name]
return canteen_meta_feed_xml(canteen)


@app.route('/canteens/<canteen_name>/menu')
def canteen_menu_feed(canteen_name):
config = read_canteen_config()

if canteen_name not in config:
return canteen_not_found(config, canteen_name)

canteen = config[canteen_name]
menu = get_menu_cached(canteen)
return canteen_feed_xml(canteen, menu)
return canteen_menu_feed_xml(menu)


@app.route('/')
@app.route('/canteens')
def canteen_index():
config = read_canteen_config()
return jsonify({key: url_for('canteen_meta_feed', canteen_name=key, _external=True) for key in config})


@app.route('/health_check')
def health_check():
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<openmensa version="2.1" xmlns="http://openmensa.org/open-mensa-v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://openmensa.org/open-mensa-v2 http://openmensa.org/open-mensa-v2.xsd">
<canteen>
<name>Mensa Griebnitzsee</name>
<address>August-Bebel-Straße 69, Haus 6</address>
<city>14482 Potsdam</city>
<day date="2018-09-19">
<category name="Angebot 1">
<meal>
Expand Down
12 changes: 12 additions & 0 deletions tests/resources/meta_output.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<openmensa version="2.1" xmlns="http://openmensa.org/open-mensa-v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://openmensa.org/open-mensa-v2 http://openmensa.org/open-mensa-v2.xsd">
<canteen>
<name>Mensa Griebnitzsee</name>
<address>August-Bebel-Straße 69, Haus 6</address>
<city>14482 Potsdam</city>
<feed name="full" priority="0">
<schedule dayOfMonth="*" dayOfWeek="*" hour="8-18" minute="0" retry="30 1"/>
<url>canteens/griebnitzsee/menu</url>
</feed>
</canteen>
</openmensa>
26 changes: 21 additions & 5 deletions tests/test_consistency.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,32 @@ def _menu():
return json.load(f)


def _expected_feed():
with io.open(_resource_path('output.xml'), encoding='utf-8') as f:
def _expected_meta_feed():
with io.open(_resource_path('meta_output.xml'), encoding='utf-8') as f:
return f.read()


def test_consistency():
def _expected_menu_feed():
with io.open(_resource_path('menu_output.xml'), encoding='utf-8') as f:
return f.read()


def test_meta_consistency():
canteen = _canteen()
menu = _menu()
menu_feed_url = "canteens/{}/menu".format(canteen.key)

actual = feed.render_meta(canteen, menu_feed_url)

expected = _expected_meta_feed()
assert expected == actual


def test_menu_consistency():
canteen = _canteen()
menu = _menu()

actual = feed.render(canteen, menu)
actual = feed.render_menu(menu)

expected = _expected_feed()
expected = _expected_menu_feed()
assert expected == actual
3 changes: 2 additions & 1 deletion tests/test_retrieval.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ def canteen(request):
@requires_online_api
def test_retrieval(canteen):
menu = download_menu(MenuParams(canteen_id=canteen.id, chash=canteen.chash))
feed.render(canteen, menu)
feed.render_meta(canteen, menu)
feed.render_menu(canteen, menu)