Skip to content

Commit

Permalink
feat: can delete
Browse files Browse the repository at this point in the history
    * 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 <[email protected]>
Co-Authored-by: Bertrand Zuchuat <[email protected]>
Signed-off-by: Peter Weber <[email protected]>
  • Loading branch information
rerowep and Garfield-fr committed Oct 3, 2018
1 parent f364218 commit 68986d0
Show file tree
Hide file tree
Showing 19 changed files with 122 additions and 31 deletions.
6 changes: 6 additions & 0 deletions rero_ils/bundles.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion rero_ils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
10 changes: 10 additions & 0 deletions rero_ils/modules/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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."""
Expand Down
17 changes: 4 additions & 13 deletions rero_ils/modules/documents_items/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand All @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
};
}])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,8 @@ <h1>{{record.title}}</h1>
{% 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 %}
Expand Down Expand Up @@ -377,7 +378,8 @@ <h3>{{_('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 %}
Expand Down Expand Up @@ -422,4 +424,5 @@ <h3>{{_('Export Formats')}}</h3>
{%- block javascript %}
{{ super() }}
{% assets "rero_ils_detailed_js" %}<script src="{{ ASSET_URL }}"></script>{% endassets %}
{% assets "rero_ils_tooltips_js" %}<script src="{{ ASSET_URL }}"></script>{% endassets %}
{%- endblock javascript %}
5 changes: 5 additions & 0 deletions rero_ils/modules/items/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
3 changes: 2 additions & 1 deletion rero_ils/modules/items/templates/rero_ils/_item_head.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ <h1>{{ item_dumps.barcode }}</a></h1>
{% 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 %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,8 @@ <h3>{{ _('Pending') }} ({{ item_dumps.requests_count }})</h3>
{% endif %}
</div>
{%- endblock %}

{%- block javascript %}
{{ super() }}
{% assets "rero_ils_tooltips_js" %}<script src="{{ ASSET_URL }}"></script>{% endassets %}
{%- endblock javascript %}
1 change: 1 addition & 0 deletions rero_ils/modules/items/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions rero_ils/modules/locations/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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."""

Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,16 @@ <h1>{{record.name}}</h1>
{% 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 %}

</div>
{%- endblock %}

{%- block javascript %}
{{ super() }}
{% assets "rero_ils_tooltips_js" %}<script src="{{ ASSET_URL }}"></script>{% endassets %}
{%- endblock javascript %}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ <h1>{{record.name}}</h1>
{% 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 %}
Expand Down Expand Up @@ -85,7 +86,8 @@ <h2>{{ location.name }}</h2>
{% 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 %}
Expand All @@ -96,3 +98,8 @@ <h2>{{ location.name }}</h2>

</div>
{%- endblock %}

{%- block javascript %}
{{ super() }}
{% assets "rero_ils_tooltips_js" %}<script src="{{ ASSET_URL }}"></script>{% endassets %}
{%- endblock javascript %}
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,16 @@ <h3>{{_('Locations')}}</h3>
{% 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 %}

</div>
{%- endblock %}

{%- block javascript %}
{{ super() }}
{% assets "rero_ils_tooltips_js" %}<script src="{{ ASSET_URL }}"></script>{% endassets %}
{%- endblock javascript %}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ <h1>{{record.name}}</h1>
{% 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 %}
Expand Down Expand Up @@ -87,7 +88,8 @@ <h2>
{% 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 %}
Expand All @@ -99,3 +101,8 @@ <h2>
{%- endblock %}
</div>
{%- endblock %}

{%- block javascript %}
{{ super() }}
{% assets "rero_ils_tooltips_js" %}<script src="{{ ASSET_URL }}"></script>{% endassets %}
{%- endblock javascript %}
18 changes: 12 additions & 6 deletions rero_ils/modules/patrons/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,15 @@ <h1>{{record.last_name}}, {{record.first_name}}</h1>
{% 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 %}
</div>
{%- endblock %}

{%- block javascript %}
{{ super() }}
{% assets "rero_ils_tooltips_js" %}<script src="{{ ASSET_URL }}"></script>{% endassets %}
{%- endblock javascript %}
3 changes: 3 additions & 0 deletions rero_ils/static/js/rero_ils/tooltips.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$(document).ready(function(){
$('[data-toggle="tooltip"]').tooltip();
});
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 68986d0

Please sign in to comment.