From 80c784985fbcfc958ffe27b0f2c45481dbdddc69 Mon Sep 17 00:00:00 2001 From: Tim Bart Date: Tue, 26 Apr 2016 12:08:57 +0200 Subject: [PATCH 1/2] Making namespace reflect cookie value --- api/namespace.py | 9 ++++++++- app.yaml | 2 +- static/css/base.css | 7 ++++++- static/js/base.js | 41 +++++++++++++++++++++++++++++++++++++++++ template/base.html | 6 ++---- url/api_urls.py | 3 ++- 6 files changed, 60 insertions(+), 8 deletions(-) diff --git a/api/namespace.py b/api/namespace.py index 263db29..894857b 100644 --- a/api/namespace.py +++ b/api/namespace.py @@ -1,6 +1,6 @@ import logging as log import datetime - +import json from google.appengine.api import namespace_manager from core.handlers.base import ProtectedRequestHandler @@ -16,3 +16,10 @@ def persist_namespace(self): def get(self): log.info("User {} set namespace to be {}".format(self.current_user_email, self.request.get("namespace"))) self.redirect("/") + + +class NamespaceAPIList(ProtectedRequestHandler): + def get(self): + context = self._extra_context({}) + self.response.write(json.dumps(context['available_namespaces'])) + diff --git a/app.yaml b/app.yaml index cbc71d5..78c5e69 100644 --- a/app.yaml +++ b/app.yaml @@ -1,5 +1,5 @@ application: 'hello-admin' -version: 60f +version: namespace runtime: python27 api_version: 1 diff --git a/static/css/base.css b/static/css/base.css index 5991e60..37bd48e 100644 --- a/static/css/base.css +++ b/static/css/base.css @@ -100,4 +100,9 @@ html { .btn-round {border-radius: 50%;} .btn-fade {opacity: 0.5;} -#err {color: #FFA081;} \ No newline at end of file +#err {color: #FFA081;} + + +.namespace-active{background-color: #42B5FF;margin-right:10px;color:#FFF;} +.namespace-inactive{color: black;margin-right:10px;} + diff --git a/static/js/base.js b/static/js/base.js index 1562f74..ee510b2 100644 --- a/static/js/base.js +++ b/static/js/base.js @@ -70,11 +70,52 @@ var Err = React.createClass({ } }); + +var NameSpaceChecker = React.createClass({ + getInitialState: function() { + return {namespace: getNamespace(), availableNamespaces: []} + }, + getNamespaceFromCookie: function() { + this.setState({namespace: getNamespace()}) + }, + getFromServer: function() { + $.ajax({ + url: "/api/namespace/list", + dataType: 'json', + cache: false, + success: function(data) { + this.setState({availableNamespaces: data}); + }.bind(this), + error: function(xhr, status, err) { + console.error(this.props.url, status, err.toString()); + }.bind(this) + }); + }, + componentDidMount: function() { + this.getFromServer(); + setInterval(this.getNamespaceFromCookie, 5000); + }, + render: function() { + var el = { + this.state.availableNamespaces.map(function(ns){ + var url = "/api/namespace/?namespace=" + ns; + var cName = (this.state.namespace == ns) ? "namespace-active" : "namespace-inactive"; + return {ns} + }.bind(this)) + }; + + return el + } +}); + + if (React.render) { React.render(, document.getElementById("err")); + React.render(, document.getElementById("namespace-checker")); } else { React.renderComponent(, document.getElementById("err")); + React.renderComponent(, document.getElementById("namespace-checker")); } diff --git a/template/base.html b/template/base.html index 653536d..becb39b 100644 --- a/template/base.html +++ b/template/base.html @@ -39,10 +39,8 @@
Admin Panel
-
- {% for nsp in available_namespaces %} - {{nsp}}   - {% endfor %} + +
diff --git a/url/api_urls.py b/url/api_urls.py index f4db232..382bd7e 100644 --- a/url/api_urls.py +++ b/url/api_urls.py @@ -6,7 +6,7 @@ from api.feedback import FeedbackAPI from api.insights import InsightsAPI, InsightsGenericAPI from api.logs_level import LogsLevelAPI -from api.namespace import NamespaceAPI +from api.namespace import NamespaceAPI, NamespaceAPIList from api.password import PasswordResetAPI, PasswordForceUpdateAPI from api.feature import FeaturesAPI from api.device import ActiveDevices15MinutesHistoryAPI, PillColorAPI, DropoutDevicesAPI @@ -155,6 +155,7 @@ ('/api/clearbit/?$', ClearbitAPI), ('/api/create/buggy_firmware/?$', CreateBuggyFirmwareAPI), ("/api/init/?$", InitializeDataStore), + ("/api/namespace/list", NamespaceAPIList), ("/api/namespace/?$", NamespaceAPI), ('/update', UpdateAdminAccessTokenAPI), ('/pill_bin_upload', PillKeyDecryptAPI), From 37e582a08b49f4805f54ff471a3b9350c9a76271 Mon Sep 17 00:00:00 2001 From: Tim Bart Date: Tue, 26 Apr 2016 12:20:09 +0200 Subject: [PATCH 2/2] Make it look nice --- app.yaml | 2 +- core/handlers/base.py | 2 ++ static/css/base.css | 2 +- static/js/base.js | 4 +++- 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/app.yaml b/app.yaml index 78c5e69..cbc71d5 100644 --- a/app.yaml +++ b/app.yaml @@ -1,5 +1,5 @@ application: 'hello-admin' -version: namespace +version: 60f runtime: python27 api_version: 1 diff --git a/core/handlers/base.py b/core/handlers/base.py index 9b51d68..bd66acb 100644 --- a/core/handlers/base.py +++ b/core/handlers/base.py @@ -1,6 +1,7 @@ import os import logging as log from copy import copy +import datetime from google.appengine.api.namespace_manager import namespace_manager import jinja2 @@ -52,6 +53,7 @@ def namespace(self): def persist_namespace(self): namespace_from_cookies = self.request.cookies.get("namespace", None) namespace = namespace_from_cookies or "production" + self.response.set_cookie('namespace', namespace, expires=datetime.datetime.now() + datetime.timedelta(days=365)) namespace_manager.set_namespace(namespace) @staticmethod diff --git a/static/css/base.css b/static/css/base.css index 37bd48e..0bf0bbb 100644 --- a/static/css/base.css +++ b/static/css/base.css @@ -103,6 +103,6 @@ html { #err {color: #FFA081;} -.namespace-active{background-color: #42B5FF;margin-right:10px;color:#FFF;} +.namespace-active{background-color: #42B5FF;margin-right:10px;color:#FFF;padding:0 5px;border-radius: 2px;} .namespace-inactive{color: black;margin-right:10px;} diff --git a/static/js/base.js b/static/js/base.js index ee510b2..9eea486 100644 --- a/static/js/base.js +++ b/static/js/base.js @@ -76,7 +76,9 @@ var NameSpaceChecker = React.createClass({ return {namespace: getNamespace(), availableNamespaces: []} }, getNamespaceFromCookie: function() { - this.setState({namespace: getNamespace()}) + var ns = getNamespace(); + console.log(ns); + this.setState({namespace: ns}) }, getFromServer: function() { $.ajax({