models.py 79.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 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783
#-*- coding:utf-8 -*-

from django.contrib.auth.models import User
from django.db.models import * 

class Configuration(Model):
    name = CharField(max_length=16, primary_key=True, unique=True, db_column='nazwa_konfiguracji')
    selected_conf = BooleanField(db_column='wybrana_konfiguracja', default=False)
    min_1M_freq = PositiveIntegerField(db_column='minimalna_frekwencja_1M', default=3)
    min_300M_freq = PositiveIntegerField(db_column='minimalna_frekwencja_300M', default=25)
    
    def __unicode__(self):
        return '%s' % self.name             

    class Meta:
        db_table = 'konfiguracje_narzedzia'
        ordering = ['name']
        permissions = (
          ('manage_tool_configuration', u'Moลผe zmieniaฤ‡ konfiguracjฤ™ narzฤ™dzia.'),
        )


class VocabularyFormat(Model):
    format = CharField(max_length=64, primary_key=True, unique=True, db_column='format_slownika')
    
    def __unicode__(self):
        return '%s' % self.format
  
class WalentyStat(Model):
    date = DateTimeField(auto_now_add=True, db_column='data_aktualizacji')
    label = CharField(max_length=128, db_column='etykieta')
    value = CharField(max_length=16, db_column='wartosc')
    
    def __unicode__(self):
        return '%s:\t%s' % (self.label, self.value)
   
class Vocabulary(Model):
    name = CharField(max_length=64, primary_key=True, unique=True, db_column='slownik') 
    editors = ManyToManyField(User, blank=True,
                            related_name='editable_vocabularies')
    viewers = ManyToManyField(User, blank=True,
                            related_name='visible_vocabularies')
    
    def __unicode__(self):
        return '%s' % self.name

    class Meta:
        db_table = 'slowniki'
        ordering = ['name']
        permissions = (
          ('manage_vocabulary', u'Moลผe zarzฤ…dzaฤ‡ sล‚ownikami'),
          ('download_vocabulary', u'Moลผe eksportowaฤ‡ sล‚owniki'),
          ('view_vocab_stats', u'Moลผe oglฤ…daฤ‡ statystyki sล‚ownikรณw'),
        )

def visible_vocabularies(user):
  if user.has_perm('dictionary.view_all_lemmas'):
    return Vocabulary.objects.all()
  else:
    return user.visible_vocabularies.all()

class Lemma_Status(Model):
    status = CharField(max_length=16, db_column='status', verbose_name=u'status')
    # okresla czy po zmianie na wskazany status trzeba walidowac schematy
    validate = BooleanField(db_column='czy_walidowac', default=False) 
    # okresla czy po zmianie na wskazany status trzeba sprawdzac czy potwierdzono przyklady wlasne
    check_examples = BooleanField(db_column='sprawdzic_przyklady', default=False)
    # okresla czy po zmianie na wskazany status trzeba sprawdzac ramki semantyczne
    check_semantics = BooleanField(db_column='sprawdzic_semantyke', default=False)
    # kolejne mozliwe statusy edycyjne
    next_statuses = ManyToManyField('Lemma_Status', related_name='prev_statuses',
                                    blank=True, null=True)
    # status edycyjny do ktรณrego przechodzi siฤ™ przy cofaniu aktualnego
    abort_status = ForeignKey('Lemma_Status', related_name='nabort_statuses',
                              blank=True, null=True)
    # typ statusu
    type = ForeignKey('LemmaStatusType', related_name='lemma_statuses',
                      blank=True, null=True)
    # etap prac
    stage_of_work = ForeignKey('StageOfWork', related_name='lemma_statuses')
    # okresla kolejnosc prezentowania statusow
    priority = PositiveIntegerField(db_column='priorytet', blank=True, null=True)
    
    def __unicode__(self):
        return '%s' % (self.status) 
    
    class Meta:
      db_table = 'statusy_hasel'
      ordering = ['priority']
      permissions = (
        ('confirm_lemma', u'Moลผe zaznaczaฤ‡ hasล‚a jako sprawdzone.'),
        ('see_stats', u'Moลผe oglฤ…daฤ‡ swoje statystyki.'),
        ('see_all_stats', u'Moลผe oglฤ…daฤ‡ statystyki wszystkich.'),
      )
      
def get_checked_statuses():
    checked_type = LemmaStatusType.objects.get(sym_name='checked')
    return Lemma_Status.objects.filter(type__priority__gte=checked_type.priority).distinct()
      
def get_ready_statuses():
    ready_type = LemmaStatusType.objects.get(sym_name='ready')
    return Lemma_Status.objects.filter(type__priority__gte=ready_type.priority).distinct()

def get_statuses(min_status_type):
    min_type = LemmaStatusType.objects.get(sym_name=min_status_type)
    return Lemma_Status.objects.filter(type__priority__gte=min_type.priority).distinct()
 
      
class LemmaStatusType(Model):
    # nazwa statusu
    name = CharField(max_length=16, db_column='nazwa', verbose_name=u'nazwa')
    # symboliczna nazwa statusu
    sym_name = CharField(max_length=16, db_column='symboliczna_nazwa', verbose_name=u'symboliczna_nazwa')
    # okresla kolejnosc prezentowania typow statusow
    priority = PositiveIntegerField(db_column='priorytet', blank=True, null=True)
    
    def __unicode__(self):
        return '%s' % (self.name)
    
    class Meta:
        ordering = ['priority']
        
# zakres pracy dla ktorego obowiazuje status
class StageOfWork(Model):
    name = CharField(max_length=16, db_column='nazwa', verbose_name=u'nazwa')
    sym_name = CharField(max_length=16, db_column='symboliczna_nazwa', verbose_name=u'symboliczna_nazwa')
    priority = PositiveIntegerField(db_column='priorytet', blank=True, null=True)
    
    def __unicode__(self):
        return '%s' % (self.name)
    
    class Meta:
        ordering = ['priority']

class Lemma(Model):
    id = AutoField(primary_key=True, db_column='l_id')
    # czasownik w bezokoliczniku --> musi zostac wywalone, entry bedzie pochodzilo z obiektow modelu Entry
    entry = CharField(max_length=64, db_column='haslo', db_index=True,
                      verbose_name=u'hasล‚o')
    # wpis w slowniku --> koniecznie to musi zostac dodane jako glowne powiazanie z entry
    entry_obj = ForeignKey('Entry', db_column='wpis', 
                            related_name='lemmas')
    # wlasciciel hasla
    owner = ForeignKey(User, blank=True, null=True, db_column='wlasciciel',
                       related_name='lemmas')
    # przypisany frazeolog
    phraseologist = ForeignKey(User, blank=True, null=True, db_column='frazeolog',
                               related_name='phraseologist_lemmas')
    # przypisany semantyk
    semanticist = ForeignKey(User, blank=True, null=True, db_column='semantyk',
                               related_name='semanticist_lemmas')
    # slownik do ktorego nalezy
    vocabulary = ForeignKey('Vocabulary', db_column='slownik', 
                            related_name='lemmas')
    # status obrobki hasla
    status = ForeignKey('Lemma_Status', db_column='status', 
                       related_name='lemmas')
    # osoba blokujaca haslo na czas zapisu
    locker = ForeignKey(User, blank=True, null=True, db_column='osoba_zapisujaca',
                       related_name='locked_lemmas')
    # liczba wystapien lematu w 300 milionowym podkorpusie NKJP
    frequency_300M = PositiveIntegerField(db_column='frekwencja_300M', blank=True, 
                                          null=True, default=0)
    # liczba wystapien lematu w 1 milionowym podkorpusie NKJP
    frequency_1M = PositiveIntegerField(db_column='frekwencja_1M', blank=True, 
                                        null=True, default=0)
    # dawne wersje hasla
    old_versions = ManyToManyField('Change', db_table='haslo_stareWersje',
                                   blank=True, null=True, related_name='lemmas')
    # czy haslo jest aktualne czy nie (pomocne w systemie kontroli zmian)
    old = BooleanField(db_column='nieaktualny') 
    #schematy
    frames = ManyToManyField('Frame', db_table='haslo_ramki', blank=True, null=True,
                             related_name='lemmas')
    # stare schematy
    old_frames = ManyToManyField('Old_Frame', db_table='haslo_stareRamki',
                                 blank=True, null=True, related_name='lemmas')
    #schematy ze skล‚adnicy
    skladnica_frames = ManyToManyField('Old_Frame', db_table='haslo_ramkiSkladnicowe',
                                       blank=True, null=True, related_name='lemmas_skladnica')
    #schematy B
    B_frames = ManyToManyField('B_Frame', db_table='haslo_ramkiB',
                               blank=True, null=True, related_name='lemmas_B')
    # przyklady z NKJP dla ramek hasla
    nkjp_examples = ManyToManyField('NKJP_Example', db_table='haslo_ramkiNkjp',
                                    blank=True, null=True, related_name='lemmas')
    # oceny ramek dla ramek hasla
    frame_opinions = ManyToManyField('Frame_Opinion', db_table='lemma_ramkiOpinie',
                                     blank=True, null=True, related_name='lemmas')
    # przyklady przyporzadkowane do hasla jako calosci (pola frame i atributes beda niewypelniane)
    lemma_nkjp_examples = ManyToManyField('NKJP_Example', db_table='haslo_Nkjp',
                                    blank=True, null=True, related_name='lemmasSelf')
    # historia zmiany statusรณw hasล‚a
    status_history = ManyToManyField('StatusChange', db_table='historia_statusow',
                                    blank=True, null=True, related_name='lemmas')
    
    def __unicode__(self):
        return '%s (%s)' % (self.entry, self.vocabulary)
  
    def get_frames_by_char_values(self, reflex_val, neg_val, pred_val, aspect_val):
        matching_frames = self.frames.filter(characteristics__value=reflex_val
                                             ).filter(characteristics__value=neg_val
                                                      ).filter(characteristics__value=pred_val
                                                               ).filter(characteristics__value=aspect_val)
        return matching_frames
    
    def get_existing_frame_char_values(self, char_type):
        char_vals = []
        for frame in self.frames.all():
            char_value = frame.get_char_value(char_type)
            if not char_value in char_vals:
                char_vals.append(char_value.pk)
        char_vals_query = Frame_Char_Value.objects.filter(pk__in=char_vals)
        return char_vals_query.order_by('-priority')
    
    def contains_frame_with_exact_positions(self, positions):
        contains = False
        frames = self.frames
        for position in positions:
            frames = frames.filter(positions__text_rep=position.text_rep)
        for frame in frames.all():
            if frame.positions.count() == len(positions):
                contains = True
                break   
        return contains
    
    def get_schema_opinion(self, frame):
        frame_opinion_name = ''
        try:
            frame_opinion = self.frame_opinions.get(frame=frame)
            frame_opinion_name = unicode(frame_opinion)
        except Frame_Opinion.DoesNotExist:
            pass
        return frame_opinion_name

    def phraseology_ready(self):
        actual_status = self.status
        ready_status = Lemma_Status.objects.filter(type__sym_name='ready').order_by('priority')[0]
        if (actual_status.priority >= ready_status.priority and
                    actual_status.type.sym_name != 'edit_f'):
            return True
        return False
        
    def semantics_ready(self):
        actual_status = self.status
        ready_s_status = Lemma_Status.objects.get(type__sym_name='ready_s')
        if actual_status.priority >= ready_s_status.priority:
            return True
        return False
        
    class Meta:
      db_table = 'hasla'
      permissions = (
        ('view_lemma', u'Moลผe oglฤ…daฤ‡ leksemy'), 
        ('change_all_lemmas', u'Moลผe zmieniaฤ‡ wszystkie hasล‚a'),
        ('change_lemmas', u'Moลผe zmieniaฤ‡ hasล‚a, ktรณrych jest wล‚aล›cicielem'),
        ('add_phraseologic_frames', u'Moลผe dodawaฤ‡ ramki frazeologiczne'),
        ('add_syntactic_frames', u'Moลผe dodawaฤ‡ ramki syntaktyczne'),
        ('add_semantic_frames', u'Moลผe dodawaฤ‡ ramki semantyczne'),
        ('own_lemmas', u'Moลผe posiadaฤ‡ hasล‚a'),
        ('enter_lemmas', u'Moลผe tworzyฤ‡ lemat jednostki'),
      )
      
class B_Frame(Model):
    # tekst ramki zgodnie ze skladnica
    text_rep = TextField(db_column='tekst_rep', verbose_name=u'reprezentacja tekstowa')
    # identyfikator tekstowy ramki
    text_id = TextField(db_column='tekst_id', verbose_name=u'identyfikator tekstowy', unique=True)
    # zwrotnosc
    reflex = BooleanField(db_column='zwrotnosc')
    # aspect
    aspect = ForeignKey('Frame_Characteristic', db_column='aspekt', related_name='B_frames_aspect')
    # negatywnosc
    negativity = ForeignKey('Frame_Characteristic', db_column='negatywnosc', related_name='B_frames_negativity')
    # przechodnioล›ฤ‡
    trans = BooleanField(db_column='przechodniosc')
    # niewlasciwa
    impr = BooleanField(db_column='niewlasciwosc')
    # argumenty starej ramki
    arguments = ManyToManyField('B_Argument', related_name='B_frames', null=True, blank=True)
    
    def __unicode__(self):
        return self.text_rep
 
    
class B_Argument(Model):
    # reprezentacja tekstowa argumentu, zgodnie z notacja skladnicowa
    text_rep = CharField(max_length=64, db_column='reprezentacja_tekstowa', 
                         verbose_name=u'reprezentacja tekstowa')
    # identyfikator tekstowy argumentu zgodnie z notacja B
    text_id = CharField(max_length=64, db_column='identyfikator_tekstowy', 
                        verbose_name=u'identyfikator_tekstowy', unique=True)
#    # wyrazenie regularne reprezentujace argument
#    regex = CharField(max_length=64, db_column='regex', 
#                      verbose_name=u'wyrazenie_regularne')
    # moลผliwe argumenty
    possible_args = ManyToManyField('Arg_Possibility',
                                    related_name='B_args',
                                    null=True, blank=True)
    
    def __unicode__(self):
        return u'%s' % (self.text_rep)


class StatusChange(Model):
    """Model representing lemma status changes."""
    # data dokonania zmiany statusu
    date = DateTimeField(auto_now_add=True, db_column='data_zmiany')
    # aktualny wlasciciel hasla
    act_owner = ForeignKey(User, db_column='aktualny_wlasciciel', 
                           related_name='status_changes',
                           blank=True, null=True)
    # osoba ktora zmienila status hasla
    changer = ForeignKey(User, db_column='zmieniajacy', 
                           related_name='chn_status_changes',
                           blank=True, null=True)
    # aktualna postac hasla, po zmianie statusu
    lemma = ForeignKey(Lemma, db_column='postac_hasla', related_name='status_changes',
                       blank=True, null=True)
    # ramy semantyczne w momencie zmiany statusu
    semantic_frames = ManyToManyField('semantics.SemanticFrame',
                                      blank=True, null=True, related_name='status_changes')
    # osiagniety status
    status = ForeignKey(Lemma_Status, db_column='status', 
                        related_name='status_changes',
                        blank=True, null=True)
    
    def __unicode__(self):
        return self.lemma.entry + ':' + str(self.date)
  
    class Meta:
        permissions = (
            ('view_status_changes', u'Moลผe oglฤ…daฤ‡ historiฤ™ zmian statusu'),
        )

class Frame_Opinion(Model):
    frame = ForeignKey('Frame', db_column='ramka', related_name='opinions',
                       blank=True, null=True)
    value = ForeignKey('Frame_Opinion_Value', db_column='opinia', related_name='opinions')
    
    def __unicode__(self):
        return self.value.value


class Frame_Opinion_Value(Model):
    value = CharField(max_length=16, db_column='wartosc', 
                         verbose_name=u'wartosc', unique=True)
    short = CharField(max_length=16, db_column='nazwa_skrocona', 
                         verbose_name=u'nazwa_skrocona', blank=True, 
                         null=True)
    # okresla kolejnosc prezentowania opinii
    priority = PositiveIntegerField(db_column='priorytet', blank=True, null=True)
    
    def __unicode__(self):
      return '%s' % (self.value)


class Frame(Model):
    id = AutoField(primary_key=True, db_column='f_id')
    # pozycje skladniowe
    positions = ManyToManyField('Position', db_table='ramka_pozycje', 
                                blank=True, null=True, related_name='frames')
    # charakterystyki ramy
    characteristics = ManyToManyField('Frame_Characteristic', db_table='cechy_ramy',
                                      blank=True, null=True, related_name='frames')  
    # reprezentacja tekstowa
    text_rep = TextField(db_column='tekst_rep', verbose_name=u'reprezentacja tekstowa', unique=True)
    # czy ramka bazowa (widoczna w interfejsie dodawania nowych ramek jako bazowa)
    base = BooleanField(db_column='ramka_bazowa', default=False)
    # czy ramka jest frazeologiczna
    phraseologic = BooleanField(db_column='frazeologiczna', default=False)
    
    def char_exists(self, char_type, value):
        return self.characteristics.filter(type=char_type, 
                                           value__value=value).exists()
                                           
    def has_phraseologic_arguments(self):
        has_phraseologic_arguments = False
        phraseologic_arg_models = Argument_Model.objects.filter(phraseologic=True)
        phraseologic_arg_models_names = [arg_model.arg_model_name for arg_model in phraseologic_arg_models.all()]
        if (self.positions.filter(arguments__type__in=phraseologic_arg_models_names).exists() or
            self.positions.filter(arguments__atributes__values__argument__type__in=phraseologic_arg_models_names).exists()):
            has_phraseologic_arguments = True
        return has_phraseologic_arguments
    
    def has_inactive_arguments(self):
        has_inactive_arguments = False
        inactive_arg_models = Argument_Model.objects.filter(active=False)
        inactive_arg_models_names = [arg_model.arg_model_name for arg_model in inactive_arg_models.all()]
        if self.positions.filter(arguments__type__in=inactive_arg_models_names).exists():
            has_inactive_arguments = True
        return has_inactive_arguments
    
    def is_fully_lexicalized(self):
        is_fully_lexicalized = True
        for position in self.positions.all():
            if not position.is_fully_lexicalized():
                is_fully_lexicalized = False
                break
        return is_fully_lexicalized
    
    def get_char_value(self, char_type):
        return self.characteristics.get(type=char_type).value
    
    def get_position_spaced_text_rep(self):
        sorted_frame_chars = sortFrameChars(self.characteristics.all())
        sorted_positions = sortPositions(self.positions.all())
        frame_chars_str_tab = [char.value.value for char in sorted_frame_chars]
        positions_str_tab = [position['position'].text_rep for position in sorted_positions]
        return u'%s:%s' % (':'.join(frame_chars_str_tab), ' + '.join(positions_str_tab))
    
    def __unicode__(self):
        return '%s' % (self.text_rep)
    
def positions_to_frame(positions, reflex, negativity, predicativity, aspect):
    frame_chars = []
    frame_chars.append(Frame_Characteristic.objects.get(type=u'ZWROTNOลšฤ†', value__value=reflex))
    frame_chars.append(Frame_Characteristic.objects.get(type=u'NEGATYWNOลšฤ†', value__value=negativity))
    frame_chars.append(Frame_Characteristic.objects.get(type=u'PREDYKATYWNOลšฤ†', value__value=predicativity))
    frame_chars.append(Frame_Characteristic.objects.get(type=u'ASPEKT', value__value=aspect))

    frame_chars = sortFrameChars(frame_chars)
        
    frame_char_vals_str_ls = list([frame_char.value.value for frame_char in frame_chars])
    sorted_positions = sortPositions(positions)
    sort_pos_str_tab = [position['position'].text_rep for position in sorted_positions]
    
    frame_text_rep = u'%s:%s' % (':'.join(frame_char_vals_str_ls), 
                                 '+'.join(sort_pos_str_tab))
    try: 
        frame_obj = Frame.objects.get(text_rep=frame_text_rep)
    except Frame.DoesNotExist:
        frame_obj = Frame(text_rep=frame_text_rep)
        frame_obj.save()
        frame_obj.positions.add(*positions)
        frame_obj.characteristics.add(*frame_chars)
        if frame_obj.has_phraseologic_arguments():
            frame_obj.phraseologic = True
            frame_obj.save()
    return frame_obj

def get_schemata_by_type(sch_type, schemata_query):
    if sch_type == 'normal':
        schemata_query = get_normal_schemata_only(schemata_query)
    elif sch_type == 'phraseologic':
        schemata_query = get_phraseologic_schemata_only(schemata_query)
    return schemata_query

def get_normal_schemata_only(schemata_query):
    schemata_query = schemata_query.filter(phraseologic=False)
    return schemata_query

def get_phraseologic_schemata_only(schemata_query):
    schemata_query = schemata_query.filter(phraseologic=True)
    return schemata_query


class NKJP_Example(Model):
    # ramka ktorej dotyczy przyklad
    frame = ForeignKey(Frame, db_column='ramka', related_name='nkjp_examples',
                       blank=True, null=True)
    # argumenty ramki, ktorych dotyczy przyklad
    arguments = ManyToManyField('NKJP_ArgSelection', db_table='przykladNKJP_argumenty',
                                blank=True, null=True, related_name='nkjp_examples')
    # przykladowe zdanie
    sentence = TextField(db_column='zdanie', verbose_name=u'zdanie')
    # zrodlo
    source = ForeignKey('NKJP_Source', db_column='zrodlo', related_name='nkjp_examples')
    # komentarz do przykladu
    comment = TextField(db_column='komentarz', verbose_name=u'komentarz')#,
                        #blank=True, null=True)
    # ocena przykladu
    opinion = ForeignKey('NKJP_Opinion', db_column='opinia', related_name='nkjp_examples')
    # osoby ktore zatwierdzily haslo
    approvers = ManyToManyField(User, db_table='osoby_zatwierdzajace',
                                blank=True, null=True, related_name='nkjp_examples')
    # jesli przyklad wlasny, to czy zostal w pelni zatwierdzony
    approved = BooleanField(db_column='zatwierdzony', default=True)
    
    # czy przyklad zostal dodany w trakcie prac semantycznych
    semantic = BooleanField(db_column='semantyczny', default=False)
    
    
    def __unicode__(self):
        return '%s' % (self.sentence)
  
    def pinned_to(self, arg_text_rep):
        if self.arguments.filter(arguments__text_rep=arg_text_rep):
            return True
        return False
  
    class Meta:
        db_table = 'nkjp_przyklad'
        permissions = (
          ('confirm_example', u'Moลผe potwierdzaฤ‡ przykล‚ady.'),
          ('modify_lemma_examples', u'Moลผe modyfikowaฤ‡ przykล‚ady niedowiฤ…zane'),
          ('modify_all_examples', u'Moลผe dowolnie modyfikowaฤ‡ przykล‚ady'),
          
          ('add_semantic_examples', u'Moลผe dodawaฤ‡ przykล‚ady semantyczne'), 
          ('delete_semantic_examples', u'Moลผe usuwaฤ‡ przykล‚ady semantyczne'),
        )
      
def get_or_create_nkjp_example(frame, arguments, sentence, source,
                                   comment, opinion, approvers, approved, semantic):
    created = False
    example = None
    possible_examples = NKJP_Example.objects.filter(frame=frame, sentence=sentence,
                                                    source=source, comment=comment,
                                                    opinion=opinion, approved=approved,
                                                    semantic=semantic)
    if possible_examples.exists():
        for arg_sel in arguments:
            possible_examples = possible_examples.filter(arguments=arg_sel)
        for approver in approvers:
            possible_examples = possible_examples.filter(approvers=approver)
        if possible_examples.exists():
            for poss_example in possible_examples.all():
                if (poss_example.approvers.count() == len(approvers) and
                    poss_example.arguments.count() == len(arguments)):
                    example = poss_example
                    break
            if not example:
                example = create_nkjp_example(frame=frame, arguments=arguments, 
                                              sentence=sentence, source=source,
                                              comment=comment, opinion=opinion, 
                                              approvers=approvers, approved=approved,
                                              semantic=semantic)
                created = True
        else:
            example = create_nkjp_example(frame=frame, arguments=arguments, 
                                          sentence=sentence, source=source,
                                          comment=comment, opinion=opinion, 
                                          approvers=approvers, approved=approved,
                                          semantic=semantic)
            created = True
    else:
        example = create_nkjp_example(frame=frame, arguments=arguments, 
                                      sentence=sentence, source=source,
                                      comment=comment, opinion=opinion, 
                                      approvers=approvers, approved=approved,
                                      semantic=semantic)
        created = True
    return example, created
        
def create_nkjp_example(frame, arguments, sentence, source,
                           comment, opinion, approvers, approved, semantic):
    example = NKJP_Example(frame=frame, sentence=sentence,
                           source=source, comment=comment,
                           opinion=opinion, approved=approved,
                           semantic=semantic)
    example.save()
    example.arguments.add(*arguments)
    example.approvers.add(*approvers)
    return example
      
class NKJP_ArgSelection(Model):
    # pozycja na ktorej wystepuja zaznaczone argumenty
    position = ForeignKey('Position', db_column='pozycja', related_name='nkjp_argSelections',
                          blank=True, null=True)
    # zaznaczone argumenty
    arguments = ManyToManyField('Argument', db_table='wyborArgNKJP_argumenty',
                                blank=True, null=True, related_name='nkjp_argSelections')
    
    def __unicode__(self):
      arguments = [arg.text_rep for arg in self.arguments.all()] 
      return '[%s <%s>]' % (self.position.text_rep, u';'.join(arguments))
  
def get_or_create_nkjp_arg_selection(position, arguments):
    created = False
    nkjp_arg_selection = None
    possible_selections = NKJP_ArgSelection.objects.filter(position=position)
    if possible_selections.exists():
        for arg in arguments:
            possible_selections = possible_selections.filter(arguments=arg)
        if possible_selections.exists():
            for poss_sel in possible_selections:
                if poss_sel.arguments.count() == len(arguments):
                    nkjp_arg_selection = poss_sel
                    break
            if not nkjp_arg_selection:
                nkjp_arg_selection = create_nkjp_arg_selection(position, arguments)
                created = True
        else:
            nkjp_arg_selection = create_nkjp_arg_selection(position, arguments)
            created = True
    else:
        created = True
        nkjp_arg_selection = create_nkjp_arg_selection(position, arguments)
    return nkjp_arg_selection, created

def create_nkjp_arg_selection(position, arguments):
    nkjp_arg_selection = NKJP_ArgSelection(position=position)
    nkjp_arg_selection.save()
    nkjp_arg_selection.arguments.add(*arguments)
    return nkjp_arg_selection
    
      
class NKJP_Source(Model):
    source = CharField(max_length=64, db_column='zrodlo', 
                         verbose_name=u'zrodlo', unique=True)
    sym_name = CharField(max_length=32, db_column='nazwa_symboliczna', default='NKJP')
    # flaga oznaczajaca czy dane zrodlo wymaga dodatkowego komentarza
    comment_required = BooleanField(db_column='wymagany_komentarz', default=False)
    # flaga oznaczajaca czy przyklad pochodzacy z dango zrodla powinien zostac potwierdzony
    confirmation_required = BooleanField(db_column='wymagane_potwierdzenie', default=False)
    # okresla kolejnosc prezentowania zrodel przykladu
    priority = PositiveIntegerField(db_column='priorytet', blank=True, null=True)
    
    class Meta:
        ordering = ['priority']
    
    def __unicode__(self):
        return '%s' % (self.source)
 
  
class NKJP_Opinion(Model):
    opinion = CharField(max_length=16, db_column='opinia', 
                         verbose_name=u'opinia', unique=True)
    # okresla kolejnosc prezentowania opinii o przykladzie na liscie rozwijanej
    priority = PositiveIntegerField(db_column='priorytet', blank=True, null=True)
    
    class Meta:
      ordering = ['priority']
    
    def __unicode__(self):
      return '%s' % (self.opinion)
 
      
class Position(Model):
    # kategoria pozycji
    categories = ManyToManyField('PositionCategory', db_table='pozycja_kategorie', null=True, blank=True,
                             related_name='positions')
    # argumenty pozycji
    arguments = ManyToManyField('Argument', db_table='pozycja_argumenty',
                                blank=True, null=True, related_name='positions')
    # reprezentacja tekstowa                                                 # UWAGA, TUTAJ swieze, wywalono unique
    text_rep = TextField(db_column='tekst_rep', verbose_name=u'reprezentacja tekstowa')#, unique=True)
    # liczba wystapien
    occurrences = PositiveIntegerField(db_column='wystapienia', default=0)
    
    def is_fully_lexicalized(self):
        is_fully_lexicalized = True        
        for argument in self.arguments.all():
            if not argument.is_fully_lexicalized():
                is_fully_lexicalized = False
                break
        return is_fully_lexicalized
    
    def ordered_categories(self):
        return self.categories.order_by('priority')
    
    def __unicode__(self):
      return '%s' % (self.text_rep)
    
    class Meta:
      db_table = 'pozycje_skladniowe'
      
def get_or_create_position(categories=[], arguments=[]): # nie uwzglednia kontroli, dodac w razie czego
    arguments = list(set(arguments))
    arguments = sortArguments(arguments)
    categories_str_tab = [category.category for category in categories]
    categories_str_tab = sortPosCatsAsStrTab(categories_str_tab)
    args_str_tab = [arg.text_rep for arg in arguments]   
    pos_text_rep = '%s{%s}' % (','.join(categories_str_tab),';'.join(args_str_tab))
    pos_objs = Position.objects.filter(text_rep=pos_text_rep) 
    if pos_objs.count() > 0:
        pos_obj = pos_objs.all()[0]
    else:
        pos_obj = Position(text_rep=pos_text_rep)
        pos_obj.save()
        pos_obj.arguments.add(*arguments)
        pos_obj.categories.add(*categories)
    return pos_obj

def sort_positions(positions):
    sorted_positions_dicts = sortPositions(positions)
    return [position['position'] for position in sorted_positions_dicts]
      
class PositionCategory(Model):
    category = CharField(max_length=16, db_column='kategoria', 
                         verbose_name=u'typ', unique=True)
    # czy kategoria jest zwiazana z kontrola
    control = BooleanField(db_column='kontrola')
    #priorytet prezentowania danych
    priority = PositiveIntegerField(db_column='priorytet', blank=True, null=True)
    # mozliwe czesci mowy
    poss = ManyToManyField('POS', db_table='czesci_mowy_kategorie_pozycji',
                           related_name='position_categories')
    
    def __unicode__(self):
        return '%s' % (self.category)
  
    
class Argument(Model): 
    type = CharField(max_length=16, db_column='typ_argumentu', 
                     verbose_name=u'typ argumentu')
    atributes = ManyToManyField('Atribute', db_table='argument_atrybuty', 
                                null=True, blank=True, related_name='arguments')
    # reprezentacja tekstowa
    text_rep = TextField(db_column='tekst_rep', verbose_name=u'reprezentacja tekstowa', unique=True)
    realizations = ManyToManyField('ArgRealization', db_table='realizacje', 
                                   related_name='arguments', null=True, blank=True)
    
    def model_used_for_limitations(self):
        model_for_limitations = Argument_Model.objects.get(arg_model_name=self.type)
        if model_for_limitations.use_subarg_for_limitations:
            sub_arg_model_name = self.atributes.all()[0].values.all()[0].argument.type
            model_for_limitations = Argument_Model.objects.get(arg_model_name=sub_arg_model_name)
        return model_for_limitations
    
    def is_phraseologic(self):
        arg_model = Argument_Model.objects.get(arg_model_name=self.type)
        return arg_model.phraseologic
        
    def is_fully_lexicalized(self):
        arg_model = Argument_Model.objects.get(arg_model_name=self.type)
        if arg_model.phraseologic:
            is_fully_lexicalized = self.__is_phraseologic_and_fully_lexicalized()
        elif self.atributes.filter(type=u'KATEGORIA').exists(): #xp
            is_fully_lexicalized = self.__have_subtypes_and_is_fully_lexicalized()
        else:
            is_fully_lexicalized = False
        return is_fully_lexicalized
    
    def __is_phraseologic_and_fully_lexicalized(self):
        if self.atributes.filter(type=u'MODYFIKACJA').exists():
            modification_attr = self.atributes.get(type=u'MODYFIKACJA')
            if modification_attr.selection_mode.sym_name == 'natr':
                is_fully_lexicalized = True
            elif modification_attr.values.exists():
                is_fully_lexicalized = True
                for pos_val in modification_attr.values.all():
                    if not pos_val.position.is_fully_lexicalized():
                        is_fully_lexicalized = False
                        break
            else:
                is_fully_lexicalized = False
        elif self.atributes.filter(type=u'TYPY FRAZ').exists(): # compar
            is_fully_lexicalized = True
            arguments_attr = self.atributes.get(type=u'TYPY FRAZ')
            for arg_val in arguments_attr.values.all():
                if not arg_val.argument.is_fully_lexicalized():
                    is_fully_lexicalized = False
                    break
        else:
            is_fully_lexicalized = True
        return is_fully_lexicalized  
    
    def __have_subtypes_and_is_fully_lexicalized(self):
        is_fully_lexicalized = True
        category_attr = self.atributes.get(type=u'KATEGORIA')
        if category_attr.values.count() == 0: # xp bez podtypow
            is_fully_lexicalized = False
        else:
            for arg_val in category_attr.values.all():
                if not arg_val.argument.is_fully_lexicalized():
                    is_fully_lexicalized = False
                    break
        return is_fully_lexicalized
    
    def __unicode__(self):
        return '%s' % (self.text_rep)
   
    def contains_parameter_attribute(self, attr_type_name, param_model_name):
        return self.atributes.filter(type=attr_type_name,
                                     values__parameter__type__name=param_model_name).exists()
                                     
    def has_real_only_parameter(self):
        for attr in self.atributes.all():
            for value in attr.values.filter(type__sym_name=u'parameter'):
                if value.parameter.type.realization_only:
                    return True
        return False
    
    def main_phrase_type(self):
        category_attrs = self.atributes.filter(type=u'KATEGORIA')
        if not category_attrs.exists() or category_attrs.all()[0].values.count() == 0: # xp bez podtypow
            return self
        else:
            return Argument.objects.get(text_rep=u'%s(%s)' % (self.type, category_attrs.all()[0].selection_mode.name))
    
    class Meta:
      permissions = (
        ('view_realization', u'Moลผe oglฤ…daฤ‡ realizacje argumentรณw.'),
        ('create_realization', u'Moลผe kreowaฤ‡ realizacje argumentรณw.'),
        ('view_arg_stats', u'Moลผe oglฤ…daฤ‡ statystyki argumentรณw.'),
      ) 

def sort_arguments(arguments):
    return sortArguments(arguments)

def reflex_phrase_types():
    return ['refl', 'recip']               
    
class ArgRealization(Model):
    type = ForeignKey('RealizationType', related_name='realizations')
    argument = ForeignKey('Argument', blank=True, null=True)
    positions = ManyToManyField('Position', blank=True, null=True, 
                                related_name='realizations')
    opinion = ForeignKey('ArgRealOpinion', related_name='realizations')
    
    def is_used(self):
        if self.arguments.exists():
            return True
        return False
    
    def __unicode__(self):
        text_rep = ''
        if self.type.sym_name == 'phrase_type':
            text_rep = self.argument.text_rep
        elif self.type.sym_name == 'positions':
            sorted_positions = sort_positions(self.positions.all())
            positions_str_tab = [unicode(position) for position in sorted_positions]
            text_rep = ' + '.join(positions_str_tab)
        return text_rep
    
def get_or_create_extension(extension_type, phrase_type, positions, opinion):
    created = False
    if extension_type.sym_name == 'phrase_type':
        extension, created = get_or_create_phrase_type_extension(phrase_type, opinion)
    elif extension_type.sym_name == 'positions':
        extension, created = get_or_create_positions_extension(positions, opinion)
    return extension, created

def get_or_create_phrase_type_extension(phrase_type, opinion):
    extension_type = RealizationType.objects.get(sym_name='phrase_type')
    extension, created = ArgRealization.objects.get_or_create(opinion=opinion,
                                                         argument=phrase_type,
                                                         type=extension_type)
    return extension, created
    
def get_or_create_positions_extension(positions, opinion):
    created = False
    extension = None
    extension_type = RealizationType.objects.get(sym_name='positions')
    extensions = ArgRealization.objects.annotate(positions_count=Count('positions'))
    extensions = extensions.filter(positions_count=len(positions),
                                   opinion=opinion,
                                   type=extension_type)
    for position in positions:
        extensions = extensions.filter(positions=position)
    if extensions.exists():
        extension = extensions.all()[0]
    else:
        extension = ArgRealization(opinion=opinion, type=extension_type)
        extension.save()
        extension.positions.add(*positions)
        created = True
    return extension, created
  
class RealizationType(Model):
    name = CharField(max_length=24)
    sym_name = CharField(max_length=24)
    priority = PositiveIntegerField(db_column='priorytet')
    
    def __unicode__(self):
        return self.name
    
class ArgRealOpinion(Model):
    value = CharField(max_length=16, db_column='wartosc', unique=True)
    priority = PositiveIntegerField(db_column='priorytet')
    
    def __unicode__(self):
        return self.value
    
class Argument_Model(Model):
    # nazwa typu argumentu
    arg_model_name = CharField(max_length=16, db_column='nazwa_modelu', 
                               verbose_name=u'nazwa modelu', primary_key=True, unique=True)
    # mozliwe wartosci przyjmowane przez atrybut o nazwie model_name
    atribute_models = ManyToManyField('Atribute_Model', db_table='wzory_atrybutow',
                                      related_name='argument_models', 
                                      blank=True, null=True) # to pole jednak nie jest wymagane, patrz refl, epsilon
    # czy wystepuje tylko jako realizacja xp, lp, comprepnp 
    # nie ma juz typow argumentow wystepujacych tylko jako realizacje
    realization_only = BooleanField(db_column='tylko_realizacja', default=False)
    has_realizations = BooleanField(db_column='posiada_realizacje', default=False)
    # czy wystepuje tylko wewnatrz argumentow zleksykalizowanych
    lex_only = BooleanField(db_column='tylko_w_leksykalizacjach', default=False)
    # wystepuje wewnatrz argumentow zleksykalizowanych (lex, fixed)
    used_in_lex = BooleanField(db_column='wykorzystywany_w_leksykalizacjach', default=False)
    # czy typ argumentu ma byc widoczny w reprezentacji tekstowej
    hide_type = BooleanField(db_column='ukryty_typ', default=False)
    # modele parametrow nie przyjmowane przez model argumentu
    exclude_param_models = ManyToManyField('AttributeParameterModel', db_table='wylaczone_parametry',
                                           related_name='argument_models', 
                                           blank=True, null=True)
    # czy argument jest frazeologiczny
    phraseologic = BooleanField(db_column='argument_frazeologiczny', default=False)
    # leksykalizacja (frazeologia) wykluczone modele atrybutow dla wybranego parametru (TYP FRAZY)
    sec_attr_limitations = ManyToManyField('Atribute_Model', db_table='wtornie_wykluczone_atrybuty',
                                           blank=True, null=True)
    # wykorzystaj podtyp (np. dla xp) by ograniczyc atrybuty we frazeologii
    use_subarg_for_limitations = BooleanField(db_column='wyklucz_przez_podtyp', default=False)
    # czy model jest aktywny
    active = BooleanField(db_column='aktywny', default=True)
    # mozna wybierac posrod wszystkich wartosci atrybutow przy tworzeniu argumentow zleksykalizowanych (tez fixed)
    all_attr_params_used_in_lex = BooleanField(default=False)
    # mozliwe czesci mowy
    poss = ManyToManyField('POS', db_table='czesci_mowy_modele_argumentow',
                           related_name='argument_models')
    # mozliwe tagi dla atrybutu LEMAT LICZEBNIKA, jeล›li dotyczy (walidacja morfeuszowa)
    possible_num_lemma_tags = ManyToManyField('NkjpPosTag', related_name='argument_models_num',
                                              db_table='tagi_liczebnikow_modele_argumentow', 
                                              blank=True, null=True)
    # mozliwe tagi dla atrybutu LEMAT, jeล›li dotyczy (walidacja morfeuszowa)
    possible_lemma_tags = ManyToManyField('NkjpPosTag', related_name='argument_models',
                                          db_table='tagi_lematow_modele_argumentow', 
                                          blank=True, null=True)
    # priorytet prezentowania argumentow w pozycji
    priority = PositiveIntegerField(db_column='priorytet', blank=True, null=True)
    
    def get_possible_lemma_tags(self, attr_model):
        lemma_tags = NkjpPosTag.objects.none()
        if attr_model.sym_name == 'lemma':
            lemma_tags = self.possible_lemma_tags.all()
        elif attr_model.sym_name == 'numeral_lemma':
            lemma_tags = self.possible_num_lemma_tags.all()
        return lemma_tags
    
    def __unicode__(self):
        return '%s' % (self.arg_model_name)
  
class Atribute_Model(Model):
    # typ atrybutu
    type = ForeignKey('AttributeType', db_column='typ', related_name='attribute_models',
                      default=1)
    # nazwa typu atrybutu
    atr_model_name = CharField(max_length=32, db_column='nazwa_atrybutu', 
                               verbose_name=u'nazwa modelu atrybutu')
    # nazwa modelu wykorzytywana przy generacji xmla
    sym_name = CharField(max_length=32, db_column='nazwa_skrocona', 
                         verbose_name=u'nazwa_skrocona', blank=True, 
                         null=True)
    # mozliwe modele parametrow
    possible_parameters = ManyToManyField('AttributeParameterModel', db_table='przyjmowane_parametry', 
                                          null=True, blank=True, related_name='attribute_models')
    # sposob sortowania wartosci w interfejsie realizacji
    realizations_values_order = CharField(max_length=32, db_column='sortowanie_wartosci_realizacje', 
                                          default='priority')
    # sposob sortowania wartosci w interfejsie edycji hasla
    entry_edit_values_order = CharField(max_length=32, db_column='sortowanie_wartosci_edycja',
                                        default='priority')
    # tryb wybierania argumentรณw z listy
    values_selection_modes = ManyToManyField('AttrValueSelectionMode', db_table='tryb_wybierania_wartosci', 
                                             null=True, blank=True, related_name='attribute_models')
    # mozliwe znaki oddzielajฤ…ce kolejne wartosci
    value_separators = ManyToManyField('AttrValuesSeparator', db_table='modele_atrybutow_do_separatorow_wartosci', 
                                       null=True, blank=True, related_name='atribute_models')
    # znak konczacy liste wartosci atrybutu
    val_list_start = CharField(max_length=1, db_column='poczatek_listy_wartosci', 
                               default='(', null=True, blank=True)
    # znak zamykajacy liste wartosci atrybutu
    val_list_end = CharField(max_length=1, db_column='koniec_listy_wartosci', 
                             default=')', null=True, blank=True)
    # co najmniej jedna "wartosc" atrybutu musi byc zleksykalizowana
    need_lexicalized_values = BooleanField(db_column='potrzebuje_zleksykalizowych_wartosci', 
                                           default=False)
    # dla atrybutow typu text, okresla, ze wartoscia lematu moลผe byc liczba
    value_can_be_number = BooleanField(db_column='moze_byc_liczba', default=False)
    # priorytet prezentowania atrybutow w argumencie
    priority = PositiveIntegerField(db_column='priorytet', primary_key=True)
    
    def use_subparams(self):
        if (self.possible_parameters.exists() and 
            self.possible_parameters.annotate(subparams_count=Count('possible_subparams')).filter(subparams_count__gt=0).exists()):
            return True
        return False
    
    def __unicode__(self):
      return '%s' % (self.atr_model_name)

# Mozliwe wartosci atrybutow
class Atribute_Value(Model):
    # typ wartosci atrybutu
    type = ForeignKey('AttributeType', db_column='typ', related_name='attribute_values',
                      default=2)
    # wypelniane jesli atrybut jest typu tekstowego
    text = CharField(max_length=32, db_column='tekst', 
                     verbose_name=u'tekst', blank=True, null=True)
    parameter = ForeignKey('AttributeParameter', db_column='parametr', 
                           related_name='attribute_values',
                           blank=True, null=True)
    # argument jesli pole przyjmuje argumenty jako wartosci atrybutu
    argument = ForeignKey('Argument', db_column='argument', related_name='attribute_values',
                          blank=True, null=True)
    # pozycja jesli pole przyjmuje pozycje jako wartosci atrybutu
    position = ForeignKey('Position', db_column='pozycja', related_name='attribute_values',
                          blank=True, null=True)
    # priorytet waznosci atrybutow
    priority = PositiveIntegerField(db_column='priorytet', blank=True, null=True)
    
    def __unicode__(self):
        text_rep = ''
        if self.text:
            text_rep = self.text
        elif self.parameter:
            text_rep = unicode(self.parameter)
        elif self.argument:
            text_rep = self.argument.text_rep
        elif self.position:
            text_rep = self.position.text_rep
        return text_rep
    
def get_or_create_parameter_attr_value(parameter):
    created = False
    parameter_type = AttributeType.objects.get(sym_name='parameter')
    try:
        attr_val_obj = Atribute_Value.objects.get(parameter=parameter,
                                                  type=parameter_type)
    except Atribute_Value.DoesNotExist:
        attr_val_obj = Atribute_Value(parameter=parameter, priority=2000,
                                      type=parameter_type)
        attr_val_obj.save()
        #attr_val_obj.poss.add(*POS.objects.exclude(tag='unk').all())
        created = True
    return attr_val_obj, created

class AttributeParameterModel(Model):
    name = CharField(max_length=24, db_column='nazwa', 
                     verbose_name=u'nazwa')
    sym_name = CharField(max_length=24, db_column='nazwa_symboliczna', 
                         verbose_name=u'nazwa_symboliczna')
    possible_subparams = ManyToManyField('AttributeSubparameter', db_table='mozliwe_podparametry', 
                                    null=True, blank=True, related_name='attr_parameter_models')
    # parametr dostepny tylko w argumentach bedacych realizacjami
    realization_only = BooleanField(db_column='tylko_realizacja', default=False)
    # mozliwe czesci mowy
    poss = ManyToManyField('POS', db_table='czesci_mowy_vs_modele_parametrow',
                           related_name='attr_parameter_models')
    # wartosc dostepna tylko dla dowiazanego modelu parametru
    related = BooleanField(db_column='powiazany', default=False)
    # powiazane modele parametrow
    related_param_models = ManyToManyField('AttributeParameterModel', db_table='powiazane_modele_parametrow', 
                                           null=True, blank=True, related_name='main_param_models')
    # czy model jest aktywny
    active = BooleanField(db_column='aktywny', default=True)
    priority = PositiveIntegerField(db_column='priorytet')
    
    def __unicode__(self):
        return self.name

class AttributeParameter(Model):
    type = ForeignKey('AttributeParameterModel', db_column='typ', 
                      related_name='attribute_parameters')
    subparameters = ManyToManyField('AttributeSubparameter', db_table='podparametry', 
                                    null=True, blank=True, related_name='attribute_parameters')
    priority = PositiveIntegerField(db_column='priorytet')
    
    def __unicode__(self):
        if self.subparameters.exists():
            subparameters_str_tab = [subparam.name for subparam in self.subparameters.order_by('name')]
            text_rep = u'%s[%s]' % (self.type.name, ';'.join(subparameters_str_tab))
        else:
            text_rep = self.type.name
        return text_rep
    
def get_or_create_attr_parameter(model, subparameters):
    created = False
    possible_param_objs = AttributeParameter.objects.annotate(subparams_count=Count('subparameters')).filter(subparams_count=len(subparameters))
    for subparam in subparameters:
        possible_param_objs = possible_param_objs.filter(subparameters=subparam)
    possible_param_objs = possible_param_objs.distinct()
    try:
        param_obj = possible_param_objs.get(type=model)
    except AttributeParameter.DoesNotExist:
        param_obj = AttributeParameter(type=model,
                                       priority=2000)
        param_obj.save()
        param_obj.subparameters.add(*subparameters)
        created = True
    return param_obj, created

class AttributeSubparameter(Model):
    name = CharField(max_length=16, db_column='nazwa', 
                     verbose_name=u'nazwa')
    sym_name = CharField(max_length=16, db_column='nazwa_symboliczna', 
                         verbose_name=u'nazwa_symboliczna')
    opinion = ForeignKey('ArgRealOpinion', 
                         related_name='attr_subparameters')
    priority = PositiveIntegerField(db_column='priorytet')
    
    def __unicode__(self):
        return self.name
############# 
  
class Atribute(Model):
    # wzor/typ atrybutu
    type = CharField(max_length=32, db_column='typ_atrybutu', 
                         verbose_name=u'typ atrybutu') 
    values = ManyToManyField('Atribute_Value', db_table='wartosci', 
                             related_name='attributes')
    # separator (jesli posiada wiecej niz jedna wartosc)
    separator = ForeignKey('AttrValuesSeparator', db_column='separator_wartosci',
                            null=True, blank=True, verbose_name=u'separator wartoล›ci',
                            related_name='atributes')
    # tryb wyboru wartosci
    selection_mode = ForeignKey('AttrValueSelectionMode', db_column='tryb_wyboru_wartosci',
                                null=True, blank=True, verbose_name=u'tryb wyboru wartosci',
                                related_name='atributes')

    def sorted_arguments_text_reps(self):
        arguments = [value.argument for value in self.values.all()]
        sorted_arguments = sortArguments(arguments)
        arguments_text_reps = [arg.text_rep for arg in sorted_arguments]
        return arguments_text_reps

    def sorted_positions_text_reps(self):
        positions = [value.position for value in self.values.all()]
        sorted_positions = sortPositions(positions)
        positions_text_reps = [pos['position'].text_rep for pos in sorted_positions]
        return positions_text_reps
    
    def get_values_text_reps(self):
        values_text_reps = []
        attribute_model = Atribute_Model.objects.get(atr_model_name=self.type)
        attribute_type = attribute_model.type.sym_name
        if attribute_type == 'text':
            sorted_values = self.values.order_by('text')
            values_text_reps = [value.text for value in sorted_values]
        elif attribute_type == 'parameter':
            sorted_values = self.values.order_by('parameter__type__priority')
            values_text_reps = [unicode(value.parameter) for value in sorted_values]
        elif attribute_type == 'argument':
            values_text_reps = self.sorted_arguments_text_reps()
        elif attribute_type == 'position':
            values_text_reps = self.sorted_positions_text_reps()
        return values_text_reps
    
    def __unicode__(self):
        text_rep = u''
        values_text_rep = self.get_values_text_reps()
        attr_model = Atribute_Model.objects.get(atr_model_name=self.type)
        if not self.selection_mode:
            text_rep = values_text_rep[0]
        elif self.selection_mode and not self.values.exists():
            text_rep = self.selection_mode.name
        elif self.selection_mode and not self.selection_mode.name and self.values.exists():
            text_rep = self.separator.symbol.join(values_text_rep)
        elif self.selection_mode and self.selection_mode.name and self.values.exists():
            text_rep = u'%s%s%s%s' % (self.selection_mode, 
                                      attr_model.val_list_start,
                                      self.separator.symbol.join(values_text_rep),
                                      attr_model.val_list_end)
        return text_rep
 
def get_or_create_attribute(attribute_model, values, 
                               selection_mode, separator):
    possible_attr_objs = Atribute.objects.annotate(values_count=Count('values')).filter(values_count=len(values))
    for value in values:
        possible_attr_objs = possible_attr_objs.filter(values=value)
    possible_attr_objs = possible_attr_objs.distinct()
    try:
        attr_obj = possible_attr_objs.get(type=attribute_model.atr_model_name,
                                          selection_mode=selection_mode,
                                          separator=separator)
    except Atribute.DoesNotExist:
        attr_obj = Atribute(type=attribute_model.atr_model_name,
                            selection_mode=selection_mode, 
                            separator=separator)
        attr_obj.save()
        attr_obj.values.add(*values)
    return attr_obj
 
     
class Old_Frame(Model):
    # tekst starej ramki zgodnie ze starym slownikiem
    old_frame_value = CharField(max_length=64, db_column='tekst_ramki', 
                                verbose_name=u'tekst ramki',
                                null=True, blank=True) 
    property = ForeignKey('Old_Frame_Property', db_column='wlasciwosc',
                          related_name='old_frames')
    # czy pochodzi od hasla w formie zwrotnej
    reflex = BooleanField(db_column='zwrotna')
    # identyfikator zdania ze skladnicy
    sent_id = TextField(db_column='sent_id', verbose_name=u'identyfikator zdania w skladnicy')
    # argumenty starej ramki
    arguments = ManyToManyField('Skladnica_Argument', db_table='ramka_argumenty',
                                related_name='skladnica_frames', 
                                null=True, blank=True)
    # przyklad
    example = OneToOneField('NKJP_Example', related_name='skladnica_frame',
                            null=True, blank=True)
    # tag okreslajacy czesc mowy
    pos_tag = CharField(max_length=16, db_column='czesc_mowy', 
                        verbose_name=u'czฤ™ล›ฤ‡ mowy', default='fin') 
    # podramki danej ramki skladnicowej
    sub_frames = ManyToManyField('Old_Frame', db_column='zawarte_ramki',
                                 related_name='skladnica_frames',
                                 null=True, blank=True)
    # czy ramka jest widoczna w interfejsie dodawanie nowych ramek
    visible = BooleanField(db_column='widoczna', default=True) 
    # prawda jesli przyklad nie pasuje do ramki (informacja zwrotna dla skladnicy)
    wrong_example = BooleanField(db_column='bledny_przyklad', default=False)
    
    def __unicode__(self):
        try:
            prepnp = self.arguments.get(text_rep='advp').prepnp.text_rep
            str = '%s: [%s] x %s' % (self.old_frame_value, prepnp, smart_str(self.sub_frames.count()+1))
        except:
            str = '%s: [] x %s' % (self.old_frame_value, smart_str(self.sub_frames.count()+1))
        return str
 
  
class Skladnica_Argument(Model):
    # pomocniczy identyfikator tekstowy argumentu
    text_id = TextField(db_column='text_id', verbose_name=u'identyfikator tekstowy',
                        unique=True)
    # reprezentacja tekstowa w notacji skladnicowej (nie jest unikalna, bo np advp)
    text_rep = TextField(db_column='tekst_rep', verbose_name=u'reprezentacja tekstowa')
    # moลผliwe argumenty w formacie nowych ramek
    possible_args = ManyToManyField('Arg_Possibility', db_column='mozliwe_argumenty',
                                    related_name='skladnica_args',
                                    null=True, blank=True)
    # fraza przyimkowe (z reguly odpowiedzialna za przeksztalcenie advp w konkretne xp)
    prepnp = ForeignKey('Skladnica_Argument', db_column='prepnp',
                        related_name='skladnica_args', null=True, blank=True)  
    
    def __unicode__(self):
        return u'%s' % (self.text_id)


class Arg_Possibility(Model):
    position_category = ForeignKey('PositionCategory', db_column='kategoria_pozycji', 
                                   related_name='arg_possibilities',
                                   null=True, blank=True)
    argument = ForeignKey('Argument', db_column='argument', 
                          related_name='arg_possibilities',
                          null=True, blank=True)
    
    def __unicode__(self):
        try:
            return u'%s: %s' % (self.position_category.category, self.argument.text_rep)
        except:
            return u'nc: %s' % (self.argument.text_rep)

  
class Old_Frame_Property(Model):
    name = CharField(max_length=16, db_column='nazwa', 
                         verbose_name=u'nazwa', unique=True)
    value = CharField(max_length=16, db_column='pelna_nazwa', 
                         verbose_name=u'pelna_nazwa')
    priority = PositiveIntegerField(db_column='priorytet', blank=True, null=True)
    
    def __unicode__(self):
      return '%s' % (self.name)
    
class Frame_Characteristic(Model):
    # wzor/typ charakterystyki ramki
    type = CharField(max_length=16, db_column='typ_char', 
                         verbose_name=u'typ charakterystyki')
    # wartosc cechy ramki
    value = ForeignKey('Frame_Char_Value', db_column='wartosc_cechy_ramki',
                       related_name='frame_characteristic')
    
    def __unicode__(self):
      return '%s' % (self.value.value)
  
def get_frame_char_and_its_value(pk, none_value):
    char_val = none_value
    if pk:
        try:
            char_obj = Frame_Characteristic.objects.get(pk=pk)
            char_val = char_obj.value.value
        except Frame_Characteristic.DoesNotExist:
            char_obj = None
    else:
        char_obj = None
    return char_obj, char_val

def get_frame_char_by_type_and_value_pk(type, value_pk):
    model = Frame_Char_Model.objects.get(model_name=type)
    value = Frame_Char_Value.objects.get(pk=value_pk)
    char_obj, created = Frame_Characteristic.objects.get_or_create(type=model.model_name, value=value)
    return char_obj
    
class Frame_Char_Model(Model):
    # nazwa typu modelu argumentu
    model_name = CharField(max_length=16, db_column='nazwa_modelu', 
                           verbose_name=u'nazwa modelu', primary_key=True, unique=True)
    # mozliwe wartosci przyjmowane przez dany model charakterystyki ramki
    frame_char_values = ManyToManyField('Frame_Char_Value', db_table='model_wartosciCechRamy',
                                        related_name='frame_char_models')
    # priorytet prezentowania kolejnych klas cech w ramce
    priority = PositiveIntegerField(db_column='priorytet', blank=True, null=True)
    
    def __unicode__(self):
      return '%s' % (self.model_name)
  
def sorted_frame_char_values_dict():
    return {'sorted_reflex_vals': Frame_Char_Model.objects.get(model_name=u'ZWROTNOลšฤ†').frame_char_values.order_by('-priority'),
            'sorted_aspect_vals': Frame_Char_Model.objects.get(model_name=u'ASPEKT').frame_char_values.order_by('-priority'),
            'sorted_neg_vals': Frame_Char_Model.objects.get(model_name=u'NEGATYWNOลšฤ†').frame_char_values.order_by('-priority'),
            'sorted_pred_vals': Frame_Char_Model.objects.get(model_name=u'PREDYKATYWNOลšฤ†').frame_char_values.order_by('-priority')}
    
class Frame_Char_Value(Model):
    value = CharField(max_length=32, db_column='wartosc', 
                         verbose_name=u'wartoล›ฤ‡ cechy ramki', blank=True, null=True)
    # czy wartosc domyslna
    default = BooleanField(db_column='domyslna', default=False)
    # mozliwe czesci mowy
    poss = ManyToManyField('POS', db_table='czesci_mowy',
                           related_name='frame_char_values')
    # priorytet
    priority = PositiveIntegerField(db_column='priorytet', blank=True, null=True)
    
    def __unicode__(self):
      return '%s' % (self.value)
  
def sorted_default_frame_char_vals(): 
    return Frame_Char_Value.objects.filter(default=True).order_by('frame_char_models__priority')
    
class Message(Model):
    # tytul wiadomosci
    topic = TextField(db_column='tytul', verbose_name=u'tytuล‚')
    # tekst wiadomosci
    message_text = TextField(db_column='wiadomosc', verbose_name=u'wiadomoล›ฤ‡')
    # uzytkownik pozostawiajacy wiadomosc
    sender = ForeignKey(User, db_column='uzytkownik', related_name='messages')
    # haslo dla ktorego pozostawiono wiadomosc
    lemma = ForeignKey(Lemma, db_column='postac_hasla', related_name='messages',
                       blank=True, null=True)    
    # odbiorca wiadomosci
    recipient = ForeignKey(User, db_column='odbiorca', related_name='recived_messages',
                           blank=True, null=True)
    #data nadania wiadomosci
    time = DateTimeField(auto_now_add=True, db_column='data_nadania') # tutaj, zmieniono z auto_now na auto_now_add
    # czy wiadomosc jest prywatna
    private = BooleanField(db_column='prywatna', default=False)
    # czy wiadomosc jeszcze nie byla czytana
    new = BooleanField(db_column='nowa', default=True) 
    
    class Meta:
      db_table = 'wiadomosci'
      permissions = (
        ('add_notes', u'Moลผe dodawaฤ‡ wiadomoล›ci'),
        ('view_notes', u'Moลผe przeglฤ…daฤ‡ swoje notatki')
      )
    
    def __unicode__(self):
      return '%s' % (self.message_text)

class Change(Model):
    # uzytkownik ktory dokonal zmiany
    user = ForeignKey( User, db_column='modyfikujacy', related_name='changes')
    # data modyfikacji
    time = DateTimeField(auto_now_add=True, db_column='data_modyfikacji')
    # stara wersja hasla
    entry = ForeignKey(Lemma, db_column='postac_hasla', related_name='changes',
                       blank=True, null=True)
    # wlasciciel hasla w czasie modyfikacji
    act_owner = ForeignKey(User, db_column='aktualny_wlasciciel', related_name='changesOwn',
                            blank=True, null=True)
    
    class Meta:
      permissions = (
        ('change_lemma_version', u'Moลผe zmieniaฤ‡ wersjฤ™ hasล‚a'),
      )
    
    def __unicode__(self):
        return '%s data: %s' % (self.entry.entry, self.time)


######################## relacje aspektowe ############################# 
class AspectRelationsGroup(Model):
    # czlonkowie relacji aspektowej
    members = ManyToManyField('Entry', db_table='czlonkowie', 
                              null=True, blank=True, 
                              related_name='aspect_relations_group')
    
    def __unicode__(self):
        str_ls = []
        for entry in self.members.all():
            str_ls.append(entry.name) 
        return ','.join(str_ls)
    
class Entry(Model):
    name = CharField(max_length=64, db_column='nazwa')
    # czesc mowy
    pos = ForeignKey('POS', db_column='czesc_mowy', related_name='entries')
    # powiazane lematy
    rel_entries = ManyToManyField('Entry', db_table='powiazane_hasla', 
                                  null=True, blank=True, 
                                  related_name='+')
    der_group = ForeignKey('DerivationalGroup',
                           null=True, blank=True,
                           related_name='entries')
    synonyms = ManyToManyField('Entry', db_table='synonimy', 
                               null=True, blank=True, 
                               related_name='rel_synonyms')
    antonyms = ManyToManyField('Entry', db_table='antonimy', 
                               null=True, blank=True, 
                               related_name='rel_antonyms')
    xcp_examples = ManyToManyField('XcpExample', db_table='przyklady_xcp', 
                                   null=True, blank=True, 
                                   related_name='entries')
    phraseologic_propositions = ManyToManyField('Frame', db_table='propozycje_frazeologiczne', 
                                                blank=True, null=True,
                                                related_name='entries')
    
    class Meta:
        unique_together = ('name', 'pos',)
        permissions = (
            ('change_semantics', u'Moลผe edytowaฤ‡ semantykฤ™.'),
            ('view_semantics', u'Moลผe oglฤ…daฤ‡ semantykฤ™.'),
        )

    def all_der_entries(self):
        rel_entries = Entry.objects.filter(pk=self.pk)
        if self.der_group is not None:
            rel_entries = self.der_group.entries.all()
        return rel_entries

    def related_entries(self):
        rel_entries = Entry.objects.none()
        if self.der_group is not None:
            rel_entries = self.der_group.entries.exclude(pk=self.pk)
        return rel_entries

    def related_frames(self):
        visible = self.visible_frames()
        actual = self.actual_frames()
        return visible.exclude(pk__in=actual)

    def visible_frames(self):
        frames = []
        act_frames = self.actual_frames()
        for frame in self.all_frames():
            if act_frames.filter(pk=frame.pk).exists():
                frames.append(frame.pk)
            else:
                for lu in frame.lexical_units.all():
                    if self.meanings.filter(pk=lu.pk).exists():
                        frames.append(frame.pk)
                        break
        return get_model('semantics', 'SemanticFrame').objects.filter(pk__in=frames)

    def all_frames(self):
        frames = get_model('semantics', 'SemanticFrame').objects.none()
        for entry in self.all_der_entries():
            new_frames = entry.actual_frames()
            frames |= new_frames
        return get_model('semantics', 'SemanticFrame').objects.filter(pk__in=frames)

    def actual_frames(self):
        return self.semantic_frames.filter(next__isnull=True, removed=False)

    def actual_schemata(self):
        return self.lemmas.get(old=False).frames.all()

    def filter_local(self, frames):
        return frames.filter(pk__in=self.semantic_frames.all())

    def filter_related(self, frames):
        return frames.exclude(pk__in=self.semantic_frames.all())
    
    def matching_connections(self, schema, position, phrase_type):

        matching_connections = []

        frames = self.visible_frames()

        for frame in frames:
            for compl in frame.complements.all():
                matching_realizations = compl.realizations.filter(frame=schema, 
                                                                  position=position, 
                                                                  argument=phrase_type)
                if matching_realizations.exists():
                    realizations_ids = [real.id for real in matching_realizations.all()]
                    matching_connections.append({'compl': compl.id,
                                                 'realizations': realizations_ids})
        return matching_connections
    
    def actual_lemma(self):
        return self.lemmas.get(old=False)

    def defined(self):
        entry_defined = True
        try:
            self.lemmas.get(old=False)
        except Lemma.DoesNotExist:
            entry_defined = False
        return entry_defined

    def __unicode__(self):
        return self.name
    
def get_or_create_entry(name, pos):
    created = False
    try:
        entry = Entry.objects.get(name=name, pos__tag='unk')
        entry.pos = pos
        entry.save()
    except Entry.DoesNotExist:
        entry, created = Entry.objects.get_or_create(name=name, pos=pos)
    return entry, created

class DerivationalGroup(Model):
    id = AutoField(primary_key=True)

def connect_entries(entry1, entry2):
    if entry1.der_group is not None:
        if entry2.der_group is not None:
            if entry1.der_group.id != entry2.der_group.id:
                entries = entry2.der_group.entries.all()
                der_group = entry2.der_group
                for entry in entries:
                    entry.der_group = entry1.der_group
                    entry.save()
                der_group.delete()
        else:
            entry2.der_group = entry1.der_group
            entry2.save()
    elif entry2.der_group is not None:
        entry1.der_group = entry2.der_group
        entry1.save()
    else:
        der_group = DerivationalGroup()
        der_group.save()
        entry1.der_group = der_group
        entry1.save()
        entry2.der_group = der_group
        entry2.save()

def disconnect_entries(entry1, entry2):
    if entries_related(entry1, entry2):
        entry2.der_group = None
        entry2.save()

def entries_related(entry1, entry2):
    if entry1.der_group is None or entry2.der_group is None:
        return False
    return entry1.der_group == entry2.der_group

class POS(Model):
    tag = CharField(max_length=16, db_column='tag', unique=True)
    name = CharField(max_length=16, db_column='nazwa')
    # liczba osob potrzebnych do zatwierdzania przykladu wlasnego dla danej czesci mowy
    example_approvers_num = PositiveIntegerField(db_column='liczba_zatwierdzajacych_przyklad', 
                                                 default=2)
    priority = PositiveIntegerField(db_column='priorytet')
    
    def __unicode__(self):
        return self.name
    
def pos_compatible(from_pos, to_pos):
    compatible = False
    if from_pos == to_pos:
        compatible = True
    elif from_pos.tag == 'noun' and to_pos.tag == 'adj':
        compatible = True
    return compatible
    
class XcpExample(Model):
    arg_regex = CharField(max_length=64, db_column='wzor_argumentu')
    #examples = ManyToManyField('NKJP_Example', related_name='xcp_examples')
    example = ForeignKey('NKJP_Example', related_name='xcp_examples')
    
    def __unicode__(self):
        arg_str = self.arg_regex.replace('.*', '_').replace('\\', '')[1:-1]
        return '%s: %s' % (arg_str, self.example.sentence)

class AttributeType(Model):
    name = CharField(max_length=32, db_column='nazwa', 
                     verbose_name=u'nazwa')
    sym_name = CharField(max_length=32, db_column='nazwa_symboliczna', 
                         verbose_name=u'nazwa_symboliczna')
    priority = PositiveIntegerField(db_column='priorytet')
    
    def __unicode__(self):
        return u'%s' % (self.name)
    
class AttrValueSelectionMode(Model):
    name = CharField(max_length=32, db_column='nazwa', 
                     verbose_name=u'nazwa', blank=True, null=True)
    sym_name = CharField(max_length=32, db_column='nazwa_symboliczna', 
                         verbose_name=u'nazwa_symboliczna')
    # czy nie posiada listy wartosci
    always_empty = BooleanField(db_column='zawsze_pusty', default=False)
    # nie musi posiadac listy wartosci
    can_be_empty = BooleanField(db_column='moze_byc_pusty', default=True)
    priority = PositiveIntegerField(db_column='priorytet')
    
    def __unicode__(self):
        return u'%s' % (self.name)
    
class AttrValuesSeparator(Model):
    symbol = CharField(max_length=8, db_column='symbol', 
                     verbose_name=u'symbol')
    priority = PositiveIntegerField(db_column='priorytet')
    
    def __unicode__(self):
        return u'%s' % (self.symbol)
    
##################### morfeusz exceptions ############################
    
class MorfeuszException(Model):
    orth = CharField(max_length=32, db_column='orth', unique=True)
    interpretations = ManyToManyField('Interpretation', db_table='interpretacje',
                                      related_name='morfeusz_exceptions')
    
    def __unicode__(self):
        interpretations_text_reps = [unicode(interp) for interp in self.interpretations.all()]
        return u'%s[%s]' % (self.orth, u','.join(interpretations_text_reps))
    
class Interpretation(Model):
    pos_tag = ForeignKey('NkjpPosTag', related_name='interpretations',
                         db_column='czesc_mowy')
    base = CharField(max_length=32, db_column='base')
    
    def __unicode__(self):
        return u'%s:%s' % (self.base, self.pos_tag.name)

class NkjpPosTag(Model):
    name = CharField(max_length=16, db_column='nazwa')
    
    def __unicode__(self):
        return u'%s' % (self.name)
    
def is_morfeusz_exception(word, possible_pos_tags):
    is_exception = False
    try:
        interpretations = MorfeuszException.objects.get(orth=word).interpretations
        for interp in interpretations.all():
            if interp.base == word and possible_pos_tags.filter(name=interp.pos_tag.name).exists():
                is_exception = True
                break
    except MorfeuszException.DoesNotExist:
        pass
    return is_exception
    
    
################################## sorting functions #######################################
# sort frame_characteristic names when presented as table of strings
def sortFrameChars(frameChars):
  sorted_frame_char_models = Frame_Char_Model.objects.order_by('priority')
  sorted_frame_chars = []
  for frame_char_model in sorted_frame_char_models:
    for frame_char in frameChars:
      if frame_char_model.model_name == frame_char.type:
        sorted_frame_chars.append(frame_char)       
  return sorted_frame_chars  

def sortatributes(argument):
    sortedatributes = []
    atribute_models = get_attribute_models(argument)
    for atribute_model in atribute_models:
        # warunek, zeby fixed stare dalo sie edytowac
        if argument.atributes.filter(type=atribute_model.atr_model_name).exists():
            sortedatributes.append(argument.atributes.get(type=atribute_model.atr_model_name))
    return sortedatributes  

def get_attribute_models(argument):
    arg_model = Argument_Model.objects.get(arg_model_name=argument.type)
    attribute_models = arg_model.atribute_models.order_by('priority')
    if (attribute_models.filter(atr_model_name=u'TYP FRAZY').exists() and
        argument.atributes.filter(type=u'TYP FRAZY').exists()): 
        
        # drugi warunek zeby dalo sie edytowac stare fixy
        arg_attr = argument.atributes.get(type=u'TYP FRAZY').values.all()[0].argument
        #argument_attr_model = Argument_Model.objects.get(arg_model_name=arg_attr.type)
        #attr_models_to_exclude = argument_attr_model.sec_attr_limitations.all()
        attr_models_to_exclude = get_attr_models_to_exclude(arg_attr)
        
        attribute_models = attribute_models.exclude(pk__in=attr_models_to_exclude)
        attribute_models = attribute_models.order_by('priority')
    return attribute_models

#####################################################  
def get_attr_models_to_exclude(argument):
    argument_model = Argument_Model.objects.get(arg_model_name=argument.type)
    if argument_model.use_subarg_for_limitations:
        sub_arg_model_name = argument.atributes.all()[0].values.all()[0].argument.type
        argument_model = Argument_Model.objects.get(arg_model_name=sub_arg_model_name)
    return argument_model.sec_attr_limitations.all()
#####################################################

def sortArguments(list):
  ret_list = []
  typed_list = []
  models = Argument_Model.objects.all().order_by('priority')
  for model in models:
    typed_list.append([])
    for elem in list:
      if(elem.type == model.arg_model_name):
        typed_list[len(typed_list)-1].append(elem.text_rep) 
  for ls in typed_list:
    ls.sort()
  for ls in typed_list:
    for text_rp in ls:
      for elem in list:
        if(elem.text_rep == text_rp):
          ret_list.append(elem)
          break    
  return ret_list 
  
def sortPositions(positions):
  ret_list = []
  pos_dict_list = []
  # sortowanie argumentow na pozycjach
  for position in positions:
    sortedArguments = sortArguments(position.arguments.all())
    position_dict = {"position": position,
                     "arguments": sortedArguments}
    pos_dict_list.append(position_dict)      
  # sortowanie pozycji wedlug kategorii
  sorted_pos_cats = PositionCategory.objects.order_by("priority")
  pos_to_sort = []
  
  for pos_cat in sorted_pos_cats:
    same_cat_positions = []
    first_pos_arg_list = []
    for position in pos_dict_list:
      if len(position['position'].categories.all()) > 0:
        first_prior_cat = position["position"].categories.order_by("priority")[0]
        if first_prior_cat == pos_cat:
          same_cat_positions.append(position)
    
    while len(same_cat_positions) != 0:
      highestPriorFrame = getHighestPriorFrame(same_cat_positions, 0)
      ret_list.append(highestPriorFrame)
      same_cat_positions.remove(highestPriorFrame)

  blanc_cat_positions = []  
  for position in pos_dict_list:
    if len(position["position"].categories.all()) == 0:
      blanc_cat_positions.append(position)
      
  while len(blanc_cat_positions) != 0:
    highestPriorFrame = getHighestPriorFrame(blanc_cat_positions, 0)
    ret_list.append(highestPriorFrame)
    blanc_cat_positions.remove(highestPriorFrame)
    
  return ret_list
  
# sort categories names when presented as table of strings
def sortPosCatsAsStrTab(pos_cat_str_tab):
  sorted_pos_cat_objs = PositionCategory.objects.order_by('priority')
  sorted_cats = []
  for pos_cat_obj in sorted_pos_cat_objs:
    for pos_cat_str in pos_cat_str_tab:
      if pos_cat_str == pos_cat_obj.category:
        sorted_cats.append(pos_cat_str)
  return sorted_cats 

# wyszukuje ramke o najwyzszym priorytecie
# pomija rozdzileanie kategorii
def getHighestPriorFrame(positions_dict_list, row_idx):
  arguments = []

  for position in positions_dict_list:
    if(row_idx < len(position['arguments'])):
      arguments.append(position['arguments'][row_idx])
      
  sorted_args = sortArguments(arguments) 
  same_args = 0
  
  for arg in sorted_args:
    if arg == sorted_args[0]:
      same_args = same_args + 1
      
  if same_args > 1:
    same_args_dict_list = []
    for position_dict in positions_dict_list:
      if position_dict['arguments'][row_idx] == sorted_args[0]:
        same_args_dict_list.append(position_dict)
    
    # sprawdz czy wszystkie pozycje nie sa takie same
    positionsSame = True
    for same_arg_pos in same_args_dict_list: 
      if len(same_arg_pos['arguments']) > row_idx+1:
        positionsSame = False 
    if positionsSame:
      return same_args_dict_list[0]
    else:  
      return getHighestPriorFrame(same_args_dict_list, row_idx+1)
   
  for position_dict in positions_dict_list:
    if len(position_dict['arguments']) <= row_idx:
      return position_dict
    elif position_dict['arguments'][row_idx]==sorted_args[0]: 
      return position_dict