Source

ultrasound / django_ultrasound / customers / models.py

from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import User
from common.customfields import ColorField


class HtmlPage(models.Model):
    title = models.CharField(_('title'), max_length=200)
    content = models.TextField(_('content'), blank=True)

    def __unicode__(self):
        return self.title

REQUESTING_TO_BE_CANDIDATE = 1
CANDIDATE = 2
CANDIDATE_WITH_DETAILS = 3
CUSTOMER = 4
DISTINGUISHED_CUSTOMER = 5
TEMPORARILY_POSTPONED = 6
POSTPONED = 7

CUSTOMER_STATUS_CHOICES = ((REQUESTING_TO_BE_CANDIDATE, _("Requesting to be candidate")),
                           (CANDIDATE, _("Candidate")),
                           (CANDIDATE_WITH_DETAILS, _("Candidate with details")),
                           (CUSTOMER, _("Customer")),
                           (DISTINGUISHED_CUSTOMER, _("Distinguished customer")),
                           (TEMPORARILY_POSTPONED, _("Temporarily postponed")),
                           (POSTPONED, _("Postponed")),
)

class CustomerStatus(models.Model):
    name = models.CharField(_("type"), max_length=100)
    code = models.IntegerField(_("Customer status code"), choices=CUSTOMER_STATUS_CHOICES)
    html_page = models.ForeignKey(HtmlPage, verbose_name=_("HTML page"))
    color = ColorField()

    class Meta:
        verbose_name=_("Customer Status")
        verbose_name_plural= _("statuses of Customers")

    def __unicode__(self):
        return u"%s-%d" % (self.name, self.code)

class CustomerAddress(models.Model):
    name = models.CharField(_("home Address"), max_length=100)

    def __unicode__(self):
        return self.name

class CustomerEduInstitute(models.Model):
    name = models.CharField(_("Education institute"), max_length=100)

    def __unicode__(self):
        return self.name

class Customer(models.Model):
    fb_key = models.CharField(_("FaceBook key"), max_length=20, unique=True)
    user = models.ForeignKey(User, verbose_name=_("user"), related_name="customer")
    id_number = models.PositiveIntegerField(_("ID number"), unique=True, help_text=_("Enter ID number"), null=True,
                                            blank=True)
    link_to_page = models.URLField(_("Link to FaceBook page"), verify_exists=False, max_length=500, null=True, blank=True)
    customer_status = models.ForeignKey(CustomerStatus, verbose_name=_("Status"), related_name="customers")
    check_data = models.BooleanField(_("check deatels on the enternce"), default=False)
    update_data = models.BooleanField(_("we have all the deatles we need"), default=False)
    sex = models.IntegerField(_("man/woman"), choices=((1, _("Man")), (2, _("Woman"))), null=True, blank=True)
    photo = models.FileField(_("photo"), upload_to="static/photos", null=True, blank=True)
    birth_day = models.DateField(_("birth day"), null=True, blank=True)
    address = models.ForeignKey(CustomerAddress, verbose_name=_("home Address"), related_name="customer", null=True,
                                blank=True)
    education_institute = models.ForeignKey(CustomerEduInstitute, verbose_name=_("education institute"), null=True,
                                            blank=True)

    class Meta:
        verbose_name=_("Customer")
        verbose_name_plural= _("Customers")

    def advance_status(self, save=False):
        new_status = {
            REQUESTING_TO_BE_CANDIDATE: CANDIDATE,
            CANDIDATE: CANDIDATE_WITH_DETAILS,
            CANDIDATE_WITH_DETAILS: CUSTOMER,
            CUSTOMER:  DISTINGUISHED_CUSTOMER,
            DISTINGUISHED_CUSTOMER: DISTINGUISHED_CUSTOMER,
            TEMPORARILY_POSTPONED: TEMPORARILY_POSTPONED,
            POSTPONED: POSTPONED,
        }[self.customer_status.code]

        if new_status != self.customer_status.code:
            self.customer_status = CustomerStatus.objects.get(code=new_status)
            if save:
                self.save()


    def __init__(self, *args, **kwargs):
        super(Customer, self).__init__(*args, **kwargs)
        self.original = {}
        id = getattr(self, 'id', None)

        for field in self._meta.fields:
            if id:
                self.original[field.name] = getattr(self, field.name, None)
            else:
                self.original[field.name] = None


    def get_old_value(self, field_name):
        return self.original.get(field_name, None)

    def __unicode__(self):
        return self.user.username