PageRenderTime 786ms CodeModel.GetById 44ms RepoModel.GetById 1ms app.codeStats 0ms

/myewb/apps/creditcard/models.py

https://github.com/divad12/myewb2
Python | 107 lines | 90 code | 6 blank | 11 comment | 1 complexity | 73528c62f4c531534d37de124c24624d MD5 | raw file
  1. """myEWB credit card models
  2. Models to store credit card transactions.
  3. This file is part of myEWB
  4. Copyright 2009 Engineers Without Borders (Canada) Organisation and/or volunteer contributors
  5. Created on: 2009-08-11
  6. Last modified: 2009-08-12
  7. @author: Francis Kung, Ben Best
  8. """
  9. import re, pycountry
  10. from django.db import models
  11. from django import forms
  12. from django.conf import settings
  13. from django.contrib.auth.models import User
  14. from django.db.models.signals import post_save, pre_save
  15. from django.utils.translation import ugettext_lazy as _
  16. from emailconfirmation.models import EmailAddress
  17. from pinax.apps.profiles.models import Profile, create_profile
  18. from networks.models import Network
  19. from datetime import date
  20. """ Thanks to http://www.djangosnippets.org/snippets/176/ """
  21. class CurrencyField (forms.RegexField):
  22. currencyRe = re.compile(r'^[0-9]{1,5}(.[0-9][0-9])?$')
  23. def __init__(self, *args, **kwargs):
  24. super(CurrencyField, self).__init__(
  25. self.currencyRe, None, None, *args, **kwargs)
  26. def clean(self, value):
  27. value = super(CurrencyField, self).clean(value)
  28. return float(value)
  29. class CurrencyInput (forms.TextInput):
  30. def render(self, name, value, attrs=None):
  31. if value != '':
  32. try:
  33. value = u"%.2f" % value
  34. except TypeError:
  35. pass
  36. return super(CurrencyInput, self).render(name, value, attrs)
  37. CC_TYPES = (
  38. ('VI', _('Visa')),
  39. ('MC', _('Mastercard')),
  40. ('AM', _('American Express')),
  41. # ('DS', _('Company')),
  42. )
  43. provinces = []
  44. provincelist = list(pycountry.subdivisions.get(country_code='CA'))
  45. for p in provincelist:
  46. provinces.append((p.code.split('-')[1], p.name))
  47. provinces = sorted(provinces)
  48. provinces2 = []
  49. provincelist = pycountry.subdivisions.get(country_code='US')
  50. for p in provincelist:
  51. provinces2.append((p.code.split('-')[1], p.name))
  52. provinces2 = sorted(provinces2)
  53. provinces += provinces2
  54. provinces.append(('', 'None'))
  55. countries2 = list(pycountry.countries)
  56. countries = []
  57. for c in countries2:
  58. countries.append((c.alpha2, c.name))
  59. class Payment(models.Model):
  60. """ Provides a base for credit card payments, including billing
  61. information and such.
  62. Most uses will probably extend this to add additional fields.
  63. """
  64. # TODO: Would be nice to pre-populate these from your profile...
  65. # maybe can do it via a nice javascript UI thing?
  66. cc_type = models.CharField(_('credit card type'), max_length=2, choices=CC_TYPES)
  67. cc_number = models.CharField(_('credit card number'), max_length=20)
  68. cc_expiry = models.DateField(_('expiry date'))
  69. # FIXME: so these max_length values are completely arbitrary...
  70. billing_name = models.CharField(_('billing name'), max_length=45)
  71. billing_address1 = models.CharField(_('billing address'), max_length=45)
  72. billing_address2 = models.CharField(_('billing address 2'), max_length=45, blank=True)
  73. billing_city = models.CharField(_('billing city'), max_length=45)
  74. billing_postalcode = models.CharField(_('billing postal code'), max_length=45)
  75. billing_province = models.CharField(_('billing province'), max_length=2, choices=provinces, default='AB')
  76. billing_country = models.CharField(_('billing country'), max_length=2, choices=countries, default='CA')
  77. phone = models.CharField(_('phone number'), max_length=45)
  78. email = models.EmailField(_('email address'))
  79. # well, really, many-to-one at the moment.
  80. products = models.ManyToManyField('Product')
  81. class Product(models.Model):
  82. """ Items / products that you can pay for by credit card
  83. """
  84. name = models.CharField(_('product_name'), max_length=45)
  85. sku = models.CharField(_('sku'), max_length=45)
  86. # amount = CurrencyField(_('amount'), widget=CurrencyInput)
  87. # FIXME: why doesn't CurrencyField work?
  88. amount = models.CharField(_('amount'), max_length=20)