models.py 39.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
# -*- coding: utf-8 -*-
import json
from collections import OrderedDict

from django.conf import settings
from django.contrib.auth.models import User, Permission, AnonymousUser
from django.db.models import Manager, Model, CharField, ForeignKey, \
    IntegerField, BooleanField, ManyToManyField, TextField, DateTimeField, F
from django.utils.translation import ugettext_lazy as _, get_language

from accounts.util import users_with_perm
from common.models import NotDeletedManager
from common.util import no_history
from patterns.models import InflectionType, BaseFormLabel, PatternType, Pattern
from tables.util import prepare_table


class LexemeNotDeletedManager(Manager):
    use_for_related_field = True

    def get_queryset(self):
        return super(LexemeNotDeletedManager, self).get_queryset().filter(
            lexeme__deleted=False)


class MultilingualText(Model):
    text_id = CharField(max_length=128, unique=True)

    def translation(self, language_code=None):
        if language_code is None:
            language_code = get_language()
        tr_set = self.translatedtext_set.filter(language_code=language_code)
        if tr_set:
            return tr_set.get().text
        else:
            return self.text_id

    def add_translated_texts(self):
        for code, name in settings.LANGUAGES:
            TranslatedText.objects.get_or_create(
                multilingual_text=self, language_code=code,
                defaults={'text': self.text_id})


class TranslatedText(Model):
    multilingual_text = ForeignKey(MultilingualText)
    language_code = CharField(max_length=16)
    text = CharField(max_length=128)


# nie podoba mi się mnożenie różnych podziałów części mowy, ale tak każą
class POSName(Model):
    abbr = CharField(max_length=16)
    extended = CharField(max_length=128)


class PartOfSpeech(Model):
    symbol = CharField(primary_key=True, max_length=16, db_column='pos')
    inflection_type = ForeignKey(InflectionType, db_column='czm')
    full_name = CharField(max_length=128, db_column='nazwa')
    pos_name = ForeignKey(POSName)
    index = IntegerField()
    color_scheme = IntegerField()

    def __unicode__(self):
        return self.symbol

    class Meta:
        db_table = 'klasygramatyczne'
        ordering = ['index']


class QualifierExclusionClass(Model):
    name = CharField(
        unique=True, max_length=64, db_column='nazwa', verbose_name=_(u'name'))
    vocabulary = ForeignKey(
        'Vocabulary', db_column='slownik', verbose_name=_(u'dictionary'))

    def __unicode__(self):
        return self.name

    class Meta:
        db_table = 'klasy_wykluczania'


def get_exclusion_classes():
    exclusion_classes = {}
    for ec in QualifierExclusionClass.objects.all():
        qualifiers = ec.qualifier_set.all()
        if qualifiers:
            q_list = [q.pk for q in qualifiers]
            exclusion_classes[ec.pk] = q_list
    return exclusion_classes


# kwalifikatory dla leksemów, odmian,
# zakonczen i form wyjatkowych
class Qualifier(Model):
    TYPE_STYLE = 'styl'
    TYPE_SCOPE = 'zakr'
    TYPE_FORM = 'form'
    TYPE_CHOICES = (
        (TYPE_STYLE, _('stylistic')),
        (TYPE_SCOPE, _('scope')),
        (TYPE_FORM, _('at forms')),
    )

    label = CharField(max_length=64, db_column='kwal', verbose_name=_(u'name'))
    type = CharField(
        max_length=4, choices=TYPE_CHOICES, verbose_name=_(u'type'))
    vocabulary = ForeignKey(
        'Vocabulary', db_column='slownik', verbose_name=_(u'dictionary'),
        related_name='qualifiers')
    exclusion_class = ForeignKey(
        QualifierExclusionClass, db_column='klasa', null=True, blank=True,
        verbose_name=_(u'exclusion class'))
    deleted = BooleanField(db_column='usuniety', default=False)

    objects = NotDeletedManager()
    all_objects = Manager()

    def is_empty(self):
        return not (
            self.lexeme_set.exists() or
            self.ending_set.exists() or
            self.inflection_set.exists())

    def set_for(self, something, add):
        if add:
            if self.exclusion_class:
                qualifiers = (something.qualifiers.all() &
                              self.exclusion_class.qualifier_set.all())
                if qualifiers.count() > 0 and list(qualifiers) != [self]:
                    return False
            if self in something.qualifiers.all():
                return False
            else:
                something.qualifiers.add(self)  # add
                return True
        else:
            if self in something.qualifiers.all():
                something.qualifiers.remove(self)  # add
                return True
            else:
                return False

    def __unicode__(self):
        # return u'%s (%s)' % (self.label, self.vocabulary.id)
        return self.label

    @staticmethod
    def visible_qualifiers(user):
        return Qualifier.objects.filter(
            vocabulary__in=Vocabulary.visible_vocabularies(user))

    @staticmethod
    def editable_qualifiers(user):
        return Qualifier.objects.filter(
            vocabulary__in=Vocabulary.editable_vocabularies(user))

    class Meta:
        unique_together = ('label', 'vocabulary')
        db_table = 'kwalifikatory'
        ordering = ['label']


class Classification(Model):
    name = CharField(
        unique=True, max_length=64, db_column='nazwa',
        verbose_name=_(u'classification name'))
    multilingual_name = ForeignKey(MultilingualText, null=True)
    parts_of_speech = ManyToManyField(PartOfSpeech)

    def value_tree(self):
        parts = []
        all_values = self.values.select_related('parent_node') \
            .prefetch_related('child_nodes')
        for v in all_values:
            if v.parent_node is None:
                parts.append(v.subtree(all_values))
        return parts

    def make_choices(self):
        return _make_choices(self.value_tree())

    def __unicode__(self):
        return self.name

    class Meta:
        db_table = 'klasyfikacje'


def _make_choices(tree):
    choices = []
    for value, subtree in tree:
        choices.append((value.pk, value.label))
        subchoices = _make_choices(subtree)
        nbsp = u'    '
        choices += [(pk, nbsp + label) for (pk, label) in subchoices]
    return choices


class ClassificationValue(Model):
    label = CharField(
        unique=True, max_length=64, db_column='nazwa',
        verbose_name=_(u'value name'))
    classification = ForeignKey(
        Classification, db_column='klas_id', related_name='values')
    parent_node = ForeignKey(
        'self', db_column='rodzic', null=True, blank=True,
        verbose_name=_(u'parent value'), related_name='child_nodes')
    lexemes = ManyToManyField('Lexeme', blank=True, through='LexemeCV')
    deleted = BooleanField(db_column='usunieta', default=False)

    objects = NotDeletedManager()
    all_objects = Manager()

    def subtree(self, all_values):
        subtrees = []
        for child in all_values:
            if child in self.child_nodes.all():
                subtrees.append(child.subtree(all_values))
        return self, subtrees

    def is_empty(self):
        return not self.lexemes.exists()

    def add_lexeme(self, lexeme):
        LexemeCV.objects.get_or_create(lexeme=lexeme, classification_value=self)

    def remove_lexeme(self, lexeme):
        LexemeCV.objects.filter(
            lexeme=lexeme, classification_value=self).delete()

    def __unicode__(self):
        return self.label

    class Meta:
        db_table = 'wartosci_klasyfikacji'
        ordering = ['label']


class LexemeCV(Model):
    lexeme = ForeignKey('Lexeme')
    classification_value = ForeignKey('ClassificationValue')

    objects = LexemeNotDeletedManager()
    all_objects = Manager()

    class Meta:
        unique_together = ['lexeme', 'classification_value']


class BorrowingSource(Model):
    label = CharField(max_length=32)

    def __unicode__(self):
        return self.label


# śmieć, trzeba trzymać ze względów historycznych
class InflectionCharacteristic(Model):
    symbol = CharField(max_length=16, blank=True, db_column='charfl')
    part_of_speech = ForeignKey(PartOfSpeech, db_column='pos')
    basic_form_label = ForeignKey(BaseFormLabel, db_column='efobaz')

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

    class Meta:
        db_table = 'charfle'
        unique_together = ('symbol', 'part_of_speech')


class Gender(Model):
    symbol = CharField(max_length=4, unique=True)
    basic_form_label = ForeignKey(BaseFormLabel)

    def __unicode__(self):
        return self.symbol


class Lexeme(Model):
    STATUS_CANDIDATE = 'cand'
    STATUS_DESCRIBED = 'desc'
    STATUS_CONFIRMED = 'conf'
    STATUS_LITTER = 'litt'
    STATUS_CHOICES = (
        (STATUS_CANDIDATE, _(u'candidate')),
        (STATUS_DESCRIBED, _(u'entered')),
        (STATUS_CONFIRMED, _(u'confirmed')),
        (STATUS_LITTER,    _(u'litter')),
    )
    HIDDEN_STATUSES = (STATUS_CANDIDATE, STATUS_LITTER)

    entry = CharField(
        max_length=64, db_column='haslo', db_index=True,
        verbose_name=_(u'entry'))
    # id w źródłowej bazie
    source_id = IntegerField(blank=True, null=True)
    # pozostałość historyczna:
    entry_suffix = CharField(
        blank=True, max_length=16, db_column='haslosuf',
        verbose_name=_(u'entry suffix'))
    gloss = TextField(blank=True, db_column='glosa', verbose_name=_(u'gloss'))
    note = TextField(blank=True, db_column='nota', verbose_name=_(u'note'))
    extended_note = TextField(blank=True, verbose_name=_(u'extended note'))
    pronunciation = TextField(
        blank=True, db_column='wymowa', verbose_name=_(u'pronunciation'))
    valence = TextField(blank=True, verbose_name=_(u'valence'))
    homonym_number = IntegerField(db_column='hom', default=1)
    part_of_speech = ForeignKey(
        PartOfSpeech, db_column='pos', verbose_name=_(u'POS'))
    owner_vocabulary = ForeignKey(
        'Vocabulary', db_column='slownik', related_name='owned_lexemes')
    source = CharField(
        max_length=64, blank=True, db_column='zrodlo',
        verbose_name=_(u'source'))
    borrowing_source = ForeignKey(
        BorrowingSource, blank=True, null=True,
        verbose_name=_(u'borrowing source'))
    status = CharField(max_length=8, db_column='status', choices=STATUS_CHOICES)
    qualifiers = ManyToManyField(
        Qualifier, blank=True, db_table='kwalifikatory_leksemow')
    qualifiers_dor = TextField(blank=True, verbose_name=_(u'Dor. qual.'))
    qualifiers_style = TextField(blank=True, verbose_name=_(u'styl. qual.'))
    qualifiers_scope = TextField(blank=True, verbose_name=_(u'scope qual.'))
    # czy termin specjalistyczny
    specialist = BooleanField(default=False, verbose_name=_(u'specialist'))
    comment = TextField(
        blank=True, db_column='komentarz', verbose_name=_(u'comment'))
    last_modified = DateTimeField(auto_now=True, db_column='data_modyfikacji')
    # osoba, ktora ostatnia zmieniala opis leksemu
    responsible = ForeignKey(
        User, blank=True, null=True, db_column='odpowiedzialny')
    patterns = ManyToManyField(Pattern, through='Inflection')
    qualifiers_cache = ManyToManyField(
        Qualifier, through='LexemeFormQualifier', related_name='all_lexemes')
    deleted = BooleanField(db_column='usuniety', default=False)

    objects = NotDeletedManager()
    all_objects = Manager()

    def inflection_tables(self, variant, qualifiers=None):
        lips = self.inflection_set.order_by(
            'index').select_related('gender', 'pattern')
        genders = OrderedDict()
        for lip in lips:
            g = lip.gender
            if g not in genders:
                genders[g] = {'patterns': [], 'pronunciations': []}
            genders[g]['patterns'].append(lip.pattern)
            if lip.pronunciation:
                genders[g]['pronunciations'].append(lip.pronunciation)
        return [
            (gender, val['patterns'], val['pronunciations']) +
            self.inflection_table(variant, gender, qualifiers=qualifiers)
            for gender, val in genders.iteritems()]

    def inflection_table(self, variant, gender, qualifiers=None):
        lips = self.inflection_set.prefetch_related(
            'qualifiers').select_related('pattern__type', 'gender')
        if gender:
            lips = lips.filter(gender=gender)
        span = unicode(variant) == u'0'
        gender_qualifiers = lips[0].qualifiers.all() & qualifiers
        attr = LexemeAttribute.objects.get(name=u'nacechowana forma')
        depr = self.attribute_value(attr)
        tables = [
            lip.inflection_table(
                variant, qualifiers=qualifiers, span=span,
                depr=depr, cell_qualifier=True,
                edit_view=(i == 0))  # brzydko, ale cóż
            for i, lip in enumerate(lips)]
        table1 = tables[0]
        for table2 in tables[1:]:
            while len(table2) > len(table1):
                table1.append([])
            for row1, row2 in zip(table1, table2):
                while len(row2) > len(row1):
                    row1.append({'type': 'empty'})
                for cell1, cell2 in zip(row1, row2):
                    if cell1['type'] == 'forms':
                        assert cell2['type'] in ('forms', 'empty')
                        if cell2['type'] == 'forms':
                            cell1['forms'].extend(cell2['forms'])
                    elif cell1['type'] == 'label':
                        assert cell2['type'] in ('label', 'empty')
                        if cell2['type'] == 'label':
                            cell1['label'].extend(cell2['label'])
                    elif cell1['type'] == 'empty':
                        cell1.update(cell2)
        prepare_table(table1)
        return gender_qualifiers, table1

    def all_forms(self, label_filter=None, variant='1'):
        forms = set()
        for lip in self.inflection_set.all():
            forms |= set(
                form for (indexes, form, qualifiers)
                in lip.all_forms(label_filter=label_filter, variant=variant))
        return forms

    def refresh_forms(self):
        self.lexemeform_set.all().delete()
        for form in self.all_forms():
            self.lexemeform_set.add(LexemeForm(form=form.strip('+')))

    def all_qualifiers(self, variant='1'):
        qualifiers = set(self.qualifiers.all())
        for lip in self.inflection_set.all():
            qualifiers |= set(
                qualifier for (indexes, form, qualifiers)
                in lip.all_forms(variant=variant)
                for qualifier in qualifiers)
        return qualifiers

    def refresh_qualifiers_cache(self):
        self.lexemeformqualifier_set.all().delete()
        for qualifier in self.all_qualifiers():
            self.lexemeformqualifier_set.create(qualifier=qualifier)

    def refresh_data(self):
        self.refresh_forms()
        self.refresh_qualifiers_cache()

    def get_root(self, pattern, gender=None):
        return pattern.get_root(self.entry, gender)

    def visible_vocabularies(self, user):
        return Vocabulary.visible_vocabularies(user) & self.vocabularies.all()

    def editable_vocabularies(self, user):
        return Vocabulary.editable_vocabularies(user) & self.vocabularies.all()

    def change_owner(self, new_owner):
        old_owner = self.owner_vocabulary
        new_owner.add_lexeme(self)
        self.owner_vocabulary = new_owner
        old_owner.remove_lexeme(self)

    def classification_values(self, classification):
        return self.classificationvalue_set.filter(
            classification=classification)

    def pattern_list(self):
        lips = self.inflection_set.all()
        patterns = []
        for lip in lips:
            if patterns == [] or lip.pattern != patterns[-1]:
                patterns.append(lip.pattern)
        return patterns

    def lip_data(self, lips=None):
        if lips is None:
            lips = self.inflection_set.all()
        patterns = []
        genders = []
        for lip in lips:
            if patterns == [] or lip.pattern.name != patterns[-1]:
                patterns.append(lip.pattern.name)
            if (lip.gender and
                    (genders == [] or lip.gender.symbol != genders[-1])):
                genders.append(lip.gender.symbol)
        patterns = '/'.join(patterns)
        genders = '/'.join(genders)
        return {'patterns': patterns, 'genders': genders}

    def vocab_list(self):
        owner = self.owner_vocabulary
        vocab_list = [owner.id]
        vocab_list += list(
            self.vocabularies.exclude(id=owner.id).values_list('id', flat=True))
        return vocab_list

    def fix_homonym_number(self):
        homonym_numbers = (Lexeme.objects.filter(
            entry=self.entry, part_of_speech=self.part_of_speech)
            .exclude(pk=self.pk)).values_list('homonym_number', flat=True)
        for i in xrange(1, len(homonym_numbers) + 2):
            if i not in homonym_numbers:
                self.homonym_number = i
                self.save()
                break
        this_hn = HomonymNumber.objects.filter(
            lexeme=self, variant_id='Morfeusz')
        this_number = this_hn.get().number if this_hn else None
        letter = HomonymNumber.MORFEUSZ_LETTERS_REVERSE.get(
            self.part_of_speech_id)
        if letter:
            pos_list = HomonymNumber.MORFEUSZ_LETTERS[letter]
            homonyms = Lexeme.objects.filter(
                entry=self.entry, part_of_speech_id__in=pos_list)
            if homonyms:
                numbers = set()
                need_numbers = []
                for homonym in homonyms:
                    if homonym != self:
                        hn = HomonymNumber.objects.filter(
                            lexeme=homonym, variant_id='Morfeusz')
                        if hn:
                            numbers.add(hn.get().number)
                        else:
                            need_numbers.append(homonym)
                for homonym in need_numbers:
                    for i in xrange(1, len(numbers) + 2):
                        if i not in numbers and i != this_number:
                            HomonymNumber.objects.create(
                                lexeme=homonym, variant_id='Morfeusz',
                                number=i)
                            numbers.add(i)
                if not this_hn or this_number in numbers:
                    for i in xrange(1, len(numbers) + 2):
                        if i not in numbers:
                            if not this_hn:
                                HomonymNumber.objects.create(
                                    lexeme=self, variant_id='Morfeusz',
                                    number=i)
                            else:
                                this_hn = this_hn.get()
                                this_hn.number = i
                                this_hn.save()
                            break

    def get_variant_homonym(self, variant_id):
        hom = HomonymNumber.objects.filter(lexeme=self, variant_id=variant_id)
        if hom:
            return hom.get().number
        else:
            return None

    def homonym_count(self, user=None):
        lexemes = Lexeme.objects.filter(entry=self.entry).exclude(pk=self.pk)
        if user is not None:
            lexemes = Lexeme.filter_visible(lexemes, user)
        return lexemes.count()

    def attributes(self, part_of_speech=None, genders=None):
        pos = part_of_speech or self.part_of_speech
        if genders is None and pos.inflection_type_id == 'subst':
            lips = self.inflection_set.all()
            genders = [lip.gender for lip in lips]
        elif genders is None:
            genders = []
        attrs = LexemeAttribute.objects.all()
        attrs = attrs.filter(parts_of_speech=pos)
        attrs = (attrs.filter(genders__in=genders) |
                 attrs.filter(takes_gender=False))
        return attrs

    def attributes_values(self, part_of_speech=None, genders=None):
        for attr in self.attributes(part_of_speech, genders):
            yield (attr, self.attribute_value(attr))

    def attribute_value(self, attribute):
        if attribute.multiple:
            return attribute.values.filter(lexemes=self)
        else:
            try:
                return attribute.values.get(lexemes=self)
            except LexemeAttributeValue.DoesNotExist:
                return None

    def add_cross_reference(self, to_lexeme, type_symbol):
        cr_type = CrossReferenceType.objects.get(
            symbol=type_symbol, from_pos_id=self.part_of_speech_id,
            to_pos_id=to_lexeme.part_of_speech_id)
        CrossReference.objects.create(
            from_lexeme=self, to_lexeme=to_lexeme, type=cr_type)

    def perm(self, user, action):
        if action == 'view':
            if not user.is_authenticated() and not self.is_public():
                return False
            return bool(self.visible_vocabularies(user))
        elif action == 'change':
            priority = user.has_perm('dictionary.lexeme_priority')
            priority |= not (
                self.responsible and
                self.responsible.has_perm('dictionary.lexeme_priority'))
            edit_vocab = self.owner_vocabulary in \
                Vocabulary.editable_vocabularies(user)
            return edit_vocab and priority

    def undelete(self):
        no_history()
        self.deleted = False
        self.fix_homonym_number()
        self.save()
        self.history_set.get(column_name='usuniety', old_value='false').delete()

    def sgjp_info(self):
        commonness = Classification.objects.get(name=u'pospolitość')
        commonness_labels = list(self.classification_values(
            commonness).values_list('label', flat=True))
        if commonness_labels == ['nazwa pospolita']:
            commonness_labels = []
        return {
            'SJPDor': bool(self.vocabularies.filter(id='SJPDor')),
            'commonness': commonness_labels,
            'qualifiers': self.qualifiers.values_list('label', flat=True),
        }

    def status_desc(self):
        return dict(self.STATUS_CHOICES)[self.status]

    def cross_references(self, user):
        return self.refs_to.order_by('type__index').distinct().filter(
            to_lexeme__vocabularies__in=Vocabulary.visible_vocabularies(user))

    def is_public(self):
        return self.status not in Lexeme.HIDDEN_STATUSES

    def __unicode__(self):
        return '%s (%s)' % (self.entry, self.part_of_speech.symbol)

    @staticmethod
    def filter_visible(lexemes, user):
        vocab_ids = [v.id for v in Vocabulary.visible_vocabularies(user)]
        # uniknięcie podzapytania *bardzo* zwiększa wydajność!
        if not user.is_authenticated():
            lexemes = lexemes.exclude(status__in=Lexeme.HIDDEN_STATUSES)
        return lexemes.filter(vocabularies__id__in=vocab_ids).distinct()

    @staticmethod
    def filter_reader(lexemes):
        return Lexeme.filter_visible(lexemes, AnonymousUser())

    class Meta:
        db_table = 'leksemy'
        permissions = (
            ('view_lexeme', _(u'Can view lexemes')),
            ('view_all_lexemes', _(u'Can view all lexemes')),
            ('lexeme_priority',
             _(u'Decisive lexeme modifications')),
            ('export_lexemes', _(u'Can export lexemes')),
        )


class LexemeAttribute(Model):
    name = CharField(max_length=32, unique=True)
    name_gender = CharField(max_length=2, default='n')
    multilingual_name = ForeignKey(MultilingualText, null=True)
    closed = BooleanField(default=False)  # czy jest zamknięta lista wartości
    multiple = BooleanField(default=False)
    required = BooleanField(default=False)
    parts_of_speech = ManyToManyField(PartOfSpeech)
    takes_gender = BooleanField(default=False)
    genders = ManyToManyField(Gender, blank=True)

    def add_lexeme(self, lexeme, value):
        assert not self.multiple
        val, created = LexemeAttributeValue.objects.get_or_create(
            value=value, attribute=self)
        LexemeAV.objects.update_or_create(
            lexeme=lexeme, attribute_value__attribute=self,
            defaults={'attribute_value': val})

    def __unicode__(self):
        return self.name


class LexemeAttributeValue(Model):
    value = CharField(max_length=128, blank=True)
    display_value = CharField(max_length=64, blank=True)
    attribute = ForeignKey(LexemeAttribute, related_name='values')
    lexemes = ManyToManyField(Lexeme, blank=True, through='LexemeAV')

    def add_lexeme(self, lexeme):
        LexemeAV.objects.get_or_create(lexeme=lexeme, attribute_value=self)

    def remove_lexeme(self, lexeme):
        LexemeAV.objects.filter(lexeme=lexeme, attribute_value=self).delete()

    def __unicode__(self):
        return self.value

    class Meta:
        ordering = ['value']


class LexemeAV(Model):
    lexeme = ForeignKey('Lexeme')
    attribute_value = ForeignKey('LexemeAttributeValue')

    objects = LexemeNotDeletedManager()
    all_objects = Manager()

    class Meta:
        unique_together = ['lexeme', 'attribute_value']


class Inflection(Model):
    lexeme = ForeignKey(Lexeme, db_column='l_id')
    index = IntegerField(db_column='oind')
    pattern = ForeignKey(Pattern, db_column='w_id', verbose_name=_(u'pattern'))
    gender = ForeignKey(
        Gender, verbose_name=_(u'gender'), blank=True, null=True)
    root = CharField(max_length=64, db_column='rdzen')
    qualifiers = ManyToManyField(
        Qualifier, blank=True, db_table='kwalifikatory_odmieniasiow')
    pronunciation = CharField(
        max_length=64, verbose_name=_(u'pronunciation'), blank=True)

    objects = LexemeNotDeletedManager()
    all_objects = Manager()

    def table_template(self, variant, attr_vals=None, pos=None):
        part_of_speech = pos or self.lexeme.part_of_speech
        from tables.models import TableTemplate
        tts = TableTemplate.objects.filter(
            parts_of_speech=part_of_speech, variant=variant,
            pattern_types=self.pattern.type)
        if attr_vals is None:
            l_attr_vals = list(self.lexeme.lexemeattributevalue_set.values_list(
                'id', flat=True))
        else:
            l_attr_vals = attr_vals.values_list('id', flat=True)
        relevant_attr_vals = list(LexemeAttributeValue.objects.filter(
            attribute__parts_of_speech=part_of_speech).exclude(
            tabletemplate=None).filter(id__in=l_attr_vals))
        for attr_val in relevant_attr_vals:
            tts = tts.filter(attribute_values=attr_val)
        try:
            return tts.get()
        except TableTemplate.MultipleObjectsReturned:
            return None
        except TableTemplate.DoesNotExist:
            return None

    def cells(self, variant='1'):
        tt = self.table_template(variant)
        if not tt:
            return []
        return tt.filter_cells(
            self.pattern.type, self.gender,
            self.lexeme.lexemeattributevalue_set.all())

    def base_endings(self, label_filter=None):
        return self.pattern.base_endings(label_filter)

    def inflection_table(self, variant, edit_view=False, attr_vals=None,
                         pos=None, depr=None, **kwargs):
        part_of_speech = pos or self.lexeme.part_of_speech
        tt = self.table_template(
            variant, attr_vals=attr_vals, pos=part_of_speech)
        if tt is None:
            return []
        lip_qualifiers = self.qualifiers.all() if not edit_view else []
        if attr_vals is None:
            attr_vals = self.lexeme.lexemeattributevalue_set.all()
        if depr and (not self.gender or self.gender.symbol != 'm1'):
            depr = None
        return tt.render_with_pattern(
            self.pattern, self.gender, attr_vals,
            root=self.root, edit_view=edit_view, lip_index=self.index,
            lip_qualifiers=lip_qualifiers, depr=depr, **kwargs)

    def all_forms(self, label_filter=None, variant='1', **kwargs):
        forms = []
        base_endings = self.base_endings(label_filter)
        for cell in self.cells(variant=variant):
            forms += cell.forms(
                base_endings=base_endings, root=self.root,
                lip_qualifiers=self.qualifiers.all(), lip_index=self.index,
                **kwargs)
        return forms

    def editable_vocabularies(self, user):
        return self.lexeme.editable_vocabularies(user)

    def get_root(self):
        return self.lexeme.get_root(self.pattern, self.gender)

    def __unicode__(self):
        return '%s : %s/%s : %s' % (
            self.lexeme.entry,
            self.pattern.name,
            self.pattern.type.symbol,
            self.gender.symbol if self.gender else '',
        )

    @staticmethod
    def filter_visible(lips, user):
        vocabs = Vocabulary.visible_vocabularies(user)
        if not user.is_authenticated():
            lips = lips.exclude(lexeme__status__in=Lexeme.HIDDEN_STATUSES)
        return lips.filter(lexeme__vocabularies__in=vocabs).distinct()

    class Meta:
        db_table = 'odmieniasie'
        unique_together = ('lexeme', 'index')
        ordering = ['index']


# Sluzy do doczepienia flag do poszczegolnych form
# poszczegolnych leksemow
# class UncommonForm(Model):
#     inflection = ForeignKey(Inflection, db_column='o_id')
#     # raczej tag, ale z jakiego tagsetu ?
#     # base_form_label/tag =
#     qualifiers = ManyToManyField(
#         Qualifier, blank=True, db_table='kwalifikatory_form')
#
#     class Meta:
#         db_table = 'formy_wyjatkowe'
#         unique_together = (
#             'inflection',
#             'tag',
#         )


class Vocabulary(Model):
    id = CharField(max_length=64, primary_key=True, db_column='slownik')
    description = TextField()
    lexemes = ManyToManyField(Lexeme, blank=True, through='LexemeAssociation',
                              related_name='vocabularies')
    managers = ManyToManyField(
        User, blank=True, related_name='managed_vocabularies')
    viewers = ManyToManyField(
        User, blank=True, related_name='visible_vocabularies')
    editors = ManyToManyField(
        User, blank=True, related_name='editable_vocabularies')
    # bardziej by pasowało w Classification, ale już trudno
    classifications = ManyToManyField(
        Classification, blank=True, related_name='vocabularies')

    def owned_lexemes_pk(self):
        return self.owned_lexemes.values_list('pk', flat=True)

    def all_viewers(self):
        perm = Permission.objects.get(codename='view_all_lexemes')
        return self.viewers.all().distinct() | users_with_perm(perm)

    def all_editors(self):
        return self.editors.all()

    def all_managers(self):
        perm = Permission.objects.get(codename='manage_all_vocabularies')
        return self.managers.all().distinct() | users_with_perm(perm)

    def add_lexeme(self, lexeme):
        la, created = LexemeAssociation.objects.get_or_create(
            lexeme=lexeme, vocabulary=self)
        return created

    def remove_lexeme(self, lexeme):
        assert self != lexeme.owner_vocabulary
        LexemeAssociation.objects.filter(
            lexeme=lexeme, vocabulary=self).delete()

    def set_lexeme(self, lexeme, add):
        if add:
            self.add_lexeme(lexeme)
        else:
            self.remove_lexeme(lexeme)

    def __unicode__(self):
        return self.id

    @staticmethod
    def visible_vocabularies(user):
        if user.has_perm('dictionary.view_all_lexemes'):
            return Vocabulary.objects.all()
        elif user.is_authenticated():
            return user.visible_vocabularies.all()
        else:
            return Vocabulary.objects.filter(id='SGJP')

    @staticmethod
    def editable_vocabularies(user):
        return user.editable_vocabularies.all()

    @staticmethod
    def readonly_vocabularies(user):
        return Vocabulary.visible_vocabularies(user).exclude(
            id__in=Vocabulary.editable_vocabularies(user))

    @staticmethod
    def managed_vocabularies(user):
        if user.has_perm('dictionary.manage_all_vocabularies'):
            return Vocabulary.objects.all()
        else:
            return user.managed_vocabularies.all()

    class Meta:
        db_table = 'slowniki'
        ordering = ['id']
        permissions = (
            ('manage_vocabulary', _(u'Can manage dictionaries')),
            ('manage_all_vocabularies', _(u'Manages all dictionaries')),
        )


class LexemeAssociation(Model):
    lexeme = ForeignKey(Lexeme, db_column='l_id')
    vocabulary = ForeignKey(Vocabulary, db_column='slownik')

    objects = LexemeNotDeletedManager()
    all_objects = Manager()

    def __unicode__(self):
        return '%s/%s' % (self.lexeme.entry, self.vocabulary.id)

    class Meta:
        db_table = 'leksemy_w_slownikach'
        unique_together = ['lexeme', 'vocabulary']


# powinno być w tabeli, ale na razie nie chcemy zmieniać struktury
REVERSE_CR_TYPE = {
    'verpact': 'pactver',
    'verppas': 'ppasver',
    'verger': 'gerver',
    'adjosc': 'oscadj',
    'adjadv': 'advadj',
    'adjadvc': 'advcadj',
    'adjcom': 'comadj',
    'adjnie': 'nieadj',
    'adjsubst': 'substadj',

    'advcom': 'comadv',
    'masfem': 'femmas',
    'veripp': 'ippver',
    'warstd': 'warnstd',

    'asp': 'asp',
    'warleks': 'warleks',
    'warpis': 'warpis',
    'warrodz': 'warrodz',
    # zdrobod, zgrubod -- brak zwrotnych odsyłaczy
}


def _complete_reverse_cr_type():
    for cr_symbol, rev_symbol in list(REVERSE_CR_TYPE.iteritems()):
        REVERSE_CR_TYPE[rev_symbol] = cr_symbol

_complete_reverse_cr_type()


class CrossReferenceType(Model):
    symbol = CharField(max_length=10, db_column='typods')
    desc = CharField(max_length=40, db_column='naglowek')
    index = IntegerField(db_column='kolejnosc')
    from_pos = ForeignKey(
        PartOfSpeech, db_column='pos1', related_name='crtype_to')
    to_pos = ForeignKey(
        PartOfSpeech, db_column='pos2', related_name='crtype_from')
    # reverse = ForeignKey('self', db_column='odwrotny')

    def __unicode__(self):
        return self.symbol

    def reversed(self):
        if self.symbol not in REVERSE_CR_TYPE:
            return None
        rev_symbol = REVERSE_CR_TYPE[self.symbol]
        return CrossReferenceType.objects.get(
            symbol=rev_symbol, from_pos=self.to_pos, to_pos=self.from_pos)

    class Meta:
        db_table = 'typyodsylaczy'


class CRManager(Manager):
    use_for_related_field = True

    def get_queryset(self):
        return super(CRManager, self).get_queryset().filter(
            from_lexeme__deleted=False, to_lexeme__deleted=False)


class CrossReference(Model):
    from_lexeme = ForeignKey(
        Lexeme, db_column='l_id_od', related_name='refs_to')
    to_lexeme = ForeignKey(
        Lexeme, db_column='l_id_do', related_name='refs_from',
        verbose_name=_(u'targer number'))
    type = ForeignKey(
        CrossReferenceType, db_column='typods_id', verbose_name=_(u'type'))

    objects = CRManager()
    all_objects = Manager()

    def reversed(self):
        rev_type = self.type.reversed()
        if rev_type:
            return self.to_lexeme.refs_to.filter(
                to_lexeme=self.from_lexeme, type=rev_type)
        else:
            return CrossReference.objects.none()

    def __unicode__(self):
        return '%s: %s -> %s' % (
            self.type.symbol, self.from_lexeme.entry, self.to_lexeme.entry)

    class Meta:
        db_table = 'odsylacze'


# na szybko i brudno
class ParadygmatyWSJP(Model):
    wariant = CharField(max_length=4)
    pos = ForeignKey(PartOfSpeech, db_column='pos')
    typr = ForeignKey(PatternType, db_column='typr')
    charfl = CharField(max_length=16, blank=True, db_column='charfl')
    podparad = CharField(max_length=4, blank=True)
    row = IntegerField()
    col = IntegerField()
    rowspan = IntegerField()
    colspan = IntegerField()
    efobaz = ForeignKey(BaseFormLabel, db_column='efobaz')
    morf = TextField()
    pref = CharField(max_length=20, blank=True)
    suf = CharField(max_length=20, blank=True)
    kskl = IntegerField()

    class Meta:
        db_table = 'paradygmatywsjp'


class HomonymNumber(Model):
    MORFEUSZ_LETTERS = {
        'v': ['v', 'pred'],
        's': ['subst', 'osc', 'skrs'],
        'a': ['adj'],
        'd': ['adv', 'advndm'],
        'n': ['num'],
        'b': ['fraz'],
        'q': ['part'],
        'i': ['interj'],
        'o': ['ppron'],
        'p': ['prep'],
        'c': ['comp'],
        'j': ['conj'],
    }

    MORFEUSZ_LETTERS_REVERSE = dict(
        (pos, letter)
        for (letter, poses) in MORFEUSZ_LETTERS.iteritems()
        for pos in poses)

    lexeme = ForeignKey(Lexeme)
    variant = ForeignKey('tables.Variant')
    number = IntegerField()

    objects = LexemeNotDeletedManager()
    all_objects = Manager()

    class Meta:
        unique_together = ['lexeme', 'variant']


class LexemeForm(Model):
    lexeme = ForeignKey(Lexeme)
    form = CharField(max_length=128, db_index=True)

    objects = LexemeNotDeletedManager()
    all_objects = Manager()


class LexemeFormQualifier(Model):
    lexeme = ForeignKey(Lexeme)
    qualifier = ForeignKey(Qualifier)

    objects = LexemeNotDeletedManager()
    all_objects = Manager()


class SavedFilter(Model):
    serialized_filter = TextField()
    name = CharField(max_length=64)
    user = ForeignKey(User)
    type = CharField(max_length=16)

    class Meta:
        unique_together = ('name', 'user')


class LexemeList(Model):
    filter = TextField()
    a_fronte = BooleanField(default=True)
    vocabularies = TextField()
    last_used = DateTimeField(auto_now_add=True)

    def remove_lexeme(self, lexeme):
        try:
            lexeme_index = self.lexemeindex_set.get(lexeme=lexeme)
            index = lexeme_index.index
            lexeme_index.delete()
            tail = self.lexemeindex_set.filter(index__gt=index)
            tail.update(index=F('index') - 1)
        except LexemeIndex.DoesNotExist:
            pass

    class Meta:
        unique_together = ('filter', 'a_fronte', 'vocabularies')


class LexemeIndex(Model):
    lexeme_list = ForeignKey(LexemeList)
    lexeme = ForeignKey(Lexeme)
    index = IntegerField(db_index=True)
    entry = CharField(max_length=64, db_index=True)

    class Meta:
        unique_together = ('lexeme_list', 'lexeme')
        ordering = ['index']


class SavedExportData(Model):
    serialized_data = TextField()
    name = CharField(max_length=64, unique=True)

    def get_data(self):
        data = json.loads(self.serialized_data)
        temp_dict = {pair['name']: pair['value'] for pair in data}
        data_dict = {
            'variant': temp_dict['variant'],
            'refl': bool(temp_dict.get('refl', False)),
            'commonness': bool(temp_dict.get('commonness', False)),
            'form_qualifiers': True,
            'homonym_numbers': True,
            'copyright': temp_dict['copyright'],
        }
        magic_ids = set()
        magic_qualifiers = []
        normal_qualifiers = []
        vocabs = []
        antivocabs = []
        for pair in data:
            key = pair['name']
            value = pair['value']
            split_key = key.split('-')
            if key == 'vocabs':
                vocabs.append(value)
            elif key == 'antivocabs':
                antivocabs.append(value)
            elif key.startswith('magic') and split_key[0] not in magic_ids:
                i, name = split_key
                magic_ids.add(i)

                qualifier_id = int(temp_dict['%s-qualifier' % i])
                regex = temp_dict['%s-regex' % i]
                if regex:
                    magic_qualifiers.append((regex, qualifier_id))
                else:
                    normal_qualifiers.append(qualifier_id)
        data_dict.update({
            'vocabs': vocabs,
            'antivocabs': antivocabs,
            'magic_qualifiers': magic_qualifiers,
            'excluding_qualifiers': normal_qualifiers,
        })
        return data_dict


class InputLexeme(Model):
    entry = CharField(max_length=64, db_index=True)


class InputForm(Model):
    input_lexeme = ForeignKey(InputLexeme)
    form = CharField(max_length=64, db_index=True)
    # tag = TextField()