From 1ad2b3c775dd53803c1ea0695cda33871ab94be2 Mon Sep 17 00:00:00 2001 From: Yoan Le Clanche Date: Wed, 25 Sep 2024 11:28:28 +0200 Subject: [PATCH] Add ConferencePublication code from crfpa --- teleforma/admin.py | 7 ++- .../teleforma-publish-notify-conferences.py | 13 +++- .../migrations/0012_auto_20240925_0929.py | 35 +++++++++++ teleforma/models/core.py | 54 +++++++++++++--- .../teleforma/inc/conference_list.html | 14 ++--- .../templates/teleforma/inc/media_list.html | 62 +++++++++++-------- teleforma/templatetags/teleforma_tags.py | 26 ++++++++ teleforma/views/core.py | 50 +++++++++++++++ 8 files changed, 218 insertions(+), 43 deletions(-) create mode 100644 teleforma/migrations/0012_auto_20240925_0929.py diff --git a/teleforma/admin.py b/teleforma/admin.py index 41ed60e1..f0e19802 100644 --- a/teleforma/admin.py +++ b/teleforma/admin.py @@ -25,7 +25,7 @@ from .models.appointment import (Appointment, AppointmentJury, from .models.core import (Conference, Course, CourseType, Department, Document, DocumentSimple, DocumentType, LiveStream, Media, MediaTranscoded, Organization, Period, Professor, - Room, StreamingServer) + Room, StreamingServer, ConferencePublication) from .models.crfpa import (IEJ, Corrector, Discount, Home, NewsItem, OptionalFee, Parameters, Payback, Payment, Profile, Student, Training) @@ -374,6 +374,9 @@ class MediaInline(admin.StackedInline): model = Media exclude = ['readers', ] +class ConferenceInline(admin.StackedInline): + model = ConferencePublication + @admin.action(description='Publish selected conferences') def publish_conferences(modeladmin, request, queryset): for conference in queryset: @@ -399,7 +402,7 @@ def duplicate_conferences(modeladmin, request, queryset): class ConferenceAdmin(admin.ModelAdmin): - inlines = [MediaInline,] + inlines = [MediaInline, ConferenceInline] exclude = ['readers'] list_per_page = 30 list_filter = ('course', 'period', 'date_begin', 'session', 'course_type') diff --git a/teleforma/management/commands/teleforma-publish-notify-conferences.py b/teleforma/management/commands/teleforma-publish-notify-conferences.py index 2e5bbc98..6300f40d 100644 --- a/teleforma/management/commands/teleforma-publish-notify-conferences.py +++ b/teleforma/management/commands/teleforma-publish-notify-conferences.py @@ -10,7 +10,7 @@ from django.core.management.base import BaseCommand, CommandError from django.contrib.auth.models import User from django.template.defaultfilters import slugify from django.urls import reverse -from teleforma.models.core import Conference, Period +from teleforma.models.core import Conference, ConferencePublication, Period from teleforma.models.crfpa import Student from teleforma.models.notification import notify from teleforma.views.core import get_courses @@ -75,6 +75,12 @@ class Command(BaseCommand): now_plus = now + datetime.timedelta(minutes=minute_high_range) publications = list(Conference.objects.filter( + period=period, + status=2, + notified=False, + date_publish__lte=now_plus, + date_publish__gte=now_minus, + )) + list(ConferencePublication.objects.filter( period=period, status=2, notified=False, @@ -87,7 +93,10 @@ class Command(BaseCommand): for publication in publications: - conference = publication + if type(publication) == ConferencePublication: + conference = publication.conference + else: + conference = publication medias = conference.media.all() diff --git a/teleforma/migrations/0012_auto_20240925_0929.py b/teleforma/migrations/0012_auto_20240925_0929.py new file mode 100644 index 00000000..107d1894 --- /dev/null +++ b/teleforma/migrations/0012_auto_20240925_0929.py @@ -0,0 +1,35 @@ +# Generated by Django 3.2.25 on 2024-09-25 09:29 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('teleforma', '0011_auto_20240311_2202'), + ] + + operations = [ + migrations.AlterField( + model_name='student', + name='confirmation_sent', + field=models.BooleanField(default=False, verbose_name='confirmation sent'), + ), + migrations.AlterField( + model_name='student', + name='is_subscribed', + field=models.BooleanField(default=False, verbose_name='subscribed'), + ), + migrations.CreateModel( + name='ConferencePublication', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('date_publish', models.DateTimeField(blank=True, null=True, verbose_name='publishing date')), + ('status', models.IntegerField(choices=[(0, 'Hidden'), (1, 'Private'), (2, 'Draft'), (3, 'Public')], default=2, verbose_name='status')), + ('notified', models.BooleanField(default=False, verbose_name='notified')), + ('conference', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='publications', to='teleforma.conference', verbose_name='conference')), + ('period', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='teleforma.period', verbose_name='period')), + ], + ), + ] diff --git a/teleforma/models/core.py b/teleforma/models/core.py index a1d360ff..a20fa082 100755 --- a/teleforma/models/core.py +++ b/teleforma/models/core.py @@ -371,6 +371,15 @@ class Room(models.Model): db_table = app_label + '_' + 'room' verbose_name = _('room') +class ConferencePublication(models.Model): + conference = models.ForeignKey('Conference', related_name='publications', verbose_name=_('conference'), + on_delete=models.CASCADE) + period = models.ForeignKey('Period', verbose_name=_('period'), + on_delete=models.CASCADE) + date_publish = models.DateTimeField(_('publishing date'), null=True, blank=True) + status = models.IntegerField( + _('status'), choices=STATUS_CHOICES, default=2) + notified = models.BooleanField(_('notified'), default=False) class Conference(models.Model): @@ -406,6 +415,13 @@ class Conference(models.Model): @property def description(self): return str(self) + + @property + def session_as_int(self): + try: + return int(self.session) + except ValueError: + return 0 @property def slug(self): @@ -427,13 +443,6 @@ class Conference(models.Model): str(self.date_begin)] return ' - '.join(list) - @property - def slug(self): - slug = '-'.join([self.course.department.slug, - self.course.slug, - self.course_type.name.lower()]) - return slug - def save(self, *args, **kwargs): if not self.public_id: self.public_id = get_random_hash() @@ -547,6 +556,37 @@ class Conference(models.Model): self.web_class_group = WebClassGroup.objet.get( name=data['web_class_group']) + def video(self): + """ + get media video + """ + try: + videos = self.media.filter(type='mp4') + if videos: + return videos[0] + except Media.DoesNotExist: + try: + return self.media.filter(type='webm')[0] + except Media.DoesNotExist: + pass + return None + + def publication_info(self, period): + """ + Get publication info according to period. + """ + publication = self.publications.filter(period=period).first() + if not publication and self.period == period: + publication = self + elif not publication: + return None + + return { + 'status': publication.status, + 'published': publication.status == 3, + 'publication_date': publication.date_publish, + 'notified': publication.notified + } class Meta(MetaCore): db_table = app_label + '_' + 'conference' verbose_name = _('conference') diff --git a/teleforma/templates/teleforma/inc/conference_list.html b/teleforma/templates/teleforma/inc/conference_list.html index 96cc8166..f3c57682 100644 --- a/teleforma/templates/teleforma/inc/conference_list.html +++ b/teleforma/templates/teleforma/inc/conference_list.html @@ -1,22 +1,23 @@ {% load teleforma_tags %} {% load i18n %} -{% with course.conference.all|from_course_type:type|streaming_only as conferences %} -{% if conferences|from_periods:period %} +{% course_ingoing_conferences as conferences %} + +{% if conferences %}

{% trans "Live conferences"%}

- {% for conference in conferences|from_periods:period %} + {% for conference in conferences %} {% for stream in conference.livestream.all %} {% if stream.stream_type == 'webm' %} {% if stream.streaming %} {% endif %} {% endfor %} - {% endfor %} + {% endfor %}
- {% trans 'Click here' %} + {% trans 'Click here' %} @@ -25,7 +26,7 @@
{% trans "Title" %}
{{ stream.conference.course.title }}
{% trans "Session" %}
{{ stream.conference.session }}
{% if stream.conference.professor.user.username %} -
{% trans "Professor" %}
{{ stream.conference.professor }}
+
{% trans "Professor" %}
{{ stream.conference.professor }}
{% endif %}
{% trans "Begin" %}
{{ stream.conference.date_begin }}
@@ -45,9 +46,8 @@
{% endif %} -{% endwith %} diff --git a/teleforma/templates/teleforma/inc/media_list.html b/teleforma/templates/teleforma/inc/media_list.html index b83a7556..720994b7 100644 --- a/teleforma/templates/teleforma/inc/media_list.html +++ b/teleforma/templates/teleforma/inc/media_list.html @@ -2,54 +2,67 @@ {% load thumbnail %} {% load i18n %} -{% if course.media.all|from_course_type:type %} +{% course_past_conferences as all_conferences %} + +{% if all_conferences %}
-

{% trans title %}

+

{% trans "Conférences en différé" %}

- {% for media in course.media.all|from_course_type:type|from_periods:period %} - {% if media.is_published or user.is_staff and not list_view %} - {% if media.type == 'webm' or media.type == 'mp4' %} + {% for conference in all_conferences %} + {% if conference.video %} + {% conference_publication conference as publication %} + {% endif %} - {% endif %} {% endfor %}
- - {% if media.poster_file %} - {% thumbnail media.poster_file "168x96" as im %} + + {% if conference.video.poster_file %} + {% thumbnail conference.video.poster_file "168x96" as im %}
- {% trans 'Click here' %} + {% trans 'Click here' %}
{% endthumbnail %} {% else %} - {% trans 'Click here' %} +
{% trans 'Click here' %}
{% endif %} + {% comment %}
{% trans 'Click here' %}
{% endcomment %}
-
{% trans "Title" %}
{{ media.conference.course.title }}
-
{% trans "Session" %}
{{ media.conference.session }}
- {% if media.conference.professor %} -
{% trans "Professor" %}
{{ media.conference.professor }}
+
{% trans "Title" %}
{{ conference.course.title }}
+
{% trans "Session" %}
{{ conference.session }}
+ {% if conference.professor %} +
{% trans "Professor" %}
{{ conference.professor }}
+ {% endif %} + {% if conference.streaming %} +
{% trans "Begin date" %}
{{ conference.date_begin }}
+ {% else %} +
{% trans "Publishing date" %}
{{ publication.publication_date }}
{% endif %} -
{% trans "Begin" %}
{{ media.conference.date_begin }}
- {% if media.conference.comment %} -
{% trans "Comment" %}
{{ media.conference.comment }}
+ {% if conference.duration %} +
{% trans "Duration" %}
{{ conference.duration }}
+ {% endif %} + {% if conference.comment %} +
{% trans "Comment" %}
{{ conference.comment }}
{% endif %}
- {% if media.is_published and user.is_staff %} - - {% elif not media.is_published and user.is_staff %} - + {% if user.is_staff or user.professor.count %} + {% if publication.published %} + + {% else %} + + {% endif %} {% endif %} - {% if media.file and media.is_published or user.is_superuser or user.is_staff %} - {% if not "video" in media.mime_type or request.user_agent.os.family == 'iOS' %} - + {% if conference.video.file %} + {% if not "video" in conference.video.mime_type or request.user_agent.os.family == 'iOS' %} + {% endif %} @@ -57,7 +70,6 @@
diff --git a/teleforma/templatetags/teleforma_tags.py b/teleforma/templatetags/teleforma_tags.py index 3395c079..0fd6b753 100644 --- a/teleforma/templatetags/teleforma_tags.py +++ b/teleforma/templatetags/teleforma_tags.py @@ -49,6 +49,7 @@ from django.utils.translation import ugettext_lazy as _ from docutils.core import publish_parts from teleforma.models.chat import ChatMessage +from teleforma.views.core import get_course_conferences from ..exam.models import Quota, Script from ..models.core import Document, Professor @@ -439,3 +440,28 @@ def chat_room(context, period=None, course=None): 'user_id': context.request.user.id } } + +@register.simple_tag(takes_context=True) +def course_ingoing_conferences(context): + course = context['course'] + confs = course.conference.filter(streaming=True, + period=context['period'], + course_type=context['type']) + return list(confs) + +@register.simple_tag(takes_context=True) +def course_past_conferences(context): + user = context['user'] + if user.is_staff or user.professor.count(): + status_min = 2 + else: + status_min = 3 + course = context['course'] + period = context['period'] + course_type = context['type'] + return get_course_conferences(period, course, course_type, status_min=status_min) + +@register.simple_tag(takes_context=True) +def conference_publication(context, conference): + period = context['period'] + return conference.publication_info(period) diff --git a/teleforma/views/core.py b/teleforma/views/core.py index 15b4f9ad..1559d26a 100644 --- a/teleforma/views/core.py +++ b/teleforma/views/core.py @@ -119,6 +119,56 @@ def get_courses(user, date_order=False, num_order=False, num_courses=False, peri return get_ae_courses(user, date_order, num_order, period) +def get_trainings(user): + trainings = [] + + if not user.is_authenticated: + return trainings + + professor = user.professor.all() + student = user.student.all() + quotas = user.quotas.all() + + if professor or quotas or user.is_staff: + trainings = Training.objects.filter(available=True) + + elif student: + student = user.student.get() + trainings = student.trainings.all() + return trainings + + + +def get_course_conferences(period, course, course_type, status_min=3): + conferences = [] + already_added = set() + + periods = [period,] + + # get conference publications + publications = ConferencePublication.objects.filter( + period__in=periods, + conference__course=course, + conference__course_type=course_type, + status__gte=status_min).distinct() + for publication in publications: + conferences.append(publication.conference) + already_added.add(publication.conference.id) + + cc = Conference.objects.filter(period__in=periods, + course=course, + course_type=course_type, + status__gte=status_min).distinct() + + for conference in cc: + # do not include conferences with publication rules + if conference.id not in already_added: + conferences.append(conference) + conferences = sorted(conferences, key=lambda c:-c.session_as_int) + return conferences + + + def stream_from_file(__file): chunk_size = 0x10000 f = open(__file, 'r') -- 2.39.5