Source

django-depts / depts / scholars / models.py

Full commit
# -*- coding: utf-8 -*-

# django
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.db.models import (CharField, EmailField, ForeignKey, IntegerField,
                              OneToOneField, TextField)
from django.utils.translation import ugettext_lazy as _

# 3rd-party
from autoslug.fields import AutoSlugField
from base.models import TrackedModel


class Scholar(TrackedModel):
    "Person's academic profile."

    # учёные степени
    ACADEMIC_DEGREE_CHOICES = (
        ('m', _('Master of Arts')),
        ('c', _('Candidate of Science')), # u'кандидат наук'
        ('d', _('Ph.D.')),                  # u'доктор наук'
    )
    # учёные звания
    ACADEMIC_TITLE_CHOICES = (
        ('d', _('associate professor')),  # u'доцент'
        ('p', _('professor')),            # u'профессор'
    )
    # номенклатура кандидатов и докторов наук
    DEGREE_FIELDS = (
        _('Architecture'),
        _('Biological Sciences'),
        _('Veterinary'),
        _('Military Sciences'),
        _('Geographical Sciences'),
        _('Geological and Mineralogical Sciences'),
        _('Art'),
        _('Historical Sciences'),
        _('Culturology'),
        _('Medical Sciences'),
        _('Education'),
        _('Political Sciences'),
        _('Psychological Science'),
        _('Agricultural Sciences'),
        _('Sociology'),
        _('Technical Sciences'),
        _('Pharmaceutical Sciences'),
        _('Physical and Mathematical Sciences'),
        _('Philology'),
        _('Philosophy'),
        _('Chemical Sciences'),
        _('Economic Sciences'),
        _('Law'),
    )
    ACADEMIC_FIELD_CHOICES = zip(range(0,len(DEGREE_FIELDS)), DEGREE_FIELDS)

    first_name = CharField(_('first name'), max_length=150)
    mid_name   = CharField(_('middle name'), max_length=150, blank=True, null=True)
    last_name  = CharField(_('last name'), max_length=150)

    slug = AutoSlugField(populate_from='last_name', unique=True, editable=True, blank=True)

    # academic info
    academic_degree = CharField(_('academic degree'), max_length=1, blank=True,
                                choices=ACADEMIC_DEGREE_CHOICES)
    academic_field = IntegerField(_('academic field'), max_length=1, blank=True,
                                  null=True, choices=ACADEMIC_FIELD_CHOICES,
                                  help_text=_('makes sense only with degree'))
    academic_title = CharField(_('academic title'), max_length=1, blank=True,
                               choices=ACADEMIC_TITLE_CHOICES)
    honor_title = CharField(_('honor title'), max_length=200, blank=True)
    publications = TextField(_('publications'), blank=True)
    consulting_schedule = CharField(_('consulting schedule'), max_length=200, blank=True)
    consulting_topics = TextField(_('scientific advisor on'), blank=True,
                                  help_text=_('possible topics of graduate works'))

    # optional contact information
    email = EmailField(blank=True)

    # optional link to auth
    user = OneToOneField(User, blank=True, null=True)

    class Meta:
        unique_together = ('first_name', 'last_name')
        ordering = ('last_name', 'first_name')
        verbose_name, verbose_name_plural = _('scholar'), _('scholars')

    def __unicode__(self):
        return self.name

    def get_absolute_url(self):
        return reverse('scholars-scholar-detail', kwargs={'slug': self.slug})

    @property
    def name(self):
        return ' '.join([self.last_name, self.first_name, self.mid_name])

    @property
    def degree(self):
        if self.academic_degree and self.academic_field:
            return _('%(degree)s in %(field)s') % {'degree': self.get_academic_degree_display(),
                                                   'field': self.get_academic_field_display()}
        return self.academic_degree

    def get_academic_props(self):
        return ', '.join([x for x in self.degree,
                                     self.get_academic_title_display(),
                                     self.honor_title  if x])

    def get_email(self):
        return self.email or (self.user and self.user.email) or None

    def get_positions_count(self):
        return self.positions.count()
    get_positions_count.short_description = _('Total positions')