diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2025-03-13 13:30:02 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2025-03-13 15:48:10 +0100 |
commit | ce71f7f286819ee1399fef5f1819f9cc812149a6 (patch) | |
tree | 958d29dbd53c737182fadb61705daf50b0f6a81e | |
parent | 61cb345d6e39eac393c6fd8e4a8246b5221c69a9 (diff) | |
download | Ishtar-ce71f7f286819ee1399fef5f1819f9cc812149a6.tar.bz2 Ishtar-ce71f7f286819ee1399fef5f1819f9cc812149a6.zip |
✨ sheets: refactor document listing - add edit and unlink actions (refs #6221)
23 files changed, 195 insertions, 70 deletions
diff --git a/archaeological_context_records/templates/ishtar/sheet_contextrecord.html b/archaeological_context_records/templates/ishtar/sheet_contextrecord.html index 29d7f80c6..f43df6c4e 100644 --- a/archaeological_context_records/templates/ishtar/sheet_contextrecord.html +++ b/archaeological_context_records/templates/ishtar/sheet_contextrecord.html @@ -346,12 +346,9 @@ {% if display_documents %} <div class="tab-pane fade" id="{{window_id}}-documents" role="tabpanel" aria-labelledby="{{window_id}}-documents-tab"> - {% trans "Document from this context record" as cr_docs %} - {% if permission_view_own_document or permission_view_document %} - {% if item.documents.count %} - {% dynamic_table_document cr_docs 'documents' 'context_records' item.pk '' output %} - {% endif %} - {% endif %} + {% with permission_change_item=item|can_edit_item:request %} + {% include "ishtar/blocks/sheet_document_list.html" %} + {% endwith %} </div> {% endif %} diff --git a/archaeological_finds/models_treatments.py b/archaeological_finds/models_treatments.py index 0c684d98a..69a8b899c 100644 --- a/archaeological_finds/models_treatments.py +++ b/archaeological_finds/models_treatments.py @@ -196,14 +196,13 @@ class AssociatedFindBasket: class Treatment( DashboardFormItem, AssociatedFindBasket, - ValueGetter, DocumentItem, BaseHistorizedItem, CompleteIdentifierItem, ImageModel, OwnPerms, - ShortMenuItem, - SheetItem, + ValueGetter, + MainItem, ): SLUG = "treatment" APP = "archaeological-finds" diff --git a/archaeological_finds/templates/ishtar/sheet_exhibition.html b/archaeological_finds/templates/ishtar/sheet_exhibition.html index 820391a9e..76cf7c495 100644 --- a/archaeological_finds/templates/ishtar/sheet_exhibition.html +++ b/archaeological_finds/templates/ishtar/sheet_exhibition.html @@ -78,8 +78,9 @@ {% if display_documents %} - {% trans "Associated documents" as associated_docs %} - {% dynamic_table_document associated_docs 'documents' 'treatment_files' item.pk '' output %} + {% with permission_change_item=item|can_edit_item:request %} + {% include "ishtar/blocks/sheet_document_list.html" %} + {% endwith %} {% endif %} {% if item.administrative_act.count %} diff --git a/archaeological_finds/templates/ishtar/sheet_find.html b/archaeological_finds/templates/ishtar/sheet_find.html index d38b0060f..9b439825c 100644 --- a/archaeological_finds/templates/ishtar/sheet_find.html +++ b/archaeological_finds/templates/ishtar/sheet_find.html @@ -280,13 +280,9 @@ {% if display_documents %} <div class="tab-pane fade" id="{{window_id}}-documents" role="tabpanel" aria-labelledby="{{window_id}}-documents-tab"> - {% trans "Associated documents" as finds_docs %} - {% if item.documents.count %} - {% dynamic_table_document finds_docs 'documents' 'finds' item.pk '' output %} - {% elif item.documents_list %} - <h3>{{finds_docs}}</h3> - {% include "ishtar/blocks/api_document_list.html" %} - {% endif %} + {% with permission_change_item=item|can_edit_item:request %} + {% include "ishtar/blocks/sheet_document_list.html" %} + {% endwith %} </div> {% endif %} diff --git a/archaeological_finds/templates/ishtar/sheet_museum_find.html b/archaeological_finds/templates/ishtar/sheet_museum_find.html index 2f313f0a1..fb5e2adca 100644 --- a/archaeological_finds/templates/ishtar/sheet_museum_find.html +++ b/archaeological_finds/templates/ishtar/sheet_museum_find.html @@ -304,13 +304,9 @@ {% if display_documents %} <div class="tab-pane fade" id="{{window_id}}-documents" role="tabpanel" aria-labelledby="{{window_id}}-documents-tab"> - {% trans "Associated documents" as finds_docs %} - {% if item.documents.count %} - {% dynamic_table_document finds_docs 'documents' 'finds' item.pk '' output %} - {% elif item.documents_list %} - <h3>{{finds_docs}}</h3> - {% include "ishtar/blocks/api_document_list.html" %} - {% endif %} + {% with permission_change_item=item|can_edit_item:request %} + {% include "ishtar/blocks/sheet_document_list.html" %} + {% endwith %} </div> {% endif %} diff --git a/archaeological_finds/templates/ishtar/sheet_treatment.html b/archaeological_finds/templates/ishtar/sheet_treatment.html index 7fa2097f0..ae180dd5e 100644 --- a/archaeological_finds/templates/ishtar/sheet_treatment.html +++ b/archaeological_finds/templates/ishtar/sheet_treatment.html @@ -1,5 +1,5 @@ {% extends "ishtar/sheet.html" %} -{% load i18n l10n window_field from_dict link_to_window window_tables window_ope_tables window_header humanize %} +{% load i18n l10n ishtar_helpers window_field from_dict link_to_window window_tables window_ope_tables window_header humanize %} {% block head_title %}<strong>{% trans "Treatment" %}</strong> - {{ item.cached_label|default:"" }}{% endblock %} @@ -8,6 +8,12 @@ {% endblock %} {% block content %} +{# trick to set to null non existing variable #} +{% with permission_view_document=permission_view_document %} +{% with permission_view_own_document=permission_view_own_document %} +{% with can_view_documents=permission_view_own_document|or_:permission_view_document %} +{% with has_documents=item|safe_or:"documents.count|documents_list"|safe_and_not:"documents_not_available" %} +{% with display_documents=can_view_documents|and_:has_documents %} <ul class="nav nav-tabs" id="{{window_id}}-tabs" role="tablist"> <li class="nav-item"> @@ -24,7 +30,7 @@ {% trans "Finds" %} </a> </li> - {% if item.documents.count %} + {% if display_documents %} <li class="nav-item"> <a class="nav-link" id="{{window_id}}-documents-tab" data-toggle="tab" href="#{{window_id}}-documents" role="tab" @@ -130,11 +136,12 @@ </div> - {% if item.documents.count %} + {% if display_documents %} <div class="tab-pane fade" id="{{window_id}}-documents" role="tabpanel" aria-labelledby="{{window_id}}-documents-tab"> - {% trans "Associated documents" as treat_docs %} - {% dynamic_table_document treat_docs 'documents' 'treatments' item.pk '' output %} + {% with permission_change_item=item|can_edit_item:request %} + {% include "ishtar/blocks/sheet_document_list.html" %} + {% endwith %} </div> {% endif %} @@ -148,5 +155,5 @@ </div> -{% endwith %} +{% endwith %}{% endwith %}{% endwith %}{% endwith %}{% endwith %}{% endwith %} {% endblock %} diff --git a/archaeological_finds/templates/ishtar/sheet_treatmentfile.html b/archaeological_finds/templates/ishtar/sheet_treatmentfile.html index 6cca72570..c6f5d3056 100644 --- a/archaeological_finds/templates/ishtar/sheet_treatmentfile.html +++ b/archaeological_finds/templates/ishtar/sheet_treatmentfile.html @@ -79,8 +79,9 @@ {% endif %} {% if display_documents %} -{% trans "Associated documents" as associated_docs %} -{% dynamic_table_document associated_docs 'documents' 'treatment_files' item.pk '' output %} + {% with permission_change_item=item|can_edit_item:request %} + {% include "ishtar/blocks/sheet_document_list.html" %} + {% endwith %} {% endif %} {% if item.administrative_act.count %} diff --git a/archaeological_operations/templates/ishtar/sheet_operation.html b/archaeological_operations/templates/ishtar/sheet_operation.html index 6d5fa18e5..4d078a998 100644 --- a/archaeological_operations/templates/ishtar/sheet_operation.html +++ b/archaeological_operations/templates/ishtar/sheet_operation.html @@ -401,13 +401,9 @@ {% if display_documents %} <div class="tab-pane fade" id="{{window_id}}-documents" role="tabpanel" aria-labelledby="{{window_id}}-documents-tab"> - {% trans "Document from this operation" as operation_docs %} - {% if item.documents.count and not item.documents_not_available %} - {% dynamic_table_document operation_docs 'documents' 'operations' item.pk '' output %} - {% elif item.documents_list %} - <h3>{{operation_docs}}</h3> - {% include "ishtar/blocks/api_document_list.html" %} - {% endif %} + {% with permission_change_item=item|can_edit_item:request %} + {% include "ishtar/blocks/sheet_document_list.html" %} + {% endwith %} </div> {% endif %} diff --git a/archaeological_warehouse/templates/ishtar/sheet_container.html b/archaeological_warehouse/templates/ishtar/sheet_container.html index a3acd20e7..55b1d9fa6 100644 --- a/archaeological_warehouse/templates/ishtar/sheet_container.html +++ b/archaeological_warehouse/templates/ishtar/sheet_container.html @@ -295,12 +295,9 @@ {% if display_documents %} <div class="tab-pane fade" id="{{window_id}}-documents" role="tabpanel" aria-labelledby="{{window_id}}-documents-tab"> - {% trans "Document for this container" as ct_docs %} - {% if permission_view_own_document or permission_view_document %} - {% if item.documents.count %} - {% dynamic_table_document ct_docs 'documents' 'containers' item.pk '' output %} - {% endif %} - {% endif %} + {% with permission_change_item=item|can_edit_item:request %} + {% include "ishtar/blocks/sheet_document_list.html" %} + {% endwith %} </div> {% endif %} {% if not is_external and PROFILE.locate_warehouses %} diff --git a/archaeological_warehouse/templates/ishtar/sheet_warehouse.html b/archaeological_warehouse/templates/ishtar/sheet_warehouse.html index 68e0c0174..7d086d52f 100644 --- a/archaeological_warehouse/templates/ishtar/sheet_warehouse.html +++ b/archaeological_warehouse/templates/ishtar/sheet_warehouse.html @@ -260,12 +260,9 @@ {% if display_documents %} <div class="tab-pane fade" id="{{window_id}}-documents" role="tabpanel" aria-labelledby="{{window_id}}-documents-tab"> - {% trans "Document for this warehouse" as w_docs %} - {% if permission_view_own_document or permission_view_document %} - {% if item.documents.count %} - {% dynamic_table_document w_docs 'documents' 'warehouses' item.pk '' output %} - {% endif %} - {% endif %} + {% with permission_change_item=item|can_edit_item:request %} + {% include "ishtar/blocks/sheet_document_list.html" %} + {% endwith %} </div> {% endif %} {% if not is_external and PROFILE.locate_warehouses %} diff --git a/ishtar_common/models.py b/ishtar_common/models.py index ae0656c6d..6a793e326 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -2480,6 +2480,10 @@ class Area(HierarchicalType, DocumentItem, MainItem): return self.label return "{} ({})".format(self.label, self.reference) + def can_edit(self, request): + print("TODO: define specific permissions for areas") + return False + @classmethod def get_or_create_by_towns(cls, towns, get_geo=False): if hasattr(towns, "all"): # queryset diff --git a/ishtar_common/models_common.py b/ishtar_common/models_common.py index 0b1be5313..8f6ff5338 100644 --- a/ishtar_common/models_common.py +++ b/ishtar_common/models_common.py @@ -3605,6 +3605,10 @@ class Town(GeographicItem, Imported, DocumentItem, MainItem, models.Model): def history_compress(self): return {"numero_insee": self.numero_insee, "year": self.year or ""} + def can_edit(self, request): + print("TODO: define specific permissions for towns") + return False + @classmethod def get_documentation_string(cls): """ diff --git a/ishtar_common/templates/ishtar/blocks/api_document_list.html b/ishtar_common/templates/ishtar/blocks/api_document_list.html index 251885a19..2b2380496 100644 --- a/ishtar_common/templates/ishtar/blocks/api_document_list.html +++ b/ishtar_common/templates/ishtar/blocks/api_document_list.html @@ -2,7 +2,9 @@ id="{{window_id}}-docs-table"> {% for values in item.documents_list %}{% if not forloop.counter0 %} <thead> - {% for value in values %}<th class="text-center">{{value}}</th>{% endfor %} + <tr> + {% for value in values %}<th class="text-center">{{value}}</th>{% endfor %} + </tr> </thead> {% else %} <tr> diff --git a/ishtar_common/templates/ishtar/blocks/sheet_document_list.html b/ishtar_common/templates/ishtar/blocks/sheet_document_list.html new file mode 100644 index 000000000..1a1849636 --- /dev/null +++ b/ishtar_common/templates/ishtar/blocks/sheet_document_list.html @@ -0,0 +1,53 @@ +{% load i18n l10n ishtar_helpers link_to_window %} + {% with can_edit_current_item=item|can_edit_item:request %} + <h3>{% trans "Associated documents" %}</h3> + {% if item.documents.count %} + <table class='table table-striped datatables' + id="{{window_id}}-docs-table"> + {% for doc in item.documents.all %}{% if not forloop.counter0 %} + <thead> + <tr> + <th> </th> + <th>{% trans "Title" %}</th> + <th>{% trans "Type" %}</th> + <th>{% trans "Authors" %}</th> + <th>{% trans "Numerical ressource" %}</th> + {% if not IS_HISTORY and permission_change_document %}<th> </th>{% endif %} + {% if not IS_HISTORY and permission_change_item %}<th> </th>{% endif %} + </tr> + </thead> + {% endif %} + <tr> + <td> + <a class="display_details" href="#" onclick="load_window('{% url 'show-document' doc.id '' %}')"><i class="fa fa-info-circle" aria-hidden="true"></i></a> + </td> + <td> + {{doc.title|default:'-'}} + </td> + <td> + {{doc.source_type|default:'-'}} + </td> + <td> + {{doc.authors_list|default:'-'}} + </td> + <td>{% if doc.associated_url %} + <a href='{{doc.associated_url}}' target="_blank">{% trans "Link" %} <i class="fa fa-external-link" aria-hidden="true"></i></a> + {% else%}-{% endif %} + </td> + {% if not IS_HISTORY and permission_change_document %}<td> + {% autoescape off %}{% if doc|can_edit_item:request %} + <a class="btn btn-success btn-sm" title="{% trans 'Edit' %}" href="{% url 'edit-document' doc.pk %}"><i class="fa fa-pencil"></i></a> + {% else %}–{% endif %}{% endautoescape %} + </td>{% endif %} + {% if not IS_HISTORY and permission_change_item %}<td> + {% autoescape off %} + <a class="btn-qa btn btn-danger btn-sm wait-button" title="{% trans 'Unlink' %}" href="#" data-target='{% url 'document-qa-unlink' doc.pk model_slug item.pk %}'><i class="fa fa-chain-broken" aria-hidden="true"></i></a> + {% endautoescape %} + </td>{% endif %} + </tr> + {% endfor %} + </table> + {% elif item.documents_list %} + {% include "ishtar/blocks/api_document_list.html" %} + {% endif %} + {% endwith %} diff --git a/ishtar_common/templates/ishtar/blocks/sheet_geographic.html b/ishtar_common/templates/ishtar/blocks/sheet_geographic.html index 12846ef78..6212c9573 100644 --- a/ishtar_common/templates/ishtar/blocks/sheet_geographic.html +++ b/ishtar_common/templates/ishtar/blocks/sheet_geographic.html @@ -2,7 +2,6 @@ {% with search_url=item.get_search_url %} <table id='{{window_id}}-geographic-data' class="table table-striped"> <tr> - {% if not IS_HISTORY and permission_change_geo %}<th> </th>{% endif %} <th>{% trans "Main" %}</th> <th>{% trans "Data type" %}</th> <th>{% trans "Source" %}</th> @@ -13,14 +12,10 @@ <th>{% trans "Acquisition date" %}</th> <th>{% trans "Comment" %}</th> {% if not IS_HISTORY and permission_change_geo %}<th> </th>{% endif %} + {% if not IS_HISTORY and permission_change_geo %}<th> </th>{% endif %} </tr> {% for geo in geo_item.geodata.all %} <tr> - {% if not IS_HISTORY and permission_change_geo %}<td> - {% autoescape off %} - <a title="{% trans 'Edit' %}" href="{% url 'edit-geo' geo.pk %}{% if search_url or find_id %}?{% endif %}{% if find_id %}find_id={{find_id|unlocalize}}&{% endif %}{% if search_url %}back_url={{search_url}}%3Fopen_item={{geo_item.pk|unlocalize}}{% endif %}">{% if geo|can_edit_item:request %}<i class="fa fa-pencil"></i></a>{% else %}–{% endif %} - {% endautoescape %} - </td>{% endif %} <td>{% if geo.id == geo_item.main_geodata_id %}<i class="fa fa-check-circle text-success" aria-hidden="true"></i>{% else %}–{% endif %}</td> <td>{% if geo.data_type %}{{ geo.data_type }}{% else %}-{% endif %}</td> <td>{{ geo.source_label }}</td> @@ -30,11 +25,16 @@ <td>{% if geo.provider %}{{ geo.provider }}{% else %}-{% endif %}</td> <td>{% if geo.acquisition_date %}{{ geo.acquisition_date|date:"DATE_FORMAT"|default:"-" }}{% else %}-{% endif %}</td> <td>{% if geo.comment %}{{ geo.comment }}{% else %}-{% endif %}</td> + {% if not IS_HISTORY and permission_change_geo %}<td> + {% autoescape off %} + {% if geo|can_edit_item:request %}<a class="btn btn-success btn-sm" title="{% trans 'Edit' %}" href="{% url 'edit-geo' geo.pk %}{% if search_url or find_id %}?{% endif %}{% if find_id %}find_id={{find_id|unlocalize}}&{% endif %}{% if search_url %}back_url={{search_url}}%3Fopen_item={{geo_item.pk|unlocalize}}{% endif %}"><i class="fa fa-pencil"></i></a>{% else %}–{% endif %} + {% endautoescape %} + </td>{% endif %} {% if not IS_HISTORY and permission_change_geo and output != "ODT" and output != "PDF" %}<td> {% autoescape off %} - <a title="{% trans 'Delete' %}" + {% if geo|can_edit_item:request %}<a class="btn btn-danger btn-sm" title="{% trans 'Delete' %}" href="{% url 'delete-geo' geo.pk %}{% if search_url or find_id %}?{% endif %}{% if find_id %}find_id={{find_id|unlocalize}}&{% endif %}{% if search_url %}back_url={{search_url}}%3Fopen_item={{geo_item.pk|unlocalize}}{% endif %}"> - {% if geo|can_edit_item:request %}<i class="fa fa-trash text-danger"></i></a>{% else %}–{% endif %} + <i class="fa fa-trash"></i></a>{% else %}–{% endif %} {% endautoescape %} </td>{% endif %} </tr> diff --git a/ishtar_common/templates/ishtar/sheet_area.html b/ishtar_common/templates/ishtar/sheet_area.html index c4f67885e..682722dcd 100644 --- a/ishtar_common/templates/ishtar/sheet_area.html +++ b/ishtar_common/templates/ishtar/sheet_area.html @@ -92,8 +92,9 @@ {% endcomment %} {% if display_documents and item.documents.count %} - {% trans "Documents" as area_docs %} - {% dynamic_table_document area_docs 'documents' 'areas' item.pk '' output %} + {% with permission_change_item=item|can_edit_item:request %} + {% include "ishtar/blocks/sheet_document_list.html" %} + {% endwith %} {% endif %} </div> @@ -126,4 +127,4 @@ $(document).ready( function () { {% endwith %} {% endwith %} {% endwith %} {% endwith %} {% endwith %} {% endwith %} {% endwith %} {% endwith %} -{% endblock %}
\ No newline at end of file +{% endblock %} diff --git a/ishtar_common/templates/ishtar/sheet_document.html b/ishtar_common/templates/ishtar/sheet_document.html index 02c32638b..ca382f137 100644 --- a/ishtar_common/templates/ishtar/sheet_document.html +++ b/ishtar_common/templates/ishtar/sheet_document.html @@ -58,7 +58,7 @@ <h4 class="col-12">{% trans "Content" %}</h4> {% trans "File" context "Not directory" as file_label %} {% field_flex_file file_label item.associated_file %} - {% trans "Web link" as weblink_label %} + {% trans "Numerical ressource (web address)" as weblink_label %} {% field_flex_url weblink_label item.associated_url %} {% field_flex_multiple_obj "Tags" item 'tags' %} {% endif %} diff --git a/ishtar_common/templates/ishtar/sheet_town.html b/ishtar_common/templates/ishtar/sheet_town.html index e92c03b54..235182993 100644 --- a/ishtar_common/templates/ishtar/sheet_town.html +++ b/ishtar_common/templates/ishtar/sheet_town.html @@ -116,8 +116,9 @@ {% endif %} {% if display_documents and item.documents.count %} - {% trans "Documents" as town_docs %} - {% dynamic_table_document town_docs 'documents' 'towns' item.pk '' output %} + {% with permission_change_item=item|can_edit_item:request %} + {% include "ishtar/blocks/sheet_document_list.html" %} + {% endwith %} {% endif %} {% if perm_files and item.file.count %} @@ -167,4 +168,4 @@ $(document).ready( function () { {% endwith %} {% endwith %} {% endwith %} {% endwith %} {% endwith %} {% endwith %} {% endwith %} {% endwith %} {% endwith %} {% endwith %} {% endwith %} -{% endblock %}
\ No newline at end of file +{% endblock %} diff --git a/ishtar_common/templatetags/ishtar_helpers.py b/ishtar_common/templatetags/ishtar_helpers.py index cfd04a5d7..b668b8f0b 100644 --- a/ishtar_common/templatetags/ishtar_helpers.py +++ b/ishtar_common/templatetags/ishtar_helpers.py @@ -114,6 +114,18 @@ def can_edit_item(item, context): @register.filter +def can_delete_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_delete(request): + return True + + +@register.filter def user_can_do(ishtar_user, permission): return ishtar_user.user_ptr.has_perm(permission) diff --git a/ishtar_common/templatetags/link_to_window.py b/ishtar_common/templatetags/link_to_window.py index 285d9b1fd..0f857c8c5 100644 --- a/ishtar_common/templatetags/link_to_window.py +++ b/ishtar_common/templatetags/link_to_window.py @@ -76,7 +76,7 @@ def add_links(items, extra_attr=''): lbl = item.fancy_str() else: lbl = str(item) - html.append("{} {}".format(lbl, simple_link_to_window(item_lnk))) + html.append("{} {}".format(simple_link_to_window(item_lnk), lbl)) return mark_safe("<br/>".join(html)) diff --git a/ishtar_common/urls.py b/ishtar_common/urls.py index aa1f1948e..d2c954e58 100644 --- a/ishtar_common/urls.py +++ b/ishtar_common/urls.py @@ -686,6 +686,13 @@ urlpatterns += [ )(views.QADocumentDuplicateFormView.as_view()), name="document-qa-duplicate", ), + path( + "document-qa-unlink/<int:pks>/<slug:related_model>-<int:related_id>/", + check_permissions( + ["ishtar_common.change_document", "ishtar_common.change_own_document"] + )(views.QADocumentUnlink.as_view()), + name="document-qa-unlink", + ), url( r"^document-qa-packaging/(?P<pks>[0-9-]+)?/$", check_permissions( diff --git a/ishtar_common/views.py b/ishtar_common/views.py index 4c83fd818..5f175c186 100644 --- a/ishtar_common/views.py +++ b/ishtar_common/views.py @@ -77,6 +77,7 @@ from ishtar_common.models_common import QuickAction from ishtar_common.templatetags.link_to_window import simple_link_to_window from ishtar_common.utils_migrations import HOMEPAGE_TITLE from ishtar_common.utils import ( + BSMessage, clean_session_cache, CSV_OPTIONS, get_current_item_keys, @@ -1236,10 +1237,12 @@ ACTION_MODEL_DICT = { "archaeological_operations", "AdministrativeAct"), 'file': apps.get_model("archaeological_files", "File"), 'site': apps.get_model("archaeological_operations", "ArchaeologicalSite"), + 'contextrecord': apps.get_model("archaeological_context_records", "ContextRecord"), 'record': apps.get_model("archaeological_context_records", "ContextRecord"), 'find': apps.get_model("archaeological_finds", "Find"), 'treatment': apps.get_model("archaeological_finds", "Treatment"), 'treatmentfle': apps.get_model("archaeological_finds", "TreatmentFile"), + 'treatmentfile': apps.get_model("archaeological_finds", "TreatmentFile"), 'exhibition': apps.get_model("archaeological_finds", "Exhibition"), 'container': apps.get_model("archaeological_warehouse", "Container"), 'warehouse': apps.get_model("archaeological_warehouse", "Warehouse"), @@ -3160,6 +3163,9 @@ class QAItemForm(IshtarMixin, LoginRequiredMixin, FormView): action_name = None permissions = [] + def get_permissions(self): + return self.permissions + def get_quick_action(self): quick_action = self.model.get_quick_action_by_url(self.base_url) if quick_action: @@ -3169,7 +3175,7 @@ class QAItemForm(IshtarMixin, LoginRequiredMixin, FormView): url=self.base_url, icon_class=self.icon, text=self.page_name, - rights=self.permissions + rights=self.get_permissions() ) def pre_dispatch(self, request, *args, **kwargs): @@ -3401,6 +3407,53 @@ class QADocumentDuplicateFormView(QAItemForm): return data +class QADocumentUnlink(QAItemForm): + model = models.Document + page_name = _("Unlink") + form_class = forms.QAForm + base_url = "document-qa-unlink" + icon = "fa fa-chain-broken" + action_name = _("Unlink") + + def pre_dispatch(self, request, *args, **kwargs): + related_model = kwargs.get("related_model") + # model, related_id + if related_model not in ACTION_MODEL_DICT: + raise Http404() + related_model = ACTION_MODEL_DICT[related_model] + related_id = kwargs.get("related_id") + try: + self.related_item = related_model.objects.get(pk=related_id) + except related_model.DoesNotExist: + raise Http404() + return super().pre_dispatch(request, *args, **kwargs) + + def get_permissions(self): + rel_meta = self.related_item.__class__._meta + return [ + f"{rel_meta.app_label}.change_{rel_meta.model_name}", + f"{rel_meta.app_label}.change_own_{rel_meta.model_name}", + ] + + def get_form_kwargs(self): + kwargs = super().get_form_kwargs() + kwargs["confirm"] = True + return kwargs + + def form_valid(self, form): + self.related_item.documents.remove(self.items[0]) + return HttpResponseRedirect(reverse("success")) + + def get_context_data(self, **kwargs): + data = super().get_context_data(**kwargs) + data["messages"] = [ + BSMessage( + str(_('Unlink document "{}" from this item.')).format(self.items[0]), + "info", "", no_dismiss=True) + ] + return data + + class QADocumentPackagingFormView(QAItemForm): template_name = "ishtar/forms/qa_document_packaging.html" model = models.Document diff --git a/ishtar_common/views_item.py b/ishtar_common/views_item.py index 9ee5b9040..080a6600b 100644 --- a/ishtar_common/views_item.py +++ b/ishtar_common/views_item.py @@ -530,6 +530,7 @@ def show_item(model, name, extra_dct=None, model_for_perms=None, callback=None): if current_language not in PUNCTUATION: current_language = "en" dct["PUNCTUATION"] = PUNCTUATION[current_language] + dct["model_slug"] = model.SLUG dct["current_window_url"] = url_name date = None if "date" in dct: |