From 783d99a0ccd77a5042cef8932ce68e5341c4974f Mon Sep 17 00:00:00 2001 From: Jamie Matthews Date: Tue, 7 Nov 2017 13:15:14 +0000 Subject: [PATCH 1/2] Make ordering of roles deterministic --- userroles/__init__.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/userroles/__init__.py b/userroles/__init__.py index dc50f8f..4e49acb 100644 --- a/userroles/__init__.py +++ b/userroles/__init__.py @@ -33,23 +33,23 @@ def __unicode__(self): class Roles(object): - _roles_dict = None + _roles_mapping = None @property - def roles_dict(self): + def roles_mapping(self): """ - Load list style config into dict of {role_name: role_class} + Load list style config into list of of (role_name, role_class) tuples """ - if self._roles_dict is None: - self._roles_dict = {} + if self._roles_mapping is None: + self._roles_mapping = [] for item in self._config: if isinstance(item, basestring): # An item like 'manager' - self._roles_dict[item] = None + self._roles_mapping.append((item, None)) else: # Anything else raise ImproperlyConfigured(_INCORRECT_ARGS) - return self._roles_dict + return self._roles_mapping @property def choices(self): @@ -57,7 +57,7 @@ def choices(self): Return a list of two-tuples of role names, suitable for use as the 'choices' argument to a model field. """ - return [(role, role) for role in self.roles_dict.keys()] + return [(role, role) for role in self.roles_mapping] def __init__(self, config=None): """ @@ -72,9 +72,9 @@ def __getattr__(self, name): Handle custom properties for returning Role objects. For example: `roles.moderator` """ - if name in self.roles_dict.keys(): - return Role(name=name) - else: - raise AttributeError("No such role exists '%s'" % name) + for role_name, _ in self.roles_mapping: + if role_name == name: + return Role(name=name) + raise AttributeError("No such role exists '%s'" % name) roles = Roles() From 4378d9cb0bb0a042885f4bad333fe2554b8a1f8f Mon Sep 17 00:00:00 2001 From: Jamie Matthews Date: Tue, 7 Nov 2017 13:58:31 +0000 Subject: [PATCH 2/2] Fix choices --- userroles/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/userroles/__init__.py b/userroles/__init__.py index 4e49acb..0861b6f 100644 --- a/userroles/__init__.py +++ b/userroles/__init__.py @@ -57,7 +57,7 @@ def choices(self): Return a list of two-tuples of role names, suitable for use as the 'choices' argument to a model field. """ - return [(role, role) for role in self.roles_mapping] + return [(name, name) for name, value in self.roles_mapping] def __init__(self, config=None): """