From 5868a97230696692dae7818602c55e564747dfbd Mon Sep 17 00:00:00 2001 From: Andrey Date: Wed, 15 Nov 2017 11:41:41 +0300 Subject: [PATCH] =?UTF-8?q?=D0=AF=D0=BD=D0=B4=D0=B5=D0=BA=D1=81=20=D0=B4?= =?UTF-8?q?=D0=B5=D0=BD=D1=8C=D0=B3=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- courses/views.py | 1 + csv/load_student_teachers_threads.py | 8 +- finance/migrations/0013_auto_20171115_1053.py | 21 ++++++ finance/models.py | 2 +- finance/signals.py | 73 ++++++++++++++++--- finance/views.py | 2 +- .../migrations/0011_auto_20171114_1652.py | 20 +++++ journals/models.py | 4 +- journals/serilizers.py | 13 +++- 9 files changed, 126 insertions(+), 18 deletions(-) create mode 100644 finance/migrations/0013_auto_20171115_1053.py create mode 100644 journals/migrations/0011_auto_20171114_1652.py diff --git a/courses/views.py b/courses/views.py index d14b281..1aed85a 100644 --- a/courses/views.py +++ b/courses/views.py @@ -99,5 +99,6 @@ class VertexDetail(APIView): return Response("permission denied", status=403) res = VertexSerializer(vertex).data + res['is_in_progress'] = vertex.course.progress_set.filter(user=request.user)[0].active_obj == vertex return Response(res, status=200) if status == 200 else Response(status=204) diff --git a/csv/load_student_teachers_threads.py b/csv/load_student_teachers_threads.py index d9c8acc..017b89e 100644 --- a/csv/load_student_teachers_threads.py +++ b/csv/load_student_teachers_threads.py @@ -61,18 +61,20 @@ if __name__ == '__main__': content_type=ct, object_id=vertex.id, action_type=action, - date=row['date'], ) + journal.date = row['date'] + journal.save() journal_comment = Journal.objects.create( thread=child_child_thread, user=owner, content_type=ct, object_id=vertex.id, - action_type=7, + action_type="comment", extra_data=row['text'], - date=row['date'], ) + journal_comment.date = row['date'] + journal_comment.save() for file_id in row['files'].split("[")[1].split("]")[0].split(", "): if file_id: diff --git a/finance/migrations/0013_auto_20171115_1053.py b/finance/migrations/0013_auto_20171115_1053.py new file mode 100644 index 0000000..5dac866 --- /dev/null +++ b/finance/migrations/0013_auto_20171115_1053.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.6 on 2017-11-15 10:53 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('finance', '0012_auto_20171110_1302'), + ] + + operations = [ + migrations.AlterField( + model_name='invoice', + name='yandex_pay', + field=models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='yandex_money.Payment'), + ), + ] diff --git a/finance/models.py b/finance/models.py index 7b8cc9c..bde80ae 100755 --- a/finance/models.py +++ b/finance/models.py @@ -48,7 +48,7 @@ class Invoice(models.Model): help_text='Сумма, минус комиссия') method = models.CharField(verbose_name='Способ оплаты', max_length=2, default='Y', choices=BILL_METHOD) key = models.CharField(verbose_name='Ключ платежа', blank=True, max_length=255, null=True) - yandex_pay = models.ForeignKey(to=Payment, blank=True, null=True) + yandex_pay = models.OneToOneField(to=Payment, blank=True, null=True) comment = models.TextField(verbose_name='Комментарий продавца', help_text='Будет показано пользователю', blank=True, editable=False) bill = models.ForeignKey(to=Bill, verbose_name="Связный счёт") diff --git a/finance/signals.py b/finance/signals.py index 11623f9..6c527ce 100644 --- a/finance/signals.py +++ b/finance/signals.py @@ -1,22 +1,75 @@ -from django.core.mail import send_mail -from django.db.models.signals import pre_save +from django.core.mail import EmailMessage +from django.db.models.signals import pre_save, post_save from django.dispatch import receiver +from yandex_money.models import Payment from finance.models import Invoice +from access.models import Progress @receiver(pre_save, sender=Invoice) def delete_dependencies(instance, **kwargs): """Отправка сообщения после сохранения платежа""" if instance.yandex_pay and instance.method == 'Y' and instance.status == 'P': - body = { - "subject": 'Вам выставлен новый счёт', - "message": '''Вам выставлен счёт, для оплаты перейдите по ссылке + EmailMessage( + 'Вам выставлен новый счёт', + '''Вам выставлен счёт, для оплаты перейдите по ссылке https://go.skillbox.ru/api/v1/finance/payment/%s/''' % instance.yandex_pay.id, - "from_email": 'robo@skillbox.ru', - "recipient_list": [instance.yandex_pay.cps_email], - } + 'robo@skillbox.ru', + [instance.yandex_pay.cps_email], + [instance.bill.opener.email], + reply_to=[instance.bill.opener.email], + ) + + if instance.status == 'F': + if instance.is_open: + Progress.objects.create( + course=instance.bill.course, + user=instance.bill.user, + active_obj=instance.bill.course.get_first(['tutorial', 'task',]) + ) + EmailMessage( + 'Ваш платёж прошёл успешно', + '''Вам открыт доступ к курсу "%s", вы можете перейти по ссылке и + ознакомиться с материабламиhttps://go.skillbox.ru/course/%s/''' + % (instance.bill.course.title, instance.bill.course.slug), + 'robo@skillbox.ru', + [instance.yandex_pay.cps_email], + [instance.bill.opener.email], + reply_to=[instance.bill.opener.email], + ) + else: + EmailMessage( + 'Ваш платёж прошёл успешно', + '''Курс "%s" был забронирован''' % instance.bill.course.title, + 'robo@skillbox.ru', + [instance.yandex_pay.cps_email], + [instance.bill.opener.email], + reply_to=[instance.bill.opener.email], + ) - send_mail( - **body + if instance.status == 'C': + EmailMessage( + 'Ошибка платежа!' + """Внимание не прошёл платёж пользавателю %s, + по курсу "%s" ID платежа: %s. Если не получается + решить проблему самостоятельно, ответьте на это письмо, + постарайтесь подробно описать последовательность действий, + которую превела к ошибке""" + % (instance.bill.user.get_full_name(), instance.bill.course.title, instance.id), + 'robo@skillbox.ru', + [instance.bill.opener.email], + reply_to=["andrey.korolev@skillbox.ru"] ) + + +@receiver(post_save, sender=Payment) +def access_pay(instance, **kwargs): + if instance.status == 'success': + instance.invoice.status = "F" + instance.invoice.real_price = instance.shop_amount + instance.invoice.save() + + if instance.status == 'fail': + instance.invoice.status = "C" + instance.invoice.save() diff --git a/finance/views.py b/finance/views.py index 9c96e44..f154040 100644 --- a/finance/views.py +++ b/finance/views.py @@ -55,7 +55,7 @@ class BillListView(APIView): user=bill_obj.user, cps_email=bill_obj.user.email, ) - invoice.yandex_pay=yandex_pay + invoice.yandex_pay = yandex_pay invoice.save() invoices = [j for j in invoices if not j.id == invoice.id] diff --git a/journals/migrations/0011_auto_20171114_1652.py b/journals/migrations/0011_auto_20171114_1652.py new file mode 100644 index 0000000..b2f1ef3 --- /dev/null +++ b/journals/migrations/0011_auto_20171114_1652.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.6 on 2017-11-14 16:52 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('journals', '0010_auto_20171110_1646'), + ] + + operations = [ + migrations.AlterField( + model_name='journal', + name='date', + field=models.DateTimeField(auto_now_add=True), + ), + ] diff --git a/journals/models.py b/journals/models.py index a577903..0d85d6d 100755 --- a/journals/models.py +++ b/journals/models.py @@ -39,7 +39,7 @@ class Journal(models.Model): object_id = models.PositiveIntegerField() content_object = GenericForeignKey('content_type', 'object_id') action_type = models.CharField(max_length=31, choices=ACTION_CHOICES) - date = models.DateTimeField(auto_now=True) + date = models.DateTimeField(auto_now_add=True) files = models.ManyToManyField(to=Storage, blank=True) def __str__(self): @@ -70,7 +70,7 @@ class Thread(models.Model): def check_perm(self, user): res = user in self.subscribers.all() for i in self.groups.all(): - res = res or i in user.groups + res = res or i in user.groups.all() return res or sum([int(i.check_perm(user)) for i in self.parent.all()]) def child_thread_count(self): diff --git a/journals/serilizers.py b/journals/serilizers.py index 42bd2d0..02962eb 100644 --- a/journals/serilizers.py +++ b/journals/serilizers.py @@ -4,10 +4,21 @@ from journals.models import Thread, Journal class JournalSerializer(serializers.ModelSerializer): + user = serializers.SerializerMethodField() + label = serializers.SerializerMethodField() class Meta: model = Journal - exclude = ('content_type', 'object_id',) + fields = ('label', 'date', 'user', 'files', 'action_type') + + @staticmethod + def get_user(self): + return self.user.get_full_name() + + @staticmethod + def get_label(self): + return self.extra_data if self.extra_data else """%s %s"""\ + % (self.user.get_full_name(), self.get_action_type_display()) class ThreadDetailSerializer(serializers.ModelSerializer):