society-self-service/ssmembership/models.py

84 lines
2.5 KiB
Python
Raw Normal View History

2019-01-17 21:32:22 +11:00
# Society Self-Service
# Copyright © 2018-2019 Yingtong Li (RunasSudo)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from django.db import models
2019-01-17 23:24:49 +11:00
import re
2019-01-17 21:32:22 +11:00
class Member(models.Model):
email = models.CharField(max_length=100)
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
student_id = models.CharField(max_length=8)
is_msa = models.BooleanField()
2019-01-17 23:24:49 +11:00
YEARS = (
(0, 'Year A'),
(1, 'Year 1'),
(2, 'Year 2'),
(3, 'Year 3B'),
(4, 'Year 4C'),
(5, 'Year 5D'),
(97, 'BMedSc(Hons)'),
(98, 'PhD'),
(99, 'Intermission'),
2019-01-17 21:32:22 +11:00
)
2019-01-17 23:24:49 +11:00
year = models.IntegerField(choices=YEARS)
2019-01-17 21:32:22 +11:00
phone = models.CharField(max_length=20)
2019-06-19 16:04:00 +10:00
MEMBER_TYPES = (
(1, 'Ordinary Member'),
(2, 'Associate Member'),
(3, 'Honorary Life Member'),
)
member_type = models.IntegerField(choices=MEMBER_TYPES)
2019-01-17 21:32:22 +11:00
expires = models.DateField()
2019-01-17 23:24:49 +11:00
2019-03-05 20:17:14 +11:00
class Meta:
ordering = ['id']
2019-01-28 19:54:39 +11:00
def __str__(self):
return self.first_name + ' ' + self.last_name
2019-01-17 23:24:49 +11:00
def validation_problems(self):
result = []
if not self.email.strip():
result.append('Email is required')
if not self.first_name.strip():
result.append('First name is required')
if not self.last_name.strip():
result.append('Last name is required')
if not self.student_id.strip():
result.append('Student ID is required')
if not any(x[0] == self.year for x in Member.YEARS):
result.append('A valid year is required')
if not self.phone.strip():
result.append('Phone number is required')
if not self.email.strip().endswith('@student.monash.edu'):
result.append('Email must be a Monash student email address')
if not re.match(r'^[0-9]{8}$', self.student_id.strip()):
result.append('Student ID must be an 8 digit number')
if not re.match(r'^[0-9+ ]+$', self.phone.strip()):
result.append('Phone number must contain only numbers and +')
return result