Закинуть в Roistat отсутствующие заказы

remotes/origin/hotfix/send-payments-to-roistat
gzbender 6 years ago
parent b288acb9d0
commit e14108f317
  1. 77
      apps/payment/management/commands/send_transactions_to_roistat.py
  2. 9261
      orders-roistat.csv

@ -1,6 +1,9 @@
import csv
from pytz import utc
import os
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand, CommandError
from django.utils.timezone import now from django.contrib.contenttypes.models import ContentType
from paymentwall.pingback import Pingback
from apps.payment.tasks import transaction_to_roistat from apps.payment.tasks import transaction_to_roistat
from apps.payment.models import Payment from apps.payment.models import Payment
@ -14,25 +17,61 @@ class Command(BaseCommand):
def add_arguments(self, parser): def add_arguments(self, parser):
parser.add_argument( parser.add_argument(
'start_id', type=int, '--start_id', type=int,
help='Start payment id', help='Start payment id',
) )
parser.add_argument(
'--csv',
dest='csv',
help='CSV file with exist payments in roistat',
)
def handle(self, *args, **options): def handle(self, *args, **options):
start_id = options.get('start_id') start_id = options.get('start_id')
print('start_id=' + str(start_id)) csv_file = options.get('csv')
if start_id:
payments = Payment.objects.filter(id__gte=start_id, status__in=Payment.PW_PAID_STATUSES) print('start_id=' + str(start_id))
for payment in payments:
print('TRANSACTION: ' + str(payment.id)) payments = Payment.objects.filter(id__gte=start_id, status__in=Payment.PW_PAID_STATUSES)
for payment in payments:
transaction_to_roistat.delay( print('TRANSACTION: ' + str(payment.id))
payment.user.id,
payment.id, transaction_to_roistat.delay(
f'School payment', payment.user.id,
payment.amount, payment.id,
payment.created_at.strftime('%Y-%m-%d %H:%M:%S'), f'School payment',
0, payment.amount,
f'school', payment.created_at.strftime('%Y-%m-%d %H:%M:%S'),
payment.roistat_visit, 0,
) f'school',
payment.roistat_visit,
)
if csv_file:
if not os.path.exists(csv_file):
raise CommandError('File not found')
content_types = dict(ContentType.objects.filter(app_label='payment').values_list('id', 'model'))
with open(csv_file, encoding='utf-8') as csv_file:
reader = csv.reader(csv_file, delimiter='\n')
ids = [p[0] for p in reader]
print('Payments in csv', len(ids))
payments = Payment.objects.exclude(id__in=ids)
print('Other payments count', payments.count())
for payment in payments:
content_type = content_types.get(payment.polymorphic_ctype_id, '')
product_type = content_type[:content_type.find('payment')]
if product_type == 'drawingcamp':
product_type = 'drawing_camp'
if product_type == 'giftcertificate':
product_type = 'gift_certificate'
event_name = '%s payment' % product_type.title()
transaction_to_roistat.delay(
payment.user.id,
payment.id,
event_name,
payment.amount,
payment.created_at.astimezone(utc).strftime('%Y-%m-%d %H:%M:%S'),
0,
product_type,
payment.roistat_visit,
)

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save