From ff8681360e589ff3c8e63dcef6ad0d31ac8ecab8 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Tue, 24 Sep 2019 10:33:07 +0200 Subject: Allow own locker to edit their locks --- ishtar_common/forms.py | 4 +++- ishtar_common/models.py | 9 ++++++++- ishtar_common/templates/ishtar/blocks/window_nav.html | 6 +++--- ishtar_common/templates/ishtar/sheet.html | 7 ++++++- ishtar_common/templatetags/ishtar_helpers.py | 7 +++++++ ishtar_common/templatetags/window_header.py | 1 + ishtar_common/views.py | 6 +++--- ishtar_common/views_item.py | 12 ++++++++++-- 8 files changed, 41 insertions(+), 11 deletions(-) (limited to 'ishtar_common') diff --git a/ishtar_common/forms.py b/ishtar_common/forms.py index c9c88b805..a7774df1f 100644 --- a/ishtar_common/forms.py +++ b/ishtar_common/forms.py @@ -383,11 +383,13 @@ class CustomFormSearch(forms.Form): if 'user' in kwargs: user = kwargs.pop('user') super(CustomFormSearch, self).__init__(*args, **kwargs) + self.request_user = user if user and 'pk' in self.fields: self.fields['pk'].widget.user = user class LockForm(object): + need_user_for_initialization = True associated_models = {} def clean(self): @@ -412,7 +414,7 @@ class LockForm(object): item = model.objects.get(pk=pk) except model.DoesNotExist: raise forms.ValidationError(_("Invalid selection.")) - if item.locked: + if item.is_locked(self.request_user): raise forms.ValidationError(_("This item is locked " "for edition.")) return self.cleaned_data diff --git a/ishtar_common/models.py b/ishtar_common/models.py index cb1325af1..f3ef8103c 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -1852,7 +1852,9 @@ class DocumentItem(object): return actions can_add_doc = self.can_do(request, 'add_document') - if can_add_doc and not getattr(self, "locked", False): + if can_add_doc and ( + not hasattr(self, "is_locked") or + not self.is_locked(request.user)): actions = [ ( reverse("create-document") + "?{}={}".format( @@ -2164,6 +2166,11 @@ class BaseHistorizedItem(StatisticItem, TemplateItem, FullSearch, Imported, def get_verbose_name(cls): return cls._meta.verbose_name + def is_locked(self, user=None): + if not user: + return self.locked + return self.locked and (not self.lock_user or self.lock_user != user) + def merge(self, item, keep_old=False): merge_model_objects(self, item, keep_old=keep_old) diff --git a/ishtar_common/templates/ishtar/blocks/window_nav.html b/ishtar_common/templates/ishtar/blocks/window_nav.html index c248bae82..60e620f16 100644 --- a/ishtar_common/templates/ishtar/blocks/window_nav.html +++ b/ishtar_common/templates/ishtar/blocks/window_nav.html @@ -1,4 +1,4 @@ -{% load i18n %} +{% load i18n ishtar_helpers %}
{% if previous or next %}
@@ -44,7 +44,7 @@ {% endif %}
{% block extra_actions %}{% endblock %} - {% if modify_url and not item.locked %} + {% if modify_url and not item|is_locked:current_user %} @@ -56,7 +56,7 @@ {{extra_text}} {% endfor %} - {% if delete_url and not item.locked %} + {% if delete_url and not item|is_locked:current_user %} diff --git a/ishtar_common/templates/ishtar/sheet.html b/ishtar_common/templates/ishtar/sheet.html index 5676a4419..1c3ae37a0 100644 --- a/ishtar_common/templates/ishtar/sheet.html +++ b/ishtar_common/templates/ishtar/sheet.html @@ -1,4 +1,4 @@ -{% load i18n %}{% block main_head %} +{% load i18n ishtar_helpers %}{% block main_head %} {% block title %}Ishtar{% if APP_NAME %} - {{APP_NAME}}{%endif%}{% endblock %} @@ -121,12 +121,17 @@
{% if item.locked %} {% endif %} {% block content %} diff --git a/ishtar_common/templatetags/ishtar_helpers.py b/ishtar_common/templatetags/ishtar_helpers.py index b6040487a..51f5e722e 100644 --- a/ishtar_common/templatetags/ishtar_helpers.py +++ b/ishtar_common/templatetags/ishtar_helpers.py @@ -22,3 +22,10 @@ def file_content(value): if value: return mark_safe(value.read()) return "" + + +@register.filter +def is_locked(item, user): + if not hasattr(item, "is_locked"): + return False + return item.is_locked(user) diff --git a/ishtar_common/templatetags/window_header.py b/ishtar_common/templatetags/window_header.py index 3c77ccf43..583101e66 100644 --- a/ishtar_common/templatetags/window_header.py +++ b/ishtar_common/templatetags/window_header.py @@ -27,6 +27,7 @@ def window_nav(context, item, window_id, show_url, modify_url='', histo_url='', modify_url_value = modify_url delete_url = getattr(item, "DELETE_URL", False) return { + 'current_user': context['request'].user, 'show_url': show_url, 'modify_url': modify_url_value, 'delete_url': delete_url, diff --git a/ishtar_common/views.py b/ishtar_common/views.py index 44831ba9f..d8474e6b0 100644 --- a/ishtar_common/views.py +++ b/ishtar_common/views.py @@ -92,7 +92,7 @@ def wizard_is_available(wizard, request, model, pk): if not q.count(): raise Http404() obj = q.all()[0] - if hasattr(obj, "locked") and obj.locked: + if hasattr(obj, "is_locked") and obj.is_locked(request.user): raise Http404() return obj @@ -2277,9 +2277,9 @@ class QAItemEditForm(QAItemForm): request, *args, **kwargs) if redirected: return redirected - if hasattr(self.model, "locked"): + if hasattr(self.model, "is_locked"): for item in self.items: - if item.locked: + if item.is_locked(request.user): redirected = HttpResponseRedirect( reverse("qa-not-available", args=["locked"])) return redirected diff --git a/ishtar_common/views_item.py b/ishtar_common/views_item.py index 4e29a2bcf..6f5dba236 100644 --- a/ishtar_common/views_item.py +++ b/ishtar_common/views_item.py @@ -1159,6 +1159,7 @@ def _get_data_from_query(items, query_table_cols, extra_request_keys, values += ['point_x', 'point_y'] if hasattr(items.model, "locked"): values.append("locked") + values.append("lock_user_id") data_list = items.values_list(*values) return data_list @@ -1233,6 +1234,7 @@ def _get_data_from_query_old(items, query_table_cols, request, data.append(u" & ".join(my_vals) or u"") if has_lock: data.append(item.locked) + data.append(item.lock_user_id) datas.append(data) return datas @@ -1860,7 +1862,10 @@ def get_item(model, func_name, default_name, extra_request_keys=None, "" link_ext_template = '' lock = ' ' + own_lock = ' ' has_locks = hasattr(model, "locked") + current_user_id = request.user and request.user.id if data_type.startswith("json"): rows = [] if data_type == 'json-map': @@ -1881,8 +1886,11 @@ def get_item(model, func_name, default_name, extra_request_keys=None, lnk_template = link_template lnk = lnk_template.format( reverse('show-' + default_name, args=[data[0], ''])) - if has_locks and data[-1]: - lnk = lnk.replace('', lock) + if has_locks and data[-2]: + if data[-1] == current_user_id: + lnk = lnk.replace('', own_lock) + else: + lnk = lnk.replace('', lock) else: lnk = lnk.replace('', "") except NoReverseMatch: -- cgit v1.2.3