<commit_before>from django.contrib.contenttypes.models import ContentType
from django.db import models
from django.utils.translation import ugettext_lazy as _

from lily.accounts.models import Account
from django.conf import settings
from lily.tags.models import TaggedObjectMixin
from lily.utils.models.models import PhoneNumber, EmailAddress
from lily.utils.models.mixins import Common, DeletedMixin


def get_contact_picture_upload_path(instance, filename):
    return settings.CONTACT_PICTURE_UPLOAD_TO % {
        'tenant_id': instance.tenant_id,
        'contact_id': instance.pk,
        'filename': filename
    }


class Contact(Common, TaggedObjectMixin):
    """
    Contact model, this is a person's profile. Has an optional relation to an account through
    Function. Can be related to LilyUser.
    """
    MALE_GENDER, FEMALE_GENDER, UNKNOWN_GENDER = range(3)
    CONTACT_GENDER_CHOICES = (
        (MALE_GENDER, _('Male')),
        (FEMALE_GENDER, _('Female')),
        (UNKNOWN_GENDER, _('Unknown/Other')),
    )

    INACTIVE_STATUS, ACTIVE_STATUS = range(2)
    CONTACT_STATUS_CHOICES = (
        (INACTIVE_STATUS, _('Inactive')),
        (ACTIVE_STATUS, _('Active')),
    )

    FORMAL, INFORMAL = range(2)
    SALUTATION_CHOICES = (
        (FORMAL, _('Formal')),
        (INFORMAL, _('Informal')),
    )

    first_name = models.CharField(max_length=255, default='', blank=True)
    last_name = models.CharField(max_length=255, default='', blank=True)
    gender = models.IntegerField(choices=CONTACT_GENDER_CHOICES, default=UNKNOWN_GENDER)
    title = models.CharField(max_length=20, blank=True)
    status = models.IntegerField(choices=CONTACT_STATUS_CHOICES, default=ACTIVE_STATUS)
    picture = models.ImageField(upload_to=get_contact_picture_upload_path, blank=True)
    description = models.TextField(blank=True)
    salutation = models.IntegerField(choices=SALUTATION_CHOICES, default=INFORMAL)

    import_id = models.CharField(max_length=100, default='', blank=True, db_index=True)
    accounts = models.ManyToManyField(
        Account,
        through='Function',
        through_fields=('contact', 'account'),
        related_name='contacts',
    )

    @property
    def content_type(self):
        """
        Return the content type (Django model) for this model
        """
        return ContentType.objects.get(app_label='contacts', model='contact')

    @property
    def primary_email(self):
        """
        Will return the primary email address set to this contact if one exists.

        Returns:
            EmailAddress or empty string.
        """
        if not hasattr(self, '_primary_email'):
            self._primary_email = self.email_addresses.filter(status=EmailAddress.PRIMARY_STATUS).first()

            if not self._primary_email:
                self._primary_email = ''

        return self._primary_email

    @property
    def account_city(self):
        city = ''
        account = self.accounts.first()

        if account:
            city = account.main_address.city

        return city

    @property
    def work_phone(self):
        for phone in self.phone_numbers.all():
            if phone.type == 'work':
                return phone
        return None

    @property
    def mobile_phone(self):<commit_msg>Fix email template var breaking everything<commit_after>from django.contrib.contenttypes.models import ContentType
from django.db import models
from django.utils.translation import ugettext_lazy as _

from lily.accounts.models import Account
from django.conf import settings
from lily.tags.models import TaggedObjectMixin
from lily.utils.models.models import PhoneNumber, EmailAddress
from lily.utils.models.mixins import Common, DeletedMixin


def get_contact_picture_upload_path(instance, filename):
    return settings.CONTACT_PICTURE_UPLOAD_TO % {
        'tenant_id': instance.tenant_id,
        'contact_id': instance.pk,
        'filename': filename
    }


class Contact(Common, TaggedObjectMixin):
    """
    Contact model, this is a person's profile. Has an optional relation to an account through
    Function. Can be related to LilyUser.
    """
    MALE_GENDER, FEMALE_GENDER, UNKNOWN_GENDER = range(3)
    CONTACT_GENDER_CHOICES = (
        (MALE_GENDER, _('Male')),
        (FEMALE_GENDER, _('Female')),
        (UNKNOWN_GENDER, _('Unknown/Other')),
    )

    INACTIVE_STATUS, ACTIVE_STATUS = range(2)
    CONTACT_STATUS_CHOICES = (
        (INACTIVE_STATUS, _('Inactive')),
        (ACTIVE_STATUS, _('Active')),
    )

    FORMAL, INFORMAL = range(2)
    SALUTATION_CHOICES = (
        (FORMAL, _('Formal')),
        (INFORMAL, _('Informal')),
    )

    first_name = models.CharField(max_length=255, default='', blank=True)
    last_name = models.CharField(max_length=255, default='', blank=True)
    gender = models.IntegerField(choices=CONTACT_GENDER_CHOICES, default=UNKNOWN_GENDER)
    title = models.CharField(max_length=20, blank=True)
    status = models.IntegerField(choices=CONTACT_STATUS_CHOICES, default=ACTIVE_STATUS)
    picture = models.ImageField(upload_to=get_contact_picture_upload_path, blank=True)
    description = models.TextField(blank=True)
    salutation = models.IntegerField(choices=SALUTATION_CHOICES, default=INFORMAL)

    import_id = models.CharField(max_length=100, default='', blank=True, db_index=True)
    accounts = models.ManyToManyField(
        Account,
        through='Function',
        through_fields=('contact', 'account'),
        related_name='contacts',
    )

    @property
    def content_type(self):
        """
        Return the content type (Django model) for this model
        """
        return ContentType.objects.get(app_label='contacts', model='contact')

    @property
    def primary_email(self):
        """
        Will return the primary email address set to this contact if one exists.

        Returns:
            EmailAddress or empty string.
        """
        if not hasattr(self, '_primary_email'):
            self._primary_email = self.email_addresses.filter(status=EmailAddress.PRIMARY_STATUS).first()

            if not self._primary_email:
                self._primary_email = ''

        return self._primary_email

    @property
    def account_city(self):
        city = ''
        account = self.accounts.first()

        if account:
            city = account.addresses.first().city

        return city

    @property
    def work_phone(self):
        for phone in self.phone_numbers.all():
            if phone.type == 'work':
                return phone
        return None

    @property
    def mobile_phone(self):
{'fraction_non_alphanumeric': 0.1, 'fraction_numerical': 0.004236006051437216, 'mean_word_length': 3.1190031152647975, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '82147', 'n_tokens_mistral': 2035, 'n_tokens_neox': 1972, 'n_words': 426}