Set version number to 0.4.1.
[philipp/winterrodeln/wrfeed.git] / wrfeed / wrfeed.py
1 # http://flask.pocoo.org/
2 # FLASK_APP=wrfeed FLASK_DEBUG=1 WRFEED_SETTINGS=development.cfg python3 -m flask run -p 5002
3 # FLASK_APP=wrfeed WRFEED_SETTINGS=production.cfg python3 -m flask run
4 from flask import Flask, send_from_directory, abort
5 from flask_sqlalchemy import SQLAlchemy
6 from wrfeed.berichte import create_feed
7
8 app = Flask(__name__)
9 app.config.from_envvar('WRFEED_SETTINGS')
10 db = SQLAlchemy(app)
11 db.reflect()
12
13
14 @app.route("/")
15 def hello():
16     return send_from_directory('templates', 'index.html')  # actually, this is no template
17
18
19 @app.route("/berichte/alle")
20 def alle():
21     """Handles URLs like
22     http://127.0.0.1:5000/berichte/alle
23     http://www.winterrodeln.org/feed/berichte/alle
24     """
25     return app.response_class(create_feed(db, app.config['FEEDENTRYLIMIT']), mimetype='application/atom+xml')
26
27
28 @app.route("/berichte/bahn/<bahn>")
29 def bahn(bahn):
30     """Handles URLs like
31     http://127.0.0.1:5000/berichte/bahn/kemater_alm
32     http://www.winterrodeln.org/feed/berichte/bahn/kemater_alm
33     """
34     return app.response_class(create_feed(db, app.config['FEEDENTRYLIMIT'], page_title=bahn),
35                               mimetype='application/atom+xml')
36
37
38 @app.route("/berichte/bahnen/<bahnen>")
39 def bahnen(bahnen):
40     """Handles URLs like
41     http://127.0.0.1:5000/berichte/bahnen/5+280+251
42     http://www.winterrodeln.org/feed/berichte/bahnen/5+280+251
43     """
44     page_ids = bahnen.split('+')
45     try:
46         page_ids = [int(page_id) for page_id in page_ids]
47     except ValueError:
48         abort(400)  # bad request
49     return app.response_class(create_feed(db, app.config['FEEDENTRYLIMIT'], page_ids=page_ids),
50                               mimetype='application/atom+xml')
51
52
53 @app.route("/berichte/region/<region>")
54 def region(region):
55     """Handles URLs like
56     http://www.winterrodeln.org/feed/berichte/region/osttirol
57     """
58     return app.response_class(create_feed(db, app.config['FEEDENTRYLIMIT'], region_name=region),
59                               mimetype='application/atom+xml')