Commit a3131837 authored by Matheus Miranda's avatar Matheus Miranda
Browse files

Merge branch 'develop' into 'master'

Update course reports

See merge request !94
parents 10835827 d13de02d
......@@ -5,3 +5,6 @@ from django.contrib.auth import get_user_model
class CoursesConfig(AppConfig):
name = 'courses'
def ready(self):
import courses.signals
......@@ -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
......@@ -569,11 +570,12 @@ class CourseStudent(models.Model):
# TODO refator to make one query to count unts done for all lessons
from courses_learning_objects.models import LearningObject, Answer
progress_list = []
for lesson in self.course.lessons.filter(status='published'):
for lesson in self.course.lessons.all():
lesson_progress = {}
lesson_progress['name'] = lesson.name
lesson_progress['slug'] = lesson.slug
lesson_progress['position'] = lesson.position
lesson_progress['status'] = lesson.status
units_len = lesson.unit_count()
lesson_progress['activities'] = []
if units_len:
......@@ -948,6 +950,7 @@ class Lesson(PositionedModel):
blank=True,
default=None,
)
scheduled_task = GenericRelation(ScheduledTask, related_query_name='lesson')
collection_name = 'course'
class Meta:
......@@ -1047,6 +1050,7 @@ class Unit(PositionedModel):
blank=True,
default=None,
)
scheduled_task = GenericRelation(ScheduledTask, related_query_name='unit')
collection_name = 'lesson'
......
from django.apps import AppConfig
class CoursesSchedulingConfig(AppConfig):
name = 'courses.scheduling'
# 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):
initial = True
dependencies = [
('contenttypes', '0002_remove_content_type_name'),
]
operations = [
migrations.CreateModel(
name='ScheduledTask',
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',
'verbose_name_plural': 'ScheduledTasks',
},
),
]
from django.db import models
from django.conf import settings
from django.urls import reverse_lazy
from django.utils.translation import ugettext_lazy as _
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
class ScheduledTask(models.Model):
class Meta:
verbose_name = _('ScheduledTask')
verbose_name_plural = _('ScheduledTasks')
task_hash = models.CharField(
_('Name'),
max_length=255,
blank=True,
)
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,
blank=True,
default=None,
)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
from django.db.models.signals import pre_save, post_save
from django.dispatch import receiver
from .models import Lesson, Unit
from .tasks import publish_lesson, publish_unit
from .scheduling.models import ScheduledTask
from celery.result import AsyncResult
def schedule_task(content_type, instance):
previous_task_hash = ''
instance_id = instance.id if instance.id else None
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)
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=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)
from .models import Lesson, Unit
from celery.decorators import task
@task()
def publish_lesson(object_id):
lesson = Lesson.objects.filter(id=object_id)
if lesson.exists():
lesson = lesson.first()
lesson.status = 'published'
lesson.save()
@task()
def publish_unit(object_id):
unit = Unit.objects.filter(id=object_id)
if unit.exists():
unit = unit.first()
unit.status = 'published'
unit.save()
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