Commit c09252876af8c6fb450c80994079d48454280acf
Add Last Updated functionality for KHMDL
| | | | 31 | import os | 31 | import os |
---|
32 | import json | 32 | import json |
---|
33 | from werkzeug import secure_filename | 33 | from werkzeug import secure_filename |
---|
| | 34 | from datetime import datetime |
---|
34 | | 35 | |
---|
35 | PLUGIN_UPLOAD_FOLDER = os.path.join(os.path.dirname(os.path.abspath(__file__)) | 36 | PLUGIN_UPLOAD_FOLDER = os.path.join(os.path.dirname(os.path.abspath(__file__)) |
---|
36 | + '/static/user_plugins') | 37 | + '/static/user_plugins') |
---|
… | | … | |
---|
52 | db = dbClient[conf.DB] | 52 | db = dbClient[conf.DB] |
---|
53 | siteContent = db['content'] | 53 | siteContent = db['content'] |
---|
54 | siteMenu = db['menu'] | 54 | siteMenu = db['menu'] |
---|
| | 55 | lastUpdated = db['last_updated'] |
---|
55 | analytics_coll = db['analytics'] | 56 | analytics_coll = db['analytics'] |
---|
56 | | 57 | |
---|
57 | if siteMenu.find_one() == None: | 58 | if siteMenu.find_one() == None: |
---|
… | | … | |
---|
94 | del(header['_id']) | 94 | del(header['_id']) |
---|
95 | header['id'] = str(objId) | 95 | header['id'] = str(objId) |
---|
96 | | 96 | |
---|
| | 97 | last_updated = lastUpdated.find_one() |
---|
| | 98 | last_updated_date = last_updated['last_updated'] |
---|
| | 99 | |
---|
97 | return {'content': content, 'menu': menu, 'footer': footer, 'header': | 100 | return {'content': content, 'menu': menu, 'footer': footer, 'header': |
---|
98 | header} | | header} |
---|
| | 101 | header, 'last_updated': last_updated_date.isoformat()} | 99 | | 102 | |
---|
100 | | 103 | |
---|
101 | def allowed_file(filename): | 104 | def allowed_file(filename): |
---|
… | | … | |
---|
264 | def login(): | 264 | def login(): |
---|
265 | error = None | 265 | error = None |
---|
266 | if flask.request.method == 'POST': | 266 | if flask.request.method == 'POST': |
---|
267 | print flask.request.form | | print flask.request.form |
---|
268 | if flask.request.form['username'] != conf.ADMIN_USERNAME: | 267 | if flask.request.form['username'] != conf.ADMIN_USERNAME: |
---|
269 | error = 'Invalid username' | 268 | error = 'Invalid username' |
---|
270 | elif flask.request.form['password'] != conf.ADMIN_PASSWORD: | 269 | elif flask.request.form['password'] != conf.ADMIN_PASSWORD: |
---|
… | | … | |
---|
361 | if data['type'] == 'pageview': | 361 | if data['type'] == 'pageview': |
---|
362 | data['page'] = flask.request.form['page'] | 362 | data['page'] = flask.request.form['page'] |
---|
363 | | 363 | |
---|
364 | print data | | print data |
---|
365 | analytics_coll.insert(data) | 364 | analytics_coll.insert(data) |
---|
366 | total_hits = analytics_coll.find({'type': 'pageview'}).count() | 365 | total_hits = analytics_coll.find({'type': 'pageview'}).count() |
---|
367 | | 366 | |
---|
368 | return flask.jsonify(total_hits=total_hits) | 367 | return flask.jsonify(total_hits=total_hits) |
---|
| | 368 | |
---|
| | 369 | @app.route('/updated-content', methods=['POST']) |
---|
| | 370 | def updateLastUpdated(): |
---|
| | 371 | if "logged_in" not in flask.session: |
---|
| | 372 | abort(403) |
---|
| | 373 | |
---|
| | 374 | response = flask.make_response() |
---|
| | 375 | last_updated = lastUpdated.find_one() |
---|
| | 376 | if last_updated == None: |
---|
| | 377 | lastUpdated.insert({'last_updated': datetime.utcnow()}) |
---|
| | 378 | else: |
---|
| | 379 | lastUpdated.update({'_id': last_updated['_id']}, |
---|
| | 380 | {'last_updated': datetime.utcnow()}) |
---|
| | 381 | |
---|
| | 382 | response.status_code = 200 |
---|
| | 383 | return response |
---|
369 | | 384 | |
---|
370 | | 385 | |
---|
371 | @app.route('/robots.txt') | 386 | @app.route('/robots.txt') |
---|
| | | | 12 | 'click #menu-config': 'showMenu', | 12 | 'click #menu-config': 'showMenu', |
---|
13 | 'click #footer-config': 'showFooterConfig', | 13 | 'click #footer-config': 'showFooterConfig', |
---|
14 | 'click #header-config': 'showHeaderConfig', | 14 | 'click #header-config': 'showHeaderConfig', |
---|
15 | 'click #uploads': 'uploads' | | 'click #uploads': 'uploads' |
---|
| | 15 | 'click #uploads': 'uploads', | | | 16 | 'click #last-updated': 'updateLastUpdated' |
---|
16 | }, | 17 | }, |
---|
17 | initialize: function() { | 18 | initialize: function() { |
---|
18 | _.bindAll.apply(_, [this].concat(_.functions(this))); | 19 | _.bindAll.apply(_, [this].concat(_.functions(this))); |
---|
… | | … | |
---|
109 | event.preventDefault(); | 109 | event.preventDefault(); |
---|
110 | this.uploadview.render(); | 110 | this.uploadview.render(); |
---|
111 | return false; | 111 | return false; |
---|
| | 112 | }, |
---|
| | 113 | // update the last updated time of the website |
---|
| | 114 | // adding for KHMDL, because they want a last updated in their footer |
---|
| | 115 | updateLastUpdated: function() { |
---|
| | 116 | $.ajax({ |
---|
| | 117 | url: M.lastUpdatedURL(), |
---|
| | 118 | type: 'POST', |
---|
| | 119 | data: {}, |
---|
| | 120 | success: function(data) { |
---|
| | 121 | console.log('last updated updated'); |
---|
| | 122 | }, |
---|
| | 123 | error: function(jqxhr, error, status_text) { |
---|
| | 124 | console.log('Unable to update last updated'); |
---|
| | 125 | console.log(error, status_text); |
---|
| | 126 | } |
---|
| | 127 | }); |
---|
112 | }, | 128 | }, |
---|
113 | // validate the page list with menu order list | 129 | // validate the page list with menu order list |
---|
114 | validate: function() { | 130 | validate: function() { |
---|
| | | | 42 | }, | 42 | }, |
---|
43 | updatePageViewCounter: function(data) { | 43 | updatePageViewCounter: function(data) { |
---|
44 | this.$pageview_counter.html(data.total_hits); | 44 | this.$pageview_counter.html(data.total_hits); |
---|
| | 45 | }, |
---|
| | 46 | renderLastUpdated: function() { |
---|
| | 47 | var date = new Date(M.site_content.last_updated); |
---|
| | 48 | $('#last-updated').html(date.toString()); |
---|
45 | } | 49 | } |
---|
46 | }); | 50 | }); |
---|
47 | | 51 | |
---|
… | | … | |
---|
215 | | 215 | |
---|
216 | M.appView = new AppView(); | 216 | M.appView = new AppView(); |
---|
217 | M.appView.render(); | 217 | M.appView.render(); |
---|
| | 218 | M.appView.renderLastUpdated(); |
---|
218 | | 219 | |
---|
219 | var app_router = new AppRouter(); | 220 | var app_router = new AppRouter(); |
---|
220 | Backbone.history.start(); | 221 | Backbone.history.start(); |
---|
| | | | 27 | M.PageURL = function() { return "{{ url_for('insertPage') }}"; }; | 27 | M.PageURL = function() { return "{{ url_for('insertPage') }}"; }; |
---|
28 | M.PluginUploadURL = function() { return "{{ url_for('uploadPlugin') }}"; }; | 28 | M.PluginUploadURL = function() { return "{{ url_for('uploadPlugin') }}"; }; |
---|
29 | M.UploadsURL = function() { return "{{ url_for('static', filename='uploads/') }}"; }; | 29 | M.UploadsURL = function() { return "{{ url_for('static', filename='uploads/') }}"; }; |
---|
| | 30 | M.lastUpdatedURL = function() { return "{{ url_for('updateLastUpdated') }}"; }; |
---|
30 | M.site_content = {{ content|tojson|safe }}; | 31 | M.site_content = {{ content|tojson|safe }}; |
---|
31 | window.onload = function() { | 32 | window.onload = function() { |
---|
32 | M.editor.init(); | 33 | M.editor.init(); |
---|
… | | … | |
---|
146 | <p> <a href="#" id="footer-config"> Footer </a> </p> | 146 | <p> <a href="#" id="footer-config"> Footer </a> </p> |
---|
147 | <p> <a href="#" id="menu-config"> Navigation Menu </a> </p> | 147 | <p> <a href="#" id="menu-config"> Navigation Menu </a> </p> |
---|
148 | <p> <a href="#" id="uploads"> Uploads </a> </p> | 148 | <p> <a href="#" id="uploads"> Uploads </a> </p> |
---|
| | 149 | <p> <a href="#" class="btn btn-default" id="last-updated"> |
---|
| | 150 | Content updated |
---|
| | 151 | </a> </p> |
---|
149 | <p><a href="{{ url_for('index') }}"> Go to site </a></p> | 152 | <p><a href="{{ url_for('index') }}"> Go to site </a></p> |
---|
150 | </div> | 153 | </div> |
---|
151 | </script> | 154 | </script> |
---|