Skip to content

Commit

Permalink
makes manages_broker a property
Browse files Browse the repository at this point in the history
  • Loading branch information
smirolo committed Mar 14, 2024
1 parent b2625fe commit f70eded
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
2 changes: 1 addition & 1 deletion deployutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

__version__ = '0.10.9'
__version__ = '0.10.10-dev'
40 changes: 29 additions & 11 deletions deployutils/apps/django/mixins/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,22 @@ def get_managed(self, request):
return self.get_accessible_profiles(request, roles=[self.MANAGER])


def has_role(self, account, roles):
"""
Returns ``True`` if the ``request.user`` is at least one of ``roles``
for ``account``.
``account`` will be converted to a string and compared
to a profile slug.
"""
account_slug = str(account)
for accessible_profile in self.get_accessible_profiles(
self.request, roles=roles):
if account_slug == accessible_profile['slug']:
return True
return False


@property
def managed_accounts(self):
"""
Expand All @@ -136,22 +152,22 @@ def manages(self, account):
``account`` will be converted to a string and compared
to a profile slug.
"""
account_slug = str(account)
for accessible_profile in self.request.session.get(
'roles', {}).get(self.MANAGER, []):
if account_slug == accessible_profile['slug']:
return True
return False

return self.has_role(account, roles=[self.MANAGER])

@property
def manages_broker(self):
"""
Returns ``True`` if the ``request.user`` is a manager for the site.
"""
broker_slug = self.request.session.get('site', {}).get('slug')
if not broker_slug:
return False
return self.manages(broker_slug)
if not hasattr(self, '_manages_broker'):
site = self.request.session.get('site')
if not site:
return False
broker_slug = site.get('slug')
if not broker_slug:
return False
self._manages_broker = self.manages(broker_slug)
return self._manages_broker


class AccountMixin(AccessiblesMixin):
Expand Down Expand Up @@ -332,6 +348,8 @@ def _get_accessible_plans(request, profile=None, at_time=None):
"""
#pylint:disable=too-many-nested-blocks
plans = {}
if profile:
profile = str(profile)
for accessible_profiles in six.itervalues(request.session.get(
'roles', {})):
for accessible_profile in accessible_profiles:
Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
# The short X.Y version.
version = '0.10'
# The full version, including alpha/beta/rc tags.
release = '0.10.9'
release = '0.10.10-dev'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down

0 comments on commit f70eded

Please sign in to comment.