Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

abstract_models.py 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. from decimal import Decimal
  2. from django.contrib.auth.models import User
  3. from django.db import models
  4. from django.utils.translation import ugettext as _
  5. class AbstractSource(models.Model):
  6. u"""
  7. A source of payment for an order.
  8. This is normally a credit card which has been pre-authed for the order
  9. amount, but some applications will allow orders to be paid for using
  10. multiple sources such as cheque, credit accounts, gift cards. Each payment
  11. source will have its own entry.
  12. """
  13. order = models.ForeignKey('order.Order', related_name='sources')
  14. type = models.ForeignKey('payment.SourceType')
  15. allocation = models.DecimalField(decimal_places=2, max_digits=12)
  16. amount_debited = models.DecimalField(decimal_places=2, max_digits=12, default=Decimal('0.00'))
  17. reference = models.CharField(max_length=128, blank=True, null=True)
  18. class Meta:
  19. abstract = True
  20. def __unicode__(self):
  21. description = "Allocation of %.2f from type %s" % (self.allocation, self.type)
  22. if self.reference:
  23. description += " (reference: %s)" % self.reference
  24. return description
  25. class AbstractSourceType(models.Model):
  26. u"""
  27. A type of payment source.
  28. This could be an external partner like PayPal or DataCash,
  29. or an internal source such as a managed account.i
  30. """
  31. name = models.CharField(max_length=128)
  32. code = models.SlugField(max_length=128, help_text="""This is used within
  33. forms to identify this source type""")
  34. class Meta:
  35. abstract = True
  36. def __unicode__(self):
  37. return self.name
  38. def save(self, *args, **kwargs):
  39. if not self.code:
  40. self.code = slugify(self.name)
  41. super(AbstractSourceType, self).save(*args, **kwargs)
  42. class AbstractTransaction(models.Model):
  43. u"""
  44. A transaction for payment sources which need a secondary 'transaction' to actually take the money
  45. This applies mainly to credit card sources which can be a pre-auth for the money. A 'complete'
  46. needs to be run later to debit the money from the account.
  47. """
  48. source = models.ForeignKey('payment.Source', related_name='transactions')
  49. type = models.CharField(max_length=128, blank=True)
  50. delta_amount = models.FloatField()
  51. reference = models.CharField(max_length=128)
  52. date_created = models.DateField()
  53. class Meta:
  54. abstract = True
  55. def __unicode__(self):
  56. return "Transaction of %.2f" % self.delta_amount
  57. class AbstractBankcard(models.Model):
  58. user = models.ForeignKey('auth.User', related_name='bankcards')
  59. card_type = models.CharField(max_length=128)
  60. name = models.CharField(max_length=255)
  61. number = models.CharField(max_length=32)
  62. expiry_date = models.DateField()
  63. # For payment partners who are storing the full card details for us
  64. partner_reference = models.CharField(max_length=255, null=True, blank=True)
  65. class Meta:
  66. abstract = True
  67. def save(self, *args, **kwargs):
  68. self.number = self._get_obfuscated_number()
  69. super(AbstractBankcard, self).save(*args, **kwargs)
  70. def _get_obfuscated_number(self):
  71. return "XXXX-XXXX-XXXX-%s" % self.number[-4:]