Skip to content

Commit

Permalink
global: increase performance to generate records permissions.
Browse files Browse the repository at this point in the history
When we calculate permissions for records, we have double call on `get_links_to_me()`.

Co-Authored-by: Laurent Dubois <laurent.dubois@itld-solutions.be>
  • Loading branch information
lauren-d committed May 28, 2021
1 parent 33a90ce commit 58b5c95
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
11 changes: 8 additions & 3 deletions rero_ils/modules/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,8 @@ def count(cls, with_deleted=False):

def delete(self, force=False, dbcommit=False, delindex=False):
"""Delete record and persistent identifier."""
if self.can_delete:
can, _ = self.can_delete
if can:
if delindex:
self.delete_from_index()
persistent_identifier = self.get_persistent_identifier(self.id)
Expand Down Expand Up @@ -425,8 +426,12 @@ def reasons_not_to_delete(self):

@property
def can_delete(self):
"""Record can be deleted."""
return len(self.reasons_not_to_delete()) == 0
"""Record can be deleted.
:return a tuple with True|False and reasons not to delete if False.
"""
reasons = self.reasons_not_to_delete()
return len(reasons) == 0, reasons

@property
def organisation_pid(self):
Expand Down
4 changes: 2 additions & 2 deletions rero_ils/modules/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ def record_permissions(record_pid=None, route_name=None):
# before ; either the `delete_permissions_factory` for this record
# should be called. If this call send 'False' then the
# reason_not_to_delete should be "permission denied"
can_delete, reasons = record.can_delete()
permissions['delete']['can'] = \
record.can_delete and \
can_delete and \
record_permissions_factory['delete'](record=record).can()
reasons = record.reasons_not_to_delete()
if not permissions['delete']['can'] and not reasons:
# in this case, it's because config delete factory return
# `False`, so the reason is 'Permission denied'
Expand Down
3 changes: 2 additions & 1 deletion rero_ils/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ def librarian_update_permission_factory(record, *args, **kwargs):
def librarian_delete_permission_factory(
record, credentials_only=False, *args, **kwargs):
"""User can delete record."""
if credentials_only or record.can_delete:
can, _ = record.can_delete
if credentials_only or can:
return librarian_permission
return type('Check', (), {'can': lambda x: False})()

Expand Down

0 comments on commit 58b5c95

Please sign in to comment.