diff --git a/apps/school/templates/summer/schedule_purchased.html b/apps/school/templates/summer/schedule_purchased.html index 63e963fd..4de81091 100644 --- a/apps/school/templates/summer/schedule_purchased.html +++ b/apps/school/templates/summer/schedule_purchased.html @@ -38,15 +38,17 @@ {% if is_previous and not live_lessons_exists %} Записей уроков пока нет {% else %} - {% for school_schedule in school_schedules %} - {% if is_previous %} - {% if school_schedule.previous_live_lesson in live_lessons %} - {% include './_schedule_purchased_item.html' with school_schedule=school_schedule live_lesson=school_schedule.previous_live_lesson %} - {% endif %} - {% else %} - {% include './_schedule_purchased_item.html' with school_schedule=school_schedule live_lesson=school_schedule.current_live_lesson %} - {% endif %} - {% endfor %} + {% if is_previous %} + {% for live_lesson in live_lessons %} + {% if live_lesson.school_schedule %} + {% include './_schedule_purchased_item.html' with school_schedule=live_lesson.school_schedule live_lesson=live_lesson %} + {% endif %} + {% endfor %} + {% else %} + {% for school_schedule in school_schedules %} + {% include './_schedule_purchased_item.html' with school_schedule=school_schedule live_lesson=school_schedule.current_live_lesson %} + {% endfor %} + {% endif %} {% endif %}
diff --git a/apps/school/views.py b/apps/school/views.py index 7f4ab734..3c1b478a 100644 --- a/apps/school/views.py +++ b/apps/school/views.py @@ -4,7 +4,7 @@ from paymentwall import Pingback from django.contrib.auth import get_user_model from django.contrib.auth.decorators import login_required, user_passes_test from django.db.utils import IntegrityError -from django.db.models import Min, F, Func, Q +from django.db.models import Min, F, Func, Q, Value from django.http import Http404 from django.shortcuts import get_object_or_404 from django.utils.decorators import method_decorator @@ -174,6 +174,8 @@ class SummerSchoolView(TemplateView): context = super().get_context_data() is_previous = 'is_previous' in self.request.GET date_now = now().date() + yesterday = date_now - timedelta(days=1) + month_start = date_now.replace(day=1) now_time = now() try: school_schedule = SchoolSchedule.objects.get(weekday=now_time.isoweekday()) @@ -186,7 +188,24 @@ class SummerSchoolView(TemplateView): (end_at + timedelta(hours=1)).time() >= now_time.time() and school_schedule.current_live_lesson() ) + + school_schedules = SchoolSchedule.objects.all() + school_schedules_dict = {ss.weekday: ss for ss in school_schedules} + school_schedules_dict[0] = school_schedules_dict.get(7) if self.request.user.is_authenticated: + all_schedules_purchased = SchoolPayment.objects.filter( + user=self.request.user, + status__in=[ + Pingback.PINGBACK_TYPE_REGULAR, + Pingback.PINGBACK_TYPE_GOODWILL, + Pingback.PINGBACK_TYPE_RISK_REVIEWED_ACCEPTED, + ], + date_start__range=[month_start, date_now], + ).annotate( + joined_weekdays=Func(F('weekdays'), function='unnest',) + ).distinct().values_list('joined_weekdays', flat=True) + all_schedules_purchased = map(lambda x: 1 if x == 7 else x+1, all_schedules_purchased) + school_payment = SchoolPayment.objects.filter( user=self.request.user, status__in=[ @@ -221,12 +240,14 @@ class SummerSchoolView(TemplateView): school_payment_exists_future = False school_purchased_future = False school_schedules_purchased = [] - if school_payment_exists and is_previous: + if all_schedules_purchased and is_previous: live_lessons = LiveLesson.objects.filter( - date__gte=school_payment.last().date_start, - date__range=[(now_time - timedelta(days=8)).date(), (now_time - timedelta(days=1)).date()], + date__range=[month_start, yesterday], deactivated_at__isnull=True, - ) + date__week_day__in=all_schedules_purchased, + ).order_by('-date') + for ll in live_lessons: + ll.school_schedule = school_schedules_dict.get(ll.date.isoweekday()) live_lessons_exists = live_lessons.exists() else: live_lessons = None @@ -240,7 +261,7 @@ class SummerSchoolView(TemplateView): 'is_purchased': school_payment_exists, 'is_purchased_future': school_payment_exists_future, 'min_school_price': SchoolSchedule.objects.aggregate(Min('month_price'))['month_price__min'], - 'school_schedules': SchoolSchedule.objects.all(), + 'school_schedules': school_schedules, 'school_schedules_purchased': school_schedules_purchased, 'school_purchased_future': school_purchased_future, 'subscription_ends': school_payment.filter(add_days=False).first().date_end if school_payment_exists else None,