<commit_before>"""Update our cached data using stripe data."""

from django.core.management.base import BaseCommand

from pinax.stripe.actions import charges
from pinax.stripe.actions import customers
from pinax.stripe.actions import invoices
from pinax.stripe.actions import plans
from pinax.stripe.models import Customer
from pinax.stripe.models import Plan

import stripe


class StripeIterator(object):
    """Iterator over Stripe collection with extra context."""

    def __init__(self, cls):
        """Fetch total and setup iterator."""
        self.count = 0
        collection = cls.list(
            include=['total_count'],
            limit=1
        )
        self.total = collection.total_count
        self.iterator = cls.list(
            limit=100
        ).auto_paging_iter()

    def __iter__(self):
        """Self is the iterator."""
        return self

    def next(self):
        """Return internal iterator and extra context."""
        self.count += 1
        return (
            self.iterator.next(),
            self.count,
            self.total,
            int(round(100 * (float(self.count) / float(self.total))))
        )


def _sync_customers():
    created = 0
    synced = 0
    for obj, count, total, perc in StripeIterator(stripe.Customer):
        try:
            customer_obj = Customer.objects.select_related(
                'user'
            ).get(stripe_id=obj.id)
        except Customer.DoesNotExist:
            customer_obj = Customer.objects.create(
                user=None,
                stripe_id=obj.id
            )
            created += 1
        try:
            customers.sync_customer(
                customer_obj, obj
            )
        except Plan.DoesNotExist:
            print 'Plan subscribed to by customer {} not found, skipping customer.'.format(
                customer_obj.stripe_id
            )

        synced += 1
        username = getattr(
            customer_obj.user,
            customer_obj.user.USERNAME_FIELD
        ) if customer_obj.user_id else 'Unlinked'
        print("[{0}/{1} {2}%] Syncing customer {3} [{4}]".format(
            count, total, perc, username, customer_obj.user_id
        ))
    return created, synced


def _sync_charges():
    synced = 0
    skipped = 0
    for obj, count, total, perc in StripeIterator(stripe.Charge):
        try:
            charges.sync_charge_from_stripe_data(obj)<commit_msg>Include balance_transaction info when syncing charges<commit_after>"""Update our cached data using stripe data."""

from django.core.management.base import BaseCommand

from pinax.stripe.actions import charges
from pinax.stripe.actions import customers
from pinax.stripe.actions import invoices
from pinax.stripe.actions import plans
from pinax.stripe.models import Customer
from pinax.stripe.models import Plan

import stripe


class StripeIterator(object):
    """Iterator over Stripe collection with extra context."""

    def __init__(self, cls, **kwargs):
        """Fetch total and setup iterator."""
        self.count = 0
        collection = cls.list(
            include=['total_count'],
            limit=1,
        )
        self.total = collection.total_count
        self.iterator = cls.list(
            limit=100,
            **kwargs
        ).auto_paging_iter()

    def __iter__(self):
        """Self is the iterator."""
        return self

    def next(self):
        """Return internal iterator and extra context."""
        self.count += 1
        return (
            self.iterator.next(),
            self.count,
            self.total,
            int(round(100 * (float(self.count) / float(self.total))))
        )


def _sync_customers():
    created = 0
    synced = 0
    for obj, count, total, perc in StripeIterator(stripe.Customer):
        try:
            customer_obj = Customer.objects.select_related(
                'user'
            ).get(stripe_id=obj.id)
        except Customer.DoesNotExist:
            customer_obj = Customer.objects.create(
                user=None,
                stripe_id=obj.id
            )
            created += 1
        try:
            customers.sync_customer(
                customer_obj, obj
            )
        except Plan.DoesNotExist:
            print 'Plan subscribed to by customer {} not found, skipping customer.'.format(
                customer_obj.stripe_id
            )

        synced += 1
        username = getattr(
            customer_obj.user,
            customer_obj.user.USERNAME_FIELD
        ) if customer_obj.user_id else 'Unlinked'
        print("[{0}/{1} {2}%] Syncing customer {3} [{4}]".format(
            count, total, perc, username, customer_obj.user_id
        ))
    return created, synced


def _sync_charges():
    synced = 0
    skipped = 0
    for obj, count, total, perc in StripeIterator(stripe.Charge, expand=['data.balance_transaction']):
        try:
            charges.sync_charge_from_stripe_data(obj)
{'fraction_non_alphanumeric': 0.09616161616161616, 'fraction_numerical': 0.00808080808080808, 'mean_word_length': 1.9933494558645708, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6882732', 'n_tokens_mistral': 1414, 'n_tokens_neox': 1348, 'n_words': 353}