<commit_before>"""Payment forms"""

import logging

import stripe
from stripe.resource import Customer, Charge
from stripe.error import InvalidRequestError
from django import forms
from django.conf import settings
from django.utils.translation import ugettext_lazy as _

log = logging.getLogger(__name__)
stripe.api_key = getattr(settings, 'STRIPE_SECRET')


class StripeResourceMixin(object):

    """Stripe actions for resources, available as a Form mixin class"""

    def ensure_stripe_resource(self, resource, attrs):
        try:
            instance = resource.retrieve(attrs['id'])
            for (key, val) in attrs.items():
                setattr(instance, key, val)
            instance.save()
            return instance
        except (KeyError, InvalidRequestError):
            try:
                del attrs['id']
            except KeyError:
                pass
            try:
                return resource.create(**attrs)
            except InvalidRequestError:
                return None

    def get_customer_kwargs(self):
        raise NotImplementedError
<commit_msg>Clean up logic around invalid requests to stripe on object get/create<commit_after>"""Payment forms"""

import logging

import stripe
from stripe.resource import Customer, Charge
from stripe.error import InvalidRequestError
from django import forms
from django.conf import settings
from django.utils.translation import ugettext_lazy as _

log = logging.getLogger(__name__)
stripe.api_key = getattr(settings, 'STRIPE_SECRET')


class StripeResourceMixin(object):

    """Stripe actions for resources, available as a Form mixin class"""

    def ensure_stripe_resource(self, resource, attrs):
        try:
            instance = resource.retrieve(attrs['id'])
        except (KeyError, InvalidRequestError):
            try:
                del attrs['id']
            except KeyError:
                pass
            return resource.create(**attrs)
        else:
            for (key, val) in attrs.items():
                setattr(instance, key, val)
            instance.save()
            return instance

    def get_customer_kwargs(self):
        raise NotImplementedError

{'fraction_non_alphanumeric': 0.07724137931034483, 'fraction_numerical': 0.0, 'mean_word_length': 2.5789473684210527, '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': '3719072', 'n_tokens_mistral': 569, 'n_tokens_neox': 543, 'n_words': 172}