summaryrefslogtreecommitdiff
path: root/ishtar_common
diff options
context:
space:
mode:
Diffstat (limited to 'ishtar_common')
-rw-r--r--ishtar_common/models_common.py65
-rw-r--r--ishtar_common/templates/ishtar/blocks/sheet_geographic.html6
-rw-r--r--ishtar_common/templatetags/ishtar_helpers.py12
3 files changed, 76 insertions, 7 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
diff --git a/ishtar_common/templates/ishtar/blocks/sheet_geographic.html b/ishtar_common/templates/ishtar/blocks/sheet_geographic.html
index c5a413cc6..69cd67fcc 100644
--- a/ishtar_common/templates/ishtar/blocks/sheet_geographic.html
+++ b/ishtar_common/templates/ishtar/blocks/sheet_geographic.html
@@ -1,4 +1,4 @@
-{% load i18n window_field %}
+{% load i18n ishtar_helpers window_field %}
<table id='{{window_id}}-geographic-data' class="table table-striped">
<tr>
{% if permission_change_geo %}<th>&nbsp;</th>{% endif %}
@@ -13,7 +13,9 @@
</tr>
{% for geo in geo_item.geodata.all %}
<tr>
- {% if permission_change_geo %}<td><a href="#"><i class="fa fa-pencil"></i></a></td>{% endif %}
+ {% if permission_change_geo %}
+ <td><a href="#">{% if geo|can_edit_item:request %}<i class="fa fa-pencil"></i></a>{% else %}&ndash;{% endif %}</td>
+ {% endif %}
<td>{% if geo.id == geo_item.main_geodata_id %}<i class="fa fa-check-circle text-success" aria-hidden="true"></i>{% else %}&ndash;{% endif %}</td>
<td>{% if geo.data_type %}{{ geo.data_type }}{% else %}-{% endif %}</td>
<td>{{ geo.source_label }}</td>
diff --git a/ishtar_common/templatetags/ishtar_helpers.py b/ishtar_common/templatetags/ishtar_helpers.py
index 250b5719d..29ed57b08 100644
--- a/ishtar_common/templatetags/ishtar_helpers.py
+++ b/ishtar_common/templatetags/ishtar_helpers.py
@@ -54,3 +54,15 @@ def is_locked(item, user):
if not hasattr(item, "is_locked"):
return False
return item.is_locked(user)
+
+
+@register.filter
+def can_edit_item(item, context):
+ if hasattr(context, "request"): # WSGIRequest
+ request = context.request
+ elif "request" in context: # RequestContext
+ request = context['request']
+ else:
+ request = context
+ if item.can_edit(request):
+ return True