1
#!/usr/bin/python
2
3
# Mouchak Server -
4
# A Flask Application (http://flask.pocoo.org/)
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
27
import flask
28
import pymongo
29
import bson
30
import conf
31
import os
32
import json
33
from werkzeug import secure_filename
34
35
PLUGIN_UPLOAD_FOLDER = os.path.join(os.path.dirname(os.path.abspath(__file__))
36
                                    + '/static/user_plugins')
37
PLUGIN_ALLOWED_EXTENSIONS = set(['js', 'css'])
38
39
FILE_UPLOAD_FOLDER = os.path.join(os.path.dirname(os.path.abspath(__file__)) +
40
                                  '/static/uploads')
41
42
app = flask.Flask(__name__)
43
44
45
46
dbClient = pymongo.MongoClient()
47
db = dbClient[conf.DB]
48
siteContent = db['content']
49
siteMenu = db['menu']
50
if siteMenu.find_one() == None:
51
    siteMenu.insert({'customMenu': False, 'menuOrder': [], 'html': ''})
52
53
54
# handy reference to otherwise long name
55
bson.ObjId = bson.objectid.ObjectId
56
57
58
def getContent():
59
    content = []
60
    for i in siteContent.find():
61
        objId = bson.ObjId(i['_id'])
62
        del(i['_id'])
63
        i['id'] = str(objId)
64
        content.append(i)
65
66
    menu = siteMenu.find_one()
67
    objId = bson.ObjId(menu['_id'])
68
    del(menu['_id'])
69
    menu['id'] = str(objId)
70
71
    return {'content': content, 'menu': menu}
72
73
74
def allowed_file(filename):
75
    return '.' in filename and \
76
            filename.rsplit('.', 1)[1] in PLUGIN_ALLOWED_EXTENSIONS
77
78
79
@app.errorhandler(404)
80
def pageNotFound(e):
81
    return flask.render_template('404.html'), 404
82
83
84
@app.route('/', methods=['GET'])
85
def index():
86
    return flask.render_template('index.html', content=getContent(),
87
                                 title=conf.SITE_TITLE, footer=conf.SITE_FOOTER)
88
89
90
@app.route('/edit', methods=['GET'])
91
def edit():
92
    if "logged_in" in flask.session:
93
        flask.session['key'] = conf.SECRET_KEY
94
        return flask.render_template('editor.html', content=getContent(),
95
                                     title=conf.SITE_TITLE)
96
    else:
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)
131
132
133
@app.route('/page', methods=['POST'])
134
def insertPage():
135
    newpage = flask.request.json
136
    print newpage
137
    res = siteContent.insert(newpage)
138
    _id = bson.ObjId(res)
139
    newpage['id'] = str(_id)
140
    del(newpage['_id'])
141
    print newpage
142
    # FIXME: handle errors
143
    #return flask.jsonify(status='ok', page=newpage)
144
    return flask.jsonify(newpage)
145
146
147
@app.route('/page/<_id>', methods=['PUT', 'DELETE'])
148
def updatePage(_id):
149
    if flask.request.method == 'PUT':
150
        changedPage = flask.request.json
151
        print changedPage
152
        print '======='
153
        res = siteContent.update({'_id': bson.ObjId(_id)},
154
                                changedPage)
155
        print res
156
        if res['err'] == None:
157
            print changedPage
158
            #return flask.jsonify(status='ok', page=changedPage)
159
            return flask.jsonify(changedPage)
160
161
    elif flask.request.method == 'DELETE':
162
        delPage = flask.request.url
163
        print delPage
164
        print _id
165
        res = siteContent.remove({'_id': bson.ObjId(_id)})
166
        print res
167
        if res['err'] == None:
168
            return flask.jsonify(status='ok')
169
        else:
170
            return flask.jsonify(error=res['err'], status='error')
171
172
173
@app.route('/menu', methods=['POST'])
174
def insertMenu():
175
    #newmenu = flask.request.json
176
    #print newmenu
177
    #res = siteMenu.insert(newmenu)
178
    #print res
179
    #return flask.jsonify(status='success')#, content=getContent())
180
    return '200 OK'
181
182
183
@app.route('/menu/<_id>', methods=['PUT'])
184
def updateMenu(_id):
185
    if flask.request.method == 'PUT':
186
        changedMenu = flask.request.json
187
        print "changed menu:"
188
        print changedMenu
189
        res = siteMenu.update({'_id': bson.ObjId(_id)}, changedMenu)
190
        print res
191
        #return flask.jsonify(status='ok', menu=changedMenu)
192
        return flask.jsonify(changedMenu)
193
194
    #elif flask.request.method == 'DELETE':
195
    #    delMenu = flask.request.url
196
    #    print delMenu
197
    #    print _id
198
    #    res = siteMenu.remove({'_id': bson.ObjId(_id)})
199
    #    return flask.jsonify(status='deleted')
200
201
202
# Basic login for one single admin user whose credentials are in conf.py
203
@app.route('/login', methods=['GET', 'POST'])
204
def login():
205
    error = None
206
    if flask.request.method == 'POST':
207
        print flask.request.form
208
        if flask.request.form['username'] != conf.ADMIN_USERNAME:
209
            error = 'Invalid username'
210
        elif flask.request.form['password'] != conf.ADMIN_PASSWORD:
211
            error = 'Invaid password'
212
        else:
213
            flask.session['logged_in'] = True
214
            flask.session['key'] = conf.SECRET_KEY
215
            flask.flash('You were logged in')
216
            return flask.redirect(flask.url_for('edit'))
217
    return flask.render_template('login.html', error=error)
218
219
@app.route('/logout')
220
def logout():
221
    flask.session.pop('logged_in', None)
222
    flask.flash('You were logged out')
223
    return flask.redirect(flask.url_for('login'))
224
225
226
#TODO: refactor these code to classes
227
#TODO: find out if this is a good method for saving plugins..
228
@app.route('/static/user_plugins/<filename>', methods=['POST'])
229
def savePlugin(filename):
230
    if flask.request.method == 'POST':
231
        if filename and allowed_file(filename):
232
            data = flask.request.form['code']
233
            filename = secure_filename(filename)
234
            fh = open(os.path.join(PLUGIN_UPLOAD_FOLDER + '/' + filename), 'w')
235
            fh.write(data)
236
            fh.close()
237
            return flask.jsonify(saved = True)
238
239
#TODO: find out if this is a good method for uploading plugins..
240
@app.route('/upload/plugin', methods=['POST'])
241
def uploadPlugin():
242
    if flask.request.method == 'POST':
243
        print flask.request.files
244
        file = flask.request.files['plugin-file']
245
        if file and allowed_file(file.filename):
246
            filename = secure_filename(file.filename)
247
            file.save(os.path.join(app.config['PLUGIN_UPLOAD_FOLDER'],
248
                                   filename))
249
250
            #return flask.redirect(flask.url_for('uploaded_file',
251
            #            filename=filename))
252
            return flask.jsonify(uploaded = True,
253
                                 path=flask.url_for('static', filename =
254
                                                    'user_plugins/'+ filename))
255
256
@app.route('/robots.txt')
257
@app.route('/crossdomain.xml')
258
def static_from_root():
259
    return flask.send_from_directory(app.static_folder, request.path[1:])
260
261
262
app.config.from_object(conf)
263
app.config['PLUGIN_UPLOAD_FOLDER'] = PLUGIN_UPLOAD_FOLDER
264
265
import logging,os
266
from logging import FileHandler
267
268
fil = FileHandler(os.path.join(os.path.dirname(__file__),'logme'),mode='a')
269
fil.setLevel(logging.ERROR)
270
app.logger.addHandler(fil)
271
272
273
274
if __name__ == "__main__":
275
    app.run(debug=True, host=conf.HOST, port=conf.PORT)