From c3c0e0a5b5b97f44c88ec7d61354044c7826c8f2 Mon Sep 17 00:00:00 2001 From: gzbender Date: Tue, 21 Aug 2018 00:28:31 +0500 Subject: [PATCH] LIL-622 --- apps/school/models.py | 10 ++- .../templates/school/livelessons_list.html | 2 +- apps/school/templates/summer/promo.html | 2 +- .../templates/summer/schedule_purchased.html | 4 +- apps/school/urls.py | 3 +- apps/school/views.py | 76 +++---------------- apps/user/templates/user/profile.html | 2 +- project/templates/blocks/footer.html | 1 - project/templates/blocks/header.html | 11 +-- project/templates/blocks/promo.html | 2 - 10 files changed, 23 insertions(+), 90 deletions(-) diff --git a/apps/school/models.py b/apps/school/models.py index 91d007e8..fe66564b 100644 --- a/apps/school/models.py +++ b/apps/school/models.py @@ -1,9 +1,10 @@ import arrow -from datetime import datetime, timedelta +from datetime import datetime, timedelta, date from django.conf import settings from django.db import models from django.urls import reverse_lazy +from django.utils.functional import cached_property from django.utils.timezone import now from project.mixins import BaseModel, DeactivatedMixin @@ -50,15 +51,18 @@ class SchoolSchedule(models.Model): end_at = datetime.combine(now().today(), self.start_at) + timedelta(hours=1) return self.start_at <= now().time() and end_at.time() >= now().time() and self.weekday == now().isoweekday() + @cached_property def current_live_lesson(self): - now_time = now() + september2018 = date(2018, 9, 1) + date_start = max(september2018, now().date()) live_lesson = LiveLesson.objects.filter( date__week_day=self.weekday % 7 + 1, - date__range=[now_time.date(), (now_time + timedelta(days=6)).date()], + date__range=[date_start, date_start + timedelta(days=6)], deactivated_at__isnull=True, ).first() return live_lesson + @cached_property def previous_live_lesson(self): now_time = now() live_lesson = LiveLesson.objects.filter( diff --git a/apps/school/templates/school/livelessons_list.html b/apps/school/templates/school/livelessons_list.html index b2099cbe..f19f81f0 100644 --- a/apps/school/templates/school/livelessons_list.html +++ b/apps/school/templates/school/livelessons_list.html @@ -9,7 +9,7 @@
{% for livelesson in livelesson_list %} -
+
- Присоединяйтесь в Рисовальный лагерь + Подписывайтесь на онлайн-школу
diff --git a/apps/school/urls.py b/apps/school/urls.py index 47361f96..59973c6d 100644 --- a/apps/school/urls.py +++ b/apps/school/urls.py @@ -3,12 +3,11 @@ from django.urls import path, include from .views import ( LiveLessonsView, LiveLessonEditView, LiveLessonsDetailView, SchoolView, - SchoolSchedulesPrintView, SummerSchoolView, + SchoolSchedulesPrintView, ) urlpatterns = [ path('', SchoolView.as_view(), name='school'), - path('summer', SummerSchoolView.as_view(), name='summer-school'), path('schedules/print', SchoolSchedulesPrintView.as_view(), name='school_schedules-print'), path('lessons/', LiveLessonsView.as_view(), name='lessons'), path('lessons/create', LiveLessonEditView.as_view(), name='lessons-create'), diff --git a/apps/school/views.py b/apps/school/views.py index f9a0fa7a..b08f5c57 100644 --- a/apps/school/views.py +++ b/apps/school/views.py @@ -1,4 +1,4 @@ -from datetime import datetime, timedelta +from datetime import datetime, timedelta, date from paymentwall import Pingback from django.contrib.auth import get_user_model @@ -34,19 +34,22 @@ class LiveLessonsView(ListView): template_name = 'school/livelessons_list.html' def get_queryset(self): + september2018 = date(2018, 9, 1) + date_start = (now() - timedelta(days=7)).date() + if date_start < september2018: + date_start = september2018 date_range = Q( date__range=[ - (now() - timedelta(days=7)).date(), - (now() + timedelta(days=10)).date(), + date_start, + date_start + timedelta(days=17), ] ) queryset = LiveLesson.objects.filter(date_range) if queryset.count() < 17: - start_date = now() - timedelta(days=7) for i in range(18): try: LiveLesson.objects.create( - date=(start_date + timedelta(days=i)).date(), + date=date_start + timedelta(days=i), ) except IntegrityError: pass @@ -108,67 +111,6 @@ class LiveLessonEditView(TemplateView): class SchoolView(TemplateView): - template_name = 'school/school.html' - - def get_context_data(self): - context = super().get_context_data() - is_previous = 'is_previous' in self.request.GET - date_now = now().date() - now_time = now() - try: - school_schedule = SchoolSchedule.objects.get(weekday=now_time.isoweekday()) - except SchoolSchedule.DoesNotExist: - online = False - else: - end_at = datetime.combine(now_time.today(), school_schedule.start_at) - online = ( - school_schedule.start_at <= now_time.time() and - (end_at + timedelta(hours=1)).time() >= now_time.time() and - school_schedule.current_live_lesson() - ) - if self.request.user.is_authenticated: - school_payment = 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__lte=date_now, - date_end__gte=date_now - ) - school_payment_exists = school_payment.exists() - school_schedules_purchased = school_payment.annotate( - joined_weekdays=Func(F('weekdays'), function='unnest',) - ).values_list('joined_weekdays', flat=True).distinct() - else: - school_payment_exists = False - school_schedules_purchased = [] - if school_payment_exists 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()], - deactivated_at__isnull=True, - ) - live_lessons_exists = live_lessons.exists() - else: - live_lessons = None - live_lessons_exists = False - context.update({ - 'online': online, - 'live_lessons': live_lessons, - 'live_lessons_exists': live_lessons_exists, - 'is_previous': is_previous, - 'course_items': Course.objects.filter(status=Course.PUBLISHED)[:6], - 'is_purchased': school_payment_exists, - 'min_school_price': SchoolSchedule.objects.aggregate(Min('month_price'))['month_price__min'], - 'school_schedules': SchoolSchedule.objects.all(), - 'school_schedules_purchased': school_schedules_purchased, - 'subscription_ends': school_payment.filter(add_days=False).first().date_end if school_payment_exists else None, - }) - return context - -class SummerSchoolView(TemplateView): template_name = 'school/summer_school.html' def get_context_data(self): @@ -191,7 +133,7 @@ class SummerSchoolView(TemplateView): school_schedule.current_live_lesson() ) - school_schedules = SchoolSchedule.objects.all() + school_schedules = sorted(SchoolSchedule.objects.all(), key=lambda ss: ss.current_live_lesson.date) school_schedules_dict = {ss.weekday: ss for ss in school_schedules} school_schedules_dict[0] = school_schedules_dict.get(7) all_schedules_purchased = [] diff --git a/apps/user/templates/user/profile.html b/apps/user/templates/user/profile.html index f39fc6e3..e8a81d33 100644 --- a/apps/user/templates/user/profile.html +++ b/apps/user/templates/user/profile.html @@ -88,7 +88,7 @@ {% else %}
-
Вы не подписаны на лагерь!
+
Вы не подписаны на онлайн-школу!