summaryrefslogtreecommitdiff
path: root/ishtar_common/models_common.py
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2022-05-09 13:16:58 +0200
committerÉtienne Loks <etienne.loks@iggdrasil.net>2022-12-12 12:21:00 +0100
commit12de821ded4f964c89d1ac758701bcaf4750e7de (patch)
tree232c6731b19d3a860776a23beeb485ebcc5500ba /ishtar_common/models_common.py
parent5caf4058758d0e5640f1e92d919ffa3fcae0ba27 (diff)
downloadIshtar-12de821ded4f964c89d1ac758701bcaf4750e7de.tar.bz2
Ishtar-12de821ded4f964c89d1ac758701bcaf4750e7de.zip
Geodata: filter edit permissions for geo items
Diffstat (limited to 'ishtar_common/models_common.py')
-rw-r--r--ishtar_common/models_common.py65
1 files changed, 60 insertions, 5 deletions
diff --git a/ishtar_common/models_common.py b/ishtar_common/models_common.py
index 07aaa18ea..12a5db0df 100644
--- a/ishtar_common/models_common.py
+++ b/ishtar_common/models_common.py
@@ -1716,6 +1716,17 @@ class OwnPerms(object):
perm = "view_" + self.SLUG
return self.can_do(request, perm)
+ def can_edit(self, request):
+ if not getattr(request.user, "ishtaruser", None):
+ return False
+ ishtaruser = request.user.ishtaruser
+ slug = self.LONG_SLUG if hasattr(self, "LONG_SLUG") else self.SLUG
+ if ishtaruser.has_perm("change_" + slug, session=request.session):
+ return True
+ if not ishtaruser.has_perm("change_own_" + slug, session=request.session):
+ return False
+ return self.is_own(ishtaruser)
+
def can_do(self, request, action_name):
"""
Check permission availability for the current object.
@@ -1881,7 +1892,7 @@ class OwnPerms(object):
def _get_query_owns_dicts(cls, ishtaruser):
"""
List of query own dict to construct the query.
- Each dict are join with an AND operator, each dict key, values are
+ Each dict is joined with an AND operator, each dict key, values are
joined with OR operator
"""
return []
@@ -2087,7 +2098,9 @@ GEOMETRY_TYPE_LBL = {
}
-class GeoVectorData(Imported):
+class GeoVectorData(Imported, OwnPerms):
+ SLUG = "geovectordata"
+
name = models.TextField(_("Name"), default="-")
source_content_type = models.ForeignKey(
ContentType, related_name="content_type_geovectordata", on_delete=models.CASCADE
@@ -2166,6 +2179,45 @@ class GeoVectorData(Imported):
name += f" ({str(self.data_type).lower()})"
return name
+ def is_own(self, ishtaruser, alt_query_own=None):
+ ct = self.source_content_type
+ model = apps.get_model(ct.app_label, ct.model)
+ if not hasattr(model, "_get_query_owns_dicts"):
+ return False
+ sub_q = model.get_query_owns(ishtaruser)
+ if not sub_q:
+ return False
+ return self.source_id in list(
+ model.objects.filter(sub_q).values_list("id", flat=True)
+ )
+
+ @classmethod
+ def get_query_owns(cls, ishtaruser):
+ q = None
+ for app_label, model_name in (
+ ("archaeological_operations", "Operation"),
+ ("archaeological_operations", "ArchaeologicalSite"),
+ ("archaeological_context_records", "ContextRecord"),
+ ("archaeological_finds", "BaseFind"),
+ ("archaeological_warehouse", "Warehouse"),
+ ("archaeological_warehouse", "Container"),
+ ):
+ model = apps.get_model(app_label, model_name)
+ sub_q = cls._construct_query_own(
+ "", model._get_query_owns_dicts(ishtaruser)
+ )
+ q2 = Q(
+ source_id__in=list(sub_q.values_list("id", flat=True)),
+ source_content_type__app_label=app_label,
+ source_content_type__model=model_name.lower(),
+ )
+ if not q:
+ q = q2
+ else:
+ q |= q2
+
+ return q
+
@property
def source_label(self):
return str(self.source)
@@ -2347,9 +2399,12 @@ class GeoVectorData(Imported):
for coord in feature["geometry"]["coordinates"]
]
geojson_dct["features"] = features
- geojson_dct["link_template"] = simple_link_to_window(self).replace(
- "999999", "<pk>"
- )
+ try:
+ geojson_dct["link_template"] = simple_link_to_window(self).replace(
+ "999999", "<pk>"
+ )
+ except NoReverseMatch:
+ pass
geojson = json.dumps(geojson_dct)
return geojson