summaryrefslogtreecommitdiff
path: root/ishtar_common/models_common.py
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2025-02-14 17:49:37 +0100
committerÉtienne Loks <etienne.loks@iggdrasil.net>2025-02-19 14:43:48 +0100
commit4f60b4805a7eac04c2a8ec2116a245dbeec3c822 (patch)
tree561f87e11ae60c96320523c80c6317ff8f1d2f99 /ishtar_common/models_common.py
parent94f357939957dc8a5de453224913dbecdc4dc9db (diff)
downloadIshtar-4f60b4805a7eac04c2a8ec2116a245dbeec3c822.tar.bz2
Ishtar-4f60b4805a7eac04c2a8ec2116a245dbeec3c822.zip
✨ generate_permissions
manage: - possession (direct, creation, basket) - heritage - areas association - requests ({USER} special syntax)
Diffstat (limited to 'ishtar_common/models_common.py')
-rw-r--r--ishtar_common/models_common.py107
1 files changed, 65 insertions, 42 deletions
diff --git a/ishtar_common/models_common.py b/ishtar_common/models_common.py
index 28f5aba00..920b71584 100644
--- a/ishtar_common/models_common.py
+++ b/ishtar_common/models_common.py
@@ -17,11 +17,12 @@ import re
import shutil
import tempfile
import time
+from unidecode import unidecode
from django import forms
from django.apps import apps
from django.conf import settings
-from django.contrib.auth.models import User
+from django.contrib.auth.models import Permission, User
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.gis.db import models
@@ -49,6 +50,8 @@ from ishtar_common.utils import (
get_image_path,
get_columns_from_class,
human_date,
+ HistoryError,
+ SearchAltName,
SheetItem
)
from simple_history.models import HistoricalRecords as BaseHistoricalRecords
@@ -56,7 +59,8 @@ from simple_history.signals import (
post_create_historical_record,
pre_create_historical_record,
)
-from unidecode import unidecode
+
+from guardian.models import UserObjectPermission
from ishtar_common.data_importer import post_importer_action, ImporterError
from ishtar_common.model_managers import TypeManager
@@ -64,19 +68,20 @@ from ishtar_common.model_merging import merge_model_objects
from ishtar_common.models_imports import Import
from ishtar_common.templatetags.link_to_window import simple_link_to_window
from ishtar_common.utils import (
- get_cache,
+ cached_label_changed,
disable_for_loaddata,
+ duplicate_item,
+ external_id_changed,
+ GENERAL_TYPE_PREFIX,
get_all_field_names,
+ get_cache,
+ get_current_profile,
+ get_generated_id,
merge_tsvectors,
- cached_label_changed,
- external_id_changed,
+ OwnPerms,
post_save_geo,
post_save_geodata,
task,
- duplicate_item,
- get_generated_id,
- get_current_profile,
- OwnPerms
)
@@ -513,12 +518,6 @@ class GeneralType(Cached, models.Model):
res[parent_id].append((item["id"], item["label"]))
return res
- PREFIX = "&#x2502; "
- PREFIX_EMPTY = "&nbsp; "
- PREFIX_MEDIUM = "&#x251C; "
- PREFIX_LAST = "&#x2514; "
- PREFIX_CODES = ["\u2502", "\u251C", "\u2514"]
-
@classmethod
def _get_childs(
cls,
@@ -559,20 +558,20 @@ class GeneralType(Cached, models.Model):
cprefix -= 1
if not cprefix:
if (idx + 1) == total:
- p += cls.PREFIX_LAST
+ p += GENERAL_TYPE_PREFIX["prefix_last"]
else:
- p += cls.PREFIX_MEDIUM
+ p += GENERAL_TYPE_PREFIX["prefix_medium"]
elif is_last:
if mylast_of:
clast = mylast_of.pop(0)
if clast:
- p += cls.PREFIX_EMPTY
+ p += GENERAL_TYPE_PREFIX["prefix_empty"]
else:
- p += cls.PREFIX
+ p += GENERAL_TYPE_PREFIX["prefix"]
else:
- p += cls.PREFIX_EMPTY
+ p += GENERAL_TYPE_PREFIX["prefix_empty"]
else:
- p += cls.PREFIX
+ p += GENERAL_TYPE_PREFIX["prefix"]
lst.append((child[0], SafeText(p + child[1])))
clast_of = last_of[:]
clast_of.append(idx + 1 == total)
@@ -1149,17 +1148,6 @@ class FullSearch(models.Model):
return changed
-class SearchAltName(object):
- def __init__(
- self, search_key, search_query, extra_query=None, distinct_query=False, related_name=None
- ):
- self.search_key = search_key
- self.search_query = search_query
- self.extra_query = extra_query or {}
- self.distinct_query = distinct_query
- self.related_name = related_name
-
-
class Imported(models.Model):
imports = models.ManyToManyField(
Import, blank=True, related_name="imported_%(app_label)s_%(class)s",
@@ -1386,14 +1374,6 @@ class FixAssociated:
setattr(item, subkey, new_value)
-class HistoryError(Exception):
- def __init__(self, value):
- self.value = value
-
- def __str__(self):
- return repr(self.value)
-
-
class HistoricalRecords(BaseHistoricalRecords):
def get_extra_fields(self, model, fields):
def get_history_m2m(attr):
@@ -1561,6 +1541,7 @@ class BaseHistorizedItem(
EXTERNAL_ID_KEY = ""
EXTERNAL_ID_DEPENDENCIES = []
HISTORICAL_M2M = []
+ UPPER_PERMISSIONS = []
history_modifier = models.ForeignKey(
User,
@@ -1634,8 +1615,50 @@ class BaseHistorizedItem(
return cls._meta.verbose_name
@classmethod
- def get_ids_from_upper_permissions(cls, user_id):
- return []
+ def get_ids_from_upper_permissions(cls, user_id, base_permissions):
+ if not cls.UPPER_PERMISSIONS:
+ return []
+ ProfileType = apps.get_model("ishtar_common", "ProfileType")
+ item_ids = []
+ for model, attr in cls.UPPER_PERMISSIONS:
+ if isinstance(model, tuple):
+ app_label, model_name = model
+ model = apps.get_model(app_label, model_name)
+ permissions = list(set([
+ "_".join(permission.codename.split("_")[:-1])
+ + f"_{model._meta.model_name}"
+ for permission in base_permissions
+ ]))
+ q = ProfileType.objects.filter(
+ user_profiles__person__ishtaruser=user_id,
+ groups__permissions__codename__in=permissions
+ )
+ lst = []
+ if not q.count():
+ # no permissions associated for upstream model get direct attachement
+ lst = model.objects.filter(
+ ishtar_users__pk=user_id
+ ).values_list("pk", flat=True)
+ else:
+ perms = []
+ for codename in permissions:
+ perms += [
+ perm
+ for perm in Permission.objects.filter(
+ codename=codename).all()
+ ]
+ lst = []
+ for permission in perms:
+ lst += list(
+ UserObjectPermission.objects.filter(
+ permission=permission,
+ user_id=user_id
+ ).values_list("object_pk", flat=True)
+ )
+ item_ids += cls.objects.filter(
+ **{f"{attr}__in": lst}
+ ).values_list("pk", flat=True)
+ return list(set(item_ids))
def is_locked(self, user=None):
if not user: