Skip to content

Commit

Permalink
serials: fix issue.status_date field update
Browse files Browse the repository at this point in the history
* Ensure that the `issue.status_date` field is updated when an issue is
  created/updated.
* Closes #1654.

Co-Authored-by: Renaud Michotte <[email protected]>
  • Loading branch information
zannkukai committed May 11, 2021
1 parent cb94dba commit 42d6da0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
7 changes: 6 additions & 1 deletion rero_ils/modules/items/api/issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@
from flask import current_app

from .record import ItemRecord
from ..models import ItemIssueStatus
from ..models import ItemIssueStatus, TypeOfItem


class ItemIssue(ItemRecord):
"""Item issue class."""

@property
def is_issue(self):
"""Is this item is an issue or not."""
return self.get('type') == TypeOfItem.ISSUE

@property
def expected_date(self):
"""Shortcut for issue expected date."""
Expand Down
18 changes: 14 additions & 4 deletions rero_ils/modules/items/api/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from elasticsearch_dsl.query import Q
from flask_babelex import gettext as _

from ..models import TypeOfItem
from ..utils import item_pid_to_object
from ...api import IlsRecord
from ...item_types.api import ItemType
Expand Down Expand Up @@ -97,6 +98,7 @@ def create(cls, data, id_=None, delete_pid=False,
"""Create item record."""
cls._item_build_org_ref(data)
data = cls._prepare_item_record(data=data, mode='create')
data = cls._set_issue_status_date(data=data)
record = super().create(
data, id_, delete_pid, dbcommit, reindex, **kwargs)
holding = cls._increment_next_prediction_for_holding(
Expand All @@ -109,7 +111,7 @@ def create(cls, data, id_=None, delete_pid=False,
# If `dbcommit` is already set to True, this commit is already done by
# the `IlsRecord.update()` function.
#
# /!\ if we write some other operation after _increement_next_predition
# /!\ if we write some other operation after _increment_next_predition
# we need to manage ourself the `rollback()`.
#
# TODO :: best solution will be to create an invenio `post_create`
Expand Down Expand Up @@ -153,11 +155,19 @@ def _set_issue_status_date(cls, data):
:param data: The record to update.
:return: The updated record.
"""
if data.get('type') != TypeOfItem.ISSUE:
return data

status = data.get('issue', {}).get('status')
item = cls.get_record_by_pid(data.get('pid'))
if status and item and status != item.issue_status:
data['issue']['status_date'] = \
datetime.now(timezone.utc).isoformat()
now = datetime.now(timezone.utc).isoformat()

if item: # item already exists
if status and status != item.issue_status:
data['issue']['status_date'] = now
else: # item creation
if status:
data['issue']['status_date'] = now
return data

@classmethod
Expand Down
9 changes: 8 additions & 1 deletion rero_ils/modules/items/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,15 @@ class ItemMetadata(db.Model, RecordMetadataBase):
__tablename__ = 'item_metadata'


class TypeOfItem:
"""Enum class to list all possible item type."""

STANDARD = 'standard'
ISSUE = 'issue'


class ItemStatus:
"""Class holding all availabe circulation item statuses."""
"""Class holding all available circulation item statuses."""

ON_SHELF = 'on_shelf'
AT_DESK = 'at_desk'
Expand Down

0 comments on commit 42d6da0

Please sign in to comment.