Commit e35ffb1aa0cc8011de359b34492a00fcd1c60bdd
APIs to list pages
- API to get all pages [/pages]
- API to get specific page by id [/pages/:<id>]
- API to get list of pages by specifying the limit(no of pages) and the
offset [/pages?limit=x&offset=y]
| | | | 3 | # Mouchak Server - | 3 | # Mouchak Server - |
---|
4 | # A Flask Application (http://flask.pocoo.org/) | 4 | # A Flask Application (http://flask.pocoo.org/) |
---|
5 | | 5 | |
---|
| | 6 | ############################################################################### |
---|
| | 7 | # TODO: While moving rendering, routing and controlling to server side, the API |
---|
| | 8 | # has to change and improve. The API should be as follows:- |
---|
| | 9 | # -- |
---|
| | 10 | # For Pages |
---|
| | 11 | # |
---|
| | 12 | # [GET] /mouchak/api/v1a/pages/ - Retrieve list of all the pages |
---|
| | 13 | # [GET] /mouchak/api/v1a/pages/<id> - Retrieve specfic page |
---|
| | 14 | # [POST] /mouchak/api/v1a/pages - Create a new page, with data in payload |
---|
| | 15 | # [PUT] /mouchak/api/v1a/pages/<id> - Update a specific page, with data in |
---|
| | 16 | # payload |
---|
| | 17 | # [DELETE] /mouchak/api/v1a/pages/<id> - Delete a specific page |
---|
| | 18 | # -- |
---|
| | 19 | # |
---|
| | 20 | # For Sitemap (There is only one instance of sitemap in a given website, hence |
---|
| | 21 | # the API is quite simple. |
---|
| | 22 | # |
---|
| | 23 | # [GET] /mouchak/api/v1a/sitemap - Retrieve the sitemap |
---|
| | 24 | # [PUT] /mouchak/api/v1a/sitemap - Update the sitemap |
---|
| | 25 | ############################################################################### |
---|
| | 26 | |
---|
6 | import flask | 27 | import flask |
---|
7 | import pymongo | 28 | import pymongo |
---|
8 | import bson | 29 | import bson |
---|
9 | import conf | 30 | import conf |
---|
10 | import os | 31 | import os |
---|
| | 32 | import json |
---|
11 | from werkzeug import secure_filename | 33 | from werkzeug import secure_filename |
---|
12 | | 34 | |
---|
13 | PLUGIN_UPLOAD_FOLDER = os.path.join(os.path.dirname(os.path.abspath(__file__)) | 35 | PLUGIN_UPLOAD_FOLDER = os.path.join(os.path.dirname(os.path.abspath(__file__)) |
---|
… | | … | |
---|
95 | title=conf.SITE_TITLE) | 95 | title=conf.SITE_TITLE) |
---|
96 | else: | 96 | else: |
---|
97 | return flask.redirect(flask.url_for('login')) | 97 | return flask.redirect(flask.url_for('login')) |
---|
| | 98 | |
---|
| | 99 | |
---|
| | 100 | @app.route('/pages', methods=['GET']) |
---|
| | 101 | def listPages(): |
---|
| | 102 | # if limit and offset are given as query string params, |
---|
| | 103 | # send pages with that limit starting from the offset |
---|
| | 104 | if flask.request.args.get('limit'): |
---|
| | 105 | content = [] |
---|
| | 106 | limit = int(flask.request.args.get('limit')) |
---|
| | 107 | if flask.request.args.get('offset'): |
---|
| | 108 | offset = int(flask.request.args.get('offset')) |
---|
| | 109 | else: |
---|
| | 110 | offset = 0 |
---|
| | 111 | for page in siteContent.find().sort('_id', 1)[offset:offset+limit]: |
---|
| | 112 | del(page['_id']) |
---|
| | 113 | content.append(page) |
---|
| | 114 | print len(content) |
---|
| | 115 | return flask.make_response(json.dumps(content), '200 OK', |
---|
| | 116 | {'Content-Type': 'application/json'}) |
---|
| | 117 | else: |
---|
| | 118 | content = getContent()['content'] |
---|
| | 119 | return flask.make_response(json.dumps(content), '200 OK', |
---|
| | 120 | {'Content-Type': 'application/json'}) |
---|
| | 121 | |
---|
| | 122 | @app.route('/pages/<_id>', methods=['GET']) |
---|
| | 123 | def listPage(_id): |
---|
| | 124 | try: |
---|
| | 125 | page = siteContent.find_one({'_id': bson.ObjId(_id)}) |
---|
| | 126 | del(page['_id']) |
---|
| | 127 | print page |
---|
| | 128 | return flask.jsonify(page) |
---|
| | 129 | except: |
---|
| | 130 | return flask.abort(404) |
---|
98 | | 131 | |
---|
99 | | 132 | |
---|
100 | @app.route('/page', methods=['POST']) | 133 | @app.route('/page', methods=['POST']) |
---|
| | | | 5 | var types; | 5 | var types; |
---|
6 | M.types = types = {}; | 6 | M.types = types = {}; |
---|
7 | | 7 | |
---|
| | 8 | /* The master view of the entire app */ |
---|
8 | var AppView = Backbone.View.extend({ | 9 | var AppView = Backbone.View.extend({ |
---|
9 | el: 'body', | 10 | el: 'body', |
---|
10 | events: { | 11 | events: { |
---|