diff --git a/apps/payment/views.py b/apps/payment/views.py index 846b5a4a..81812bbf 100644 --- a/apps/payment/views.py +++ b/apps/payment/views.py @@ -160,12 +160,13 @@ class SchoolBuyView(TemplateView): @method_decorator(csrf_exempt, name='dispatch') class PaymentwallCallbackView(View): - def add_months(self, sourcedate, months): - month = sourcedate.month - 1 + months - year = sourcedate.year + month // 12 - month = month % 12 + 1 - day = min(sourcedate.day, calendar.monthrange(year, month)[1]) - return datetime.date(year, month, day) + def add_months(self, sourcedate, months=1): + result = arrow.get(sourcedate, settings.TIME_ZONE).shift(months=months) + if months == 1: + if (sourcedate.month == 2 and sourcedate.day >= 28) or (sourcedate.day == 31 and result.day <= 30)\ + or (sourcedate.month == 1 and sourcedate.day >= 29 and result.day == 28): + result = result.replace(day=1, month=result.month + 1) + return result def get_request_ip(self): x_forwarded_for = self.request.META.get('HTTP_X_FORWARDED_FOR') @@ -178,7 +179,6 @@ class PaymentwallCallbackView(View): def get(self, request, *args, **kwargs): payment_raw_data = request.GET.copy() pingback = Pingback(payment_raw_data, self.get_request_ip()) - september2018 = datetime.date(2018, 9, 1) if pingback.validate(): product_type_name, payment_id = pingback.get_product().get_id().split('_') @@ -231,12 +231,10 @@ class PaymentwallCallbackView(View): date_end = school_payment.date_end else: date_start = arrow.get(school_payment.date_end, settings.TIME_ZONE).shift(days=1).datetime - date_end = arrow.get(date_start, settings.TIME_ZONE).shift(months=1).datetime + date_end = self.add_months(date_start) else: date_start = now().date() - if date_start < september2018: - date_start = september2018 - date_end = arrow.get(date_start, settings.TIME_ZONE).shift(months=1, minutes=-1).datetime + date_end = self.add_months(date_start) payment.date_start = date_start payment.date_end = date_end if product_type_name == 'course': diff --git a/project/views.py b/project/views.py index c3277a98..a22a8499 100644 --- a/project/views.py +++ b/project/views.py @@ -4,6 +4,7 @@ from django.db.models import Min, Func, F from django.contrib.auth import get_user_model from django.views.generic import TemplateView from django.utils.timezone import now +from paymentwall.pingback import Pingback from apps.course.models import Course from apps.school.models import SchoolSchedule @@ -47,7 +48,12 @@ class IndexView(TemplateView): school_payment = SchoolPayment.objects.filter( user=self.request.user, date_start__lte=date_now, - date_end__gte=date_now + date_end__gte=date_now, + status__in=[ + Pingback.PINGBACK_TYPE_REGULAR, + Pingback.PINGBACK_TYPE_GOODWILL, + Pingback.PINGBACK_TYPE_RISK_REVIEWED_ACCEPTED, + ], ) school_payment_exists = school_payment.exists() school_schedules_purchased = school_payment.annotate( @@ -77,7 +83,7 @@ class IndexView(TemplateView): '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, + 'school_schedules_purchased': set(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,