From bb07f5687c7b2aeba3759304842b7f05bb3ed807 Mon Sep 17 00:00:00 2001 From: Anon Ray Date: Fri, 6 Sep 2013 22:05:24 +0530 Subject: [PATCH] Add basic admin login Admin login needed to edit. Basic admin login in place. Configure your admin username and password in the conf file. --- mouchak/classes/field.py | 9 +++--- mouchak/sampleConf.py | 3 ++ mouchak/server.py | 39 +++++++++++++++++++++++-- mouchak/templates/editor.html | 10 +++++-- mouchak/templates/login.html | 65 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 118 insertions(+), 8 deletions(-) create mode 100644 mouchak/templates/login.html diff --git a/mouchak/classes/field.py b/mouchak/classes/field.py index cb2391c..6cf66e0 100644 --- a/mouchak/classes/field.py +++ b/mouchak/classes/field.py @@ -8,7 +8,8 @@ class Field(object): def __init__(self): """@todo: to be defined1 """ - self.id = object.id - self.tags = object.tags - self.type = object.type - self.body = object.body + self.id = object.id + self.tags = object.tags + self.type = object.type + self.body = object.body + diff --git a/mouchak/sampleConf.py b/mouchak/sampleConf.py index d16e391..885c641 100644 --- a/mouchak/sampleConf.py +++ b/mouchak/sampleConf.py @@ -7,3 +7,6 @@ SITE_FOOTER = ''' ''' HOST = '0.0.0.0' PORT = 5000 +SECRET_KEY = 'a-uuid-string-see-python-uuid' +ADMIN_USERNAME = 'youradminusername' +ADMIN_PASSWORD = 'youradminpassword' diff --git a/mouchak/server.py b/mouchak/server.py index e9b584f..7d4a576 100644 --- a/mouchak/server.py +++ b/mouchak/server.py @@ -49,8 +49,12 @@ def index(): @app.route('/edit', methods=['GET']) def edit(): - return flask.render_template('editor.html', content=getContent(), - title=conf.SITE_TITLE) + if "logged_in" in flask.session: + flask.session['key'] = conf.SECRET_KEY + return flask.render_template('editor.html', content=getContent(), + title=conf.SITE_TITLE) + else: + return flask.redirect(flask.url_for('login')) @app.route('/page', methods=['POST']) @@ -104,6 +108,7 @@ def updatePage(_id): def updateMenu(_id): if flask.request.method == 'PUT': changedMenu = flask.request.json + print "changed menu:" print changedMenu res = siteMenu.update({'_id': bson.ObjId(_id)}, changedMenu) print res @@ -117,6 +122,36 @@ def updateMenu(_id): # return flask.jsonify(status='deleted') +# Basic login for one single admin user whose credentials are in conf.py +@app.route('/login', methods=['GET', 'POST']) +def login(): + error = None + if flask.request.method == 'POST': + print flask.request.form + if flask.request.form['username'] != conf.ADMIN_USERNAME: + error = 'Invalid username' + elif flask.request.form['password'] != conf.ADMIN_PASSWORD: + error = 'Invaid password' + else: + flask.session['logged_in'] = True + flask.session['key'] = conf.SECRET_KEY + flask.flash('You were logged in') + return flask.redirect(flask.url_for('edit')) + return flask.render_template('login.html', error=error) + +@app.route('/logout') +def logout(): + flask.session.pop('logged_in', None) + flask.flash('You were logged out') + return flask.redirect(flask.url_for('login')) + +@app.route('/robots.txt') +@app.route('/crossdomain.xml') +def static_from_root(): + return flask.send_from_directory(app.static_folder, request.path[1:]) + + +app.config.from_object(conf) if __name__ == "__main__": app.run(debug=True, host=conf.HOST, port=conf.PORT) diff --git a/mouchak/templates/editor.html b/mouchak/templates/editor.html index ff63829..5612eae 100644 --- a/mouchak/templates/editor.html +++ b/mouchak/templates/editor.html @@ -23,8 +23,14 @@
- -
+ +
+
diff --git a/mouchak/templates/login.html b/mouchak/templates/login.html new file mode 100644 index 0000000..ea20ada --- /dev/null +++ b/mouchak/templates/login.html @@ -0,0 +1,65 @@ + + + + + + + + + Mouchak | Login + + + + + + + + + + + + +
+
+ {% if error %} +
Error: {{ error }}
+ {% endif %} +

Mouchak Login

+

Please enter admin credentials to login

+
+
+ +
+ +
+
+
+ +
+ +
+
+
+
+ + +
+
+
+
+
+
+ + + -- 1.7.10.4