From 64382b5afed930ab4c21e90a0f2e34ae928de47f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20G=C3=B3mez?= Date: Wed, 4 Dec 2013 04:12:06 +0000 Subject: [PATCH] * Added cache flush functionality to dashboard * added rest method to flush cache * Fixed tabletools "stuff" left behind, removing all this on any "main link" click --- .../org.eclipse.core.resources.prefs | 2 + server/src/uds/REST/methods/cache.py | 74 +++++++++++++++++++ server/src/uds/REST/methods/gui_callback.py | 18 ----- server/src/uds/core/managers/CryptoManager.py | 12 +-- server/src/uds/core/util/Config.py | 1 + server/src/uds/core/util/__init__.py | 33 +++++++++ .../src/uds/static/adm/js/gui-definition.js | 14 +++- server/src/uds/static/adm/js/gui.js | 47 +++++++----- .../templates/uds/admin/snippets/navbar.html | 8 +- 9 files changed, 162 insertions(+), 47 deletions(-) create mode 100644 server/src/uds/REST/methods/cache.py diff --git a/server/.settings/org.eclipse.core.resources.prefs b/server/.settings/org.eclipse.core.resources.prefs index b6ffbc22..4bcdc6ce 100644 --- a/server/.settings/org.eclipse.core.resources.prefs +++ b/server/.settings/org.eclipse.core.resources.prefs @@ -11,6 +11,7 @@ encoding//src/server/urls.py=utf-8 encoding//src/uds/REST/__init__.py=utf-8 encoding//src/uds/REST/handlers.py=utf-8 encoding//src/uds/REST/methods/authenticators.py=utf-8 +encoding//src/uds/REST/methods/cache.py=utf-8 encoding//src/uds/REST/methods/gui_callback.py=utf-8 encoding//src/uds/REST/methods/login_logout.py=utf-8 encoding//src/uds/REST/methods/networks.py=utf-8 @@ -107,6 +108,7 @@ encoding//src/uds/core/util/ThreadPool.py=utf-8 encoding//src/uds/core/util/UniqueIDGenerator.py=utf-8 encoding//src/uds/core/util/UniqueMacGenerator.py=utf-8 encoding//src/uds/core/util/UniqueNameGenerator.py=utf-8 +encoding//src/uds/core/util/__init__.py=utf-8 encoding//src/uds/core/util/connection.py=utf-8 encoding//src/uds/core/util/html.py=utf-8 encoding//src/uds/core/util/log.py=utf-8 diff --git a/server/src/uds/REST/methods/cache.py b/server/src/uds/REST/methods/cache.py new file mode 100644 index 00000000..4dcc0dbd --- /dev/null +++ b/server/src/uds/REST/methods/cache.py @@ -0,0 +1,74 @@ +# -*- coding: utf-8 -*- + +# +# Copyright (c) 2014 Virtual Cable S.L. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# * Neither the name of Virtual Cable S.L. nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +''' +@author: Adolfo Gómez, dkmaster at dkmon dot com +''' +from __future__ import unicode_literals + +from uds.core.util.Cache import Cache as uCache +from uds.REST import Handler, RequestError, NotFound + +import logging + +logger = logging.getLogger(__name__) + +# Enclosed methods under /auth path + +class Cache(Handler): + authenticated = True # Public method + needs_staff = True # + + def get(self): + ''' + This login uses parameters to generate auth token + The alternative is to use the template tag inside "REST" that is called auth_token, that extracts an auth token from an user session + We can use any of this forms due to the fact that the auth token is in fact a session key + Parameters: + mandatory: + username: + password: + auth: + optional: + locale: (defaults to "en") + Result: + on success: { 'result': 'ok', 'auth': [auth_code] } + on error: { 'result: 'error', 'error': [error string] } + ''' + + logger.debug('Params: {0}'.format(self._params)) + if len(self._args) == 0: + return {} + + if len(self._args) != 1: + raise RequestError('Invalid Request') + + + uCache.purge() + return 'done' diff --git a/server/src/uds/REST/methods/gui_callback.py b/server/src/uds/REST/methods/gui_callback.py index cf9ec257..9aca1ec4 100644 --- a/server/src/uds/REST/methods/gui_callback.py +++ b/server/src/uds/REST/methods/gui_callback.py @@ -47,24 +47,6 @@ class Callback(Handler): needs_staff = True # def get(self): - ''' - This login uses parameters to generate auth token - The alternative is to use the template tag inside "REST" that is called auth_token, that extracts an auth token from an user session - We can use any of this forms due to the fact that the auth token is in fact a session key - Parameters: - mandatory: - username: - password: - auth: - optional: - locale: (defaults to "en") - Result: - on success: { 'result': 'ok', 'auth': [auth_code] } - on error: { 'result: 'error', 'error': [error string] } - ''' - - logger.debug('Params: {0}'.format(self._params)) - if len(self._args) != 1: raise RequestError('Invalid Request') diff --git a/server/src/uds/core/managers/CryptoManager.py b/server/src/uds/core/managers/CryptoManager.py index e0beacd7..e7eca806 100644 --- a/server/src/uds/core/managers/CryptoManager.py +++ b/server/src/uds/core/managers/CryptoManager.py @@ -30,6 +30,7 @@ ''' @author: Adolfo Gómez, dkmaster at dkmon dot com ''' +from __future__ import unicode_literals from server.settings import RSA_KEY from Crypto.PublicKey import RSA @@ -59,17 +60,18 @@ class CryptoManager(object): def encrypt(self, string): atfork() - return self._rsa.encrypt(string, '')[0].encode(CryptoManager.CODEC) + return self._rsa.encrypt(string.encode('utf-8'), '')[0].encode(CryptoManager.CODEC) def decrypt(self, string): atfork() - return self._rsa.decrypt(string.decode(CryptoManager.CODEC)) + return self._rsa.decrypt(string.decode(CryptoManager.CODEC)).decode('utf-8') def xor(self, s1, s2): + s1, s2 = s1.encode('utf-8'), s2.encode('utf-8') mult = (len(s1)/len(s2)) + 1 - s1 = array.array('B', s1) - s2 = array.array('B', s2 * mult) - return array.array('B', (s1[i] ^ s2[i] for i in range(len(s1)))).tostring() + s1 = array.array(b'B', s1) + s2 = array.array(b'B', s2 * mult) + return array.array(b'B', (s1[i] ^ s2[i] for i in range(len(s1)))).tostring() def loadPrivateKey(self, rsaKey): try: diff --git a/server/src/uds/core/util/Config.py b/server/src/uds/core/util/Config.py index 5d31050b..7b56783a 100644 --- a/server/src/uds/core/util/Config.py +++ b/server/src/uds/core/util/Config.py @@ -30,6 +30,7 @@ ''' @author: Adolfo Gómez, dkmaster at dkmon dot com ''' +from __future__ import unicode_literals from django.conf import settings from uds.models import Config as dbConfig diff --git a/server/src/uds/core/util/__init__.py b/server/src/uds/core/util/__init__.py index e69de29b..e0f99c1a 100644 --- a/server/src/uds/core/util/__init__.py +++ b/server/src/uds/core/util/__init__.py @@ -0,0 +1,33 @@ +# -*- coding: utf-8 -*- + +# +# Copyright (c) 2012 Virtual Cable S.L. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# * Neither the name of Virtual Cable S.L. nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +''' +@author: Adolfo Gómez, dkmaster at dkmon dot com +''' +from __future__ import unicode_literals diff --git a/server/src/uds/static/adm/js/gui-definition.js b/server/src/uds/static/adm/js/gui-definition.js index 212c5f2c..71f1d91c 100644 --- a/server/src/uds/static/adm/js/gui-definition.js +++ b/server/src/uds/static/adm/js/gui-definition.js @@ -243,7 +243,7 @@ gui.connectivity.link = function(event) { }); }); }, - onDelete: function(value, event, table, refreshFnc) { + onDelete: function(value, event, table, refreshFncs) { // TODO: Add confirmation to deletion gui.connectivity.transports.rest.del(value.id, function(){ refreshFnc(); @@ -258,3 +258,15 @@ gui.connectivity.link = function(event) { }); }; + +// Tools +gui.clear_cache = new BasicGuiElement('Clear cache'); +gui.clear_cache.link = function() { + "use strict"; + api.getJson('cache/flush', { + success: function() { + gui.launchModal(gettext('Cache'), gettext('Cache has been flushed'), ' ' ); + }, + }); + +}; \ No newline at end of file diff --git a/server/src/uds/static/adm/js/gui.js b/server/src/uds/static/adm/js/gui.js index d2f175f9..48921634 100644 --- a/server/src/uds/static/adm/js/gui.js +++ b/server/src/uds/static/adm/js/gui.js @@ -151,32 +151,41 @@ }; gui.setLinksEvents = function() { - var sidebarLinks = [ { - id : 'lnk-dashboard', - exec : gui.dashboard.link, - }, { - id : 'lnk-service_providers', - exec : gui.providers.link - }, { - id : 'lnk-authenticators', - exec : gui.authenticators.link - }, { - id : 'lnk-osmanagers', - exec : gui.osmanagers.link - }, { - id : 'lnk-connectivity', - exec : gui.connectivity.link - }, { - id : 'lnk-deployed_services', - exec : gui.deployed_services - }, ]; + var sidebarLinks = [ + { + id : 'lnk-dashboard', + exec : gui.dashboard.link, + }, { + id : 'lnk-service_providers', + exec : gui.providers.link + }, { + id : 'lnk-authenticators', + exec : gui.authenticators.link + }, { + id : 'lnk-osmanagers', + exec : gui.osmanagers.link + }, { + id : 'lnk-connectivity', + exec : gui.connectivity.link + }, { + id : 'lnk-deployed_services', + exec : gui.deployed_services + }, { + id : 'lnk-clear_cache', + exec : gui.clear_cache.link, + }, + ]; $.each(sidebarLinks, function(index, value) { gui.doLog('Adding ' + value.id); $('.' + value.id).unbind('click').click(function(event) { + event.preventDefault(); if ($('.navbar-toggle').css('display') != 'none') { $(".navbar-toggle").trigger("click"); } $('html, body').scrollTop(0); + // Tabletools creates divs at end that do not get removed, here is a good place to ensure there is no garbage left behind + // And anyway, if this div does not exists, it creates a new one... + $('.DTTT_dropdown').remove(); value.exec(event); }); }); diff --git a/server/src/uds/templates/uds/admin/snippets/navbar.html b/server/src/uds/templates/uds/admin/snippets/navbar.html index 29df2abc..a8e6423d 100644 --- a/server/src/uds/templates/uds/admin/snippets/navbar.html +++ b/server/src/uds/templates/uds/admin/snippets/navbar.html @@ -3,7 +3,7 @@