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]
  • Diff rendering mode:
  • inline
  • side by side

mouchak/server.py

3# Mouchak Server -3# Mouchak Server -
4# A Flask Application (http://flask.pocoo.org/)4# 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
6import flask27import flask
7import pymongo28import pymongo
8import bson29import bson
9import conf30import conf
10import os31import os
32import json
11from werkzeug import secure_filename33from werkzeug import secure_filename
1234
13PLUGIN_UPLOAD_FOLDER = os.path.join(os.path.dirname(os.path.abspath(__file__))35PLUGIN_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'])
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
100@app.route('/page', methods=['POST'])133@app.route('/page', methods=['POST'])

mouchak/static/js/mouchak.js

5var types;5var types;
6M.types = types = {};6M.types = types = {};
77
8/* The master view of the entire app */
8var AppView = Backbone.View.extend({9var AppView = Backbone.View.extend({
9 el: 'body',10 el: 'body',
10 events: {11 events: {