def lock_for(self, user, hard_lock=False): """ Together with ``unlock_for`` this is probably the most important method on this model. If applicable to your use-case, you should lock for a specific user; that way, we can throw an exception when another user tries to unlock an object they haven't locked themselves. When using soft locks (the default), any process can still use the save method on this object. If you set ``hard_lock=True``, trying to save an object without first unlocking will raise an ``ObjectLockedError``. Don't use hard locks unless you really need them. See :doc:`design`. """ logger.debug("Attempting to initiate a lock for user `%s`" % user) if not isinstance(user, auth.User): raise ValueError("You should pass a valid auth.User to lock_for.") if self.lock_applies_to(user): raise ObjectLockedError( "This object is already locked by another user. \ May not override, except through the `unlock` method.") else: self._locked_at = datetime.today() self._locked_by = user self._hard_lock = self.__init_hard_lock = hard_lock date = self.locked_at.strftime("%H:%M:%S") # an administrative toggle, to make it easier for devs to extend `django-locking` # and react to locking and unlocking self._state.locking = True logger.debug("Initiated a %s lock for `%s` at %s" % (self.lock_type, self.locked_by, self.locked_at))
def lock_for(self, user, hard_lock=False): """ Together with ``unlock_for`` this is probably the most important method on this model. If applicable to your use-case, you should lock for a specific user; that way, we can throw an exception when another user tries to unlock an object they haven't locked themselves. When using soft locks (the default), any process can still use the save method on this object. If you set ``hard_lock=True``, trying to save an object without first unlocking will raise an ``ObjectLockedError``. Don't use hard locks unless you really need them. See :doc:`design`. """ logger.debug("Attempting to initiate a lock for user `%s`" % user) if not isinstance(user, auth.User): raise ValueError("You should pass a valid auth.User to lock_for.") if self.lock_applies_to(user): raise ObjectLockedError( "This object is already locked by another user. \ May not override, except through the `unlock` method." ) else: self._locked_at = datetime.today() self._locked_by = user self._hard_lock = self.__init_hard_lock = hard_lock date = self.locked_at.strftime("%H:%M:%S") # an administrative toggle, to make it easier for devs to extend `django-locking` # and react to locking and unlocking self._state.locking = True logger.debug("Initiated a %s lock for `%s` at %s" % (self.lock_type, self.locked_by, self.locked_at))
def unlock(self): """ This method serves solely to allow the application itself or admin users to do manual lock overrides, even if they haven't initiated these locks themselves. Otherwise, use ``unlock_for``. """ self._locked_at = self._locked_by = None # an administrative toggle, to make it easier for devs to extend `django-locking` # and react to locking and unlocking self._state.locking = True logger.debug("Disengaged lock on `%s`" % self)
def unlock_for(self, user): """ See ``lock_for``. If the lock was initiated for a specific user, unlocking will fail unless that same user requested the unlocking. Manual overrides should use the ``unlock`` method instead. Will raise a ObjectLockedError exception when the current user isn't authorized to unlock the object. """ logger.debug("Attempting to open up a lock on `%s` by user `%s`" % ( self, user)) self.unlock()
def unlock_for(self, user): """ See ``lock_for``. If the lock was initiated for a specific user, unlocking will fail unless that same user requested the unlocking. Manual overrides should use the ``unlock`` method instead. Will raise a ObjectLockedError exception when the current user isn't authorized to unlock the object. """ logger.debug("Attempting to open up a lock on `%s` by user `%s`" % (self, user)) self.unlock()
def lock_for(self, user, hard_lock=True): """ Together with ``unlock_for`` this is probably the most important method on this model. If applicable to your use-case, you should lock for a specific user; that way, we can throw an exception when another user tries to unlock an object they haven't locked themselves. When using soft locks, any process can still use the save method on this object. If you set ``hard_lock=True``, trying to save an object without first unlocking will raise an ``ObjectLockedError``. Don't use hard locks unless you really need them. See :doc:`design`. The 'hard lock' flag is set to True as the default as a fail safe method to back up javascript lock validations. This is useful when the user's lock expires or javascript fails to load, etc. Keep in mind that soft locks are set since they provide the user with a user friendly locking interface. """ logger.debug("Attempting to initiate a lock for user `%s`" % user) if not isinstance(user, get_user_model( )): # sbroumley 4/19/2016 - updated to dynamic setting raise ValueError( "You should pass a valid auth.get_user_model() to lock_for.") if self.lock_applies_to(user): raise ObjectLockedError( "This object is already locked by another" " user. May not override, except through the `unlock` method.") else: self._locked_at = datetime.today() self._locked_by = user self._hard_lock = self.__init_hard_lock = hard_lock date = self.locked_at.strftime("%H:%M:%S") # an administrative toggle, to make it easier for devs to extend `django-locking` # and react to locking and unlocking self._state.locking = True logger.debug("Initiated a %s lock for `%s` at %s" % (self.lock_type, self.locked_by, self.locked_at))
def lock_for(self, user, hard_lock=True): """ Together with ``unlock_for`` this is probably the most important method on this model. If applicable to your use-case, you should lock for a specific user; that way, we can throw an exception when another user tries to unlock an object they haven't locked themselves. When using soft locks, any process can still use the save method on this object. If you set ``hard_lock=True``, trying to save an object without first unlocking will raise an ``ObjectLockedError``. Don't use hard locks unless you really need them. See :doc:`design`. The 'hard lock' flag is set to True as the default as a fail safe method to back up javascript lock validations. This is useful when the user's lock expires or javascript fails to load, etc. Keep in mind that soft locks are set since they provide the user with a user friendly locking interface. """ logger.debug("Attempting to initiate a lock for user `%s`" % user) if not isinstance(user, get_user_model()): # sbroumley 4/19/2016 - updated to dynamic setting raise ValueError("You should pass a valid auth.get_user_model() to lock_for.") if self.lock_applies_to(user): raise ObjectLockedError("This object is already locked by another" " user. May not override, except through the `unlock` method.") else: self._locked_at = datetime.today() self._locked_by = user self._hard_lock = self.__init_hard_lock = hard_lock date = self.locked_at.strftime("%H:%M:%S") # an administrative toggle, to make it easier for devs to extend `django-locking` # and react to locking and unlocking self._state.locking = True logger.debug( "Initiated a %s lock for `%s` at %s" % ( self.lock_type, self.locked_by, self.locked_at ))
def lock_applies_to(self, user): """ A lock does not apply to the user who initiated the lock. Thus, ``lock_applies_to`` is used to ascertain whether a user is allowed to edit a locked object. """ logger.debug("Checking if the lock on `%s` applies to user `%s`" % (self, user)) # a lock does not apply to the person who initiated the lock if self.is_locked and self.locked_by != user: logger.debug("Lock applies.") return True else: logger.debug("Lock does not apply.") return False
def decorated_view(*vargs, **kwargs): response = view(*vargs, **kwargs) logger.debug("Sending a request: \n\t%s" % (response.content)) return response