def is_locked(request, app, model, id): obj = utils.gather_lockable_models()[app][model].objects.get(pk=id) response = simplejson.dumps({ "is_active": obj.is_locked, "for_user": getattr(obj.locked_by, 'username', None), "applies": obj.lock_applies_to(request.user), }) return HttpResponse(response, mimetype='application/json')
def test_gather_lockable_models(self): from locking import utils from locking.tests import models lockable_models = utils.gather_lockable_models() self.assertTrue("story" in lockable_models["tests"]) self.assertTrue("unlockable" not in lockable_models["tests"])
def lock(request, app, model, id): obj = utils.gather_lockable_models()[app][model].objects.get(pk=id) try: obj.lock_for(request.user) obj.save() return HttpResponse(status=200) except models.ObjectLockedError: # The user tried to overwrite an existing lock by another user. # No can do, pal! return HttpResponse(status=403)
def is_locked(request, app, model, id): obj = utils.gather_lockable_models()[app][model].objects.get(pk=id) response = dumps( { "is_active": obj.is_locked, "for_user": getattr(obj.locked_by, "username", None), "applies": obj.lock_applies_to(request.user), } ) return HttpResponse(response)
def is_locked(request, app, model, id): obj = utils.gather_lockable_models()[app][model].objects.get(pk=id) response = simplejson.dumps({ "is_active": obj.is_locked, "for_user": getattr(obj.locked_by, 'username', None), "applies": obj.lock_applies_to(request.user), }) return HttpResponse(response)
def unlock(request, app, model, id): obj = utils.gather_lockable_models()[app][model].objects.get(pk=id) # Users who don't have exclusive access to an object anymore may still # request we unlock an object. This happens e.g. when a user navigates # away from an edit screen that's been open for very long. # When this happens, LockableModel.unlock_for will throw an exception, # and we just ignore the request. # That way, any new lock that may since have been put in place by another # user won't get accidentally overwritten. try: obj.unlock_for(request.user) obj.save() return HttpResponse(status=200) except models.ObjectLockedError: return HttpResponse(status=403)