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

Merge branch 'develop' into 'master'

Develop

See merge request !93
parents 4173672f a3bacc19
# Generated by Django 2.2.24 on 2021-11-25 15:03
from django.db import migrations, models
import django.utils.timezone
class Migration(migrations.Migration):
dependencies = [
('courses', '0035_auto_20211119_1729'),
]
operations = [
migrations.AddField(
model_name='unit',
name='release_date',
field=models.DateTimeField(default=django.utils.timezone.now, verbose_name='Date'),
),
migrations.AddField(
model_name='unit',
name='status',
field=models.CharField(choices=[('draft', 'Draft'), ('published', 'Published')], default='draft', max_length=64, verbose_name='Status'),
),
]
# Generated by Django 2.2.24 on 2021-11-29 20:52
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('courses', '0036_auto_20211125_1203'),
]
operations = [
migrations.AlterField(
model_name='lesson',
name='release_date',
field=models.DateTimeField(blank=True, default=None, null=True, verbose_name='Date'),
),
migrations.AlterField(
model_name='unit',
name='release_date',
field=models.DateTimeField(blank=True, default=None, null=True, verbose_name='Date'),
),
]
......@@ -210,12 +210,7 @@ class Course(models.Model):
def public_lessons(self):
lessons = []
for lesson in self.lessons.all():
if lesson.status == 'draft':
if lesson.release_date and lesson.release_date <= timezone.now():
lesson.status = 'published'
lesson.save()
lessons.append(lesson)
else:
if lesson.status == 'published':
lessons.append(lesson)
return lessons
......@@ -497,23 +492,50 @@ class CourseStudent(models.Model):
def resume_next_unit(self):
try:
last_unit_done = self.units_done.latest('complete')
# try to get the next unit in same lesson
next_unit = Unit.objects.filter(lesson=last_unit_done.unit.lesson,
position__gt=last_unit_done.unit.position).order_by(
'position').first()
next_unit = None
cur_lesson = None
last_unit_done = self.units_done.latest('complete') if self.units_done else None
if last_unit_done:
cur_position = last_unit_done.unit.position
cur_lesson = last_unit_done.unit.lesson
units_done_ids = [sp.unit.id for sp in self.units_done]
if cur_lesson.status == 'published':
next_units = Unit.objects.filter(lesson=last_unit_done.unit.lesson,
position__gt=cur_position,
status='published').exclude(id__in=units_done_ids).order_by('position')
if next_units:
next_unit = next_units.first()
else:
# If there is no foward units, get the first one not completed
prev_public_units = Unit.objects.filter(lesson__course=cur_lesson.course,
lesson__status='published',
status='published').exclude(id__in=units_done_ids)
if prev_public_units:
next_unit = prev_public_units.first()
elif cur_lesson == self.course.public_lessons[-1]:
next_unit = last_unit_done.unit
if next_unit:
return next_unit
else:
next_lesson = self.course.lessons.filter(
position__gt=last_unit_done.unit.lesson.position,
status='published').order_by(
'position').first()
if next_lesson and next_lesson.first_unit():
return next_lesson.units.order_by('position').first()
else:
return self.course.first_lesson().first_unit()
lesson_position = cur_lesson.position if cur_lesson else -1
public_lessons = self.course.lessons.filter(
status='published',
position__gt=lesson_position).order_by('position')
for lesson in public_lessons:
public_units = lesson.units.filter(status='published').order_by('position')
if public_units:
return public_units.first()
return None
except StudentProgress.DoesNotExist:
first_lesson = self.course.first_lesson()
if first_lesson:
......@@ -922,7 +944,9 @@ class Lesson(PositionedModel):
)
release_date = models.DateTimeField(
_('Date'),
default=timezone.now
null=True,
blank=True,
default=None,
)
collection_name = 'course'
......@@ -972,6 +996,10 @@ class Lesson(PositionedModel):
class Unit(PositionedModel):
STATES = (
('draft', _('Draft')),
('published', _('Published')),
)
title = models.CharField(
_('Title'),
max_length=128,
......@@ -1007,6 +1035,18 @@ class Unit(PositionedModel):
blank=True,
null=True,
)
status = models.CharField(
_('Status'),
choices=STATES,
default=STATES[0][0],
max_length=64,
)
release_date = models.DateTimeField(
_('Date'),
null=True,
blank=True,
default=None,
)
collection_name = 'lesson'
......
......@@ -179,10 +179,12 @@ class ProfessorMessageSerializer(serializers.ModelSerializer):
class UnitSerializer(serializers.ModelSerializer):
video = VideoSerializer(required=False, allow_null=True)
activities = ActivitySerializer(many=True, required=False, allow_null=True)
release_date = serializers.DateTimeField(required=False)
class Meta:
model = Unit
fields = ('id', 'title', 'video', 'activities', 'side_notes', 'position', 'chat_room',)
fields = ('id', 'title', 'video', 'activities', 'side_notes',
'position', 'chat_room', 'release_date', 'status',)
class LessonSerializer(serializers.ModelSerializer):
......@@ -190,7 +192,7 @@ class LessonSerializer(serializers.ModelSerializer):
units = UnitSerializer(many=True)
is_course_last_lesson = serializers.BooleanField(read_only=True)
thumbnail_url = serializers.ReadOnlyField()
release_date = serializers.DateTimeField()
release_date = serializers.DateTimeField(required=False)
class Meta:
model = Lesson
......
......@@ -291,7 +291,7 @@ class LessonViewSet(viewsets.ModelViewSet):
def get_queryset(self):
queryset = super(LessonViewSet, self).get_queryset()
course_id = self.kwargs['course_id']
return queryset.filter(status='published', course_id=course_id)
return queryset.filter(status='published', units__status='published', course_id=course_id)
class CourseAuthorViewSet(viewsets.ModelViewSet):
......
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