From c38ed233c705726574c5ddc54f3e71c58258e8a9 Mon Sep 17 00:00:00 2001 From: Yoan Le Clanche Date: Mon, 9 Jan 2023 14:13:09 +0100 Subject: [PATCH] Conferences can now have more than one publications state and date --- .../teleforma-publish-notify-conferences.py | 52 ++++++++++++------- ..._1427.py => 0021_conferencepublication.py} | 6 +-- teleforma/models/core.py | 27 +++++++++- teleforma/templates/teleforma/courses.html | 2 + .../templates/teleforma/inc/media_list.html | 8 +-- teleforma/templatetags/teleforma_tags.py | 8 ++- teleforma/views/core.py | 32 +++++------- 7 files changed, 89 insertions(+), 46 deletions(-) rename teleforma/migrations/{0022_auto_20221222_1427.py => 0021_conferencepublication.py} (79%) diff --git a/teleforma/management/commands/teleforma-publish-notify-conferences.py b/teleforma/management/commands/teleforma-publish-notify-conferences.py index 71f40166..97231f7c 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 @@ -25,10 +25,14 @@ class Logger: def __init__(self, file): self.logger = logging.getLogger('myapp') - self.hdlr = logging.FileHandler(file) + if file: + self.hdlr = logging.FileHandler(file) + else: + self.hdlr = None self.formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') - self.hdlr.setFormatter(self.formatter) - self.logger.addHandler(self.hdlr) + if self.hdlr: + self.hdlr.setFormatter(self.formatter) + self.logger.addHandler(self.hdlr) self.logger.setLevel(logging.INFO) @@ -38,7 +42,7 @@ class Command(BaseCommand): than current date """ def add_arguments(self, parser): - parser.add_argument('--logfile', type=str, required=True, + parser.add_argument('--logfile', type=str, required=False, help='log file to use') parser.add_argument('--period', type=str, required=True, @@ -54,6 +58,7 @@ class Command(BaseCommand): logpath = options['logfile'] logger = Logger(logpath) + period_name = options['period'] period = Period.objects.get(name=period_name) @@ -71,29 +76,36 @@ class Command(BaseCommand): now_minus = datetime.datetime.now() - datetime.timedelta(minutes=minute_low_range) now_plus = datetime.datetime.now() + datetime.timedelta(minutes=minute_high_range) - conferences = Conference.objects.filter( + 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, + date_publish__lte=now_plus, + date_publish__gte=now_minus, + )) + print(publications) + logger.logger.info("Starting conference publication process") - for conference in conferences: - conference.status = 3 - conference.save() + for publication in publications: + publication.status = 3 + + if type(publication) == ConferencePublication: + conference = publication.conference + else: + conference = publication medias = conference.media.all() for media in medias: media.is_published = True media.save() - if "video/mp4" in media.mime_type: - linked_media = media logger.logger.info("Conference published: " + conference.public_id) - for conference in conferences: - medias = conference.media.all() for media in medias: if "video/mp4" in media.mime_type: linked_media = media @@ -101,19 +113,21 @@ class Command(BaseCommand): url = reverse('teleforma-media-detail', args=[conference.period.id, linked_media.id]) message = "Nouvelle conférence publiée : " + str(conference) - students = Student.objects.filter(period=conference.period, platform_only=True) + students = Student.objects.filter(period=publication.period, platform_only=True) for student in students: try: if student.user: - courses = get_courses(student.user, period=conference.period) + courses = get_courses(student.user, period=publication.period) for course in courses: if conference.course == course['course'] and \ conference.course_type in course['types']: notify(student.user, message, url) logger.logger.info("Student notified: " + student.user.username) + print("notify", student) except: #logger.logger.info("Student NOT notified: " + str(student.id)) + print("can't notify", student) continue - conference.notified = True - conference.save() + publication.notified = True + publication.save() diff --git a/teleforma/migrations/0022_auto_20221222_1427.py b/teleforma/migrations/0021_conferencepublication.py similarity index 79% rename from teleforma/migrations/0022_auto_20221222_1427.py rename to teleforma/migrations/0021_conferencepublication.py index 04994580..50f21843 100644 --- a/teleforma/migrations/0022_auto_20221222_1427.py +++ b/teleforma/migrations/0021_conferencepublication.py @@ -1,4 +1,4 @@ -# Generated by Django 3.2.13 on 2022-12-22 14:27 +# Generated by Django 3.2.13 on 2023-01-03 14:54 from django.db import migrations, models import django.db.models.deletion @@ -7,7 +7,7 @@ import django.db.models.deletion class Migration(migrations.Migration): dependencies = [ - ('teleforma', '0021_auto_20221208_1214'), + ('teleforma', '0020_chatmessage_reply_to'), ] operations = [ @@ -19,7 +19,7 @@ class Migration(migrations.Migration): ('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')), - ('trainings', models.ManyToManyField(related_name='conference_publications', to='teleforma.Training', verbose_name='trainings')), + ('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 3688e9da..4e3769ab 100755 --- a/teleforma/models/core.py +++ b/teleforma/models/core.py @@ -371,7 +371,8 @@ class Room(models.Model): class ConferencePublication(models.Model): conference = models.ForeignKey('Conference', related_name='publications', verbose_name=_('conference'), on_delete=models.CASCADE) - trainings = models.ManyToManyField('Training', related_name='conference_publications', verbose_name=_('trainings')) + 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) @@ -411,6 +412,13 @@ class Conference(models.Model): def description(self): return str(self) + @property + def session_as_int(self): + try: + return int(self.session) + except ValueError: + return 0 + @property def duration(self): if self.date_end: @@ -558,6 +566,23 @@ class Conference(models.Model): 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): diff --git a/teleforma/templates/teleforma/courses.html b/teleforma/templates/teleforma/courses.html index e78a2e36..8397810e 100644 --- a/teleforma/templates/teleforma/courses.html +++ b/teleforma/templates/teleforma/courses.html @@ -81,6 +81,7 @@

+ {% if home_video %}

{% if home_video.title %}{{ home_video.title }}{% else %}{{ home_video.course.title }}{% endif %}

@@ -100,6 +101,7 @@
+ {% endif %} {% if webclass_slots or webclass_to_subscribe and not restricted %}
diff --git a/teleforma/templates/teleforma/inc/media_list.html b/teleforma/templates/teleforma/inc/media_list.html index 3664b922..c287c2af 100644 --- a/teleforma/templates/teleforma/inc/media_list.html +++ b/teleforma/templates/teleforma/inc/media_list.html @@ -50,13 +50,15 @@
+ + {% conference_publication conference as publication %} - {% if media.is_published and user.is_staff %} + {% if publication.published and user.is_staff %} - {% elif not media.is_published and user.is_staff %} + {% elif not publication.published and user.is_staff %} {% endif %} - {% if conference.video.file and conference.video.is_published or user.is_superuser or user.is_staff %} + {% if conference.video.file %} {% if not "video" in conference.video.mime_type or request.user_agent.os.family == 'iOS' %} diff --git a/teleforma/templatetags/teleforma_tags.py b/teleforma/templatetags/teleforma_tags.py index bdbcf415..77040a29 100644 --- a/teleforma/templatetags/teleforma_tags.py +++ b/teleforma/templatetags/teleforma_tags.py @@ -479,7 +479,13 @@ def course_past_conferences(context): period = context['period'] course_type = context['type'] - return get_course_conferences(user, period, course, course_type) + return get_course_conferences(period, course, course_type) + +@register.simple_tag(takes_context=True) +def conference_publication(context, conference): + period = context['period'] + return conference.publication_info(period) + # @register.simple_tag(takes_context=True) # def course_media(context): diff --git a/teleforma/views/core.py b/teleforma/views/core.py index 38e07faf..4b40403e 100644 --- a/teleforma/views/core.py +++ b/teleforma/views/core.py @@ -57,9 +57,6 @@ from django.views.generic.base import TemplateResponseMixin, TemplateView, View from django.views.generic.detail import DetailView from django.views.generic.list import ListView from django.core.cache import cache -from django.db.models import Q -from django.contrib.sites import Site - from jsonrpc import jsonrpc_method from jsonrpc.proxy import ServiceProxy @@ -142,20 +139,21 @@ def get_trainings(user): return trainings -def get_course_conferences(user, period, course, course_type): - trainings = get_trainings(user) +def get_course_conferences(period, course, course_type): conferences = [] + already_added = set() # get conference publications publications = ConferencePublication.objects.filter( - trainings__in=trainings, status=3, conference__course=course, conference__course_type=course_type).distinct() + period=period, conference__course=course, conference__course_type=course_type).distinct() for publication in publications: conferences.append(publication.conference) + already_added.add(publication.conference.id) - for conference in Conference.objects.filter(period=period, status=3, course=course, course_type=course_type): + for conference in Conference.objects.filter(period=period, course=course, course_type=course_type): # do not include conferences with publication rules - if conference.publications.filter(trainings__in=trainings).count(): - continue - conferences.append(conference) + if conference.id not in already_added: + conferences.append(conference) + conferences = sorted(conferences, key=lambda c:-c.session_as_int) return conferences @@ -455,10 +453,9 @@ class CourseListView(CourseAccessMixin, ListView): student = user.student.all()[0] slots = [] to_subscribe = [] - student_trainings = get_trainings(user) student_courses = [course['course'] for course in get_courses(user)] - for webclass in Webclass.published.filter(trainings__in=student_trainings, iej=student.iej, course__in=student_courses): + for webclass in Webclass.published.filter(period=self.period, iej=student.iej, course__in=student_courses): # if webclass.course not in student_courses: # continue if student.platform_only and not webclass.allow_elearning: @@ -557,11 +554,10 @@ class CourseView(CourseAccessMixin, DetailView): if student: student = student[0] - trainings = get_trainings(self.request.user) if student: try: webclass = Webclass.published.filter( - trainings__in=trainings, course=course, iej=student.iej)[0] + period=self.period, course=course, iej=student.iej)[0] except IndexError: pass if webclass: @@ -577,13 +573,13 @@ class CourseView(CourseAccessMixin, DetailView): records = {} try: - records = WebclassRecord.get_records(trainings, course) + records = WebclassRecord.get_records(context['period'], course) except Exception as e: print(e) context['webclass_error'] = True context['webclass_records'] = records.get(WebclassRecord.WEBCLASS) - context['webclass_corrections_records'] = records.get( - WebclassRecord.CORRECTION) + context['webclass_corrections_records'] = records.get(WebclassRecord.CORRECTION) + return context @method_decorator(access_required) @@ -1254,5 +1250,3 @@ class PDFTemplateResponseMixin(TemplateResponseMixin): context[self.pdf_url_varname] = self.get_pdf_url() return super(PDFTemplateResponseMixin, self).render_to_response( context, **response_kwargs) - - -- 2.39.5