So I'm creating a django app that allows a user to add a new line of text to an existing group of text lines. However I don't want multiple users adding lines to the same group of text lines concurrently. So I created a BoolField isBeingEdited that is set to True once a user decides to append a specific group. Once the Bool is True no one else can append the group until the edit is submitted, whereupon the Bool is set False again. Works alright, unless someone decides to make an edit then changes their mind or forgets about it, etc. I want isBeingEdited to flip back to False after 10 minutes or so. Is this a job for cron, or is there something easier out there? Any suggestions?
所以我正在创建一个django应用程序,允许用户向现有的一组文本行添加新的文本行。但是,我不希望多个用户同时向同一组文本行添加行。所以我创建了一个BoolField isBeingEdited,一旦用户决定附加一个特定的组,就会设置为True。一旦Bool为True,在提交编辑之前没有其他人可以追加该组,于是Bool再次设置为False。工作正常,除非有人决定进行编辑然后改变他们的想法或忘记它等等。我想isBeingEdited在10分钟左右后翻转回False。这是cron的工作,还是有更容易的东西?有什么建议?
1 个解决方案
#1
Change the boolean to a "lock time"
将布尔值更改为“锁定时间”
- To lock the model, set the Lock time to the current time.
- To unlock the model, set the lock time to None
- Add an "is_locked" method. That method returns "not locked" if the current time is more than 10 minutes after the lock time.
要锁定模型,请将锁定时间设置为当前时间。
要解锁模型,请将锁定时间设置为“无”
添加“is_locked”方法。如果当前时间在锁定时间之后超过10分钟,则该方法返回“未锁定”。
This gives you your timeout without Cron and without regular hits into the DB to check flags and unset them. Instead, the time is only checked if you are interest in wieither this model is locked. A Cron would likely have to check all models.
这样可以在没有Cron的情况下为您提供超时,并且无需定期点击进入数据库以检查标志并取消设置。相反,只有当您对此模型被锁定感兴趣时,才会检查时间。 Cron可能需要检查所有型号。
from django.db import models
from datetime import datetime, timedelta
# Create your models here.
class yourTextLineGroup(models.Model):
# fields go here
lock_time = models.DateTimeField(null=True)
locked_by = models.ForeignKey()#Point me to your user model
def lock(self):
if self.is_locked(): #and code here to see if current user is not locked_by user
#exception / bad return value here
pass
self.lock_time = datetime.now()
def unlock(self):
self.lock_time = None
def is_locked(self):
return self.lock_time and datetime.now() - self.lock_time < timedelta(minutes=10)
Code above assumes that the caller will call the save method after calling lock or unlock.
上面的代码假定调用者在调用lock或unlock后调用save方法。
#1
Change the boolean to a "lock time"
将布尔值更改为“锁定时间”
- To lock the model, set the Lock time to the current time.
- To unlock the model, set the lock time to None
- Add an "is_locked" method. That method returns "not locked" if the current time is more than 10 minutes after the lock time.
要锁定模型,请将锁定时间设置为当前时间。
要解锁模型,请将锁定时间设置为“无”
添加“is_locked”方法。如果当前时间在锁定时间之后超过10分钟,则该方法返回“未锁定”。
This gives you your timeout without Cron and without regular hits into the DB to check flags and unset them. Instead, the time is only checked if you are interest in wieither this model is locked. A Cron would likely have to check all models.
这样可以在没有Cron的情况下为您提供超时,并且无需定期点击进入数据库以检查标志并取消设置。相反,只有当您对此模型被锁定感兴趣时,才会检查时间。 Cron可能需要检查所有型号。
from django.db import models
from datetime import datetime, timedelta
# Create your models here.
class yourTextLineGroup(models.Model):
# fields go here
lock_time = models.DateTimeField(null=True)
locked_by = models.ForeignKey()#Point me to your user model
def lock(self):
if self.is_locked(): #and code here to see if current user is not locked_by user
#exception / bad return value here
pass
self.lock_time = datetime.now()
def unlock(self):
self.lock_time = None
def is_locked(self):
return self.lock_time and datetime.now() - self.lock_time < timedelta(minutes=10)
Code above assumes that the caller will call the save method after calling lock or unlock.
上面的代码假定调用者在调用lock或unlock后调用save方法。