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
63c7919 by Anon Ray at 2013-05-30 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
#
4a1fae7 by Anon Ray at 2013-05-09 106
63c7919 by Anon Ray at 2013-05-30 107
@app.route('/menu/<_id>', methods=['PUT'])
108
def updateMenu(_id):
109
    if flask.request.method == 'PUT':
110
        changedMenu = flask.request.json
bb07f56 by Anon Ray at 2013-09-06 111
        print "changed menu:"
63c7919 by Anon Ray at 2013-05-30 112
        print changedMenu
113
        res = siteMenu.update({'_id': bson.ObjId(_id)}, changedMenu)
114
        print res
18b2795 by Anon Ray at 2013-07-11 115
        return flask.jsonify(status='ok', menu=changedMenu)
63c7919 by Anon Ray at 2013-05-30 116
117
    #elif flask.request.method == 'DELETE':
118
    #    delMenu = flask.request.url
119
    #    print delMenu
120
    #    print _id
121
    #    res = siteMenu.remove({'_id': bson.ObjId(_id)})
122
    #    return flask.jsonify(status='deleted')
123
124
bb07f56 by Anon Ray at 2013-09-06 125
# Basic login for one single admin user whose credentials are in conf.py
126
@app.route('/login', methods=['GET', 'POST'])
127
def login():
128
    error = None
129
    if flask.request.method == 'POST':
130
        print flask.request.form
131
        if flask.request.form['username'] != conf.ADMIN_USERNAME:
132
            error = 'Invalid username'
133
        elif flask.request.form['password'] != conf.ADMIN_PASSWORD:
134
            error = 'Invaid password'
135
        else:
136
            flask.session['logged_in'] = True
137
            flask.session['key'] = conf.SECRET_KEY
138
            flask.flash('You were logged in')
139
            return flask.redirect(flask.url_for('edit'))
140
    return flask.render_template('login.html', error=error)
141
142
@app.route('/logout')
143
def logout():
144
    flask.session.pop('logged_in', None)
145
    flask.flash('You were logged out')
146
    return flask.redirect(flask.url_for('login'))
147
148
@app.route('/robots.txt')
149
@app.route('/crossdomain.xml')
150
def static_from_root():
151
    return flask.send_from_directory(app.static_folder, request.path[1:])
152
153
154
app.config.from_object(conf)
3fd837c by Anon Ray at 2013-08-23 155
2768342 by Anon Ray at 2013-09-06 156
import logging,os
157
from logging import FileHandler
158
159
fil = FileHandler(os.path.join(os.path.dirname(__file__),'logme'),mode='a')
160
fil.setLevel(logging.ERROR)
161
app.logger.addHandler(fil)
162
163
164
63c7919 by Anon Ray at 2013-05-30 165
if __name__ == "__main__":
c94d3ad by Anon Ray at 2013-07-01 166
    app.run(debug=True, host=conf.HOST, port=conf.PORT)
668b408 by Anon Ray at 2013-07-01 167