See #230: can now use "or" operator to check permissions

This commit is contained in:
Eliot Berriot 2018-05-24 22:38:26 +02:00
parent ed6c1a9a5b
commit dfb4f5f62a
No known key found for this signature in database
GPG key ID: DD6965E2476E5C27
5 changed files with 50 additions and 7 deletions

View file

@ -72,9 +72,12 @@ class User(AbstractUser):
perms[p] = v
return perms
def has_permissions(self, *perms):
def has_permissions(self, *perms, operator='and'):
if operator not in ['and', 'or']:
raise ValueError('Invalid operator {}'.format(operator))
permissions = self.get_permissions()
return all([permissions[p] for p in perms])
checker = all if operator == 'and' else any
return checker([permissions[p] for p in perms])
def get_absolute_url(self):
return reverse('users:detail', kwargs={'username': self.username})