From b4d4402c73897db372e0b9f8c9ec081c6a3888b5 Mon Sep 17 00:00:00 2001 From: Andrey Date: Fri, 16 Mar 2018 13:23:21 +0300 Subject: [PATCH 1/3] pay --- finance/views.py | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/finance/views.py b/finance/views.py index c0f9505..98a0725 100644 --- a/finance/views.py +++ b/finance/views.py @@ -42,24 +42,40 @@ class BillListView(APIView): or request.user.is_superuser): bill = request.JSON.get('bill') children = request.JSON.get('children', []) - bill_kwarg = dict() if bill: - bill_kwarg['user'] = get_user_model().objects.get(email=bill['user']) - bill_kwarg['opener'] = get_user_model().objects.get(email=bill['opener']) - bill_kwarg['description'] = bill['description'] - bill_kwarg['comment'] = bill['comment'] - bill_kwarg['course_token'] = bill['course_token'] + user = get_user_model().objects.get(email=bill['user']) + opener = get_user_model().objects.get(email=bill['opener']) + description = bill['description'] + comment = bill['comment'] + course_token = bill['course_token'] + + try: + bill_obj = Bill.objects.get(user=user, course_token=course_token) + except Bill.DoesNotExist: + bill_obj = Bill.objects.create(user=user, course_token=course_token) + + bill_obj.opener = opener + bill_obj.description = description + bill_obj.comment = comment + bill_obj.save() - bill_obj, is_create = Bill.objects.update_or_create(**bill_kwarg) invoices = bill_obj.invoice_set.all() for i in children: - i['method'] = get_real_name(elem=i['method'], array=Invoice.BILL_METHOD) - i['status'] = get_real_name(elem=i['status'], array=Invoice.BILL_STATUSES) - i['bill'] = bill_obj - i['yandex_pay'] = None - invoice, _is_create = Invoice.objects.update_or_create(**i) + status = get_real_name(elem=i['status'], array=Invoice.BILL_STATUSES) + try: + invoice = Invoice.objects.get(id=i['id']) + if invoice.status == "P" or invoice.status == status: + continue + + except Invoice.DoesNotExist: + i['method'] = get_real_name(elem=i['method'], array=Invoice.BILL_METHOD) + i['status'] = status + i['bill'] = bill_obj + i['yandex_pay'] = None + invoice = Invoice.objects.create(**i) + if i['method'] == 'Y' and invoice.yandex_pay is None: yandex_pay = Payment.objects.create( order_amount=i['price'], From 9364171ac05c618b1cc7f3ce1ad5a67305442b74 Mon Sep 17 00:00:00 2001 From: Andrey Date: Fri, 16 Mar 2018 13:30:50 +0300 Subject: [PATCH 2/3] pay --- finance/views.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/finance/views.py b/finance/views.py index 98a0725..800786c 100644 --- a/finance/views.py +++ b/finance/views.py @@ -60,8 +60,6 @@ class BillListView(APIView): bill_obj.comment = comment bill_obj.save() - invoices = bill_obj.invoice_set.all() - for i in children: status = get_real_name(elem=i['status'], array=Invoice.BILL_STATUSES) try: @@ -102,10 +100,6 @@ class BillListView(APIView): msg.send() - invoices = [j for j in invoices if not j.id == invoice.id] - - [i.delete() for i in invoices] - res = { "bill": BillSerializer(bill_obj).data, "children": [InvoiceSerializer(i).data for i in bill_obj.invoice_set.all()], From 3c28c27bd55b9665187571c9e2129b63e4fb7ef7 Mon Sep 17 00:00:00 2001 From: Andrey Date: Fri, 16 Mar 2018 13:37:04 +0300 Subject: [PATCH 3/3] pay --- finance/views.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/finance/views.py b/finance/views.py index 800786c..5d6f2ae 100644 --- a/finance/views.py +++ b/finance/views.py @@ -63,9 +63,17 @@ class BillListView(APIView): for i in children: status = get_real_name(elem=i['status'], array=Invoice.BILL_STATUSES) try: - invoice = Invoice.objects.get(id=i['id']) - if invoice.status == "P" or invoice.status == status: - continue + invoice_id = i['id'] + except KeyError: + invoice_id = None + + try: + if not invoice_id is None: + invoice = Invoice.objects.get(id=i['id']) + if invoice.status == "P" or invoice.status == status: + continue + else: + raise Invoice.DoesNotExist except Invoice.DoesNotExist: i['method'] = get_real_name(elem=i['method'], array=Invoice.BILL_METHOD)