From b4c0514035d1507b4757feca3ff0118d561b00c2 Mon Sep 17 00:00:00 2001 From: Krzysztof Socha Date: Fri, 21 Jun 2019 13:32:34 +0200 Subject: [PATCH] Add a way to populate draft lock state from version we're checking --- djangocms_version_locking/monkeypatch/models.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/djangocms_version_locking/monkeypatch/models.py b/djangocms_version_locking/monkeypatch/models.py index b249211..76c7891 100644 --- a/djangocms_version_locking/monkeypatch/models.py +++ b/djangocms_version_locking/monkeypatch/models.py @@ -40,10 +40,19 @@ def inner(version, user): def _is_draft_version_locked(message): def inner(version, user): - draft_version = get_latest_draft_version(version) - lock = version_is_locked(draft_version) - if lock and lock.created_by != user: - raise ConditionFailed(message.format(user=lock.created_by)) + try: + # if there's a prepoluated field on version object + # representing a draft lock, use it + cached_draft_version_user_id = getattr(version, "_draft_version_user_id") + if cached_draft_version_user_id and cached_draft_version_user_id != user.pk: + raise ConditionFailed( + message.format(user="User #{}".format(cached_draft_version_user_id)) + ) + except AttributeError: + draft_version = get_latest_draft_version(version) + lock = version_is_locked(draft_version) + if lock and lock.created_by != user: + raise ConditionFailed(message.format(user=lock.created_by)) return inner