Commit 4fa6c9cb authored by Matheus Miranda's avatar Matheus Miranda
Browse files

Add signal to schedule publish tasks

parent 5c424893
from django.db.models.signals import pre_save
from django.db.models.signals import pre_save, post_save
from django.dispatch import receiver
from django.contrib.contenttypes.models import ContentType
from .models import Lesson, Unit
from .scheduling.models import ScheduledTask
from .tasks import publish_lesson, publish_unit
from .scheduling.models import ScheduledTask
from celery.result import AsyncResult
@receiver(pre_save, sender=Lesson)
def lesson_created_or_updated(sender, instance: Lesson, **kwargs):
if instance.id:
previous_lesson = Lesson.objects.get(id=instance.id)
if (not instance.id or instance.release_date != previous_lesson.release_date) and instance.release_date:
previous_task_hash = ''
ct = ContentType.objects.get_for_model(instance)
def schedule_task(content_type, instance):
previous_task_hash = ''
instance_id = instance.id if instance.id else None
st = ScheduledTask.objects.filter(content_type=ct, object_id=instance.id)
if st.exists():
st = st.first()
previous_task_hash = st.task_hash
else:
st = ScheduledTask.objects.create(content_object=instance)
st = ScheduledTask.objects.filter(content_type=content_type, object_id=instance_id)
if st.exists():
st = st.first()
previous_task_hash = st.task_hash
if previous_task_hash:
from celery.task.control import revoke
revoke(previous_task_hash)
new_task_hash = publish_lesson.apply_async(args=[instance.id], eta=instance.release_date)
if content_type.name == 'Lesson':
new_task_hash = publish_lesson.apply_async(args=[instance.id], eta=instance.release_date)
elif content_type.name == 'Unit':
new_task_hash = publish_unit.apply_async(args=[instance.id], eta=instance.release_date)
st.task_hash = new_task_hash
st.content_object = instance
st.release_date = instance.release_date
st.save()
@receiver(pre_save, sender=Unit)
def unit_created_or_updated(sender, instance: Unit, **kwargs):
previous_unit = Unit.objects.get(id=instance.id)
#publish_unit.apply_async(args=[instance.id], eta=instance.release_date)
@receiver(pre_save, sender=Lesson)
def pre_lesson_created_or_updated(sender, instance: Lesson, **kwargs):
previous_release_date = None
if instance.id:
previous_lesson = Lesson.objects.get(id=instance.id)
previous_release_date = previous_lesson.release_date
new_release_date = instance.release_date
if new_release_date:
new_release_date = new_release_date.replace(microsecond=0, second=0)
if previous_release_date:
previous_release_date = previous_release_date.replace(microsecond=0, second=0)
if (not instance.id or new_release_date != previous_release_date) \
and instance.release_date:
from django.contrib.contenttypes.models import ContentType
content_type = ContentType.objects.get_for_model(instance)
schedule_task(content_type, instance)
def create_first_task(instance):
from django.contrib.contenttypes.models import ContentType
content_type = ContentType.objects.get_for_model(instance)
st = ScheduledTask.objects.filter(content_type=content_type, object_id=instance.id)
if not st.exists():
st = ScheduledTask.objects.create(content_object=instance)
if content_type.name == 'Lesson':
publish_lesson.apply_async(args=[instance.id], eta=instance.release_date)
elif content_type.name == 'Unit':
publish_unit.apply_async(args=[instance.id], eta=instance.release_date)
@receiver(post_save, sender=Lesson)
def post_lesson_created_or_updated(sender, instance: Lesson, **kwargs):
create_first_task(instance)
@receiver(post_save, sender=Unit)
def post_unit_created_or_updated(sender, instance: Lesson, **kwargs):
create_first_task(instance)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment