<commit_before>from django.db import models
from django.db.models import signals
from django.utils.translation import ugettext_lazy as _
from decimal import Decimal
from . import factory

PAYMENT_STATUS_CHOICES = (
    ('waiting', _(u'Waiting for confirmation')),
    ('confirmed', _(u'Confirmed')),
    ('rejected', _(u'Rejected')),
)
'''
List of possible payment statuses.
'''

class Payment(models.Model):
    '''
    Represents a single transaction. Each instance has one or more PaymentItem.
    '''
    variant = models.CharField(max_length=255)
    #: Transaction status
    status = models.CharField(max_length=10, choices=PAYMENT_STATUS_CHOICES, default='waiting')
    #: Creation date and time
    created = models.DateTimeField(auto_now_add=True)
    #: Date and time of last modification
    modified = models.DateTimeField(auto_now=True)
    #: Transaction ID (if applicable)
    transaction_id = models.CharField(max_length=255, blank=True)
    #: Currency code (may be provider-specific)
    currency = models.CharField(max_length=10)
    #: Total amount (gross)
    total = models.DecimalField(max_digits=9, decimal_places=2, default='0.0')

    def get_form(self):
        '''
        Returns a form for this transaction.
        '''
        provider = factory(self.variant)
        return provider.get_form(self)

    def get_provider(self):
        '''
        Returns a provider assigned to given payment.
        '''
        provider = factory(self.variant)
        return provider

    def add_item(self, *args, **kwargs):
        '''
        Creates and returns a PaymentItem.

        All arguments are passed directly to PaymentItem constructor.
        '''
        return self.items.create(*args, **kwargs)

    def set_customer_detail(self, key, value):
        try:
            detail = self.customer_details.get(key=key)
        except CustomerDetail.DoesNotExist:
            detail = CustomerDetail(payment=self, key=key)
        detail.value = value
        detail.save()

    def get_customer_detail(self, key):
        try:
            return self.customer_details.get(key=key).value
        except CustomerDetail.DoesNotExist:
            return ''

    def change_status(self, status):
        '''
        Updates the Payment status and sends the status_changed signal.
        '''
        from signals import status_changed
        self.status = status
        self.save()
        status_changed.send(sender=self)


class PaymentItem(models.Model):<commit_msg>Give class as sender, instance as instance<commit_after>from django.db import models
from django.db.models import signals
from django.utils.translation import ugettext_lazy as _
from decimal import Decimal
from . import factory

PAYMENT_STATUS_CHOICES = (
    ('waiting', _(u'Waiting for confirmation')),
    ('confirmed', _(u'Confirmed')),
    ('rejected', _(u'Rejected')),
)
'''
List of possible payment statuses.
'''

class Payment(models.Model):
    '''
    Represents a single transaction. Each instance has one or more PaymentItem.
    '''
    variant = models.CharField(max_length=255)
    #: Transaction status
    status = models.CharField(max_length=10, choices=PAYMENT_STATUS_CHOICES, default='waiting')
    #: Creation date and time
    created = models.DateTimeField(auto_now_add=True)
    #: Date and time of last modification
    modified = models.DateTimeField(auto_now=True)
    #: Transaction ID (if applicable)
    transaction_id = models.CharField(max_length=255, blank=True)
    #: Currency code (may be provider-specific)
    currency = models.CharField(max_length=10)
    #: Total amount (gross)
    total = models.DecimalField(max_digits=9, decimal_places=2, default='0.0')

    def get_form(self):
        '''
        Returns a form for this transaction.
        '''
        provider = factory(self.variant)
        return provider.get_form(self)

    def get_provider(self):
        '''
        Returns a provider assigned to given payment.
        '''
        provider = factory(self.variant)
        return provider

    def add_item(self, *args, **kwargs):
        '''
        Creates and returns a PaymentItem.

        All arguments are passed directly to PaymentItem constructor.
        '''
        return self.items.create(*args, **kwargs)

    def set_customer_detail(self, key, value):
        try:
            detail = self.customer_details.get(key=key)
        except CustomerDetail.DoesNotExist:
            detail = CustomerDetail(payment=self, key=key)
        detail.value = value
        detail.save()

    def get_customer_detail(self, key):
        try:
            return self.customer_details.get(key=key).value
        except CustomerDetail.DoesNotExist:
            return ''

    def change_status(self, status):
        '''
        Updates the Payment status and sends the status_changed signal.
        '''
        from signals import status_changed
        self.status = status
        self.save()
        status_changed.send(sender=type(self), instance=self)


class PaymentItem(models.Model):
{'fraction_non_alphanumeric': 0.09972244250594767, 'fraction_numerical': 0.0055511498810467885, 'mean_word_length': 2.904798761609907, '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': '3261244', 'n_tokens_mistral': 1425, 'n_tokens_neox': 1373, 'n_words': 368}