Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

abstract_models.py 3.2KB

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