From 16749e68ddcd3626f016bdb0946f694b5f587937 Mon Sep 17 00:00:00 2001 From: Ivlev Denis Date: Thu, 5 Jul 2018 15:49:16 +0300 Subject: [PATCH] Refactoring code --- apps/school/views.py | 86 ++++++++------------------------------------ apps/user/models.py | 1 - project/views.py | 8 ++--- 3 files changed, 18 insertions(+), 77 deletions(-) diff --git a/apps/school/views.py b/apps/school/views.py index ae9b64f0..d2e0bdbb 100644 --- a/apps/school/views.py +++ b/apps/school/views.py @@ -61,27 +61,8 @@ class LiveLessonsDetailView(DetailView): def get(self, request, pk=None): response = super().get(request, pk=pk) - try: - school_payment = SchoolPayment.objects.get( - user=request.user, - add_days=False, - date_start__lte=now(), - date_end__gte=now(), - status__in=[ - Pingback.PINGBACK_TYPE_REGULAR, - Pingback.PINGBACK_TYPE_GOODWILL, - Pingback.PINGBACK_TYPE_RISK_REVIEWED_ACCEPTED, - ], - ) - except SchoolPayment.DoesNotExist: - school_payment = None - if request.user.role not in [User.ADMIN_ROLE, User.TEACHER_ROLE] and not ( - request.user.role == User.USER_ROLE and - school_payment and - school_payment.is_deliverable() - ): + if not request.user.has_assess(self.object): raise Http404 - return response @@ -120,24 +101,11 @@ class SchoolView(TemplateView): 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=2)).time() >= now_time.time() and - school_schedule.current_live_lesson() - ) + online = school_schedule.is_online() 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( + schoolpayment_queryset = self.request.user.school_payments() + school_payment_exists = schoolpayment_queryset.exists() + school_schedules_purchased = schoolpayment_queryset.annotate( joined_weekdays=Func(F('weekdays'), function='unnest',) ).values_list('joined_weekdays', flat=True).distinct() else: @@ -145,7 +113,7 @@ class SchoolView(TemplateView): school_schedules_purchased = [] if school_payment_exists and is_previous: live_lessons = LiveLesson.objects.filter( - date__gte=school_payment.last().date_start, + date__gte=schoolpayment_queryset.last().date_start, date__range=[(now_time - timedelta(days=8)).date(), (now_time - timedelta(days=1)).date()], deactivated_at__isnull=True, ) @@ -163,10 +131,11 @@ class SchoolView(TemplateView): '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, + 'subscription_ends': schoolpayment_queryset.filter(add_days=False).first().date_end if school_payment_exists else None, }) return context + class SummerSchoolView(TemplateView): template_name = 'school/summer_school.html' @@ -181,39 +150,14 @@ class SummerSchoolView(TemplateView): 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=2)).time() >= now_time.time() and - school_schedule.current_live_lesson() - ) + online = school_schedule.is_online() 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_payment_future = 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__gte=date_now, - date_end__gte=date_now - ) - + schoolpayment_queryset = self.request.user.school_payments() + school_payment_exists = schoolpayment_queryset.exists() + school_payment_future = self.request.user.school_payments() school_payment_exists_future = school_payment_future.exists() school_purchased_future = school_payment_future.last() - - school_schedules_purchased = school_payment.annotate( + school_schedules_purchased = schoolpayment_queryset.annotate( joined_weekdays=Func(F('weekdays'), function='unnest',) ).values_list('joined_weekdays', flat=True).distinct() else: @@ -223,7 +167,7 @@ class SummerSchoolView(TemplateView): school_schedules_purchased = [] if school_payment_exists and is_previous: live_lessons = LiveLesson.objects.filter( - date__gte=school_payment.last().date_start, + date__gte=schoolpayment_queryset.last().date_start, date__range=[(now_time - timedelta(days=8)).date(), (now_time - timedelta(days=1)).date()], deactivated_at__isnull=True, ) @@ -243,7 +187,7 @@ class SummerSchoolView(TemplateView): 'school_schedules': SchoolSchedule.objects.all(), '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, + 'subscription_ends': schoolpayment_queryset.filter(add_days=False).first().date_end if school_payment_exists else None, }) return context diff --git a/apps/user/models.py b/apps/user/models.py index f5c8b3a2..0d1e5eed 100644 --- a/apps/user/models.py +++ b/apps/user/models.py @@ -145,7 +145,6 @@ class User(AbstractUser): return schoolpayment_queryset.exists() or self.role == User.ADMIN_ROLE - @receiver(post_save, sender=User) def create_auth_token(sender, instance=None, created=False, **kwargs): if ( diff --git a/project/views.py b/project/views.py index 2bf46b4d..cfe34194 100644 --- a/project/views.py +++ b/project/views.py @@ -38,11 +38,9 @@ class IndexView(TemplateView): if self.request.user.is_authenticated: schoolpayment_queryset = self.request.user.school_payments() school_payment_exists = schoolpayment_queryset.exists() - school_schedules_purchased = list( - chain.from_iterable( - schoolpayment_queryset.values_list('weekdays', flat=True) - ) - ) + school_schedules_purchased = schoolpayment_queryset.annotate( + joined_weekdays=Func(F('weekdays'), function='unnest',) + ).values_list('joined_weekdays', flat=True).distinct() school_payment_future = self.request.user.school_payments() school_payment_exists_future = school_payment_future.exists() school_purchased_future = school_payment_future.last()