From e35ffb1aa0cc8011de359b34492a00fcd1c60bdd Mon Sep 17 00:00:00 2001 From: Anon Ray Date: Mon, 9 Dec 2013 15:18:10 +0530 Subject: [PATCH] APIs to list pages - API to get all pages [/pages] - API to get specific page by id [/pages/:] - API to get list of pages by specifying the limit(no of pages) and the offset [/pages?limit=x&offset=y] --- mouchak/server.py | 55 ++++++++++++++++++++++++++++++++++++++++++ mouchak/static/js/mouchak.js | 1 + 2 files changed, 56 insertions(+) diff --git a/mouchak/server.py b/mouchak/server.py index 278053a..57c233b 100644 --- a/mouchak/server.py +++ b/mouchak/server.py @@ -3,11 +3,33 @@ # Mouchak Server - # A Flask Application (http://flask.pocoo.org/) +############################################################################### +# TODO: While moving rendering, routing and controlling to server side, the API +# has to change and improve. The API should be as follows:- +# -- +# For Pages +# +# [GET] /mouchak/api/v1a/pages/ - Retrieve list of all the pages +# [GET] /mouchak/api/v1a/pages/ - Retrieve specfic page +# [POST] /mouchak/api/v1a/pages - Create a new page, with data in payload +# [PUT] /mouchak/api/v1a/pages/ - Update a specific page, with data in +# payload +# [DELETE] /mouchak/api/v1a/pages/ - Delete a specific page +# -- +# +# For Sitemap (There is only one instance of sitemap in a given website, hence +# the API is quite simple. +# +# [GET] /mouchak/api/v1a/sitemap - Retrieve the sitemap +# [PUT] /mouchak/api/v1a/sitemap - Update the sitemap +############################################################################### + import flask import pymongo import bson import conf import os +import json from werkzeug import secure_filename PLUGIN_UPLOAD_FOLDER = os.path.join(os.path.dirname(os.path.abspath(__file__)) @@ -75,6 +97,39 @@ def edit(): return flask.redirect(flask.url_for('login')) +@app.route('/pages', methods=['GET']) +def listPages(): + # if limit and offset are given as query string params, + # send pages with that limit starting from the offset + if flask.request.args.get('limit'): + content = [] + limit = int(flask.request.args.get('limit')) + if flask.request.args.get('offset'): + offset = int(flask.request.args.get('offset')) + else: + offset = 0 + for page in siteContent.find().sort('_id', 1)[offset:offset+limit]: + del(page['_id']) + content.append(page) + print len(content) + return flask.make_response(json.dumps(content), '200 OK', + {'Content-Type': 'application/json'}) + else: + content = getContent()['content'] + return flask.make_response(json.dumps(content), '200 OK', + {'Content-Type': 'application/json'}) + +@app.route('/pages/<_id>', methods=['GET']) +def listPage(_id): + try: + page = siteContent.find_one({'_id': bson.ObjId(_id)}) + del(page['_id']) + print page + return flask.jsonify(page) + except: + return flask.abort(404) + + @app.route('/page', methods=['POST']) def insertPage(): newpage = flask.request.json diff --git a/mouchak/static/js/mouchak.js b/mouchak/static/js/mouchak.js index 97eee5a..10164ca 100644 --- a/mouchak/static/js/mouchak.js +++ b/mouchak/static/js/mouchak.js @@ -5,6 +5,7 @@ var types; M.types = types = {}; +/* The master view of the entire app */ var AppView = Backbone.View.extend({ el: 'body', events: { -- 1.7.10.4