From 68986d07c33f3cbb2d84f229440c81dd76493a57 Mon Sep 17 00:00:00 2001 From: Peter Weber Date: Tue, 2 Oct 2018 15:54:50 +0200 Subject: [PATCH] feat: can delete * NEW: test of can delete * NEW: Disable delete button with can delete test * NEW: Add message on disable delete button * FIX: cvs export Signed-off-by: Peter Weber Co-Authored-by: Bertrand Zuchuat Signed-off-by: Peter Weber --- rero_ils/bundles.py | 6 +++++ rero_ils/config.py | 2 +- rero_ils/modules/api.py | 10 +++++++ .../modules/documents_items/serializers.py | 17 +++--------- .../static/js/rero_ils/documents_items.js | 2 +- .../detailed_view_documents_items.html | 7 +++-- rero_ils/modules/items/api.py | 5 ++++ .../items/templates/rero_ils/_item_head.html | 3 ++- .../rero_ils/detailed_view_items.html | 5 ++++ rero_ils/modules/items/views.py | 1 + rero_ils/modules/locations/api.py | 27 +++++++++++++++++++ .../rero_ils/detailed_view_locations.html | 8 +++++- .../detailed_view_members_locations.html | 11 ++++++-- .../rero_ils/detailed_view_organisations.html | 8 +++++- .../detailed_view_organisations_members.html | 11 ++++++-- rero_ils/modules/patrons/api.py | 18 ++++++++----- .../rero_ils/detailed_view_patrons.html | 8 +++++- rero_ils/static/js/rero_ils/tooltips.js | 3 +++ setup.py | 1 + 19 files changed, 122 insertions(+), 31 deletions(-) create mode 100644 rero_ils/static/js/rero_ils/tooltips.js diff --git a/rero_ils/bundles.py b/rero_ils/bundles.py index 9c09b02f03..5a34c118c9 100644 --- a/rero_ils/bundles.py +++ b/rero_ils/bundles.py @@ -86,6 +86,12 @@ def catalog(domain): output='gen/rero_ils.tab.%(version)s.js' ) +tooltips_js = Bundle( + 'node_modules/bootstrap-sass/assets/javascripts/bootstrap/tooltip.js', + 'js/rero_ils/tooltips.js', + output='gen/rero_ils.tooltip.%(version)s.js' +) + _search_js = NpmBundle( 'js/rero_ils/documents_items.js', 'js/rero_ils/invenio_config.js', diff --git a/rero_ils/config.py b/rero_ils/config.py index 4872a49675..d3705bdf36 100644 --- a/rero_ils/config.py +++ b/rero_ils/config.py @@ -225,7 +225,7 @@ def _(x): search_type=None, record_serializers={ 'application/json': ('invenio_records_rest.serializers' - ':json_v1_response'), + ':json_v1_response') }, search_serializers={ 'application/rero+json': ('rero_ils.modules.serializers' diff --git a/rero_ils/modules/api.py b/rero_ils/modules/api.py index 9f03f06b5e..5562c87a4c 100644 --- a/rero_ils/modules/api.py +++ b/rero_ils/modules/api.py @@ -149,6 +149,11 @@ def delete_from_index(self): except NotFoundError: pass + @property + def can_delete(self): + """Record can be deleted.""" + return True + @property def pid(self): """Get ils record pid value.""" @@ -220,6 +225,11 @@ def remove_element(self, element, force=False, delindex=False): self.reindex(forceindex=True) return to_return + @property + def can_delete(self): + """Record can be deleted.""" + return len(self.elements) == 0 + @property def elements(self): """Return an array of elements.""" diff --git a/rero_ils/modules/documents_items/serializers.py b/rero_ils/modules/documents_items/serializers.py index 374d2ebe75..1c1de666c7 100644 --- a/rero_ils/modules/documents_items/serializers.py +++ b/rero_ils/modules/documents_items/serializers.py @@ -44,19 +44,8 @@ def format_record(self, record): bib_fields.append(str(record.get('publicationYear', ''))) bib_str = ','.join(['"%s"' % v for v in bib_fields]) - items = [] - for citem in record.get('citems', []): - item_fields = [] - item_fields.append(str(citem.get('barcode'))) - item_fields.append(str(citem.get('call_number'))) - item_fields.append(str(citem.get('location'))) - item_fields.append( - str(citem.get('_circulation', {}).get('status')) - ) - item_str = ','.join(['"%s"' % v for v in item_fields]) - items.append(','.join((bib_str, item_str))) - - return '\n'.join(items) + + return bib_str def serialize(self, pid, record, links_factory=None): """Serialize a single record and persistent identifier. @@ -65,6 +54,7 @@ def serialize(self, pid, record, links_factory=None): :param record: Record instance. :param links_factory: Factory function for record links. """ + print('++++>', record, flush=True) return self.format_record(record) def serialize_search(self, pid_fetcher, search_result, links=None, @@ -81,6 +71,7 @@ def serialize_search(self, pid_fetcher, search_result, links=None, return "\n".join(records) + documents_items_csv_v1 = TextSerializer() documents_items_csv_v1_response = record_responsify( documents_items_csv_v1, 'text/csv' diff --git a/rero_ils/modules/documents_items/static/js/rero_ils/documents_items.js b/rero_ils/modules/documents_items/static/js/rero_ils/documents_items.js index b3a09b16e4..a6986cb0ca 100644 --- a/rero_ils/modules/documents_items/static/js/rero_ils/documents_items.js +++ b/rero_ils/modules/documents_items/static/js/rero_ils/documents_items.js @@ -2,7 +2,7 @@ angular.module('reroilsUtils', []) .controller('exportController', ['$scope', function($scope) { $scope.csvURL = function() { - return window.location.href.toString().replace('search', 'api/export/records/csv').replace(/size=\d+/, 'size=19999'); + return window.location.href.toString().replace('search', 'api/export/documents/csv').replace(/size=\d+/, 'size=19999'); }; }]) diff --git a/rero_ils/modules/documents_items/templates/rero_ils/detailed_view_documents_items.html b/rero_ils/modules/documents_items/templates/rero_ils/detailed_view_documents_items.html index f93b7f89da..159e10b368 100644 --- a/rero_ils/modules/documents_items/templates/rero_ils/detailed_view_documents_items.html +++ b/rero_ils/modules/documents_items/templates/rero_ils/detailed_view_documents_items.html @@ -235,7 +235,8 @@

{{record.title}}

{% with href_update=url_for('reroils_record_editor.update_doc', pid=record.pid), href_delete=url_for('reroils_record_editor.delete_doc', pid=record.pid), - json=record + json=record, + message=_("Document cannot be deleted: there are still items linked to this document.") %} {% include 'reroils_record_editor/_button_actions.html' %} {% endwith %} @@ -377,7 +378,8 @@

{{_('Items')}} {% with href_update=url_for('reroils_record_editor.update_item', pid=item.pid, parent_pid=record.pid), href_delete=url_for('reroils_record_editor.delete_item', pid=item.pid, parent_pid=record.pid), - json=item + json=item, + message=_("Item cannot be deleted: there are still transactions linked to this item.") %} {% include 'reroils_record_editor/_button_actions.html' %} {% endwith %} @@ -422,4 +424,5 @@

{{_('Export Formats')}}

{%- block javascript %} {{ super() }} {% assets "rero_ils_detailed_js" %}{% endassets %} +{% assets "rero_ils_tooltips_js" %}{% endassets %} {%- endblock javascript %} diff --git a/rero_ils/modules/items/api.py b/rero_ils/modules/items/api.py index 3f88b97662..3c60b238d5 100644 --- a/rero_ils/modules/items/api.py +++ b/rero_ils/modules/items/api.py @@ -387,6 +387,11 @@ def available(self): return self.status == ItemStatus.ON_SHELF and\ self.number_of_item_requests() == 0 + @property + def can_delete(self): + """Record can be deleted.""" + return self.available + # ??? name ??? @check_status(statuses=[ItemStatus.ON_LOAN]) def extend_loan( diff --git a/rero_ils/modules/items/templates/rero_ils/_item_head.html b/rero_ils/modules/items/templates/rero_ils/_item_head.html index b8750d0a2a..73c680e8bf 100644 --- a/rero_ils/modules/items/templates/rero_ils/_item_head.html +++ b/rero_ils/modules/items/templates/rero_ils/_item_head.html @@ -57,7 +57,8 @@

{{ item_dumps.barcode }}

{% with href_update=url_for('reroils_record_editor.update_item', pid=record.pid), href_delete=url_for('reroils_record_editor.delete_item', pid=record.pid), - json=record + json=record, + message=_("Item cannot be deleted: there are still transactions linked to this item.") %} {% include 'reroils_record_editor/_button_actions.html' %} {% endwith %} diff --git a/rero_ils/modules/items/templates/rero_ils/detailed_view_items.html b/rero_ils/modules/items/templates/rero_ils/detailed_view_items.html index 069458dee7..f79326a50d 100644 --- a/rero_ils/modules/items/templates/rero_ils/detailed_view_items.html +++ b/rero_ils/modules/items/templates/rero_ils/detailed_view_items.html @@ -100,3 +100,8 @@

{{ _('Pending') }} ({{ item_dumps.requests_count }})

{% endif %} {%- endblock %} + +{%- block javascript %} +{{ super() }} +{% assets "rero_ils_tooltips_js" %}{% endassets %} +{%- endblock javascript %} diff --git a/rero_ils/modules/items/views.py b/rero_ils/modules/items/views.py index 524809ec55..f71fb4e567 100644 --- a/rero_ils/modules/items/views.py +++ b/rero_ils/modules/items/views.py @@ -38,6 +38,7 @@ from invenio_records_ui.signals import record_viewed from reroils_record_editor.permissions import record_edit_permission +from ...filter import format_date_filter from ...permissions import request_item_permission from ..documents_items.api import DocumentsWithItems from ..patrons.api import Patron diff --git a/rero_ils/modules/locations/api.py b/rero_ils/modules/locations/api.py index f89208afe2..22a70ffbd6 100644 --- a/rero_ils/modules/locations/api.py +++ b/rero_ils/modules/locations/api.py @@ -25,6 +25,7 @@ """API for manipulating locations.""" from invenio_pidstore.models import PersistentIdentifier +from invenio_search.api import RecordsSearch from ..api import IlsRecord from ..members_locations.models import MembersLocationsMetadata @@ -33,6 +34,15 @@ from .providers import LocationProvider +class LocationsSearch(RecordsSearch): + """RecordsSearch for borrowed documents.""" + + class Meta: + """Search only on documents index.""" + + index = 'documents' + + class Location(IlsRecord): """Location class.""" @@ -60,3 +70,20 @@ def get_all_pids(cls): locs_id.append(pid.pid_value) return locs_id + + def get_all_items_pids(self): + """Get all items pids.""" + items_with_location = LocationsSearch().filter( + "term", **{"itemslist.location_pid": self.pid} + ).source(includes=['itemslist.pid']).scan() + pids = [] + for document in items_with_location: + for items in document['itemslist']: + item = items.to_dict() + pids.append(item.get('pid')) + return sorted(pids, key=int) + + @property + def can_delete(self): + """Record can be deleted.""" + return len(self.get_all_items_pids()) == 0 diff --git a/rero_ils/modules/locations/templates/rero_ils/detailed_view_locations.html b/rero_ils/modules/locations/templates/rero_ils/detailed_view_locations.html index 2b246fe7f6..35f25e8910 100644 --- a/rero_ils/modules/locations/templates/rero_ils/detailed_view_locations.html +++ b/rero_ils/modules/locations/templates/rero_ils/detailed_view_locations.html @@ -22,10 +22,16 @@

{{record.name}}

{% with href_update=url_for('reroils_record_editor.update_loc', pid=record.pid), href_delete=url_for('reroils_record_editor.delete_loc', pid=record.pid), - json=record + json=record, + message=_("Location cannot be deleted: there are still items linked to this location.") %} {% include 'reroils_record_editor/_button_actions.html' %} {% endwith %} {%- endblock %} + +{%- block javascript %} +{{ super() }} +{% assets "rero_ils_tooltips_js" %}{% endassets %} +{%- endblock javascript %} diff --git a/rero_ils/modules/members_locations/templates/rero_ils/detailed_view_members_locations.html b/rero_ils/modules/members_locations/templates/rero_ils/detailed_view_members_locations.html index ba0d4355e6..ff1a73cf85 100644 --- a/rero_ils/modules/members_locations/templates/rero_ils/detailed_view_members_locations.html +++ b/rero_ils/modules/members_locations/templates/rero_ils/detailed_view_members_locations.html @@ -45,7 +45,8 @@

{{record.name}}

{% with href_update=url_for('reroils_record_editor.update_memb', pid=record.pid), href_delete=url_for('reroils_record_editor.delete_memb', pid=record.pid), - json=record + json=record, + message=_("Library cannot be deleted: there are still locations linked to this library.") %} {% include 'reroils_record_editor/_button_actions.html' %} {% endwith %} @@ -85,7 +86,8 @@

{{ location.name }}

{% with href_update=url_for('reroils_record_editor.update_loc', pid=record.pid, parent_pid=record.pid), href_delete=url_for('reroils_record_editor.delete_loc', pid=record.pid, parent_pid=record.pid), - json=location + json=location, + message=_("Location cannot be deleted: there are still items linked to this location.") %} {% include 'reroils_record_editor/_button_actions.html' %} {% endwith %} @@ -96,3 +98,8 @@

{{ location.name }}

{%- endblock %} + +{%- block javascript %} +{{ super() }} +{% assets "rero_ils_tooltips_js" %}{% endassets %} +{%- endblock javascript %} diff --git a/rero_ils/modules/organisations/templates/rero_ils/detailed_view_organisations.html b/rero_ils/modules/organisations/templates/rero_ils/detailed_view_organisations.html index 193ab24e30..78ff205ee3 100644 --- a/rero_ils/modules/organisations/templates/rero_ils/detailed_view_organisations.html +++ b/rero_ils/modules/organisations/templates/rero_ils/detailed_view_organisations.html @@ -98,10 +98,16 @@

{{_('Locations')}}

{% with href_update=url_for('reroils_record_editor.update_org', pid=record.pid), href_delete=url_for('reroils_record_editor.delete_org', pid=record.pid), - json=record + json=record, + message=_("Organisation cannot be deleted: there are still libraries linked to this organisation.") %} {% include 'reroils_record_editor/_button_actions.html' %} {% endwith %} {%- endblock %} + +{%- block javascript %} +{{ super() }} +{% assets "rero_ils_tooltips_js" %}{% endassets %} +{%- endblock javascript %} diff --git a/rero_ils/modules/organisations_members/templates/rero_ils/detailed_view_organisations_members.html b/rero_ils/modules/organisations_members/templates/rero_ils/detailed_view_organisations_members.html index e738ec974a..9fae4cf5bf 100644 --- a/rero_ils/modules/organisations_members/templates/rero_ils/detailed_view_organisations_members.html +++ b/rero_ils/modules/organisations_members/templates/rero_ils/detailed_view_organisations_members.html @@ -22,7 +22,8 @@

{{record.name}}

{% with href_update=url_for('reroils_record_editor.update_org', pid=record.pid), href_delete=url_for('reroils_record_editor.delete_org', pid=record.pid), - json=record + json=record, + message=_("Organisation cannot be deleted: there are still libraries linked to this organisation.") %} {% include 'reroils_record_editor/_button_actions.html' %} {% endwith %} @@ -87,7 +88,8 @@

{% with href_update=url_for('reroils_record_editor.update_memb', pid=member.pid, parent_pid=record.pid), href_delete=url_for('reroils_record_editor.delete_memb', pid=member.pid, parent_pid=record.pid), - json=member + json=member, + message=_("Library cannot be deleted: there are still locations linked to this library.") %} {% include 'reroils_record_editor/_button_actions.html' %} {% endwith %} @@ -99,3 +101,8 @@

{%- endblock %} {%- endblock %} + +{%- block javascript %} +{{ super() }} +{% assets "rero_ils_tooltips_js" %}{% endassets %} +{%- endblock javascript %} diff --git a/rero_ils/modules/patrons/api.py b/rero_ils/modules/patrons/api.py index 53fc20795f..183f225b10 100644 --- a/rero_ils/modules/patrons/api.py +++ b/rero_ils/modules/patrons/api.py @@ -122,12 +122,13 @@ def delete_by_email(cls, email, deluser=False, delindex=False): def get_borrowed_documents_pids(self): """Get pid values borrowed documents for given patron.""" - pids = [p.pid for p in BorrowedDocumentsSearch().filter( - 'term', - itemslist___circulation__holdings__patron_barcode=self.get( - 'barcode' - ) - ).source(includes=['id', 'pid']).scan()] + pids = [] + barcode = self.get('barcode') + if barcode: + pids = [p.pid for p in BorrowedDocumentsSearch().filter( + 'term', + itemslist___circulation__holdings__patron_barcode=barcode + ).source(includes=['id', 'pid']).scan()] return pids def get_borrowed_documents(self): @@ -180,6 +181,11 @@ def dumps(self, **kwargs): )) return data + @property + def can_delete(self): + """Record can be deleted.""" + return len(self.get_borrowed_documents_pids()) == 0 + @property def roles(self): """Return user roles.""" diff --git a/rero_ils/modules/patrons/templates/rero_ils/detailed_view_patrons.html b/rero_ils/modules/patrons/templates/rero_ils/detailed_view_patrons.html index f7d46b9b6e..e89e389724 100644 --- a/rero_ils/modules/patrons/templates/rero_ils/detailed_view_patrons.html +++ b/rero_ils/modules/patrons/templates/rero_ils/detailed_view_patrons.html @@ -110,9 +110,15 @@

{{record.last_name}}, {{record.first_name}}

{% with href_update=url_for('reroils_record_editor.update_ptrn', pid=record.pid), href_delete=url_for('reroils_record_editor.delete_ptrn', pid=record.pid), - json=record + json=record, + message=_("Patron cannot be deleted: there are still transactions or fees linked to this patron.") %} {% include 'reroils_record_editor/_button_actions.html' %} {% endwith %} {%- endblock %} + +{%- block javascript %} +{{ super() }} +{% assets "rero_ils_tooltips_js" %}{% endassets %} +{%- endblock javascript %} diff --git a/rero_ils/static/js/rero_ils/tooltips.js b/rero_ils/static/js/rero_ils/tooltips.js new file mode 100644 index 0000000000..4f904029ca --- /dev/null +++ b/rero_ils/static/js/rero_ils/tooltips.js @@ -0,0 +1,3 @@ +$(document).ready(function(){ + $('[data-toggle="tooltip"]').tooltip(); +}); diff --git a/setup.py b/setup.py index f491a70abe..f6b9c0ad52 100644 --- a/setup.py +++ b/setup.py @@ -104,6 +104,7 @@ def run(self): 'messages = rero_ils', ], 'invenio_assets.bundles': [ + 'rero_ils_tooltips_js = rero_ils.bundles:tooltips_js', 'rero_ils_person_search_js = rero_ils.bundles:person_js', 'rero_ils_detailed_js = rero_ils.bundles:detailed_js', 'rero_ils_search_js = rero_ils.bundles:search_js',