如何重复django事件发生周期?
How to repeat django event occurrence period?
我有一个模型可以获取事件应该始终发生的日期和时间。此外,人们可以参加活动前的天数。
def validate_only_one(obj):
model = obj.__class__
if model.objects.count() > 0 and obj.id != model.objects.get().id:
raise ValidationError("Você só pode adicionar um evento") # You can add only one schedule
class ModelPresenca(models.Model):
DIAS = (
# The days are in Portuguese
('Domingo', 'Domingo'),
('Segunda', 'Segunda'),
('Terça', 'Terça'),
('Quarta', 'Quarta'),
('Quinta', 'Quinta'),
('Sexta', 'Sexta'),
('Sábado', 'Sábado'),
)
dia = models.CharField('Dia da Pelada', max_length=10, help_text='Escolha o dia da pelada', choices=DIAS) # day of the event
hora_pelada = models.TimeField('Horário da pelada (ex: 19:30)', help_text='Hora em que sua pelada é realizada') # hour of the event
dias_antecedencia = models.PositiveIntegerField('Dias antecedência',
default=1,
validators=[MinValueValidator(1), MaxValueValidator(6)],
help_text='Em quantos dias de antecência os peladeiros devem '
'marcar presença') # numbers of days before event
但是,我不知道如何在不要求用户每周添加相同信息的情况下重复此事件。我正在考虑以下算法:
def period(request):
event_data = ModelPresenca.objects.all() # I have always only one object
hour_timefield = datetime.strptime(h.hora_pelada, '%H:%M') # TimeField to string
day = event_data.dia # Day of the event
b_days = event_data.dias_antecedencia # Number of days before event
week_d = week_day(day) # week_day is a function that converts day to int
event_d = event_day(week_d, hour_timefield) # event_day is a function that returns the date of full event occur
conf_d = event_open_date(b_days, event_d) # Is a function that returns date of open to confirm
n = datetime.now()
t_str = '{}-{}-{}'.format(n.year, n.month, n.day)
t_hour = ' {}:{}'.format(n.hour, n.minute)
today = t_str + t_hour
today = datetime.strptime(today, '%Y-%m-%d %H:%M')
if (conf_d <= today) and (today < event_d):
# Show formulary to confirm presence
else:
# another thing, ie, hidden formulary
我认为最后一个代码块必须重复,但我不知道这样做,也不知道这是否是最好的方法。如果有人可以帮助我,我将不胜感激。
您可以使用 Django Celery。 Celery 是一个基于分布式消息传递的异步任务 queue/job 队列。它专注于实时操作,但也支持调度。
这是 django celery 的第一步。您可以在您的模型中创建一个 post_save 方法,您将在其中 运行 一个异步任务,它将根据您的需要添加事件。此外,还有一个 Celery beat,可让您创建异步周期性任务。
例如:
@app.task()
def period():
# Do your stuff here
然后在您的设置文件中添加您的芹菜节拍时间表:
CELERY_BEAT_SCHEDULE = {
'add-periodic-events': {
'task': 'my_app.my_tasks.periodic', # this is a path to your file separated by '.'
'schedule': crontab(minute=0, hour=0), # Execute daily at midnight.
},
}
我有一个模型可以获取事件应该始终发生的日期和时间。此外,人们可以参加活动前的天数。
def validate_only_one(obj):
model = obj.__class__
if model.objects.count() > 0 and obj.id != model.objects.get().id:
raise ValidationError("Você só pode adicionar um evento") # You can add only one schedule
class ModelPresenca(models.Model):
DIAS = (
# The days are in Portuguese
('Domingo', 'Domingo'),
('Segunda', 'Segunda'),
('Terça', 'Terça'),
('Quarta', 'Quarta'),
('Quinta', 'Quinta'),
('Sexta', 'Sexta'),
('Sábado', 'Sábado'),
)
dia = models.CharField('Dia da Pelada', max_length=10, help_text='Escolha o dia da pelada', choices=DIAS) # day of the event
hora_pelada = models.TimeField('Horário da pelada (ex: 19:30)', help_text='Hora em que sua pelada é realizada') # hour of the event
dias_antecedencia = models.PositiveIntegerField('Dias antecedência',
default=1,
validators=[MinValueValidator(1), MaxValueValidator(6)],
help_text='Em quantos dias de antecência os peladeiros devem '
'marcar presença') # numbers of days before event
但是,我不知道如何在不要求用户每周添加相同信息的情况下重复此事件。我正在考虑以下算法:
def period(request):
event_data = ModelPresenca.objects.all() # I have always only one object
hour_timefield = datetime.strptime(h.hora_pelada, '%H:%M') # TimeField to string
day = event_data.dia # Day of the event
b_days = event_data.dias_antecedencia # Number of days before event
week_d = week_day(day) # week_day is a function that converts day to int
event_d = event_day(week_d, hour_timefield) # event_day is a function that returns the date of full event occur
conf_d = event_open_date(b_days, event_d) # Is a function that returns date of open to confirm
n = datetime.now()
t_str = '{}-{}-{}'.format(n.year, n.month, n.day)
t_hour = ' {}:{}'.format(n.hour, n.minute)
today = t_str + t_hour
today = datetime.strptime(today, '%Y-%m-%d %H:%M')
if (conf_d <= today) and (today < event_d):
# Show formulary to confirm presence
else:
# another thing, ie, hidden formulary
我认为最后一个代码块必须重复,但我不知道这样做,也不知道这是否是最好的方法。如果有人可以帮助我,我将不胜感激。
您可以使用 Django Celery。 Celery 是一个基于分布式消息传递的异步任务 queue/job 队列。它专注于实时操作,但也支持调度。
这是 django celery 的第一步。您可以在您的模型中创建一个 post_save 方法,您将在其中 运行 一个异步任务,它将根据您的需要添加事件。此外,还有一个 Celery beat,可让您创建异步周期性任务。
例如:
@app.task()
def period():
# Do your stuff here
然后在您的设置文件中添加您的芹菜节拍时间表:
CELERY_BEAT_SCHEDULE = {
'add-periodic-events': {
'task': 'my_app.my_tasks.periodic', # this is a path to your file separated by '.'
'schedule': crontab(minute=0, hour=0), # Execute daily at midnight.
},
}