From f3edc58046a64376e2a1b45642b41569c8565e25 Mon Sep 17 00:00:00 2001 From: gzbender Date: Mon, 11 Feb 2019 15:20:48 +0500 Subject: [PATCH] =?UTF-8?q?=D0=BE=D1=82=D1=80=D0=B8=D1=86=D0=B0=D1=82?= =?UTF-8?q?=D0=B5=D0=BB=D1=8C=D0=BD=D1=8B=D0=B5=20=D0=B1=D0=B0=D0=BB=D0=B0?= =?UTF-8?q?=D0=BD=D1=81=D1=8B=20=D1=83=20=D0=BF=D1=80=D0=B5=D0=BF=D0=BE?= =?UTF-8?q?=D0=B4=D0=B0=D0=B2=D0=B0=D1=82=D0=B5=D0=BB=D0=B5=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../management/commands/fix_access_expire.py | 25 ++++++++ .../commands/update_courses_slug.py | 2 +- apps/payment/models.py | 58 +++++++++---------- apps/user/models.py | 6 +- 4 files changed, 57 insertions(+), 34 deletions(-) create mode 100644 apps/course/management/commands/fix_access_expire.py diff --git a/apps/course/management/commands/fix_access_expire.py b/apps/course/management/commands/fix_access_expire.py new file mode 100644 index 00000000..cf15f5ff --- /dev/null +++ b/apps/course/management/commands/fix_access_expire.py @@ -0,0 +1,25 @@ +from datetime import timedelta + +from django.core.management.base import BaseCommand + +from apps.payment.models import CoursePayment +from apps.course.models import Course + + +class Command(BaseCommand): + help = 'Fix access duration in all paid courses' + + def add_arguments(self, parser): + parser.add_argument('access_duration', type=int, help='New access duration',) + + def handle(self, *args, **options): + access_duration = options.get('access_duration') + for course in Course.objects.filter(price__gt=0): + course.access_duration = access_duration + course.save() + + for payment in CoursePayment.objects.filter(status__in=CoursePayment.PW_PAID_STATUSES): + payment.access_expire = payment.created_at.date() + timedelta(days=payment.course.access_duration) + payment.save() + + diff --git a/apps/course/management/commands/update_courses_slug.py b/apps/course/management/commands/update_courses_slug.py index 24999a2e..f154e8f9 100644 --- a/apps/course/management/commands/update_courses_slug.py +++ b/apps/course/management/commands/update_courses_slug.py @@ -7,7 +7,7 @@ from apps.course.models import Course class Command(BaseCommand): - help = 'Upload users to Roistat' + help = 'Update courses slug' def handle(self, *args, **options): courses = Course.objects.filter(Q(slug__isnull=True) | Q(slug='')) diff --git a/apps/payment/models.py b/apps/payment/models.py index 07547e19..63664693 100644 --- a/apps/payment/models.py +++ b/apps/payment/models.py @@ -63,7 +63,7 @@ class AuthorBalance(models.Model): verbose_name_plural = 'Балансы' def save(self, *args, **kwargs): - if self.status != self.ACCEPTED: + if self.type == self.IN and not self.id: self.commission = self.calc_commission() if self.type == self.OUT: if self.status == self.DECLINED and not self.declined_send_at: @@ -228,36 +228,34 @@ class Payment(PolymorphicModel): ] def save(self, *args, **kwargs): - amount_data = Payment.calc_amount(payment=self) - if self.status is None and not self.bonus: - self.amount = amount_data.get('amount') - if isinstance(self, SchoolPayment): - self.weekdays = amount_data.get('weekdays') + if not self.is_paid(): + if not self.bonus: + amount_data = Payment.calc_amount(payment=self) + self.amount = amount_data.get('amount') + if isinstance(self, SchoolPayment): + self.weekdays = amount_data.get('weekdays') super().save(*args, **kwargs) - if isinstance(self, CoursePayment) and self.is_paid(): - author_balance = getattr(self, 'authorbalance', None) - if not author_balance: - AuthorBalance.objects.create( - author=self.course.author, - amount=self.amount, - payment=self, - ) - else: - author_balance.amount = self.amount - author_balance.save() - if isinstance(self, GiftCertificatePayment) and self.is_paid(): - ugs, created = UserGiftCertificate.objects.get_or_create(user=self.user, gift_certificate=self.gift_certificate, - payment=self) - if created: - from apps.notification.tasks import send_gift_certificate - send_gift_certificate(ugs.id) - # Если юзер реферал и нет платежа, где применялась скидка - if hasattr(self.user, 'referral') and not self.user.referral.payment and self.is_paid(): - # Платеж - как сигнал, что скидка применилась - self.user.referral.payment = self - self.user.referral.save() - # Отправляем кэшбэк - self.user.referral.send_bonuses(amount_data.get('referral_bonus'), amount_data.get('referrer_bonus')) + if self.is_paid(): + if isinstance(self, CoursePayment): + if not getattr(self, 'authorbalance', None): + AuthorBalance.objects.create( + author=self.course.author, + amount=self.amount, + payment=self, + ) + if isinstance(self, GiftCertificatePayment): + ugs, created = UserGiftCertificate.objects.get_or_create(user=self.user, gift_certificate=self.gift_certificate, + payment=self) + if created: + from apps.notification.tasks import send_gift_certificate + send_gift_certificate(ugs.id) + # Если юзер реферал и нет платежа, где применялась скидка + if hasattr(self.user, 'referral') and not self.user.referral.payment: + # Платеж - как сигнал, что скидка применилась + self.user.referral.payment = self + self.user.referral.save() + # Отправляем кэшбэк + self.user.referral.send_bonuses(amount_data.get('referral_bonus'), amount_data.get('referrer_bonus')) class CoursePayment(Payment): diff --git a/apps/user/models.py b/apps/user/models.py index 5a7337bd..5713e6db 100644 --- a/apps/user/models.py +++ b/apps/user/models.py @@ -108,9 +108,9 @@ class User(AbstractUser): @cached_property def balance(self): - from apps.payment.models import Payment + from apps.payment.models import Payment, AuthorBalance income = self.balances.filter( - type=0, + type=AuthorBalance.IN, payment__isnull=False, payment__status__in=Payment.PW_PAID_STATUSES, ).aggregate( @@ -120,7 +120,7 @@ class User(AbstractUser): income_amount = income.get('amount__sum') or 0 income_commission = income.get('commission__sum') or 0 - payout = self.balances.filter(type=1, status=1).aggregate(models.Sum('amount')) + payout = self.balances.filter(type=AuthorBalance.OUT, status=AuthorBalance.ACCEPTED).aggregate(models.Sum('amount')) payout_amount = payout.get('amount__sum') or 0 return income_amount - income_commission - payout_amount