Commit 7dfe1684 authored by Matheus Miranda's avatar Matheus Miranda
Browse files

Update migration for generic relation

parent f595786b
......@@ -24,6 +24,7 @@ from .notes.models import Note
from .course_material.models import CourseMaterial
from .classes.models import Class
from .classroom.models import Classroom, Event
from .scheduling.models import ScheduledTask
import re
......@@ -948,6 +949,7 @@ class Lesson(PositionedModel):
blank=True,
default=None,
)
scheduled_task = GenericRelation(ScheduledTask, related_query_name='lesson')
collection_name = 'course'
class Meta:
......@@ -1047,6 +1049,7 @@ class Unit(PositionedModel):
blank=True,
default=None,
)
scheduled_task = GenericRelation(ScheduledTask, related_query_name='unit')
collection_name = 'lesson'
......
# Generated by Django 2.2.24 on 2021-12-07 12:11
# Generated by Django 2.2.24 on 2021-12-07 13:32
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
......@@ -8,6 +9,7 @@ class Migration(migrations.Migration):
initial = True
dependencies = [
('contenttypes', '0002_remove_content_type_name'),
]
operations = [
......@@ -16,9 +18,11 @@ class Migration(migrations.Migration):
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('task_hash', models.CharField(blank=True, max_length=255, verbose_name='Name')),
('object_id', models.PositiveIntegerField()),
('release_date', models.DateTimeField(blank=True, default=None, null=True, verbose_name='Date')),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('content_type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='contenttypes.ContentType')),
],
options={
'verbose_name': 'ScheduledTask',
......
from django.db import models
from django.conf import settings
from django.urls import reverse_lazy
from courses.models import Lesson, Unit
from django.utils.translation import ugettext_lazy as _
from django.contrib.contenttypes.fields import GenericRelation
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
class ScheduledTask(models.Model):
......@@ -18,9 +17,15 @@ class ScheduledTask(models.Model):
max_length=255,
blank=True,
)
related_obj = GenericRelation(Lesson, Unit,)
content_type = models.ForeignKey(
ContentType,
models.CASCADE,
)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey(
'content_type',
'object_id',
)
release_date = models.DateTimeField(
_('Date'),
null=True,
......
from django.db.models.signals import pre_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 celery.result import AsyncResult
@receiver(pre_save, sender=Lesson)
def lesson_created_or_updated(sender, instance: Lesson, **kwargs):
......@@ -11,7 +15,25 @@ def lesson_created_or_updated(sender, instance: Lesson, **kwargs):
previous_lesson = Lesson.objects.get(id=instance.id)
if (not instance.id or instance.release_date != previous_lesson.release_date) and instance.release_date:
publish_lesson.apply_async(args=[instance.id], eta=instance.release_date)
previous_task_hask = ''
ct = ContentType.objects.get_for_model(comment)
st = ScheduledTask.objects.filter(Content_type=ct, object_id=instance.id)
if st.exists():
previous_task_hask = st.task_hash
else:
st = ScheduledTask.objects.create(content_object=instance)
if previous_task_hask:
res = AsyncResult(previous_task_hask)
res.revoke()
new_task_hash = publish_lesson.apply_async(args=[instance.id], eta=instance.release_date)
st.task_hash = 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):
......
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