diff options
author | Your Name <you@example.com> | 2013-07-28 18:47:40 (GMT) |
---|---|---|
committer | Your Name <you@example.com> | 2013-07-28 18:47:40 (GMT) |
commit | e8ffc8333c790d2cb9815996f556c6074581df95 (patch) | |
tree | bf48b0e5d01bdd77edf8cf05bdbf91a9c83873d9 | |
parent | 6a9181f269714eeb0b20ec583ac682b0980c5e9a (diff) |
Patches by alsroot to go with SN polish
-rw-r--r-- | sugar_network_webui/__init__.py | 19 | ||||
-rw-r--r-- | sugar_network_webui/app.py | 10 | ||||
-rw-r--r-- | sugar_network_webui/client.py | 39 | ||||
-rw-r--r-- | sugar_network_webui/cursor.py | 10 | ||||
-rw-r--r-- | sugar_network_webui/objects.py | 21 |
5 files changed, 43 insertions, 56 deletions
diff --git a/sugar_network_webui/__init__.py b/sugar_network_webui/__init__.py index 44ce039..95392ed 100644 --- a/sugar_network_webui/__init__.py +++ b/sugar_network_webui/__init__.py @@ -16,14 +16,21 @@ from .env import webui, webui_host, webui_port -def get_app(commands_processor, api_url, anonymous=False): +def get_app(call, api_url, anonymous=False): from . import client - client.commands_processor = commands_processor - client.anonymous = anonymous - client.api_url = api_url + from .client import Client + from .app import app from . import cursors - # XXX + + client._call = call + Client.api_url = api_url + Client.anonymous = anonymous + if anonymous: + Client.sugar_uid = 'demo' cursors.home_mount = cursors.network_mount - from .app import app + else: + from sugar_network.client import sugar_uid + Client.sugar_uid = sugar_uid() + return app diff --git a/sugar_network_webui/app.py b/sugar_network_webui/app.py index 93e6d6d..56e87ea 100644 --- a/sugar_network_webui/app.py +++ b/sugar_network_webui/app.py @@ -34,7 +34,6 @@ from werkzeug import secure_filename import simplejson import tempfile -from sugar_network import client from sugar_network.toolkit.http import NotFound from client import Client @@ -98,8 +97,7 @@ def timedelta(mtime): def get_colors(): - from . import client - if client.anonymous: + if Client.anonymous: return ('#000000', '#000000') try: @@ -148,7 +146,7 @@ def inject_vars(): # Here we can inject variables into every template call stroke, fill = get_colors() kwvar = { - 'userid': client.sugar_uid(), + 'userid': Client.sugar_uid, 'sugar_nick' : get_user() } return dict(stroke=stroke, fill=fill, **kwvar) @@ -221,7 +219,7 @@ def stars(context=None): guid = context[5:] # remove "stars-" from id favorite = request.args.get('favorite') - Client.call('PUT', document='context', guid=guid, cmd='favorite', + Client.call('PUT', ['context', guid], 'favorite', content=(favorite == 'true')) # TODO Need to reset query object until supporting notifications @@ -238,7 +236,7 @@ def moon(context=None): clone = request.args.get('clone', None) guid = context[5:] # remove "moon-" from id - Client.call('PUT', document='context', guid=guid, cmd='clone', + Client.call('PUT', ['context', guid], 'clone', content=1 if clone == 'true' else 0) return jsonify(clone=clone) diff --git a/sugar_network_webui/client.py b/sugar_network_webui/client.py index 57c4109..88992dd 100644 --- a/sugar_network_webui/client.py +++ b/sugar_network_webui/client.py @@ -13,15 +13,13 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. +import json import logging -from sugar_network import db, client from sugar_network.toolkit import coroutine -commands_processor = None -anonymous = False -api_url = None +_call = None _logger = logging.getLogger('sugar_network') @@ -31,22 +29,16 @@ class ServerError(RuntimeError): class Client(object): + anonymous = False + sugar_uid = None + api_url = None + _subscriptions = {} _pull = None @classmethod - def call(cls, method, cmd=None, content=None, - content_type='application/json', **kwargs): - request = db.Request(**kwargs) - request.access_level = db.ACCESS_LOCAL - request.principal = client.sugar_uid() - request['method'] = method - if cmd: - request['cmd'] = cmd - request.content = content - if method != 'GET': - request.content_type = content_type - return commands_processor.call(request) + def call(cls, *args, **kwargs): + return _call(*args, **kwargs) @classmethod def connect(cls, callback, **condition): @@ -61,8 +53,8 @@ class Client(object): @classmethod def _pull_events(cls): - while True: - event = commands_processor._pooler.wait() + for event in Client.call('GET', [], 'subscribe'): + event = json.loads(event[6:]) for callback, condition in cls._subscriptions.items(): for key, value in condition.items(): if event.get(key) != value: @@ -78,7 +70,7 @@ class Client(object): @property def inline(self): - return self.call('GET', 'inline') or anonymous + return self.call('GET', [], 'inline') or Client.anonymous def launch(self, context, command='activity', object_id=None, uri=None, args=None): @@ -101,9 +93,8 @@ class Client(object): optional list of arguments to pass to launching implementation """ - return self.call('GET', cmd='launch', - document='context', guid=context, object_id=object_id, uri=uri, - args=args) + return self.call('GET', ['context', context], 'launch', + object_id=object_id, uri=uri, args=args) def __getattr__(self, name): """Class-like object to access to a resource or call a method. @@ -130,7 +121,7 @@ class _Resource(object): self.document = name def url(self, guid, prop): - return api_url + '/%s/%s/%s' % (self.document, guid, prop) + return Client.api_url + '/%s/%s/%s' % (self.document, guid, prop) def cursor(self, query=None, order_by=None, reply=None, page_size=18, **filters): @@ -164,7 +155,7 @@ class _Resource(object): resource object's GUID """ - return Client.call('DELETE', document=self.document, guid=guid) + return Client.call('DELETE', [self.document, guid]) def __call__(self, guid=None, reply=None, **kwargs): from .objects import Object diff --git a/sugar_network_webui/cursor.py b/sugar_network_webui/cursor.py index a25cb4c..cfc96c1 100644 --- a/sugar_network_webui/cursor.py +++ b/sugar_network_webui/cursor.py @@ -18,7 +18,7 @@ import collections from sugar_network.toolkit import coroutine, enforce from objects import Object -from client import Client, api_url +from client import Client _QUERY_PAGES_NUMBER = 2 @@ -88,7 +88,7 @@ class Cursor(object): self._reset() def url(self, guid, prop): - return api_url + '/%s/%s/%s' % (self.document, guid, prop) + return Client.api_url + '/%s/%s/%s' % (self.document, guid, prop) def read_events(self): if self._wait_session is None: @@ -200,9 +200,7 @@ class Cursor(object): def _fetch_page(self, page): offset = page * self._page_size - params = { - 'document': self.document, - } + params = {} for key, value in self._filters.items(): if value is not None: params[key] = value @@ -216,7 +214,7 @@ class Cursor(object): params['reply'] = self._reply try: - response = Client.call('GET', **params) + response = Client.call('GET', [self.document], **params) self._total = response['total'] except Exception: _logger.exception('Failed to fetch %r query', params) diff --git a/sugar_network_webui/objects.py b/sugar_network_webui/objects.py index 1ebd4f9..83186d6 100644 --- a/sugar_network_webui/objects.py +++ b/sugar_network_webui/objects.py @@ -23,7 +23,6 @@ from client import Client _logger = logging.getLogger('sugar_network.objects') -_uid = client.sugar_uid() class Object(object): @@ -46,7 +45,7 @@ class Object(object): @property def is_author(self): - return _uid in [(i.get('guid') or i.get('name')) for i in self['author']] + return Client.sugar_uid in [(i.get('guid') or i.get('name')) for i in self['author']] def get(self, prop): if prop == 'guid': @@ -70,8 +69,8 @@ class Object(object): if not to_fetch: return - response = Client.call('GET', - document=self.document, guid=self._guid, reply=to_fetch) + response = Client.call('GET', [self.document, self._guid], + reply=to_fetch) response.update(self._props) self._props = response @@ -84,25 +83,19 @@ class Object(object): props[i] = self._props.get(i) if self._guid: - Client.call('PUT', - document=self.document, guid=self._guid, content=props, - content_type='application/json') + Client.call('PUT', [self.document, self._guid], content=props) else: - self._guid = Client.call('POST', - document=self.document, content=props, - content_type='application/json') + self._guid = Client.call('POST', [self.document], content=props) self._dirty.clear() return self._guid def get_blob(self, prop): - return Client.call('GET', - document=self.document, guid=self._guid, prop=prop) + return Client.call('GET', [self.document, self._guid, prop]) def upload_blob(self, prop, content, content_type): enforce(self._guid, 'Object needs to be posted first') - Client.call('PUT', - document=self.document, guid=self._guid, prop=prop, + Client.call('PUT', [self.document, self._guid, prop], content=content, content_type=content_type) def __getitem__(self, prop): |