Skip to content

Commit

Permalink
Allow expired lock on api requests
Browse files Browse the repository at this point in the history
When renewing or deleting (and perhaps getting?) a lock via the api an expired
lock should be allowed.
References issue rparent#6. See comment
rparent#6 (comment)
To enable this get_valid_lock_token_or_error now uses the new
LockManager.get_for_contenttype_and_id. (LockManager's get_for_object now also
uses this new method so there's no code duplication)
Via the parameter allow_expired it is possible to exclude expired tokens,
though the default is True.
  • Loading branch information
BrendaH committed Nov 15, 2018
1 parent 2bb54a3 commit 2fafb8e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
5 changes: 4 additions & 1 deletion lock_tokens/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ class LockTokenManager(Manager):

def get_for_object(self, obj, allow_expired=True):
contenttype = ContentType.objects.get_for_model(obj)
return self.get_for_contenttype_and_id(contenttype, obj.id, allow_expired)

def get_for_contenttype_and_id(self, contenttype, object_id, allow_expired=True):
lookup_fields = {
'locked_object_content_type': contenttype,
'locked_object_id': obj.id
'locked_object_id': object_id
}
if not allow_expired:
lookup_fields['locked_at__gte'] = get_oldest_valid_tokens_datetime()
Expand Down
9 changes: 4 additions & 5 deletions lock_tokens/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from lock_tokens.exceptions import AlreadyLockedError
from lock_tokens.models import LockToken
from lock_tokens.settings import API_CSRF_EXEMPT
from lock_tokens.utils import get_oldest_valid_tokens_datetime


def lock_tokens_csrf_exempt(view):
Expand All @@ -35,12 +34,12 @@ def get_object_or_404(self, app_label, model, object_id):
except contenttype.model_class().DoesNotExist:
raise Http404("The object with id %s does not exist" % object_id)

def get_valid_lock_token_or_error(self, app_label, model, object_id, token):
def get_valid_lock_token_or_error(self, app_label, model, object_id, token, allow_expired=True):
contenttype = self.get_contenttype_or_404(app_label, model)
try:
lock_token = LockToken.objects.get(locked_object_content_type=contenttype,
locked_object_id=object_id,
locked_at__gte=get_oldest_valid_tokens_datetime())
lock_token = LockToken.objects.get_for_contenttype_and_id(contenttype,
object_id,
allow_expired)
except LockToken.DoesNotExist:
raise Http404("No valid token for this resource.")
if not token == lock_token.token_str:
Expand Down

0 comments on commit 2fafb8e

Please sign in to comment.