From 2a5ca2bbd6c5daae279dcfc8f0d829243220c4a3 Mon Sep 17 00:00:00 2001 From: Ivlev Denis Date: Tue, 8 May 2018 19:08:13 +0300 Subject: [PATCH 1/8] Fix check online --- apps/school/models.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/apps/school/models.py b/apps/school/models.py index 1e6a44c6..ad9efb2e 100644 --- a/apps/school/models.py +++ b/apps/school/models.py @@ -44,7 +44,8 @@ class SchoolSchedule(models.Model): return dict(self.WEEKDAY_CHOICES).get(self.weekday, '') def is_online(self): - return now().isoweekday() == self.weekday and now().time() >= self.start_at + end_at = datetime.combine(now().today(), self.start_at) + timedelta(hours=2) + return self.start_at <= now().time() and end_at.time() >= now().time() def current_live_lesson(self): now_time = now() From 4359250cbbc641c6828e68062f18728c2c70bc6d Mon Sep 17 00:00:00 2001 From: Ivlev Denis Date: Thu, 10 May 2018 11:42:22 +0300 Subject: [PATCH 2/8] Raise exception if pingback not valid --- apps/payment/views.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/apps/payment/views.py b/apps/payment/views.py index 608d94d3..412b5516 100644 --- a/apps/payment/views.py +++ b/apps/payment/views.py @@ -6,6 +6,7 @@ from urllib.parse import urlsplit from django.contrib import messages from django.contrib.auth.decorators import login_required +from django.core.exceptions import DisallowedHost from django.http import HttpResponse from django.shortcuts import redirect, get_object_or_404 from django.views.generic import View, TemplateView @@ -247,4 +248,5 @@ class PaymentwallCallbackView(View): payment.author_balance.save() return HttpResponse('OK') else: + raise DisallowedHost return HttpResponse(status=403) From a7baef7a20c9579e73fe6e62d4c40969f9b62bff Mon Sep 17 00:00:00 2001 From: Ivlev Denis Date: Thu, 10 May 2018 12:31:00 +0300 Subject: [PATCH 3/8] Fix user payments template --- apps/user/templates/user/payment-history.html | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/apps/user/templates/user/payment-history.html b/apps/user/templates/user/payment-history.html index f39fa3ff..5643dfa5 100644 --- a/apps/user/templates/user/payment-history.html +++ b/apps/user/templates/user/payment-history.html @@ -75,7 +75,7 @@
История платежей
- {% if request.user.payments.all|length %} + {% if request.user.payments.all.exists %} {% for payment in request.user.payments.all %}
{% if payment.course %} @@ -83,7 +83,6 @@ {% else %}
Школа. {% if payment.date_start and payment.date_end %}{{ payment.date_start }} - {{ payment.date_end }}{% endif %} - {{ payment }}
{% endif %} {% if payment.balance %} From a1b90d90826e7b479540d24ccbd1d285c3ac2b03 Mon Sep 17 00:00:00 2001 From: Ivlev Denis Date: Thu, 10 May 2018 12:31:24 +0300 Subject: [PATCH 4/8] Update payment all qs --- apps/payment/models.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/apps/payment/models.py b/apps/payment/models.py index 10146971..8dbbccb6 100644 --- a/apps/payment/models.py +++ b/apps/payment/models.py @@ -2,6 +2,7 @@ import arrow from paymentwall import Pingback from polymorphic.models import PolymorphicModel +from polymorphic.managers import PolymorphicManager from django.db import models from django.contrib.auth import get_user_model @@ -73,6 +74,12 @@ class AuthorBalance(models.Model): return self.amount * config.SERVICE_COMMISSION / 100 +class PaymentManger(PolymorphicManager): + + def all(self): + return self.filter(status__isnull=False) + + class Payment(PolymorphicModel): PW_STATUS_CHOICES = ( (Pingback.PINGBACK_TYPE_REGULAR, 'regular',), @@ -93,13 +100,12 @@ class Payment(PolymorphicModel): created_at = models.DateTimeField(auto_now_add=True) update_at = models.DateTimeField(auto_now=True) - non_polymorphic = models.Manager() + objects = PaymentManger() class Meta: verbose_name = 'Платеж' verbose_name_plural = 'Платежи' ordering = ('created_at',) - base_manager_name = 'non_polymorphic' def calc_commission(self): return self.amount * config.SERVICE_COMMISSION / 100 From 4e0739235401e4e1ba4ecd55ca7a79c49eb252a7 Mon Sep 17 00:00:00 2001 From: Ivlev Denis Date: Thu, 10 May 2018 12:34:22 +0300 Subject: [PATCH 5/8] Update payment qs in api --- api/v1/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/v1/views.py b/api/v1/views.py index 6fbde9c6..41cd881a 100644 --- a/api/v1/views.py +++ b/api/v1/views.py @@ -419,7 +419,7 @@ class AuthorRequestViewSet(ExtendedModelViewSet): class PaymentViewSet(ExtendedModelViewSet): - queryset = Payment.objects.order_by('-created_at') + queryset = Payment.objects.filter(status__isnull=False).order_by('-created_at') serializer_class = PaymentSerializer permission_classes = (IsAdmin,) filter_fields = ('status',) From 4cc52836e9ab343408e5b7a668685bb3092c68ab Mon Sep 17 00:00:00 2001 From: Ivlev Denis Date: Thu, 10 May 2018 12:48:59 +0300 Subject: [PATCH 6/8] Revert school tab in user profile --- apps/user/templates/user/profile.html | 8 ++++++++ apps/user/views.py | 17 +++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/apps/user/templates/user/profile.html b/apps/user/templates/user/profile.html index 0dbd54b2..c1bb7f81 100644 --- a/apps/user/templates/user/profile.html +++ b/apps/user/templates/user/profile.html @@ -71,6 +71,7 @@ КУРСЫ {% endif %} +
@@ -89,6 +90,13 @@
{% endif %} +
+
+
+ {% include "course/school.html" %} +
+
+
diff --git a/apps/user/views.py b/apps/user/views.py index bf2a6ebd..4a082294 100644 --- a/apps/user/views.py +++ b/apps/user/views.py @@ -63,6 +63,23 @@ class UserView(DetailView): ], ), ).distinct() + school_payment = SchoolPayment.objects.filter( + user=self.object, + date_start__lte=now(), + date_end__gt=now(), + status__in=[ + Pingback.PINGBACK_TYPE_REGULAR, + Pingback.PINGBACK_TYPE_GOODWILL, + Pingback.PINGBACK_TYPE_RISK_REVIEWED_ACCEPTED, + ], + ).last() + context['school_payment'] = school_payment + if school_payment and school_payment.date_end: + context['school_days_left'] = (school_payment.date_end - now().date()).days + context['school_schedules'] = SchoolSchedule.objects.filter( + weekday__in=school_payment.weekdays if school_payment else [], + ) + return context From e3396acbd06ca446af04b73b5c1b78cbe56423a9 Mon Sep 17 00:00:00 2001 From: Ivlev Denis Date: Thu, 10 May 2018 12:56:45 +0300 Subject: [PATCH 7/8] Update profile school qs --- apps/user/views.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/apps/user/views.py b/apps/user/views.py index 4a082294..a771152e 100644 --- a/apps/user/views.py +++ b/apps/user/views.py @@ -17,6 +17,7 @@ from django.contrib.auth import get_user_model from django.contrib.auth.decorators import login_required, permission_required from django.contrib.auth.hashers import check_password, make_password from django.http import Http404 +from django.db.models import F, Func from django.urls import reverse_lazy from django.utils.decorators import method_decorator from django.utils.timezone import now @@ -72,12 +73,15 @@ class UserView(DetailView): Pingback.PINGBACK_TYPE_GOODWILL, Pingback.PINGBACK_TYPE_RISK_REVIEWED_ACCEPTED, ], - ).last() + ) + school_schedules_purchased = school_payment.annotate( + joined_weekdays=Func(F('weekdays'), function='unnest',) + ).values_list('joined_weekdays', flat=True).distinct() context['school_payment'] = school_payment - if school_payment and school_payment.date_end: - context['school_days_left'] = (school_payment.date_end - now().date()).days + if school_payment.exists() and school_payment.last().date_end: + context['school_days_left'] = (school_payment.last().date_end - now().date()).days context['school_schedules'] = SchoolSchedule.objects.filter( - weekday__in=school_payment.weekdays if school_payment else [], + weekday__in=school_schedules_purchased if school_payment.exists() else [], ) return context From ad002c15c135e1f6e89190704160235630515733 Mon Sep 17 00:00:00 2001 From: Ivlev Denis Date: Thu, 10 May 2018 16:17:03 +0300 Subject: [PATCH 8/8] Fix _create_user --- apps/user/models.py | 1 - 1 file changed, 1 deletion(-) diff --git a/apps/user/models.py b/apps/user/models.py index fd94ab81..6adf6cf4 100644 --- a/apps/user/models.py +++ b/apps/user/models.py @@ -24,7 +24,6 @@ class UserManager(BaseUserManager): username = email if not password: password = self.make_random_password() - super().save(*args, **kwargs) email = self.normalize_email(email) username = self.model.normalize_username(username) user = self.model(username=username, email=email, **extra_fields)