Skip to content
This repository has been archived by the owner on Dec 6, 2021. It is now read-only.

Deterministic ordering #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions userroles/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,31 +33,31 @@ 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):
"""
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 [(name, name) for name, value in self.roles_mapping]

def __init__(self, config=None):
"""
Expand All @@ -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()