models.py 56.2 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 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646
# -*- coding: utf-8 -*-
from collections import OrderedDict
from itertools import izip
import json
from django.conf import settings

from django.contrib.auth.models import User, Permission
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 django_extensions.db.fields.json import JSONField

from common.util import no_history, GroupDict
from accounts.util import users_with_perm


class NotDeletedManager(Manager):
    use_for_related_field = True

    def get_query_set(self):
        return super(NotDeletedManager, self).get_query_set().filter(
            deleted=False)


class LexemeNotDeletedManager(Manager):
    use_for_related_field = True

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


class PatternNotDeletedManager(Manager):
    use_for_related_field = True

    def get_query_set(self):
        return super(PatternNotDeletedManager, self).get_query_set().filter(
            pattern__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)


# to jest tak naprawdę typ odmiany (inflection type)
# patterns?
class LexicalClass(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 PartOfSpeech(Model):
    symbol = CharField(primary_key=True, max_length=16, db_column='pos')
    lexical_class = ForeignKey(LexicalClass, db_column='czm')
    full_name = CharField(max_length=128, db_column='nazwa')
    abbr_name = CharField(max_length=16)
    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'nazwa'))
    vocabulary = ForeignKey(
        'Vocabulary', db_column='slownik', verbose_name=_(u'słownik'))

    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, _('stylistyczny')),
        (TYPE_SCOPE, _('zakresowy')),
        (TYPE_FORM, _('przy formach')),
    )

    label = CharField(max_length=64, db_column='kwal', verbose_name=_(u'nazwa'))
    type = CharField(
        max_length=4, choices=TYPE_CHOICES, verbose_name=_(u'typ'))
    vocabulary = ForeignKey(
        'Vocabulary', db_column='slownik', verbose_name=_(u'słownik'),
        related_name='qualifiers')
    exclusion_class = ForeignKey(
        QualifierExclusionClass, db_column='klasa', null=True, blank=True,
        verbose_name=_(u'klasa wykluczania'))
    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.lexemeinflectionpattern_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
            something.qualifiers.add(self)  # add
        else:
            something.qualifiers.remove(self)  # add

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

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


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


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


class Classification(Model):
    name = CharField(
        unique=True, max_length=64, db_column='nazwa',
        verbose_name=_(u'nazwa klasyfikacji'))
    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'nazwa wartości'))
    classification = ForeignKey(
        Classification, db_column='klas_id', related_name='values')
    parent_node = ForeignKey(
        'self', db_column='rodzic', null=True, blank=True,
        verbose_name=_(u'rodzic wartości'), 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


# patterns
class BaseFormLabel(Model):
    symbol = CharField(max_length=32, blank=True, db_column='efobaz')
    lexical_class = ForeignKey(LexicalClass)
    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']


# ś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


# patterns
class DummyEnding(object):
    def __init__(self, string):
        self.index = 0
        self.string = string
        self.qualifiers = Qualifier.objects.none()


# patterns
class PatternType(Model):
    lexical_class = ForeignKey(
        LexicalClass, db_column='czm', verbose_name=_(u'typ odmiany'))
    # 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'typ wzoru'))
    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']


# patterns
class Pattern(Model):
    STATUS_NEW = 'nowy'
    STATUS_TEMP = 'temp'
    STATUS_CHOICES = (
        (STATUS_NEW, _(u'nowy')),
        (STATUS_TEMP, _(u'tymczasowy')),
    )
    name = CharField(
        max_length=32, unique=True, db_column='w_id', verbose_name=_(u'nazwa'))
    old_name = CharField(
        max_length=32, verbose_name=_(u'stara nazwa'), null=True)
    type = ForeignKey(
        PatternType, db_column='typ', verbose_name=_(u'typ'))
    # rdzeń przykładowej formy hasłowej
    example = CharField(
        max_length=64, db_column='przyklad', verbose_name=_(u'przykład'),
        blank=True)
    basic_form_ending = CharField(
        max_length=32, db_column='zakp', blank=True,
        verbose_name=_(u'zakończenie formy podstawowej'))
    status = CharField(
        max_length=8, choices=STATUS_CHOICES, verbose_name=_(u'status'))
    comment = TextField(
        blank=True, db_column='komentarz', verbose_name=_(u'komentarz'))
    deleted = BooleanField()

    objects = NotDeletedManager()
    all_objects = Manager()

    def ending_set(self, subroot='', tag_prefix=None):
        endings = self.endings
        if tag_prefix:
            endings = endings.filter(
                base_form_label__symbol__startswith=tag_prefix)
        return set(subroot + e
                   for e in endings.values_list('string', flat=True))

    def base_endings(self, label_filter=None):
        bfls = self.type.base_form_labels.all()
        endings = Ending.objects.filter(
            base_form_label__patterntype=self.type, pattern=self) \
            .select_related('base_form_label').prefetch_related('qualifiers')
        if label_filter is not None:
            endings = endings.filter(
                base_form_label__symbol__regex=label_filter)
        bfl_dict = GroupDict((bfl, []) for bfl in bfls)
        for ending in endings:
            bfl_dict.add(ending.base_form_label, ending)
        return bfl_dict

    def __unicode__(self):
        return self.name

    class Meta:
        db_table = 'wzory'
        ordering = ['name']
        permissions = (
            ('view_pattern', _(u'Może oglądać wzory')),
        )


def prepare_table(table):
    for row in table:
        for cell in row:
            if type(cell) == dict and 'forms' in cell:
                cell['forms'].sort()
                seen_forms = []
                unique_forms = []
                form_patterns = {}
                for form in cell['forms']:
                    if form[1] not in seen_forms:
                        seen_forms.append(form[1])
                        unique_forms.append(form)
                        form_patterns[form[1]] = set()
                    form_patterns[form[1]].add(form[3])
                cell['forms'] = [
                    {
                        'form': form,
                        'qualifiers': qualifiers,
                        'patterns': form_patterns[form],
                    }
                    for (key, form, qualifiers, pattern) in unique_forms]
            elif type(cell) == dict and 'label' in cell:
                seen_labels = []

                def is_new(label):
                    new = label not in seen_labels
                    seen_labels.append(label)
                    return new

                cell['label'] = filter(is_new, cell['label'])
    return table


# zakonczenie formy bazowej
# patterns
class Ending(Model):
    pattern = ForeignKey(Pattern, related_name='endings', db_column='w_id')
    # etykieta (tag) formy bazowej
    base_form_label = ForeignKey(BaseFormLabel, db_column='efobaz')
    # kolejnosc dla zakonczen o tym samym base_form_label
    index = IntegerField(db_column='zind')
    string = CharField(max_length=16, db_column='zak', blank=True)
    qualifiers = ManyToManyField(
        Qualifier, blank=True, db_table='kwalifikatory_zakonczen')

    objects = PatternNotDeletedManager()
    all_objects = Manager()

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

    def __unicode__(self):
        return '%s : %s : %s' % (
            self.pattern.name, self.string, self.base_form_label)

    class Meta:
        db_table = 'zakonczenia'
        unique_together = ('pattern', 'base_form_label', 'index')
        ordering = ['base_form_label__index', 'index']


class Lexeme(Model):
    STATUS_CANDIDATE = 'cand'
    STATUS_DESCRIBED = 'desc'
    STATUS_CONFIRMED = 'conf'
    STATUS_CHOICES = (
        (STATUS_CANDIDATE, _(u'kandydat')),
        (STATUS_DESCRIBED, _(u'wprowadzony')),
        (STATUS_CONFIRMED, _(u'zatwierdzony')),
    )

    entry = CharField(
        max_length=64, db_column='haslo', db_index=True,
        verbose_name=_(u'hasło'), blank=True)  # dla nowo utworzonych
    # 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'sufiks hasła'))
    gloss = TextField(blank=True, db_column='glosa', verbose_name=_(u'glosa'))
    note = TextField(blank=True, db_column='nota', verbose_name=_(u'nota'))
    extended_note = TextField(blank=True, verbose_name=_(u'nota rozszerzona'))
    pronunciation = TextField(
        blank=True, db_column='wymowa', verbose_name=_(u'wymowa'))
    valence = TextField(blank=True, verbose_name=_(u'łączliwość'))
    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'źródło'))
    borrowing_source = ForeignKey(
        BorrowingSource, blank=True, null=True,
        verbose_name=_(u'źródło zapożyczenia'))
    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'kwal. z Dor.'))
    qualifiers_style = TextField(blank=True, verbose_name=_(u'kwal. styl.'))
    qualifiers_scope = TextField(blank=True, verbose_name=_(u'kwal. zakr.'))
    # czy termin specjalistyczny
    specialist = BooleanField(default=False, verbose_name=_(u'specjalistyczny'))
    comment = TextField(
        blank=True, db_column='komentarz', verbose_name=_(u'komentarz'))
    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='LexemeInflectionPattern')
    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.lexemeinflectionpattern_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.lexemeinflectionpattern_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, 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.lexemeinflectionpattern_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))

    def all_qualifiers(self, variant='1'):
        qualifiers = set(self.qualifiers.all())
        for lip in self.lexemeinflectionpattern_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 get_root(self.entry, pattern, gender)

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

    def editable_vocabularies(self, user):
        return 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.lexemeinflectionpattern_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.lexemeinflectionpattern_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 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 = 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.lexical_class_id == 'subst':
            lips = self.lexemeinflectionpattern_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':
            vocabs = self.vocabularies.all()
            return bool(vocabs & 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 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 == ['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=visible_vocabularies(user))

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

    class Meta:
        db_table = 'leksemy'
        permissions = (
            ('view_lexeme', _(u'Może oglądać leksemy')),
            ('view_all_lexemes', _(u'Może oglądać wszystkie leksemy')),
            ('lexeme_priority',
             _(u'Ważniejszy głos przy modyfikowaniu leksemów')),
            ('export_lexemes', _(u'Może eksportować leksemy')),
        )


def filter_visible(lexemes, user):
    vocab_ids = [v.id for v in visible_vocabularies(user)]
    # uniknięcie podzapytania *bardzo* zwiększa wydajność!
    return lexemes.filter(vocabularies__id__in=vocab_ids).distinct()


def get_root(basic_form, pattern, gender, use_pattern_ending=False):
    ends = []
    basic_endings = []
    if gender:
        bfl = gender.basic_form_label
        basic_endings = pattern.endings.filter(base_form_label=bfl)
    elif pattern.type.lexical_class.symbol == 'v':
        basic_endings = pattern.endings.filter(base_form_label__symbol='5')
    elif pattern.type.lexical_class.symbol == 'num':
        basic_endings = pattern.endings.filter(
            base_form_label__symbol__in=('1', '1,z1'))
    else:
        use_pattern_ending = True
    if use_pattern_ending:
        ends.append(pattern.basic_form_ending)
    else:
        ends += [e.string for e in basic_endings]
    good_ends = [end for end in ends if basic_form.endswith(end)]
    assert len(set(good_ends)) <= 1  # inaczej rdzeń nie jest jednoznaczny
    if good_ends:
        return basic_form[:len(basic_form) - len(good_ends[0])]
    else:
        if not use_pattern_ending:
            return get_root(
                basic_form, pattern, gender, use_pattern_ending=True)
        else:
            return None


class LexemeAttribute(Model):
    name = CharField(max_length=32, unique=True)
    multilingual_name = ForeignKey(MultilingualText, null=True)
    closed = BooleanField()  # czy jest zamknięta lista wartości
    multiple = BooleanField()
    required = BooleanField()
    parts_of_speech = ManyToManyField(PartOfSpeech)
    takes_gender = BooleanField()
    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)
        lav, created = LexemeAV.objects.get_or_create(
            lexeme=lexeme, attribute_value__attribute=self,
            defaults={'attribute_value': val})
        if not created:
            lav.attribute_value = val
            lav.save()

    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 LexemeInflectionPattern(Model):
    lexeme = ForeignKey(Lexeme, db_column='l_id')
    index = IntegerField(db_column='oind')
    pattern = ForeignKey(Pattern, db_column='w_id', verbose_name=_(u'wzór'))
    gender = ForeignKey(
        Gender, verbose_name=_(u'rodzaj'), 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'wymowa'), 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
        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 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 get_root(self.lexeme.entry, 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 '',
        )

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


def combine_qualifiers(lip_qualifiers, e_qualifiers):
    # qualifiers = set(l_qualifiers)
    if not lip_qualifiers:
        return e_qualifiers
    qualifiers = set()
    for q in list(lip_qualifiers) + list(e_qualifiers):
        if q.exclusion_class:
            excluded = set(q.exclusion_class.qualifier_set.all())
            qualifiers -= excluded
        qualifiers.add(q)
    return qualifiers


def filter_visible_lips(lips, user):
    vocabs = visible_vocabularies(user)
    return lips.filter(lexeme__vocabularies__in=vocabs).distinct()


# Sluzy do doczepienia flag do poszczegolnych form
# poszczegolnych leksemow
# class UncommonForm(Model):
#     lexeme_inflection_pattern = ForeignKey(
#         LexemeInflectionPattern, 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 = (
#             'lexeme_inflection_pattern',
#             '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

    class Meta:
        db_table = 'slowniki'
        ordering = ['id']
        permissions = (
            ('manage_vocabulary', _(u'Może zarządzać słownikami')),
            ('manage_all_vocabularies', _(u'Zarządza wszystkimi słownikami')),
        )


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')


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


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


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


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']


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

    class Meta:
        db_table = 'typyodsylaczy'


class CRManager(Manager):
    use_for_related_field = True

    def get_query_set(self):
        return super(CRManager, self).get_query_set().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'nr docelowy'))
    type = ForeignKey(
        CrossReferenceType, db_column='typods_id', verbose_name=_(u'typ'))

    objects = CRManager()
    all_objects = Manager()

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

    class Meta:
        db_table = 'odsylacze'


class Variant(Model):
    TYPE_TABLE = 'table'
    TYPE_EXPORT = 'export'
    TYPE_CHOICES = (
        (TYPE_TABLE, 'tabelka'),
        (TYPE_EXPORT, 'eksport'),
    )

    id = CharField(max_length=32, primary_key=True, db_column='wariant')
    type = CharField(max_length=10, choices=TYPE_CHOICES)

    def __unicode__(self):
        return self.id

    class Meta:
        db_table = 'warianty'


class TableTemplate(Model):
    name = TextField()
    variant = ForeignKey(Variant)
    parts_of_speech = ManyToManyField(PartOfSpeech)
    pattern_types = ManyToManyField(PatternType)
    attributes = ManyToManyField(LexemeAttribute)
    attribute_values = ManyToManyField(LexemeAttributeValue)
    cell_attributes = ManyToManyField(LexemeAttribute, related_name='templates')
    takes_gender = BooleanField()

    def filter_cells_attr(self, cells, attr_vals):
        for attr in self.cell_attributes.all():
            attr_val = [a for a in attr_vals if a.attribute == attr]
            if len(attr_val) != 1:
                return []
            cells = cells.filter(attribute_values=attr_val[0])
        return cells

    def filter_cells(self, pattern_type, gender, attr_vals):
        if self.variant.type == Variant.TYPE_TABLE:
            cells = self.table_cells.filter(
                pattern_types=pattern_type).select_related('base_form_label')
        else:
            cells = self.export_cells.filter(
                pattern_types=pattern_type).select_related('base_form_label')
        if self.takes_gender and gender:
            cells = cells.filter(genders=gender)
        if attr_vals:
            cells = self.filter_cells_attr(cells, attr_vals)
        return cells

    def filter_table_headers(self, pattern_type, gender, attr_vals):
        headers = self.headers.filter(pattern_types=pattern_type)
        if self.takes_gender and gender:
            headers = headers.filter(genders=gender)
        if attr_vals:
            headers = self.filter_cells_attr(headers, attr_vals)
        return headers

    def render_with_pattern(self, pattern, *args, **kwargs):
        base_endings = pattern.base_endings()
        return self.render(
            pattern.type, *args, pattern=pattern, base_endings=base_endings,
            **kwargs)

    def render_with_pattern_type(self, pattern_type, *args, **kwargs):
        base_endings = pattern_type.dummy_base_endings()
        return prepare_table(self.render(
            pattern_type, *args, base_endings=base_endings, numbers=True,
            **kwargs))

    def render(self, pattern_type, gender, attr_vals, separated=False,
               numbers=False, pattern=None, **forms_kwargs):
        table_cells = self.filter_cells(pattern_type, gender, attr_vals)
        headers = self.filter_table_headers(pattern_type, gender, attr_vals)
        if table_cells is None or headers is None:
            return []
        rows = set()
        last_col = 0
        for table_cell in table_cells:
            rows.add(table_cell.row)
            col = table_cell.col + table_cell.colspan - 1
            if col > last_col:
                last_col = col
        for header in headers:
            rows.add(header.row)
            col = header.col + header.colspan - 1
            if col > last_col:
                last_col = col
        if numbers:
            last_col += 1
        table = [[{'type': 'empty'} for i in xrange(last_col)] for row in rows]
        rows = sorted(rows)
        if numbers:
            for row, table_row in izip(rows, table):
                table_row[0] = {
                    'type': 'header',
                    'label': [row],
                    'css_class': 'blank',
                    'rowspan': 1,
                    'colspan': 1}
        # słownik: nr rzędu w bazie -> rzeczywisty numer rzędu
        row_translate = dict((r, i) for i, r in enumerate(rows))
        for tc in table_cells:
            if numbers:
                x = tc.col
            else:
                x = tc.col - 1
            y = row_translate[tc.row]
            table_cell = table[y][x]
            assert table_cell['type'] != 'span'
            separator = u'·' if separated else u''
            forms = [f + (pattern,)
                     for f in tc.forms(separator=separator, **forms_kwargs)]
            if not forms:
                continue
            if table_cell['type'] == 'empty':
                table[y][x] = {
                    'type': 'forms',
                    'forms': forms,
                    'rowspan': tc.rowspan,
                    'colspan': tc.colspan,
                }
                for i in xrange(tc.colspan):
                    for j in xrange(tc.rowspan):
                        if (i, j) != (0, 0):
                            assert table[y + j][x + i]['type'] == 'empty'
                            table[y + j][x + i]['type'] = 'span'
            else:
                assert tc.rowspan == table_cell['rowspan']
                assert tc.colspan == table_cell['colspan']
                table_cell['forms'] += forms
        for header in headers:
            if numbers:
                x = header.col
            else:
                x = header.col - 1
            y = row_translate[header.row]
            assert table[y][x]['type'] == 'empty'
            table[y][x] = {
                'type': 'label',
                'label': [header.label],
                'css_class': header.css_class,
                'rowspan': header.rowspan,
                'colspan': header.colspan,
            }
            for i in xrange(header.colspan):
                for j in xrange(header.rowspan):
                    if (i, j) != (0, 0):
                        assert table[y + j][x + i]['type'] == 'empty'
                        table[y + j][x + i]['type'] = 'span'
        if numbers:
            first_row = [{
                'type': 'header',
                'label': [col if col != 0 else ''],
                'css_class': 'blank',
                'rowspan': 1,
                'colspan': 1} for col in xrange(last_col)]
            table = [first_row] + table
        return [row for row in table
                if not all(cell['type'] == 'empty' for cell in row)]

    def __unicode__(self):
        return self.name


class Cell(Model):
    base_form_label = ForeignKey(BaseFormLabel)
    prefix = CharField(max_length=20, blank=True)
    suffix = CharField(max_length=20, blank=True)

    def get_index(self):
        return None

    def get_marked_attribute_values(self):
        return set()

    def forms(self, base_endings=None, separator=u'', root=u'',
              lip_qualifiers=None, lip_index=0, qualifiers=None,
              edit_view=False, span=False, depr=None):
        if qualifiers:
            qualifiers_set = set(qualifiers)

        def filter_quals(quals):
            if not qualifiers:
                return set(quals)
            else:
                return set(quals) & qualifiers_set

        if lip_qualifiers and not edit_view:
            # l_qual = filter_quals(lexeme_qualifiers)
            lip_qual = filter_quals(lip_qualifiers)
        else:
            lip_qual = set()
        endings = base_endings[self.base_form_label]
        if span:
            form_template = (
                '<span class="root">%s</span>'
                '%s<span class="ending">%s</span>')
        else:
            form_template = '%s%s%s'
        if self.prefix and span:
            form_template = '<span class="prefix">%s</span>' + form_template
        else:
            form_template = '%s' + form_template
        if self.suffix and span:
            form_template += '<span class="suffix">%s</span>'
        else:
            form_template += '%s'
        if depr and depr in self.get_marked_attribute_values():
            form_template = (
                '<span class="marked-form" title="%s">%s</span>'
                % (depr.display_value, form_template))
        forms = [
            (
                (self.get_index(), lip_index, ending.index),
                (form_template % (
                 self.prefix, root, separator, ending.string, self.suffix)),
                combine_qualifiers(
                    lip_qual, filter_quals(ending.qualifiers.all())),
            )
            for ending in endings
        ]
        return forms

    class Meta:
        abstract = True


class TableCell(Cell):
    table_template = ForeignKey(TableTemplate, related_name='table_cells')
    pattern_types = ManyToManyField(PatternType)
    genders = ManyToManyField(Gender)
    attribute_values = ManyToManyField(
        LexemeAttributeValue, related_name='table_cells')
    marked_attribute_values = ManyToManyField(
        LexemeAttributeValue, related_name='marked_cells')
    row = IntegerField()
    col = IntegerField()
    rowspan = IntegerField()
    colspan = IntegerField()
    index = IntegerField()

    def get_index(self):
        return self.index

    def get_marked_attribute_values(self):
        return set(self.marked_attribute_values.all())

    def __unicode__(self):
        return '%s:%s %s-%s-%s (%s, %s)' % (
            self.table_template.variant.id, self.table_template.name,
            self.prefix, self.base_form_label.symbol, self.suffix,
            self.row, self.col)


class ExportCell(Cell):
    table_template = ForeignKey(TableTemplate, related_name='export_cells')
    pattern_types = ManyToManyField(PatternType)
    genders = ManyToManyField(Gender)
    attribute_values = ManyToManyField(LexemeAttributeValue)
    tag_template = TextField()

    def __unicode__(self):
        return '%s:%s %s-%s-%s (%s)' % (
            self.table_template.variant.id, self.table_template.name,
            self.prefix, self.base_form_label.symbol, self.suffix,
            self.tag_template)


class TableHeader(Model):
    table_template = ForeignKey(TableTemplate, related_name='headers')
    pattern_types = ManyToManyField(PatternType)
    genders = ManyToManyField(Gender)
    attribute_values = ManyToManyField(LexemeAttributeValue)
    row = IntegerField()
    col = IntegerField()
    rowspan = IntegerField()
    colspan = IntegerField()
    label = CharField(max_length=64, blank=True, db_column='nagl')
    css_class = CharField(max_length=8, db_column='styl')


# 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(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)

    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

    # TODO: insert_lexeme (trzeba sprawdzić filtry i odpowiednio wsortować)

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


class LexemeIndex(Model):
    lexeme_list = ForeignKey(LexemeList)
    lexeme = ForeignKey(Lexeme)
    index = IntegerField()

    class Meta:
        unique_together = (
            ('lexeme_list', 'lexeme'),
            ('lexeme_list', 'index'),
        )
        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


# model przeznaczony tylko do odczytu!
# history
class History(Model):
    table_name = CharField(max_length=120, db_column='table_name_')
    column_name = CharField(
        max_length=120, db_column='column_name_', blank=True)
    timestamp = DateTimeField(db_column='timestamp_')
    user = ForeignKey(User, db_column='user_id_', db_index=True)
    old_value = TextField(db_column='old_value_', blank=True)
    new_value = TextField(db_column='new_value_', blank=True)
    lexeme = ForeignKey(
        Lexeme, db_column='lexeme_id_', null=True, blank=True, db_index=True)
    pattern = ForeignKey(
        Pattern, db_column='pattern_id_', null=True, blank=True, db_index=True)
    row_id = IntegerField(db_column='id_')
    operation = CharField(max_length=120, db_column='operation_')
    table_oid = IntegerField(db_column='table_oid_')
    column_ord = IntegerField(db_column='ordinal_position_of_column_')
    transaction_began = DateTimeField(
        db_column='transaction_began_', db_index=True)

    def __unicode__(self):
        return '%s %s.%s %s -> %s' % (
            self.operation, self.table_name, self.column_name,
            repr(self.old_value),
            repr(self.new_value))

    class Meta:
        db_table = 'history'


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()