models.py 2.13 KB
# -*- coding: utf-8 -*-
from django.db.models import Model, CharField, IntegerField, ForeignKey, \
    ManyToManyField
from django.utils.translation import ugettext_lazy as _

from common.util import GroupDict


class InflectionType(Model):
    symbol = CharField(primary_key=True, max_length=16, db_column='czm')
    color_scheme = IntegerField()
    full_name = CharField(max_length=128)

    def __unicode__(self):
        return self.symbol

    class Meta:
        db_table = 'czescimowy'


class BaseFormLabel(Model):
    symbol = CharField(max_length=32, blank=True, db_column='efobaz')
    lexical_class = ForeignKey(InflectionType)
    index = IntegerField()

    def __unicode__(self):
        return '%s/%s' % (self.symbol, self.lexical_class.symbol)

    class Meta:
        db_table = 'efobazy'
        unique_together = ['symbol', 'lexical_class']
        ordering = ['index']


class DummyEnding(object):
    def __init__(self, string):
        self.index = 0
        self.string = string
        from dictionary.models import Qualifier
        self.qualifiers = Qualifier.objects.none()


class PatternType(Model):
    lexical_class = ForeignKey(
        InflectionType, db_column='czm', verbose_name=_(u'inflection type'))
    # typ wzoru (np. dla rzeczowników: odmiana męska, żeńska lub nijaka)
    symbol = CharField(
        max_length=32, blank=True, db_column='wtyp',
        verbose_name=_(u'pattern type'))
    base_form_labels = ManyToManyField(BaseFormLabel)

    def dummy_base_endings(self):
        bfl_dict = dict(
            (bfl, [DummyEnding(bfl.symbol or self.lexical_class_id)])
            for bfl in self.base_form_labels.all())
        return bfl_dict

    @classmethod
    def options(cls):
        pattern_types = GroupDict()
        for pt in cls.objects.all():
            pattern_types.add(pt.lexical_class.symbol, (pt.id, unicode(pt)))
        return sorted(pattern_types.items())

    def __unicode__(self):
        return self.symbol.replace('"', "''") or '[%s]' % self.lexical_class_id
        # '%s (%s)' % (self.symbol, self.lexical_class_id)

    class Meta:
        db_table = 'typywzorow'
        ordering = ['symbol']