From 1ae91d113063d6d0155059329f19c0e570dafd60 Mon Sep 17 00:00:00 2001 From: Oskar Hahn Date: Fri, 3 Aug 2012 14:23:13 +0200 Subject: [PATCH] changed user-api, so it can be filter the result for the prefix and the id --- openslides/participant/models.py | 36 ++++++++++++++++--------------- openslides/utils/user/__init__.py | 27 ++++++++++++----------- openslides/utils/user/signals.py | 2 +- 3 files changed, 35 insertions(+), 30 deletions(-) diff --git a/openslides/participant/models.py b/openslides/participant/models.py index d892b38e2..fe19ff9de 100644 --- a/openslides/participant/models.py +++ b/openslides/participant/models.py @@ -89,17 +89,6 @@ class Profile(models.Model, UserMixin): ) -class ParticipantUsers(object): - user_prefix = Profile.user_prefix - - def __iter__(self): - for profile in Profile.objects.all(): - yield profile - - def __getitem__(self, key): - return Profile.objects.get(pk=key) - - class DjangoGroup(models.Model, UserMixin): user_prefix = 'djangogroup' @@ -109,20 +98,33 @@ class DjangoGroup(models.Model, UserMixin): return unicode(self.group) -class DjangoGroupUsers(object): - user_prefix = DjangoGroup.user_prefix +class ParticipantUsers(object): + def __init__(self, user_prefix=None, id=None): + self.user_prefix = user_prefix + self.id = id def __iter__(self): - for group in DjangoGroup.objects.all(): - yield group + if not self.user_prefix or self.user_prefix == Profile.user_prefix: + if self.id: + yield Profile.objects.get(pk=self.id) + else: + for profile in Profile.objects.all(): + yield profile + + if not self.user_prefix or self.user_prefix == DjangoGroup.user_prefix: + if self.id: + yield DjangoGroup.objects.get(pk=self.id) + else: + for group in DjangoGroup.objects.all(): + yield group def __getitem__(self, key): - return DjangoGroup.objects.get(pk=key) + return Profile.objects.get(pk=key) @receiver(receiv_users, dispatch_uid="participant_profile") def receiv_users(sender, **kwargs): - return [ParticipantUsers(), DjangoGroupUsers()] + return ParticipantUsers(user_prefix=kwargs['user_prefix'], id=kwargs['id']) @receiver(default_config_value, dispatch_uid="participant_default_config") diff --git a/openslides/utils/user/__init__.py b/openslides/utils/user/__init__.py index 229ada3c4..d65ebb21a 100644 --- a/openslides/utils/user/__init__.py +++ b/openslides/utils/user/__init__.py @@ -87,28 +87,31 @@ def split_uid(uid): def get_user(uid): try: - user_type, id = split_uid(uid) + user_prefix, id = split_uid(uid) except TypeError: return EmtyUser() - for receiver, users_list in receiv_users.send(sender='get_user'): - for users in users_list: - if users.user_prefix == user_type: - return users[id] - # TODO: Use own Exception - raise Exception('User with uid %s does not exist' % uid) + return Users(user_prefix=user_prefix, id=id)[0] class Users(object): """ A Storage for a multiplicity of different User-Objects. """ + def __init__(self, user_prefix=None, id=None): + self.user_prefix = user_prefix + self.id = id + def __iter__(self): - for receiver, users_list in receiv_users.send(sender='users'): - for users in users_list: - # Does iter(users) work? - for user in users: - yield user + for receiver, users in receiv_users.send(sender='users', user_prefix=self.user_prefix, id=self.id): + # Does iter(users) work? + for user in users: + yield user + + def __getitem__(self, key): + user_list = list(self) + print user_list + return user_list[key] def generate_uid(prefix, id): diff --git a/openslides/utils/user/signals.py b/openslides/utils/user/signals.py index 8f1b1c20d..5779db7ab 100644 --- a/openslides/utils/user/signals.py +++ b/openslides/utils/user/signals.py @@ -12,4 +12,4 @@ from django.dispatch import Signal -receiv_users = Signal(providing_args=[]) +receiv_users = Signal(providing_args=['user_prefix', 'id'])