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