Commit 2380a0c5 authored by Matheus Miranda's avatar Matheus Miranda
Browse files

Merge branch 'develop' into 'master'

Develop

See merge request !97
parents a3131837 dd3b7188
This diff is collapsed.
# Generated by Django 2.2.25 on 2021-12-21 19:26
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('courses', '0038_auto_20211215_1805'),
]
operations = [
migrations.RemoveField(
model_name='course',
name='slug_en',
),
migrations.RemoveField(
model_name='course',
name='slug_es',
),
migrations.RemoveField(
model_name='course',
name='slug_pt_br',
),
]
......@@ -27,6 +27,7 @@ from .classroom.models import Classroom, Event
from .scheduling.models import ScheduledTask
import re
import pytz
class CourseCategory(models.Model):
......@@ -903,6 +904,7 @@ class Lesson(PositionedModel):
('draft', _('Draft')),
('published', _('Published')),
)
TIMEZONE_CHOICES = zip(pytz.all_timezones, pytz.all_timezones)
course = models.ForeignKey(
Course,
......@@ -950,6 +952,7 @@ class Lesson(PositionedModel):
blank=True,
default=None,
)
release_date_timezone = models.CharField(max_length=255, default='UTC', blank=True, choices=TIMEZONE_CHOICES)
scheduled_task = GenericRelation(ScheduledTask, related_query_name='lesson')
collection_name = 'course'
......@@ -1003,6 +1006,8 @@ class Unit(PositionedModel):
('draft', _('Draft')),
('published', _('Published')),
)
TIMEZONE_CHOICES = zip(pytz.all_timezones, pytz.all_timezones)
title = models.CharField(
_('Title'),
max_length=128,
......@@ -1050,6 +1055,7 @@ class Unit(PositionedModel):
blank=True,
default=None,
)
release_date_timezone = models.CharField(max_length=255, default='UTC', blank=True, choices=TIMEZONE_CHOICES)
scheduled_task = GenericRelation(ScheduledTask, related_query_name='unit')
collection_name = 'lesson'
......
......@@ -6,6 +6,8 @@ from .tasks import publish_lesson, publish_unit
from .scheduling.models import ScheduledTask
from celery.result import AsyncResult
from celery import Celery
app = Celery()
def schedule_task(content_type, instance):
......@@ -18,8 +20,7 @@ def schedule_task(content_type, instance):
previous_task_hash = st.task_hash
if previous_task_hash:
from celery.task.control import revoke
revoke(previous_task_hash)
app.control.revoke(previous_task_hash)
if content_type.name == 'Lesson':
new_task_hash = publish_lesson.apply_async(args=[instance.id], eta=instance.release_date)
......@@ -33,20 +34,27 @@ def schedule_task(content_type, instance):
@receiver(pre_save, sender=Lesson)
def pre_lesson_created_or_updated(sender, instance: Lesson, **kwargs):
previous_release_date = None
prev_release_date = None
prev_release_date_tz = 'UTC'
if instance.id:
previous_lesson = Lesson.objects.get(id=instance.id)
previous_release_date = previous_lesson.release_date
prev_release_date = previous_lesson.release_date
prev_release_date_tz = previous_lesson.release_date_timezone
import pytz
tz = pytz.timezone(instance.release_date_timezone)
instance.release_date = instance.release_date.astimezone(tz)
new_release_date = instance.release_date
new_release_date_tz = instance.release_date_timezone
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 prev_release_date:
prev_release_date = prev_release_date.replace(microsecond=0, second=0)
if (not instance.id or new_release_date != previous_release_date) \
and instance.release_date:
if (not instance.id or new_release_date != prev_release_date or new_release_date_tz != prev_release_date_tz) \
and instance.release_date and instance.release_date_timezone:
from django.contrib.contenttypes.models import ContentType
content_type = ContentType.objects.get_for_model(instance)
......
from .models import Lesson, Unit
from celery.decorators import task
#fro celery.decorators import task
from celery import Celery
app = Celery()
@task()
@app.task()
def publish_lesson(object_id):
lesson = Lesson.objects.filter(id=object_id)
if lesson.exists():
......@@ -10,7 +12,7 @@ def publish_lesson(object_id):
lesson.status = 'published'
lesson.save()
@task()
@app.task()
def publish_unit(object_id):
unit = Unit.objects.filter(id=object_id)
if unit.exists():
......
......@@ -8,9 +8,8 @@ from .certification.models import CertificateTemplate
@register(Course)
class CourseTranslationOptions(TranslationOptions):
fields = ('slug', 'name', 'application',
'requirement', 'abstract', 'structure', 'workload',
'course_load', 'pronatec', 'thumbnail', 'home_thumbnail',
fields = ('name', 'application', 'requirement', 'abstract', 'structure',
'workload', 'course_load', 'pronatec', 'thumbnail', 'home_thumbnail',
'min_percent_to_complete', 'informations', 'description')
......
......@@ -14,6 +14,7 @@ from .views import (
CourseAuthorViewSet,
CoursePictureUploadViewSet,
TranslateSlugView,
TimezonesView,
)
from .videos.views import VideoViewSet
from .course_material.views import CourseMaterialViewSet, CourseMaterialFileViewSet
......@@ -48,7 +49,6 @@ from courses.notes.views import (
NotesViewSet,
UserNotesViewSet,
)
from courses.certification.views import (
CourseCertificationPDFView,
)
......@@ -111,6 +111,8 @@ urlpatterns = [
url(r'^user-access', UserAccessView.as_view(), name='user-access'),
url(r'^translate-slug', TranslateSlugView.as_view(), name='translate-slug'),
url(r'^timezones', TimezonesView.as_view(), name='timezones'),
url(r'^certificate/(?P<slug>[-a-zA-Z0-9_]+)/pdf/$',
CourseCertificationPDFView.as_view(template_name="certificate_pdf.html"),
name='certificate-pdf',
......
from django.db.models import Q
from django.db import IntegrityError
from django.contrib.auth import get_user_model
from django.http import HttpResponse
from rest_framework import views, viewsets, mixins, permissions, status
from rest_framework.response import Response
......@@ -335,3 +336,13 @@ class TranslateSlugView(views.APIView):
return Response({'slug': translated_slug}, status=status.HTTP_200_OK)
class TimezonesView(views.APIView):
permission_classes = [permissions.IsAuthenticated]
def get(self, request, *args, **kwargs):
import pytz
import json
timezones = json.dumps({"timezones": pytz.all_timezones})
return HttpResponse(timezones, content_type ="application/json")
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