--- a/swtr.py +++ b/swtr.py @@ -53,6 +53,7 @@ def validateSweet(payload): for i in payload: + print i try: if len(i['who']) and len(i['what']) and len(i['where']) and\ len(i['how']) and len(i['created']): @@ -96,7 +97,8 @@ def show_entries(): print 'request:' print request.method - res = g.collection.find().sort('_id',direction=-1) + print session + res = g.collection.find().sort('_id', direction=-1) entries = make_list(res) return render_template('show_entries.html', entries=entries) @@ -129,12 +131,15 @@ try: payload = [{'who': request.form['who'], 'what': request.form['what'], 'where': request.form['where'], 'how': request.form['how']}] + print payload except: try: payload = request.json except: payload = json.loads(request.data) + if type(payload) is dict: + payload = [payload] valid = validateSweet(payload) if not valid: @@ -161,12 +166,34 @@ def login(): error = None if request.method == 'POST': + db = g.connection[app.config['DATABASE']] + collection = db['sweet_users'] + for i in collection.find(): + if i['user'] == request.form['username'] and i['key'] == request.form['password']: + session['logged_in'] = True + session['username'] = request.form['username'] + flash('You were logged in') + res = g.collection.find({'who': request.form['username']}) + + if res.count() < 1: + return render_template('show_entries.html', entries=[]) + swt_list = [] + for swt in res: + _id = swt['_id'] + del(swt['_id']) + swt['id'] = str(_id) + swt_list.append(swt) + return render_template('show_entries.html', entries=swt_list) + else: + pass + if request.form['username'] != app.config['USERNAME']: error = 'Invalid username' elif request.form['password'] != app.config['PASSWORD']: error = 'Invalid password' else: session['logged_in'] = True + session['isAdmin'] = True flash('You were logged in') return redirect(url_for('show_entries')) return render_template('login.html', error=error) @@ -234,29 +261,39 @@ -@app.route('/posts/',methods=['GET']) +@app.route('/posts/', methods=['GET', 'POST']) def show_specific_entry(post_id): - try: - res = g.collection.find({'_id':ObjectId(post_id)}) - if(res.count() > 0): - #entries = make_list(res) - entries = [] - for i in res: - _id = i['_id'] - del(i['_id']) - i['id'] = _id - entries.append(i) - return render_template('show_posts.html', entries=entries, str=str) - else: + if request.method == 'GET': + try: + res = g.collection.find({'_id': ObjectId(post_id)}) + if(res.count() > 0): + #entries = make_list(res) + entries = [] + for i in res: + _id = i['_id'] + del(i['_id']) + i['id'] = _id + entries.append(i) + return render_template('show_posts.html', entries=entries, str=str) + else: + abort(404) + except InvalidId: abort(404) - except InvalidId: - abort(404) + else: + how = {} + for item in request.form: + how[item] = request.form[item] + try: + g.collection.update({'_id': ObjectId(post_id)}, {"$set":{'how':how}}) + response = make_response() + return response + except: + abort(404) - @app.route('/posts/delete/', methods=['POST']) def delete_post(): try: - g.collection.remove({'_id':ObjectId(request.form['post_id'])}) + g.collection.remove({'_id': ObjectId(request.form['post_id'])}) return jsonify(status='ok') except: abort(500) @@ -264,6 +301,8 @@ @app.route('/logout') def logout(): session.pop('logged_in', None) + session.pop('username', None) + session.pop('isAdmin', None) flash('You were logged out') return redirect(url_for('show_entries')) @@ -312,7 +351,7 @@ return render_template("users.html", users=users) -@app.route('/authenticate', methods=['POST','GET']) +@app.route('/authenticate', methods=['POST', 'GET']) def authenticate(): if request.method == "POST": response = make_response() --- a/templates/layout.html +++ b/templates/layout.html @@ -3,7 +3,10 @@ SWeeT Store - + + + + {% block head %}{% endblock %} @@ -26,7 +29,7 @@ {% block body %}{% endblock %} - + --- a/templates/login.html +++ b/templates/login.html @@ -2,14 +2,24 @@ {% block body %}

Login

{% if error %}

Error: {{ error }}{% endif %} -

-
-
Username: -
-
Password: -
-
-
+ +
+ +
+ +
+
+
+ +
+ +
+
+
+
+ +
+
{% endblock %} --- a/templates/show_entries.html +++ b/templates/show_entries.html @@ -18,7 +18,7 @@ created: {{entry.created }} UTC {% endif %} - + {% endif %} --- a/templates/show_posts.html +++ b/templates/show_posts.html @@ -13,6 +13,15 @@ } return false; } + function editPost(entry) { + t = _.template($("#editTemplate").html()); + $(".modal-body").append(t(items=entry)); + $("input").each(function(item) { + $(this).val(entry[$(this).attr('for')]); + + }, this); + $("#editModal").modal(); + } + + + + {% endblock %}