Skip to content

Commit

Permalink
Merge pull request rparent#8 from BrendaH/api_soft_lock
Browse files Browse the repository at this point in the history
Allow expired lock on api requests
  • Loading branch information
rparent authored Nov 29, 2018
2 parents 2bb54a3 + 2fafb8e commit d023dcd
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 d023dcd

Please sign in to comment.