3579442 by Anon Ray at 2013-05-03 1
#!/usr/bin/python
63c7919 by Anon Ray at 2013-05-30 2
4a1fae7 by Anon Ray at 2013-05-09 3
# Mouchak Server -
4
# A Flask Application (http://flask.pocoo.org/)
5
3579442 by Anon Ray at 2013-05-03 6
import flask
4a1fae7 by Anon Ray at 2013-05-09 7
import pymongo
8
import bson
668b408 by Anon Ray at 2013-07-01 9
import conf
3579442 by Anon Ray at 2013-05-03 10
11
app = flask.Flask(__name__)
12
63c7919 by Anon Ray at 2013-05-30 13
14
4a1fae7 by Anon Ray at 2013-05-09 15
dbClient = pymongo.MongoClient()
668b408 by Anon Ray at 2013-07-01 16
db = dbClient[conf.DB]
63c7919 by Anon Ray at 2013-05-30 17
siteContent = db['content']
18
siteMenu = db['menu']
19
if siteMenu.find_one() == None:
3fd837c by Anon Ray at 2013-08-23 20
    siteMenu.insert({'customMenu': False, 'menuOrder': []})
21
63c7919 by Anon Ray at 2013-05-30 22
4a1fae7 by Anon Ray at 2013-05-09 23
# handy reference to otherwise long name
24
bson.ObjId = bson.objectid.ObjectId
25
63c7919 by Anon Ray at 2013-05-30 26
4a1fae7 by Anon Ray at 2013-05-09 27
def getContent():
28
    content = []
63c7919 by Anon Ray at 2013-05-30 29
    for i in siteContent.find():
4a1fae7 by Anon Ray at 2013-05-09 30
        objId = bson.ObjId(i['_id'])
31
        del(i['_id'])
32
        i['id'] = str(objId)
33
        content.append(i)
63c7919 by Anon Ray at 2013-05-30 34
35
    menu = siteMenu.find_one()
36
    objId = bson.ObjId(menu['_id'])
37
    del(menu['_id'])
38
    menu['id'] = str(objId)
39
40
    return {'content': content, 'menu': menu}
41
4a1fae7 by Anon Ray at 2013-05-09 42
43
3579442 by Anon Ray at 2013-05-03 44
@app.route('/', methods=['GET'])
45
def index():
63c7919 by Anon Ray at 2013-05-30 46
    return flask.render_template('index.html', content=getContent(),
7a60100 by Anon Ray at 2013-08-23 47
                                 title=conf.SITE_TITLE, footer=conf.SITE_FOOTER)
4a1fae7 by Anon Ray at 2013-05-09 48
49
63c7919 by Anon Ray at 2013-05-30 50
@app.route('/edit', methods=['GET'])
4a1fae7 by Anon Ray at 2013-05-09 51
def edit():
bb07f56 by Anon Ray at 2013-09-06 52
    if "logged_in" in flask.session:
53
        flask.session['key'] = conf.SECRET_KEY
54
        return flask.render_template('editor.html', content=getContent(),
55
                                     title=conf.SITE_TITLE)
56
    else:
57
        return flask.redirect(flask.url_for('login'))
4a1fae7 by Anon Ray at 2013-05-09 58
59
63c7919 by Anon Ray at 2013-05-30 60
@app.route('/page', methods=['POST'])
61
def insertPage():
62
    newpage = flask.request.json
63
    print newpage
64
    res = siteContent.insert(newpage)
65
    _id = bson.ObjId(res)
66
    newpage['id'] = str(_id)
67
    del(newpage['_id'])
68
    print newpage
69
    # FIXME: handle errors
70
    return flask.jsonify(status='ok', page=newpage)
71
72
73
@app.route('/page/<_id>', methods=['PUT', 'DELETE'])
74
def updatePage(_id):
4a1fae7 by Anon Ray at 2013-05-09 75
    if flask.request.method == 'PUT':
76
        changedPage = flask.request.json
77
        print changedPage
63c7919 by Anon Ray at 2013-05-30 78
        print '======='
79
        res = siteContent.update({'_id': bson.ObjId(_id)},
4a1fae7 by Anon Ray at 2013-05-09 80
                                changedPage)
81
        print res
63c7919 by Anon Ray at 2013-05-30 82
        if res['err'] == None:
83
            print changedPage
84
            return flask.jsonify(status='ok', page=changedPage)
4a1fae7 by Anon Ray at 2013-05-09 85
86
    elif flask.request.method == 'DELETE':
87
        delPage = flask.request.url
88
        print delPage
89
        print _id
63c7919 by Anon Ray at 2013-05-30 90
        res = siteContent.remove({'_id': bson.ObjId(_id)})
4a1fae7 by Anon Ray at 2013-05-09 91
        print res
63c7919 by Anon Ray at 2013-05-30 92
        if res['err'] == None:
93
            return flask.jsonify(status='ok')
94
        else:
95
            return flask.jsonify(error=res['err'], status='error')
4a1fae7 by Anon Ray at 2013-05-09 96
3579442 by Anon Ray at 2013-05-03 97
40ce989 by Anon Ray at 2013-09-07 98
@app.route('/menu', methods=['POST'])
99
def insertMenu():
100
    #newmenu = flask.request.json
101
    #print newmenu
102
    #res = siteMenu.insert(newmenu)
103
    #print res
104
    #return flask.jsonify(status='success')#, content=getContent())
105
    return '200 OK'
106
4a1fae7 by Anon Ray at 2013-05-09 107
63c7919 by Anon Ray at 2013-05-30 108
@app.route('/menu/<_id>', methods=['PUT'])
109
def updateMenu(_id):
110
    if flask.request.method == 'PUT':
111
        changedMenu = flask.request.json
bb07f56 by Anon Ray at 2013-09-06 112
        print "changed menu:"
63c7919 by Anon Ray at 2013-05-30 113
        print changedMenu
114
        res = siteMenu.update({'_id': bson.ObjId(_id)}, changedMenu)
115
        print res
18b2795 by Anon Ray at 2013-07-11 116
        return flask.jsonify(status='ok', menu=changedMenu)
63c7919 by Anon Ray at 2013-05-30 117
118
    #elif flask.request.method == 'DELETE':
119
    #    delMenu = flask.request.url
120
    #    print delMenu
121
    #    print _id
122
    #    res = siteMenu.remove({'_id': bson.ObjId(_id)})
123
    #    return flask.jsonify(status='deleted')
124
125
bb07f56 by Anon Ray at 2013-09-06 126
# Basic login for one single admin user whose credentials are in conf.py
127
@app.route('/login', methods=['GET', 'POST'])
128
def login():
129
    error = None
130
    if flask.request.method == 'POST':
131
        print flask.request.form
132
        if flask.request.form['username'] != conf.ADMIN_USERNAME:
133
            error = 'Invalid username'
134
        elif flask.request.form['password'] != conf.ADMIN_PASSWORD:
135
            error = 'Invaid password'
136
        else:
137
            flask.session['logged_in'] = True
138
            flask.session['key'] = conf.SECRET_KEY
139
            flask.flash('You were logged in')
140
            return flask.redirect(flask.url_for('edit'))
141
    return flask.render_template('login.html', error=error)
142
143
@app.route('/logout')
144
def logout():
145
    flask.session.pop('logged_in', None)
146
    flask.flash('You were logged out')
147
    return flask.redirect(flask.url_for('login'))
148
149
@app.route('/robots.txt')
150
@app.route('/crossdomain.xml')
151
def static_from_root():
152
    return flask.send_from_directory(app.static_folder, request.path[1:])
153
154
155
app.config.from_object(conf)
3fd837c by Anon Ray at 2013-08-23 156
2768342 by Anon Ray at 2013-09-06 157
import logging,os
158
from logging import FileHandler
159
160
fil = FileHandler(os.path.join(os.path.dirname(__file__),'logme'),mode='a')
161
fil.setLevel(logging.ERROR)
162
app.logger.addHandler(fil)
163
164
165
63c7919 by Anon Ray at 2013-05-30 166
if __name__ == "__main__":
c94d3ad by Anon Ray at 2013-07-01 167
    app.run(debug=True, host=conf.HOST, port=conf.PORT)
668b408 by Anon Ray at 2013-07-01 168