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]
  
33# Mouchak Server -
44# A Flask Application (http://flask.pocoo.org/)
55
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
627import flask
728import pymongo
829import bson
930import conf
1031import os
32import json
1133from werkzeug import secure_filename
1234
1335PLUGIN_UPLOAD_FOLDER = os.path.join(os.path.dirname(os.path.abspath(__file__))
9595 title=conf.SITE_TITLE)
9696 else:
9797 return flask.redirect(flask.url_for('login'))
98
99
100@app.route('/pages', methods=['GET'])
101def 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'])
123def 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)
98131
99132
100133@app.route('/page', methods=['POST'])
  
55var types;
66M.types = types = {};
77
8/* The master view of the entire app */
89var AppView = Backbone.View.extend({
910 el: 'body',
1011 events: {