summaryrefslogtreecommitdiff
path: root/ishtar_common/models.py
diff options
context:
space:
mode:
Diffstat (limited to 'ishtar_common/models.py')
-rw-r--r--ishtar_common/models.py141
1 files changed, 60 insertions, 81 deletions
diff --git a/ishtar_common/models.py b/ishtar_common/models.py
index 5f57e8398..b1f226088 100644
--- a/ishtar_common/models.py
+++ b/ishtar_common/models.py
@@ -2665,7 +2665,7 @@ class Organization(Address, Merge, OwnPerms, BaseGenderedType, ValueGetter, Main
icon_class="fa fa-pencil",
text=_("Bulk update"),
target="many",
- rights=["change_organization"],
+ rights=["ishtar_common.change_organization"],
)
QUICK_ACTIONS = [QA_EDIT]
@@ -2946,7 +2946,7 @@ class Person(Address, Merge, OwnPerms, ValueGetter, MainItem):
icon_class="fa fa-pencil",
text=_("Bulk update"),
target="many",
- rights=["change_person"],
+ rights=["ishtar_common.change_person"],
)
QUICK_ACTIONS = [QA_EDIT]
@@ -3130,72 +3130,6 @@ class Person(Address, Merge, OwnPerms, ValueGetter, MainItem):
def is_natural(self):
return not self.attached_to
- def has_right(self, right_name, session=None, obj=None):
- if "." in right_name:
- right_name = right_name.split(".")[-1]
- res, cache_key = "", ""
- if session:
- cache_key = "session-{}-{}".format(session.session_key, right_name)
- res = cache.get(cache_key)
- if res in (True, False):
- return res
- # list all cache key in order to clean them on profile change
- cache_key_list = "sessionlist-{}".format(session.session_key)
- key_list = cache.get(cache_key_list, [])
- key_list.append(cache_key)
- cache.set(cache_key_list, key_list, settings.CACHE_TIMEOUT)
- if isinstance(right_name, (list, tuple)):
- res = (
- bool(
- self.profiles.filter(
- current=True, profile_type__txt_idx__in=right_name
- ).count()
- )
- or bool(
- self.profiles.filter(
- current=True,
- profile_type__groups__permissions__codename__in=right_name,
- ).count()
- )
- or bool(
- self.ishtaruser.user_ptr.groups.filter(
- permissions__codename__in=right_name
- ).count()
- )
- or bool(
- self.ishtaruser.user_ptr.user_permissions.filter(
- codename__in=right_name
- ).count()
- )
- )
- else:
- res = (
- bool(
- self.profiles.filter(
- current=True, profile_type__txt_idx=right_name
- ).count()
- )
- or bool(
- self.profiles.filter(
- current=True,
- profile_type__groups__permissions__codename=right_name,
- ).count()
- )
- or bool(
- self.ishtaruser.user_ptr.groups.filter(
- permissions__codename__in=[right_name]
- ).count()
- )
- or bool(
- self.ishtaruser.user_ptr.user_permissions.filter(
- codename__in=[right_name]
- ).count()
- )
- )
- if session:
- cache.set(cache_key, res, settings.CACHE_TIMEOUT)
- return res
-
def full_label(self):
values = []
if self.title:
@@ -3330,7 +3264,8 @@ class BiographicalNote(BaseHistorizedItem, ValueGetter, MainItem):
url="biographicalnote-qa-edit",
icon_class="fa fa-pencil",
text=_("Edit biographical note"),
- rights=["change_biographicalnote", "change_own_biographicalnote"],
+ rights=["ishtar_common.change_biographicalnote",
+ "ishtar_common.change_own_biographicalnote"],
)
class Meta:
@@ -3367,7 +3302,8 @@ class BiographicalNote(BaseHistorizedItem, ValueGetter, MainItem):
return res
def set_slug(self):
- self.slug = create_slug(self.__class__, self.denomination, max_length=250, pk=self.pk)
+ self.slug = create_slug(self.__class__, self.denomination, max_length=250,
+ pk=self.pk)
def get_extra_actions(self, request):
"""
@@ -3375,7 +3311,7 @@ class BiographicalNote(BaseHistorizedItem, ValueGetter, MainItem):
"""
# url, base_text, icon, extra_text, extra css class, is a quick action,
actions = super().get_extra_actions(request)
- can_edit = self.can_do(request, "change_biographicalnote")
+ can_edit = self.can_do(request, "ishtar_common.change_biographicalnote")
if not can_edit:
return actions
actions += [
@@ -3823,11 +3759,54 @@ class IshtarUser(FullSearch):
)
return cls.objects.create(user_ptr=user, person=person)
- def has_right(self, right_name, session=None):
- return self.person.has_right(right_name, session=session)
-
- def has_perm(self, perm, model=None, session=None, obj=None):
- return self.person.has_right(perm, session=session, obj=None)
+ @property
+ def is_ishtaradmin(self):
+ is_ishtaradmin = getattr(self, "_is_ishtaradmin", None)
+ if is_ishtaradmin is not None:
+ return is_ishtaradmin
+ self._is_ishtaradmin = bool(
+ self.person.profiles.filter(
+ current=True, profile_type__txt_idx="administrator"
+ ).exists()
+ )
+ return self._is_ishtaradmin
+
+ def has_permission(self, permission, obj=None):
+ if permission == "ishtaradmin":
+ return self.is_ishtaradmin
+ if "." not in permission:
+ # TODO: perm delete
+ print(permission)
+ raise
+ if obj:
+ return self.user_ptr.has_perm(permission, obj)
+ return self.user_ptr.has_perm(permission)
+ """
+ res = (
+ bool(
+ self.profiles.filter(
+ current=True, profile_type__txt_idx=permission
+ ).count()
+ )
+ or bool(
+ self.profiles.filter(
+ current=True,
+ profile_type__groups__permissions__codename=permission,
+ ).count()
+ )
+ or bool(
+ self.ishtaruser.user_ptr.groups.filter(
+ permissions__codename__in=[permission]
+ ).count()
+ )
+ or bool(
+ self.ishtaruser.user_ptr.user_permissions.filter(
+ codename__in=[permission]
+ ).count()
+ )
+ )
+ return res
+ """
def full_label(self):
return self.person.full_label()
@@ -4557,7 +4536,7 @@ class Document(
icon_class="fa fa-pencil",
text=_("Bulk update"),
target="many",
- rights=["change_document", "change_own_document"],
+ rights=["ishtar_common.change_document", "ishtar_common.change_own_document"],
)
QUICK_ACTIONS = [
QA_EDIT,
@@ -4566,14 +4545,14 @@ class Document(
icon_class="fa fa-clone",
text=_("Duplicate"),
target="one",
- rights=["change_document", "change_own_document"],
+ rights=["ishtar_common.change_document", "ishtar_common.change_own_document"],
),
QuickAction(
url="document-qa-packaging",
icon_class="fa fa-gift",
text=_("Packaging"),
target="many",
- rights=["change_document", "change_own_document"],
+ rights=["ishtar_common.change_document", "ishtar_common.change_own_document"],
module="warehouse",
),
]
@@ -4937,7 +4916,7 @@ class Document(
actions = super(Document, self).get_extra_actions(request)
# is_locked = self.is_locked(request.user)
- can_edit_document = self.can_do(request, "change_document")
+ can_edit_document = self.can_do(request, "ishtar_common.change_document")
if not can_edit_document:
return actions
actions += [
@@ -4961,7 +4940,7 @@ class Document(
True,
)
)
- can_create_document = self.can_do(request, "add_document")
+ can_create_document = self.can_do(request, "ishtar_common.add_document")
if can_create_document:
actions += [
(