from datetime import datetime, timedelta from itertools import chain from django.db.models import Min from django.contrib.auth import get_user_model from django.views.generic import TemplateView from django.utils.timezone import now from apps.course.models import Course from apps.school.models import SchoolSchedule from apps.payment.models import SchoolPayment User = get_user_model() class AboutView(TemplateView): template_name = 'templates/lilcity/about.html' class IndexView(TemplateView): template_name = 'templates/lilcity/main.html' def get_context_data(self): context = super().get_context_data() now_time = now() try: school_schedule = SchoolSchedule.objects.get(weekday=now_time.isoweekday()) except SchoolSchedule.DoesNotExist: online = False online_coming_soon = False school_schedule = None else: online = school_schedule.is_online() online_coming_soon = school_schedule.online_coming_soon() date_now = now_time.date() 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_payment_future = self.request.user.school_payments() school_payment_exists_future = school_payment_future.exists() school_purchased_future = school_payment_future.last() else: school_payment_exists = False school_payment_exists_future = False school_purchased_future = False school_schedules_purchased = [] context.update({ 'online': online, 'online_coming_soon': online_coming_soon, 'school_schedule': school_schedule, '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, 'teachers': User.objects.filter(role=User.TEACHER_ROLE, show_in_mainpage=True), 'subscription_ends': schoolpayment_queryset.filter(add_days=False).first().date_end if school_payment_exists else None, 'subscription_ends_humanize': schoolpayment_queryset.filter(add_days=False).first().date_end_humanize if school_payment_exists else None, 'school_purchased_future': school_purchased_future, 'is_purchased_future': school_payment_exists_future, }) return context class SchoolSchedulesView(TemplateView): template_name = 'templates/lilcity/school_schedules.html' def get_context_data(self): context = super().get_context_data() context['school_schedules'] = SchoolSchedule.objects.all() return context