LIL-561. Refactoring code

remotes/origin/hasaccess
Ivlev Denis 8 years ago
parent bfbc5eda6e
commit c28f7fcaeb
  1. 17
      apps/school/models.py
  2. 44
      apps/user/models.py
  3. 31
      project/views.py

@ -48,7 +48,12 @@ class SchoolSchedule(models.Model):
def is_online(self):
end_at = datetime.combine(now().today(), self.start_at) + timedelta(hours=2)
return self.start_at <= now().time() and end_at.time() >= now().time() and self.weekday == now().isoweekday()
return (
self.start_at <= now().time() and
end_at.time() >= now().time() and
self.weekday == now().isoweekday() and
self.current_live_lesson()
)
def current_live_lesson(self):
now_time = now()
@ -68,6 +73,16 @@ class SchoolSchedule(models.Model):
).first()
return live_lesson
def online_coming_soon(self):
now_time = now()
return (
school_schedule.start_at > now_time.time() and
(
datetime.combine(datetime.today(), self.start_at) - timedelta(hours=12)
).time() <= now_time.time() and
self.current_live_lesson()
)
@property
def start_at_humanize(self):
return arrow.get(datetime.combine(datetime.today(), self.start_at), settings.TIME_ZONE).humanize(locale='ru') if self.start_at else None

@ -1,5 +1,6 @@
from json import dumps
from rest_framework.authtoken.models import Token
from paymentwall import Pingback
from phonenumber_field.modelfields import PhoneNumberField
from django.db import models
@ -11,8 +12,8 @@ from django.utils.timezone import now
from django.utils.translation import gettext_lazy as _
from api.v1 import serializers
from apps.notification.utils import send_email
from apps.notification.utils import send_email
from apps.user.tasks import user_to_mixpanel
@ -103,6 +104,47 @@ class User(AbstractUser):
commission = aggregate.get('commission__sum') or 0
return amount - commission
def school_payments(self):
from apps.payment.models import SchoolPayment
schoolpayment_queryset = SchoolPayment.objects.filter(
user=self,
date_start__lte=now().date(),
date_end__gte=now().date(),
status__in=[
Pingback.PINGBACK_TYPE_REGULAR,
Pingback.PINGBACK_TYPE_GOODWILL,
Pingback.PINGBACK_TYPE_RISK_REVIEWED_ACCEPTED,
]
)
return schoolpayment_queryset
def has_assess(self, resource):
from apps.course.models import Course, Lesson
from apps.school.models import SchoolSchedule, LiveLesson
from apps.payment.models import CoursePayment
if isinstance(resource, Course):
return True
elif isinstance(resource, Lesson):
if not self.is_active:
return False
if resource.course.author and (resource.course.author == self or resource.course.author.role == User.ADMIN_ROLE):
return True
try:
CoursePayment.objects.get(course=resource.course, user=self)
except CoursePayment.DoesNotExist:
return False
else:
return True
elif isinstance(resource, SchoolSchedule):
isoweekday = resource.weekday
schoolpayment_queryset = self.school_payments().filter(weekdays__contains=[isoweekday])
return schoolpayment_queryset.exists() or self.role == User.ADMIN_ROLE
elif isinstance(resource, LiveLesson):
isoweekday = resource.date.isoweekday()
schoolpayment_queryset = self.school_payments().filter(weekdays__contains=[isoweekday])
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):

@ -1,3 +1,5 @@
from time import time
import decimal
from datetime import datetime, timedelta
from django.db.models import Min
@ -31,28 +33,14 @@ class IndexView(TemplateView):
school_schedule = None
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_coming_soon = (
school_schedule.start_at > now_time.time() and
(
datetime.combine(datetime.today(), school_schedule.start_at) - timedelta(hours=12)
).time() <= now_time.time() and
school_schedule.current_live_lesson()
)
online = school_schedule.is_online()
online_coming_soon = school_schedule.online_coming_soon()
date_now = now_time.date()
if self.request.user.is_authenticated:
school_payment = SchoolPayment.objects.filter(
user=self.request.user,
date_start__lte=date_now,
date_end__gte=date_now
)
school_payment_exists = school_payment.exists()
school_schedules_purchased = school_payment.values_list('weekdays', flat=True)
schoolpayment_queryset = self.request.user.school_payments()
school_payment_exists = schoolpayment_queryset.exists()
school_schedules_purchased = schoolpayment_queryset.values_list('weekdays', flat=True)
school_schedules_purchased = school_schedules_purchased[0] if school_schedules_purchased else []
school_payment_future = SchoolPayment.objects.filter(
@ -69,7 +57,6 @@ class IndexView(TemplateView):
school_payment_exists_future = False
school_purchased_future = False
school_schedules_purchased = []
context.update({
'online': online,
'online_coming_soon': online_coming_soon,
@ -80,8 +67,8 @@ class IndexView(TemplateView):
'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': school_payment.filter(add_days=False).first().date_end if school_payment_exists else None,
'subscription_ends_humanize': school_payment.filter(add_days=False).first().date_end_humanize if school_payment_exists else None,
'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,

Loading…
Cancel
Save