PageRenderTime 28ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/member/models.py

https://github.com/kd7lxl/memrec
Python | 149 lines | 142 code | 5 blank | 2 comment | 0 complexity | 9f9864ef90e8f85ef2bb48613df7fc25 MD5 | raw file
  1. from django.db import models
  2. from django.core.validators import RegexValidator
  3. from datetime import date
  4. import re
  5. phone_re = re.compile(r'^[\d]{10}$')
  6. validate_phone = RegexValidator(phone_re, (u"Enter a 10-digit phone number with no punctuation."), 'invalid')
  7. hostname_re = re.compile(r'^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$')
  8. validate_hostname = RegexValidator(hostname_re, (u"Enter a valid hostname."), 'invalid')
  9. class Person(models.Model):
  10. """
  11. Create a person:
  12. >>> person = Person(last_name='Smith', first_name='John')
  13. >>> person.save()
  14. >>> person
  15. <Person: John Smith>
  16. """
  17. last_name = models.CharField(max_length=30)
  18. first_name = models.CharField(max_length=30)
  19. dem_number = models.PositiveIntegerField(null=True, blank=True)
  20. dob = models.DateField(null=True,blank=True)
  21. join_date = models.DateField(null=True, blank=True)
  22. person_type = models.CharField(max_length=20, choices=(
  23. ('contact', 'contact'),
  24. ('recruit', 'recruit'),
  25. ('member', 'member'),
  26. ))
  27. emergency_contact_1 = models.ForeignKey('self', blank=True, null=True, related_name='emergency_contact1_for')
  28. emergency_contact_2 = models.ForeignKey('self', blank=True, null=True, related_name='emergency_contact2_for')
  29. date_added = models.DateField(auto_now_add=True)
  30. def __unicode__(self):
  31. return u'%s %s' % (self.first_name, self.last_name)
  32. def age(self, today=date.today()):
  33. born = self.dob
  34. try: # raised when birth date is February 29 and the current year is not a leap year
  35. birthday = born.replace(year=today.year)
  36. except:
  37. birthday = born.replace(year=today.year, day=born.day-1)
  38. if birthday > today:
  39. yearsold = today.year - born.year - 1
  40. else:
  41. yearsold = today.year - born.year
  42. if yearsold == 0:
  43. if birthday > today:
  44. monthsold = today.month - born.month - 1
  45. else:
  46. monthsold = today.month - born.month
  47. return '%s mo' % (monthsold)
  48. elif yearsold == 1:
  49. return '%s yr' % (yearsold)
  50. else:
  51. return '%s yrs' % (yearsold)
  52. def time_in_unit(self, today=date.today()):
  53. born = self.join_date
  54. try: # raised when birth date is February 29 and the current year is not a leap year
  55. birthday = born.replace(year=today.year)
  56. except:
  57. birthday = born.replace(year=today.year, day=born.day-1)
  58. if birthday > today:
  59. yearsold = today.year - born.year - 1
  60. else:
  61. yearsold = today.year - born.year
  62. if yearsold == 0:
  63. if birthday > today:
  64. monthsold = today.month - born.month - 1
  65. else:
  66. monthsold = today.month - born.month
  67. return '%s mo' % (monthsold)
  68. elif yearsold == 1:
  69. return '%s yr' % (yearsold)
  70. else:
  71. return '%s yrs' % (yearsold)
  72. class Meta:
  73. ordering = ['last_name', 'first_name']
  74. class MembershipFeePayment(models.Model):
  75. person = models.ForeignKey(Person)
  76. payment_date = models.DateField()
  77. payment_amount = models.DecimalField(max_digits=5, decimal_places=2)
  78. def __unicode__(self):
  79. return u'%s paid $%02.f on %s' % (self.person, self.payment_amount, self.payment_date)
  80. class EmailAddress(models.Model):
  81. person = models.ForeignKey(Person)
  82. email_address = models.EmailField()
  83. def __unicode__(self):
  84. return u'%s' % (self.email_address)
  85. class Meta:
  86. verbose_name_plural = 'email addresses'
  87. class Address(models.Model):
  88. person = models.ForeignKey(Person)
  89. address1 = models.CharField(max_length=60)
  90. address2 = models.CharField(max_length=60, null=True, blank=True)
  91. city = models.CharField(max_length=30)
  92. state = models.CharField(max_length=2)
  93. postal_code = models.CharField(max_length=9)
  94. class Meta:
  95. verbose_name_plural = 'addresses'
  96. class ServiceProvider(models.Model):
  97. name = models.CharField(max_length=30)
  98. sms_email_hostname = models.CharField(max_length=60, validators=[validate_hostname])
  99. def __unicode__(self):
  100. return u'%s' % (self.name)
  101. class Meta:
  102. ordering = ['name']
  103. class Phone(models.Model):
  104. person = models.ForeignKey(Person)
  105. phone_number = models.CharField(max_length=10, validators=[validate_phone], help_text='Numbers only please, no punctuation.')
  106. phone_type = models.CharField(max_length=10, null=True, blank=True, choices=(
  107. ('home', 'home'),
  108. ('work', 'work'),
  109. ('mobile', 'mobile'),
  110. ('pager', 'pager'),
  111. ('fax', 'fax'),
  112. ))
  113. service_provider = models.ForeignKey(ServiceProvider, null=True, blank=True)
  114. sms_enabled = models.BooleanField(default=False)
  115. def __unicode__(self):
  116. return u'(%s) %s-%s' % (
  117. self.phone_number[0:3],
  118. self.phone_number[3:6],
  119. self.phone_number[6:10],
  120. )
  121. def sms_email_address(self):
  122. if self.service_provider is not None \
  123. and self.sms_enabled is True:
  124. return '%s@%s' % (self.phone_number, self.service_provider.sms_email_hostname)
  125. class Meta:
  126. verbose_name = 'phone number'