Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
wbcrm / crm / models / contacts.py
Size: Mime:
import logging
import sys
from enum import Enum

from django.contrib.postgres.fields import ArrayField, JSONField
from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models
from django.db.models import Q
from django.db.models.signals import (m2m_changed, post_delete, post_save,
                                      pre_save)
from django.dispatch import receiver
from django_fsm import FSMField, transition
from django_fsm_log.decorators import fsm_log_by, fsm_log_description
from dynamic_preferences.registries import global_preferences_registry
from slugify import slugify


class ContactLocation(models.Model):
    location = models.CharField(max_length=255)

    class Meta:
        verbose_name = 'Contact Location'
        verbose_name_plural = 'Contact Locations'

    def __str__(self):
        return self.location


class ContactTelephoneType(models.Model):
    telephone_type = models.CharField(max_length=255)

    class Meta:
        verbose_name = 'Contact Telephone Type'
        verbose_name_plural = 'Contact Telephone Types'

    def __str__(self):
        return self.telephone_type


class ContactCountry(models.Model):
    country_name = models.CharField(max_length=255)
    country_code = models.CharField(max_length=3)

    class Meta:
        verbose_name = 'Contact Country'
        verbose_name_plural = 'Contact Countries'

    def __str__(self):
        return '%s' % (self.country_name,)


class PrimaryContactMixin(object):

    def save(self, *args, **kwargs):
        if self.primary:
            related_name = self._meta.get_field('entry').remote_field.related_name
            contacts = getattr(self.entry, related_name).filter(primary=True)
            if self.id:
                contacts.exclude(id=self.id)
            contacts.update(primary=False)
        return super().save(*args, **kwargs)


class BankingContact(PrimaryContactMixin, models.Model):
    primary = models.BooleanField(default=False)
    location = models.ForeignKey('ContactLocation', related_name='banking', on_delete=models.PROTECT)
    entry = models.ForeignKey('Entry', related_name='banking', on_delete=models.CASCADE)
    institute = models.CharField(max_length=255)
    institute_additional = models.CharField(max_length=255, blank=True, null=True)
    iban = models.CharField(max_length=34)
    swift_bic = models.CharField(max_length=11, blank=True, null=True)

    # currency = models.ForeignKey('investment.Currency', null=True, blank=True, on_delete=models.SET_NULL) #TODO: This has to be changed maybe...
    currency = models.CharField(max_length=255, null=True, blank=True)
    
    def __str__(self):
        return '%s, %s' % (self.institute, self.iban)

    class Meta:
        verbose_name = 'Banking Contact'
        verbose_name_plural = 'Banking Contacts'


class AddressContact(PrimaryContactMixin, models.Model):
    primary = models.BooleanField(default=False)
    location = models.ForeignKey('ContactLocation', related_name='addresses', on_delete=models.PROTECT)
    entry = models.ForeignKey('Entry', related_name='addresses', on_delete=models.CASCADE)
    street = models.CharField(max_length=255, null=True, blank=True)
    street_additional = models.CharField(max_length=255, null=True, blank=True)
    zip = models.CharField(max_length=255, null=True, blank=True)
    city = models.CharField(max_length=255)
    province = models.CharField(max_length=255, blank=True, null=True)
    country = models.ForeignKey('ContactCountry', related_name='address_contacts', on_delete=models.PROTECT)

    def __str__(self):
        return '%s, %s %s, %s' % (self.street, self.zip, self.city, self.country)

    class Meta:
        verbose_name = 'Address Contact'
        verbose_name_plural = 'Address Contacts'


class TelephoneContact(PrimaryContactMixin, models.Model):
    primary = models.BooleanField(default=False)
    location = models.ForeignKey('ContactLocation', related_name='telephones', on_delete=models.PROTECT)
    entry = models.ForeignKey('Entry', related_name='telephones', on_delete=models.CASCADE)
    number = models.CharField(max_length=255)
    telephone_type = models.ForeignKey('ContactTelephoneType', related_name='telephone_contacts', on_delete=models.PROTECT)

    def __str__(self):
        return self.number

    class Meta:
        verbose_name = 'Telephone Contact'
        verbose_name_plural = 'Telephone Contacts'


class EmailContact(PrimaryContactMixin, models.Model):
    primary = models.BooleanField(default=False)
    location = models.ForeignKey('ContactLocation', related_name='emails', on_delete=models.PROTECT)
    entry = models.ForeignKey('Entry', related_name='emails', on_delete=models.CASCADE)
    address = models.EmailField()

    def __str__(self):
        return self.address

    class Meta:
        verbose_name = 'E-Mail Contact'
        verbose_name_plural = 'E-Mail Contacts'


class WebsiteContact(PrimaryContactMixin, models.Model):
    primary = models.BooleanField(default=False)
    location = models.ForeignKey('ContactLocation', related_name='websites', on_delete=models.PROTECT)
    entry = models.ForeignKey('Entry', related_name='websites', on_delete=models.CASCADE)
    url = models.URLField()

    def __str__(self):
        return self.url

    class Meta:
        verbose_name = 'Website Contact'
        verbose_name_plural = 'Website Contacts'