From aff22391a1241c6fc5c37238391d4dc7c9653383 Mon Sep 17 00:00:00 2001 From: gzbender Date: Tue, 11 Dec 2018 20:08:57 +0500 Subject: [PATCH] =?UTF-8?q?LIL-713=20=D0=9D=D0=B0=D0=BF=D0=B8=D1=81=D0=B0?= =?UTF-8?q?=D1=82=D1=8C=20=D0=BA=D0=BE=D0=BC=D0=B0=D0=BD=D0=B4=D1=83,=20?= =?UTF-8?q?=D1=87=D1=82=D0=BE=D0=B1=D1=8B=20=D0=BD=D0=B0=D1=87=D0=B8=D1=81?= =?UTF-8?q?=D0=BB=D0=B8=D1=82=D1=8C=20100=20=D0=BB=D0=B8=D0=BB=D0=B8=D0=BA?= =?UTF-8?q?=D0=BE=D0=B2=20=D0=BA=D0=BB=D0=B8=D0=B5=D0=BD=D1=82=D0=B0=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../management/commands/give_bonuses.py | 34 +++++++++++++++++++ .../migrations/0029_auto_20181211_1731.py | 23 +++++++++++++ apps/payment/models.py | 2 ++ apps/user/models.py | 4 +-- apps/user/templates/user/bonus-history.html | 12 ++++--- apps/user/views.py | 5 ++- 6 files changed, 71 insertions(+), 9 deletions(-) create mode 100644 apps/payment/management/commands/give_bonuses.py create mode 100644 apps/payment/migrations/0029_auto_20181211_1731.py diff --git a/apps/payment/management/commands/give_bonuses.py b/apps/payment/management/commands/give_bonuses.py new file mode 100644 index 00000000..a9e7ba60 --- /dev/null +++ b/apps/payment/management/commands/give_bonuses.py @@ -0,0 +1,34 @@ +from decimal import Decimal +from django.core.management.base import BaseCommand +from django.db.models import F + +from apps.payment.models import Payment, AuthorBalance, UserBonus +from apps.user.models import User + + +class Command(BaseCommand): + help = 'Fix payment and author balance amount based on payment.data.effective_price_amount' + + def add_arguments(self, parser): + parser.add_argument( + 'action_name', type=str, + help='Name of action', + ) + parser.add_argument( + 'amount', type=int, + help='Bonuses amount', + ) + + def handle(self, *args, **options): + action_name = options.get('action_name') + amount = options.get('amount') + + excluded_users = UserBonus.objects.filter(is_service=True, action_name=action_name).values_list('user_id', flat=True) + for user in User.objects.filter(role=User.USER_ROLE, is_active=True).exclude( + id__in=excluded_users).values_list('id', flat=True): + UserBonus.objects.create(user_id=user, amount=amount, is_service=True, action_name=action_name) + print('%d bonuses was sent to user %d in action %s' % (amount, user, action_name)) + + + + diff --git a/apps/payment/migrations/0029_auto_20181211_1731.py b/apps/payment/migrations/0029_auto_20181211_1731.py new file mode 100644 index 00000000..8a2d25d3 --- /dev/null +++ b/apps/payment/migrations/0029_auto_20181211_1731.py @@ -0,0 +1,23 @@ +# Generated by Django 2.0.7 on 2018-12-11 17:31 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('payment', '0028_add_gift_certificates'), + ] + + operations = [ + migrations.AddField( + model_name='userbonus', + name='action_name', + field=models.CharField(blank=True, default='', max_length=10), + ), + migrations.AddField( + model_name='userbonus', + name='is_service', + field=models.BooleanField(default=False), + ), + ] diff --git a/apps/payment/models.py b/apps/payment/models.py index 413e8bf8..e9abf4d4 100644 --- a/apps/payment/models.py +++ b/apps/payment/models.py @@ -308,6 +308,8 @@ class UserBonus(models.Model): payment = models.ForeignKey(Payment, on_delete=models.SET_NULL, null=True) referral = models.ForeignKey('user.Referral', on_delete=models.SET_NULL, null=True) created_at = models.DateTimeField(auto_now_add=True) + is_service = models.BooleanField(default=False) + action_name = models.CharField(max_length=10, blank=True, default='') class Meta: ordering = ('created_at',) diff --git a/apps/user/models.py b/apps/user/models.py index 3f41a016..f9bac9dc 100644 --- a/apps/user/models.py +++ b/apps/user/models.py @@ -1,5 +1,6 @@ from json import dumps +from django.db.models import Q from django.utils.functional import cached_property from rest_framework.authtoken.models import Token from phonenumber_field.modelfields import PhoneNumberField @@ -123,8 +124,7 @@ class User(AbstractUser): def bonus(self): from apps.payment.models import Payment return int(self.bonuses.filter( - payment__isnull=False, - payment__status__in=Payment.PW_PAID_STATUSES, + Q(payment__isnull=False, payment__status__in=Payment.PW_PAID_STATUSES) | Q(is_service=True), ).aggregate(models.Sum('amount')).get('amount__sum') or 0) diff --git a/apps/user/templates/user/bonus-history.html b/apps/user/templates/user/bonus-history.html index 11c67381..105592a5 100644 --- a/apps/user/templates/user/bonus-history.html +++ b/apps/user/templates/user/bonus-history.html @@ -53,14 +53,18 @@
Подарочный сертификат
{% else %}
- {% if request.user_agent.is_mobile %} - Школа. {% if payment.date_start and payment.date_end %}{{ payment.date_start|date:"j b" }} - {{ payment.date_end|date:"j b" }}{% endif %} + {% if payment %} + {% if request.user_agent.is_mobile %} + Школа. {% if payment.date_start and payment.date_end %}{{ payment.date_start|date:"j b" }} - {{ payment.date_end|date:"j b" }}{% endif %} + {% else %} + Школа. {% if payment.date_start and payment.date_end %}{{ payment.date_start }} - {{ payment.date_end }}{% endif %} + {% endif %} {% else %} - Школа. {% if payment.date_start and payment.date_end %}{{ payment.date_start }} - {{ payment.date_end }}{% endif %} + {% if bonus.action_name %}Зачисление по акции{% endif %} {% endif %}
{% endif %} -
{{payment.amount }}
+
{% if payment %}{{payment.amount }}{% endif %}
{% if bonus.referral %}{{ bonus.referral.referral.get_full_name }}{% endif %}
diff --git a/apps/user/views.py b/apps/user/views.py index e456af1c..d1a6f02b 100644 --- a/apps/user/views.py +++ b/apps/user/views.py @@ -14,7 +14,7 @@ from django.views.generic import DetailView, UpdateView, TemplateView, FormView from django.contrib import messages from django.contrib.auth import get_user_model from django.contrib.auth.decorators import login_required -from django.db.models import F, Func, Sum, Min, Max +from django.db.models import F, Func, Sum, Min, Max, Q from django.urls import reverse_lazy from django.utils.decorators import method_decorator from django.utils.timezone import now @@ -303,8 +303,7 @@ class BonusHistoryView(TemplateView): config = Config.load() context = self.get_context_data(**kwargs) context['bonuses'] = request.user.bonuses.filter( - payment__isnull=False, - payment__status__in=Payment.PW_PAID_STATUSES, + Q(payment__isnull=False, payment__status__in=Payment.PW_PAID_STATUSES) | Q(is_service=True), ) context['referrer_url'] = 'https://%s%s?referrer=%s' % ( settings.MAIN_HOST, reverse('index'), short_url.encode_url(request.user.id)