django-orchestra/orchestra/contrib/contacts/models.py

81 lines
3.2 KiB
Python
Raw Normal View History

2014-10-30 16:34:02 +00:00
from django.core.exceptions import ValidationError
from django.core.validators import RegexValidator
2014-05-08 16:59:35 +00:00
from django.db import models
from django.utils.translation import ugettext_lazy as _
from orchestra.core import validators
2014-05-08 16:59:35 +00:00
from orchestra.models.fields import MultiSelectField
from . import settings
2015-04-08 14:41:09 +00:00
from .validators import validate_phone
2014-10-30 16:34:02 +00:00
2014-09-04 15:55:43 +00:00
class ContactQuerySet(models.QuerySet):
def filter(self, *args, **kwargs):
usages = kwargs.pop('email_usages', [])
qs = models.Q()
for usage in usages:
qs = qs | models.Q(email_usage__regex=r'.*(^|,)+%s($|,)+.*' % usage)
return super(ContactQuerySet, self).filter(qs, *args, **kwargs)
2014-05-08 16:59:35 +00:00
class Contact(models.Model):
2014-09-04 15:55:43 +00:00
BILLING = 'BILLING'
EMAIL_USAGES = (
('SUPPORT', _("Support tickets")),
('ADMIN', _("Administrative")),
(BILLING, _("Billing")),
('TECH', _("Technical")),
('ADDS', _("Announcements")),
('EMERGENCY', _("Emergency contact")),
)
objects = ContactQuerySet.as_manager()
2014-05-08 16:59:35 +00:00
account = models.ForeignKey('accounts.Account', verbose_name=_("Account"),
2015-04-05 10:46:24 +00:00
related_name='contacts', null=True)
2014-05-08 16:59:35 +00:00
short_name = models.CharField(_("short name"), max_length=128)
full_name = models.CharField(_("full name"), max_length=256, blank=True)
email = models.EmailField()
email_usage = MultiSelectField(_("email usage"), max_length=256, blank=True,
2015-04-05 10:46:24 +00:00
choices=EMAIL_USAGES,
default=settings.CONTACTS_DEFAULT_EMAIL_USAGES)
2014-10-30 16:34:02 +00:00
phone = models.CharField(_("phone"), max_length=32, blank=True,
2015-04-05 10:46:24 +00:00
validators=[validate_phone])
2014-10-30 16:34:02 +00:00
phone2 = models.CharField(_("alternative phone"), max_length=32, blank=True,
2015-04-05 10:46:24 +00:00
validators=[validate_phone])
2014-05-08 16:59:35 +00:00
address = models.TextField(_("address"), blank=True)
2014-10-30 16:34:02 +00:00
city = models.CharField(_("city"), max_length=128, blank=True)
zipcode = models.CharField(_("zip code"), max_length=10, blank=True,
2015-04-05 10:46:24 +00:00
validators=[
RegexValidator(r'^[0-9,A-Z]{3,10}$',
_("Enter a valid zipcode."), 'invalid')
])
2014-05-08 16:59:35 +00:00
country = models.CharField(_("country"), max_length=20, blank=True,
2015-04-05 10:46:24 +00:00
choices=settings.CONTACTS_COUNTRIES,
default=settings.CONTACTS_DEFAULT_COUNTRY)
2014-05-08 16:59:35 +00:00
2015-04-02 16:14:55 +00:00
def __str__(self):
2014-11-17 14:17:33 +00:00
return self.full_name or self.short_name
def clean(self):
self.short_name = self.short_name.strip()
self.full_name = self.full_name.strip()
self.phone = self.phone.strip()
self.phone2 = self.phone2.strip()
self.address = self.address.strip()
self.city = self.city.strip()
self.country = self.country.strip()
2014-11-05 20:22:01 +00:00
errors = {}
2014-10-30 16:34:02 +00:00
if self.address and not (self.city and self.zipcode and self.country):
2014-11-05 20:22:01 +00:00
errors['__all__'] = _("City, zipcode and country must be provided when address is provided.")
2014-10-30 16:34:02 +00:00
if self.zipcode and not self.country:
2014-11-05 20:22:01 +00:00
errors['country'] = _("Country must be provided when zipcode is provided.")
2014-10-30 16:34:02 +00:00
elif self.zipcode and self.country:
2014-11-05 20:22:01 +00:00
try:
validators.validate_zipcode(self.zipcode, self.country)
2015-04-01 15:49:21 +00:00
except ValidationError as error:
2014-11-05 20:22:01 +00:00
errors['zipcode'] = error
if errors:
raise ValidationError(errors)