|
6 | 6 |
|
7 | 7 | from AngularFlask import app |
8 | 8 |
|
9 | | -# special file handlers |
10 | | -@app.route('/favicon.ico') |
11 | | -def favicon(): |
12 | | - return send_from_directory(os.path.join(app.root_path, 'static'), 'img/favicon.ico') |
| 9 | +### |
| 10 | +# controllers/routing for API endpoints |
| 11 | +# (auto-generated from the models listed in app.config['API_MODELS']) |
| 12 | +### |
| 13 | +from AngularFlask.core import api_manager |
| 14 | +from AngularFlask.models import * |
13 | 15 |
|
14 | | -# 404 error handler |
15 | | -@app.errorhandler(404) |
16 | | -def page_not_found(e): |
17 | | - return render_template('404.html'), 404 |
| 16 | +api_models = app.config['API_MODELS'] |
| 17 | +for model_name in api_models: |
| 18 | + model_class = api_models[model_name] |
| 19 | + api_manager.create_api(model_class, methods=['GET', 'POST']) |
| 20 | + |
| 21 | +session = api_manager.session |
18 | 22 |
|
19 | | -# basic page url handler |
| 23 | +### |
| 24 | +# controllers/routing for basic pages (pass routing onto the Angular app) |
| 25 | +### |
20 | 26 | @app.route('/') |
21 | 27 | @app.route('/about') |
| 28 | +@app.route('/blog') |
22 | 29 | def basic_pages(**kwargs): |
23 | 30 | return make_response(open('AngularFlask/templates/index.html').read()) |
24 | 31 |
|
25 | | -# API |
26 | | -from AngularFlask.core import api_manager |
27 | | -from AngularFlask.models import Post |
28 | | - |
29 | | -session = api_manager.session |
30 | | - |
31 | | -api_manager.create_api(Post, methods=['GET', 'POST']) |
32 | | - |
33 | | -# RESTful page url handler |
| 32 | +### |
| 33 | +# controllers/routing for CRUD-style endpoints, or ones that refer to a particular resource or collection |
| 34 | +# (pass routing onto the Angular app only if the corresponding resource exists in the db) |
| 35 | +### |
34 | 36 | from sqlalchemy.sql import exists |
35 | 37 |
|
36 | | -supported_models = ['post'] |
| 38 | +crud_url_models = app.config['CRUD_URL_MODELS'] |
37 | 39 |
|
38 | | -@app.route('/<model_name>') |
| 40 | +@app.route('/<model_name>/') |
39 | 41 | @app.route('/<model_name>/<item_id>') |
40 | 42 | def rest_pages(model_name, item_id=None): |
41 | | - if model_name in supported_models: |
42 | | - model = eval(model_name.capitalize()) |
43 | | - if session.query(exists().where(model.id == item_id)).scalar(): |
| 43 | + if model_name in crud_url_models: |
| 44 | + model_class = crud_url_models[model_name] |
| 45 | + if item_id is None or session.query(exists().where(model_class.id == item_id)).scalar(): |
44 | 46 | return make_response(open('AngularFlask/templates/index.html').read()) |
45 | 47 | abort(404) |
46 | 48 |
|
| 49 | +### |
| 50 | +# special file handlers |
| 51 | +## |
| 52 | +@app.route('/favicon.ico') |
| 53 | +def favicon(): |
| 54 | + return send_from_directory(os.path.join(app.root_path, 'static'), 'img/favicon.ico') |
| 55 | + |
| 56 | +### |
| 57 | +# error handlers |
| 58 | +## |
| 59 | +@app.errorhandler(404) |
| 60 | +def page_not_found(e): |
| 61 | + return render_template('404.html'), 404 |
| 62 | + |
| 63 | + |
47 | 64 |
|
0 commit comments