diff --git a/apps/config/migrations/0010_auto_20180820_0853.py b/apps/config/migrations/0010_auto_20180820_0853.py new file mode 100644 index 00000000..0b9af168 --- /dev/null +++ b/apps/config/migrations/0010_auto_20180820_0853.py @@ -0,0 +1,23 @@ +# Generated by Django 2.0.6 on 2018-08-20 08:53 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('config', '0009_auto_20180729_0503'), + ] + + operations = [ + migrations.RenameField( + model_name='config', + old_name='REFERRAL_DISCOUNT', + new_name='REFERRAL_BONUS', + ), + migrations.RenameField( + model_name='config', + old_name='REFERRER_CASHBACK', + new_name='REFERRER_BONUS', + ), + ] diff --git a/apps/config/models.py b/apps/config/models.py index 8baa4c86..7e7e833a 100644 --- a/apps/config/models.py +++ b/apps/config/models.py @@ -17,8 +17,8 @@ class Config(models.Model): SERVICE_DISCOUNT = models.IntegerField(default=1000) SCHOOL_LOGO_IMAGE = models.ImageField(null=True, blank=True) MAIN_PAGE_TOP_IMAGE = models.ImageField(null=True, blank=True) - REFERRER_CASHBACK = models.IntegerField(default=10) - REFERRAL_DISCOUNT = models.IntegerField(default=10) + REFERRER_BONUS = models.IntegerField(default=10) + REFERRAL_BONUS = models.IntegerField(default=10) def save(self, *args, **kwargs): self.pk = 1 diff --git a/apps/payment/migrations/0020_userbonus.py b/apps/payment/migrations/0020_userbonus.py new file mode 100644 index 00000000..4b029f87 --- /dev/null +++ b/apps/payment/migrations/0020_userbonus.py @@ -0,0 +1,28 @@ +# Generated by Django 2.0.6 on 2018-08-20 06:51 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('user', '0025_auto_20180820_0651'), + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('payment', '0019_payment_roistat_visit'), + ] + + operations = [ + migrations.CreateModel( + name='UserBonus', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('amount', models.DecimalField(decimal_places=2, default=0, editable=False, max_digits=8)), + ('created_at', models.DateTimeField(auto_now_add=True)), + ('payment', models.OneToOneField(null=True, on_delete=django.db.models.deletion.SET_NULL, to='payment.Payment')), + ('referral', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='user.Referral')), + ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='bonuses', to=settings.AUTH_USER_MODEL)), + ], + ), + ] diff --git a/apps/payment/models.py b/apps/payment/models.py index ec3936e2..9f8d4d31 100644 --- a/apps/payment/models.py +++ b/apps/payment/models.py @@ -11,6 +11,7 @@ from django.core.validators import RegexValidator from django.utils.timezone import now from django.conf import settings +from apps.user.models import Referral from project.utils import weekday_in_date_range from apps.course.models import Course @@ -247,3 +248,12 @@ class SchoolPayment(Payment): @property def date_end_humanize(self): return arrow.get(self.date_end, settings.TIME_ZONE).humanize(locale='ru') + + +class UserBonus(models.Model): + user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='bonuses') + amount = models.DecimalField(max_digits=8, decimal_places=2, default=0, editable=False) + payment = models.OneToOneField(Payment, on_delete=models.SET_NULL, null=True) + referral = models.ForeignKey(Referral, on_delete=models.SET_NULL, null=True) + created_at = models.DateTimeField(auto_now_add=True) + diff --git a/apps/user/migrations/0025_auto_20180820_0651.py b/apps/user/migrations/0025_auto_20180820_0651.py new file mode 100644 index 00000000..ad53086e --- /dev/null +++ b/apps/user/migrations/0025_auto_20180820_0651.py @@ -0,0 +1,20 @@ +# Generated by Django 2.0.6 on 2018-08-20 06:51 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('user', '0024_referral_payment'), + ] + + operations = [ + migrations.AlterField( + model_name='referral', + name='referral', + field=models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='referral', to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/apps/user/models.py b/apps/user/models.py index 286c61c3..230d99d1 100644 --- a/apps/user/models.py +++ b/apps/user/models.py @@ -102,6 +102,10 @@ class User(AbstractUser): commission = aggregate.get('commission__sum') or 0 return amount - commission + @property + def bonus(self): + return self.balances.aggregate(models.Sum('amount')).get('amount__sum') or 0 + @receiver(post_save, sender=User) def create_auth_token(sender, instance=None, created=False, **kwargs): diff --git a/apps/user/templates/blocks/menu.html b/apps/user/templates/blocks/menu.html new file mode 100644 index 00000000..35c4df2d --- /dev/null +++ b/apps/user/templates/blocks/menu.html @@ -0,0 +1,9 @@ +
+
+ +
+
diff --git a/apps/user/templates/user/bonus-history.html b/apps/user/templates/user/bonus-history.html new file mode 100644 index 00000000..b4095470 --- /dev/null +++ b/apps/user/templates/user/bonus-history.html @@ -0,0 +1,54 @@ +{% extends "templates/lilcity/index.html" %} +{% load static %} +{% load rupluralize from plural %} + +{% block content %} +{% include "../blocks/menu.html" %} + +
+
+
Бонусы
+
У вас {{ request.user.bonus|rupluralize:"бонус,бонуса,бонусов" }}
+
+ Вы можете пригласить своих друзей в наш сервис, за это вы получите {{ config.REFERRER_BONUS }} % от суммы + их первой покупки и они получат {{ config.REFERRAL_BONUS }} % от суммы покупки на свой бонусный счет
+ Просто отправьте им эту ссылку, по которой они смогут зарегестрироваться: +
+
{{ referrer_url }}
+
+
+ +
+
+
История бонусов
+
+
+ {% for bonus in bonuses %} +
+ {% with payment=bonus.payment %} + {% if payment.course %} +
Курс. {{ payment.course.title }}
+ {% else %} +
+ Школа. {% if payment.date_start and payment.date_end %}{{ payment.date_start }} - {{ payment.date_end }}{% endif %} +
+ {% endif %} + {% if payment.balance %} +
{{payment.balance.amount}}
+ {% else %} +
{{payment.amount}}
+ {% endif %} + {% if bonus.referral %} + + {% endif %} +
{{ bonus.amount }}
+ {% endwith %} +
+ {% empty %} + Нет истории бонусов + {% endfor %} +
+
+
+
+{% endblock content %} diff --git a/apps/user/templates/user/payment-history.html b/apps/user/templates/user/payment-history.html index c27a3faa..75773722 100644 --- a/apps/user/templates/user/payment-history.html +++ b/apps/user/templates/user/payment-history.html @@ -1,13 +1,5 @@ {% extends "templates/lilcity/index.html" %} {% load static %} {% block content %} -
-
- -
-
+{% include "../blocks/menu.html" %} {% if not user.is_email_proved %}
diff --git a/apps/user/views.py b/apps/user/views.py index 309fb0e0..ed9cc404 100644 --- a/apps/user/views.py +++ b/apps/user/views.py @@ -4,6 +4,7 @@ from uuid import uuid4 from os.path import splitext from datetime import timedelta from paymentwall import Pingback +import short_url from django.conf import settings from django.contrib.auth import login @@ -297,3 +298,17 @@ class AuthorRequestView(FormView): 'facebook': self.request.user.facebook, } return super().get_context_data(**kwargs) + + +@method_decorator(login_required, name='dispatch') +class BonusHistoryView(TemplateView): + model = User + template_name = 'user/bonus-history.html' + + def get(self, request, *args, **kwargs): + context = self.get_context_data(**kwargs) + context['bonuses'] = request.user.bonuses.all() + context['referrer_url'] = '%s%s?referrer=%s' % ( + settings.MAIN_HOST, reverse('index'), short_url.encode(request.user.id) + ) + return self.render_to_response(context) diff --git a/project/context_processors.py b/project/context_processors.py index 738e5b3c..ee33ef54 100644 --- a/project/context_processors.py +++ b/project/context_processors.py @@ -1,6 +1,7 @@ from django.utils.timezone import now from paymentwall.pingback import Pingback from django.contrib.auth import get_user_model +from django.conf import settings from apps.config.models import Config from apps.content.models import Baner from apps.payment.models import SchoolPayment @@ -41,3 +42,6 @@ def referrer(request): referrer = None return {'referrer': referrer} + +def settings(request): + return {'settings': settings} diff --git a/project/settings.py b/project/settings.py index 1228211c..837df261 100644 --- a/project/settings.py +++ b/project/settings.py @@ -31,6 +31,7 @@ SECRET_KEY = os.getenv('SECRET_KEY', 'jelm*91lj(_-o20+6^a+bgv!4s6e_efry^#+f#=1ak DEBUG = os.getenv('DEBUG', False) ALLOWED_HOSTS = os.getenv('ALLOWED_HOSTS', '*').split(',') +MAIN_HOST = 'lil.school' # Application definition @@ -96,6 +97,7 @@ TEMPLATES = [ 'project.context_processors.baner', 'project.context_processors.is_summer_school_purchased', 'project.context_processors.referrer', + 'project.context_processors.settings', 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', diff --git a/project/templates/blocks/user_menu.html b/project/templates/blocks/user_menu.html index 95e2b7d3..1e20df2d 100644 --- a/project/templates/blocks/user_menu.html +++ b/project/templates/blocks/user_menu.html @@ -1,4 +1,5 @@ {% load static %} {% load thumbnail %} +{% load rupluralize from plural %} {% if request.user.is_authenticated %}