<commit_before># -*- coding: utf-8 -*-
"""
.. module:: djstripe.stripe_objects
   :synopsis: dj-stripe - Abstract model definitions to provide our view of Stripe's objects

.. moduleauthor:: <NAME> (@wahuneke)

This module is an effort to isolate (as much as possible) the API dependent code in one
place. Primarily this is:

1) create models containing the fields that we care about, mapping to Stripe's fields
2) create methods for consistently syncing our database with Stripe's version of the objects
3) centralized methods for creating new database records to match incoming Stripe objects

This module defines abstract models which are then extended in models.py to provide the remaining
dj-stripe functionality.
"""


import decimal

from django.conf import settings
from django.db import models
from django.utils.encoding import python_2_unicode_compatible, smart_text

from model_utils.models import TimeStampedModel

from .context_managers import stripe_temporary_api_version
from .fields import (StripeFieldMixin, StripeCharField, StripeDateTimeField, StripeCurrencyField,
                     StripeIntegerField, StripeTextField, StripePositiveIntegerField, StripeIdField,
                     StripeBooleanField, StripeNullBooleanField, StripeJSONField)
from .managers import StripeObjectManager


import stripe
stripe.api_version = getattr(settings, "STRIPE_API_VERSION", "2013-02-11")


# ============================================================================ #
#                           Stripe Object Base                                 #
# ============================================================================ #


@python_2_unicode_compatible
class StripeObject(TimeStampedModel):
    # This must be defined in descendants of this model/mixin
    # e.g. "Event", "Charge", "Customer", etc.
    stripe_api_name = None
    objects = models.Manager()
    stripe_objects = StripeObjectManager()

    stripe_id = StripeIdField(unique=True, stripe_name='id')
    livemode = StripeNullBooleanField(default=False, null=True,
                                      help_text="Null here indicates that the livemode status is unknown "
                                                "or was previously unrecorded. Otherwise, this field indicates "
                                                "whether this record comes from Stripe test mode or live "
                                                "mode operation.")

    class Meta:
        abstract = True

    @classmethod
    def api(cls):
        """
        Get the api object for this type of stripe object (requires
        stripe_api_name attribute to be set on model).
        """
        if cls.stripe_api_name is None:
            raise NotImplementedError("StripeObject descendants are required to define "
                                      "the stripe_api_name attribute")
        # e.g. stripe.Event, stripe.Charge, etc
        return getattr(stripe, cls.stripe_api_name)

    def api_retrieve(self, expand=None):
        """
        Implement very commonly used API function 'retrieve'
        """
        # Run stripe.X.retreive(id)
        return type(self).api().retrieve(id=self.stripe_id, api_key=settings.STRIPE_SECRET_KEY, expand=expand)

    @classmethod
    def api_create(cls, **kwargs):
        """
        Call the stripe API's create operation for this model<commit_msg>livemode isn't on Account, Card, or BankAccount models. Setting to unrequired<commit_after># -*- coding: utf-8 -*-
"""
.. module:: djstripe.stripe_objects
   :synopsis: dj-stripe - Abstract model definitions to provide our view of Stripe's objects

.. moduleauthor:: <NAME> (@wahuneke)

This module is an effort to isolate (as much as possible) the API dependent code in one
place. Primarily this is:

1) create models containing the fields that we care about, mapping to Stripe's fields
2) create methods for consistently syncing our database with Stripe's version of the objects
3) centralized methods for creating new database records to match incoming Stripe objects

This module defines abstract models which are then extended in models.py to provide the remaining
dj-stripe functionality.
"""


import decimal

from django.conf import settings
from django.db import models
from django.utils.encoding import python_2_unicode_compatible, smart_text

from model_utils.models import TimeStampedModel

from .context_managers import stripe_temporary_api_version
from .fields import (StripeFieldMixin, StripeCharField, StripeDateTimeField, StripeCurrencyField,
                     StripeIntegerField, StripeTextField, StripePositiveIntegerField, StripeIdField,
                     StripeBooleanField, StripeNullBooleanField, StripeJSONField)
from .managers import StripeObjectManager


import stripe
stripe.api_version = getattr(settings, "STRIPE_API_VERSION", "2013-02-11")


# ============================================================================ #
#                           Stripe Object Base                                 #
# ============================================================================ #


@python_2_unicode_compatible
class StripeObject(TimeStampedModel):
    # This must be defined in descendants of this model/mixin
    # e.g. "Event", "Charge", "Customer", etc.
    stripe_api_name = None
    objects = models.Manager()
    stripe_objects = StripeObjectManager()

    stripe_id = StripeIdField(unique=True, stripe_name='id')
    livemode = StripeNullBooleanField(default=False, null=True, stripe_required=False,
                                      help_text="Null here indicates that the livemode status is unknown "
                                                "or was previously unrecorded. Otherwise, this field indicates "
                                                "whether this record comes from Stripe test mode or live "
                                                "mode operation.")

    class Meta:
        abstract = True

    @classmethod
    def api(cls):
        """
        Get the api object for this type of stripe object (requires
        stripe_api_name attribute to be set on model).
        """
        if cls.stripe_api_name is None:
            raise NotImplementedError("StripeObject descendants are required to define "
                                      "the stripe_api_name attribute")
        # e.g. stripe.Event, stripe.Charge, etc
        return getattr(stripe, cls.stripe_api_name)

    def api_retrieve(self, expand=None):
        """
        Implement very commonly used API function 'retrieve'
        """
        # Run stripe.X.retreive(id)
        return type(self).api().retrieve(id=self.stripe_id, api_key=settings.STRIPE_SECRET_KEY, expand=expand)

    @classmethod
    def api_create(cls, **kwargs):
        """
        Call the stripe API's create operation for this model
{'fraction_non_alphanumeric': 0.10982912224331824, 'fraction_numerical': 0.004089382211187382, 'mean_word_length': 2.819297267150028, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4594870', 'n_tokens_mistral': 1802, 'n_tokens_neox': 1663, 'n_words': 574}