# HG changeset patch # User Dennis Fink # Date 2017-10-08 17:06:30 # Node ID 1438a15c1120f62fc0b994f736243054206debd9 # Parent 7264fcee6f30f2d1a558ea794ef15a4b10ec99c5 Add spaceapi v14 support diff --git a/spaceapi/__init__.py b/spaceapi/__init__.py --- a/spaceapi/__init__.py +++ b/spaceapi/__init__.py @@ -99,7 +99,8 @@ def create_app(): from .sensors import sensors_views app.register_blueprint(sensors_views, url_prefix='/sensors') - from .utils import ActiveStatus + from .utils import ActiveStatus, ActiveStatusv14 ActiveStatus().reload() + ActiveStatusv14().reload() return app diff --git a/spaceapi/utils.py b/spaceapi/utils.py --- a/spaceapi/utils.py +++ b/spaceapi/utils.py @@ -12,11 +12,20 @@ import tweepy default_json_file = os.path.abspath('default.json') last_state_file = os.path.abspath('laststate.json') +default_json_file_v14 = os.path.abspath('default_v14.json') +last_state_file_v14 = os.path.abspath('laststate_v14.json') + if not os.path.exists(default_json_file): raise RuntimeError('default.json does not exists!') elif not os.path.isfile(default_json_file): raise RuntimeError('default.json is not a file!') +if not os.path.exists(default_json_file_v14): + raise RuntimeError('default_v14.json does not exists!') +elif not os.path.isfile(default_json_file): + raise RuntimeError('default_v14.json is not a file!') + + possible_open_tweets = ( 'The space is now open!', 'The space is open! Come in and hack something!', @@ -67,13 +76,17 @@ class Singleton: class ActiveStatus(Singleton, dict): + def __init__(self): + self.default_json_file = default_json_file + self.last_state_file = last_state_file + def reload(self): - with open(default_json_file, encoding='utf-8') as f: + with open(self.default_json_file, encoding='utf-8') as f: self.update(json.load(f)) - if os.path.exists(last_state_file) and os.path.isfile(last_state_file): - with open(last_state_file, encoding='utf-8') as f: + if os.path.exists(self.last_state_file) and os.path.isfile(self.last_state_file): + with open(self.last_state_file, encoding='utf-8') as f: last_state_json = json.load(f) self['state'] = last_state_json['state'] @@ -81,7 +94,7 @@ class ActiveStatus(Singleton, dict): def save_last_state(self): - with open(last_state_file, mode='w', encoding='utf-8') as f: + with open(self.last_state_file, mode='w', encoding='utf-8') as f: last_state = {} last_state['state'] = self['state'] last_state['sensors'] = self['sensors'] @@ -167,6 +180,13 @@ class ActiveStatus(Singleton, dict): self['state']['message'] = message +class ActiveStatusv14(ActiveStatus): + + def __init__(self): + self.default_json_file = default_json_file_v14 + self.last_state_file = last_state_file_v14 + + def request_wants_json(): best = request.accept_mimetypes.best_match( ['application/json', 'text/html'] diff --git a/spaceapi/views.py b/spaceapi/views.py --- a/spaceapi/views.py +++ b/spaceapi/views.py @@ -1,6 +1,6 @@ from flask import Blueprint, jsonify, render_template, abort, request, redirect, url_for, current_app -from .utils import request_wants_json, ActiveStatus +from .utils import request_wants_json, ActiveStatus, ActiveStatusv14 from .auth import httpauth root_views = Blueprint('root', __name__) @@ -18,6 +18,11 @@ def status_json(): return jsonify(ActiveStatus()) +@root_views.route('/v14/status.json') +def v14_json(): + return jsonify(ActiveStatusv14()) + + @root_views.route('/reload') @httpauth.login_required def reload(): @@ -26,11 +31,20 @@ def reload(): return jsonify(active) +@root_views.route('/v14/reload') +@httpauth.login_required +def v14_reload(): + active = ActiveStatusv14() + active.reload() + return jsonify(active) + + @root_views.route('/open', methods=('GET', 'POST')) @httpauth.login_required def open(): if request.method == 'POST': active = ActiveStatus() + activev14 = ActiveStatusv14() try: if httpauth.username() in current_app.config['STATE_TRIGGER_PERSON_ALLOWED']: @@ -46,7 +60,9 @@ def open(): new_state = True active.set_new_state(value=new_state, trigger_person=trigger_person) + activev14.set_new_state(value=new_state, trigger_person=trigger_person) active.save_last_state() + activev14.save_last_state() return redirect(url_for('root.index')) return render_template('open.html') @@ -58,13 +74,18 @@ def present(): if request.method == 'POST': active = ActiveStatus() + activev14 = ActiveStatusv14() if active['state']['open']: + user = httpauth.username() if 'present' in request.form: - active.add_user_present(httpauth.username()) + active.add_user_present(user) + activev14.add_user_present(user) elif 'leave' in request.form: - active.remove_user_present(httpauth.username()) + active.remove_user_present(user) + activev14.remove_user_present(user) active.save_last_state() + activev14.save_last_state() return redirect(url_for('root.index')) return render_template('present.html')