models.py 28.8 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
#-*- coding:utf-8 -*-

from django.db.models import *
from django.contrib.auth.models import User, Permission
from common.util import no_history

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):
  label = CharField(max_length=64, db_column='kwal', verbose_name=u'nazwa')
  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')

  def is_empty(self):
    return not (
      self.lexeme_set.filter(deleted=False).exists() or
      self.ending_set.exists() or
      self.lexemeinflectionpattern_set.filter(lexeme__deleted=False).exists())

  def set_for(self, something, set):
    if set:
      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), deleted=False)

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

class Classification(Model):
  name = CharField(
    unique=True, max_length=64, db_column='nazwa',
    verbose_name=u'nazwa klasyfikacji')

  def value_tree(self):
    parts = []
    for v in self.values.filter(parent_node__isnull=True, deleted=False):
      parts.append(v.subtree())
    return parts

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

  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)
    choices += [(pk, u'    ' + 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)
  deleted = BooleanField(db_column='usunieta')

  def subtree(self):
    subtrees = []
    for child in self.child_nodes.filter(deleted=False):
      subtrees.append(child.subtree())
    return self, subtrees

  def is_empty(self):
    return not self.lexemes.filter(deleted=False).exists()

  def __unicode__(self):
    return self.label

  class Meta:
    db_table = 'wartosci_klasyfikacji'

class LexicalClass(Model):
  symbol = CharField(primary_key=True, max_length=16, db_column='czm')

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

  def __unicode__(self):
    return self.symbol

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

class BaseFormLabel(Model):
  entry = CharField(max_length=32, blank=True, db_column='efobaz')

  def __unicode__(self):
    return self.entry

  class Meta:
    db_table = 'efobazy'

class InflectionCharacteristic(Model):
  entry = 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.entry, self.part_of_speech.symbol)

  class Meta:
    db_table = 'charfle'
    unique_together = ['entry', 'part_of_speech']

class PatternType(Model):
  lexical_class = ForeignKey(
    LexicalClass, db_column='czm', verbose_name=u'cz. mowy')
  # typ wzoru (np. dla rzeczowników:
  # odmiana męska, żeńska lub nijaka)
  entry = CharField(
    max_length=32, blank=True, db_column='wtyp', verbose_name=u'typ wzoru')

  def base_form_labels(self):
    bfl_pks = set(self.tabletemplate_set.distinct()
                      .values_list('cell__base_form_label', flat=True))
    # prowizorka ze względu na wzór 0000
    bfl_pks |= set(self.pattern_set.distinct()
                       .values_list('endings__base_form_label', flat=True))
    return BaseFormLabel.objects.filter(pk__in=bfl_pks)

  class Meta:
    db_table = 'typywzorow'

# wzór fleksyjny
class Pattern(Model):
  PATTERN_STATUS_CHOICES = (
    # (<symbol>, u'<opis>'),
  )

  # kiedyś trzeba zrobić porządek w nazwach.
  name = CharField(
    max_length=32, unique=True, db_column='w_id', verbose_name=u'nazwa wzoru')
  type = ForeignKey(PatternType, db_column='typ')
  # rdzeń przykładowej formy hasłowej
  example = CharField(max_length=64, db_column='przyklad',
                      verbose_name=u'przyklad')
  # zakończenie formy podstawowej (uwaga: w zasadzie tylko dla prezentacji
  # wzoru, rdzenie w Odmieniasie trzeba tworzyć uważniej, wiedząc, która forma
  # jest hasłowa dla danego charfla)
  basic_form_ending = CharField(
    max_length=32, db_column='zakp', blank=True,
    verbose_name=u'zakończenie formy podstawowej')
  # np. "chwilowo nieużywany, ale trzymamy, bo wymyśliliśmy"
  status = CharField(
    max_length=8, choices=PATTERN_STATUS_CHOICES, verbose_name=u'status')
  comment = TextField(blank=True, db_column='komentarz',
                      verbose_name=u'komentarz')

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

  def get_basic_form_ending(self, inflection_characteristic):
    return self.endings.filter(
      base_form_label=inflection_characteristic.basic_form_label)[0].string

  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 = []
        for form in cell['forms']:
          if form[1] not in seen_forms:
            seen_forms.append(form[1])
            unique_forms.append(form)
        cell['forms'] = [{'form': form, 'qualifiers': qualifiers}
                         for (key, form, qualifiers) in unique_forms]
      elif type(cell) == dict and 'label' in cell:
        seen_labels = []
        def new(label):
          new = label not in seen_labels
          seen_labels.append(label)
          return new
        cell['label'] = filter(new, cell['label'])

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

  entry = CharField(max_length=64, db_column='haslo', db_index=True,
                    verbose_name=u'hasło', blank=True) # dla nowo utworzonych
  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')
  homonym_number = IntegerField(db_column='hom')
  part_of_speech = ForeignKey(PartOfSpeech, db_column='pos',
                              verbose_name=u'cz. mowy')
  owner_vocabulary = ForeignKey(
    'Vocabulary', db_column='slownik', related_name='owned_lexemes')
  source = CharField(max_length=32, blank=True, db_column='zrodlo')
  status = CharField(max_length=8, db_column='status', choices=STATUS_CHOICES)
  qualifiers = ManyToManyField(
    Qualifier, blank=True, db_table='kwalifikatory_leksemow')
  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')
  deleted = BooleanField(db_column='usuniety')

  def inflection_tables(self, variant, qualifiers=None):
    lips = self.lexemeinflectionpattern_set.order_by('index')
    ics = []
    for lip in lips:
      ic = lip.inflection_characteristic
      if ic not in ics:
        ics.append(ic)
    return [(ic, self.inflection_table(variant, ic, qualifiers=qualifiers))
            for ic in ics]

  def inflection_table(self, variant, inflection_characteristic,
                       qualifiers=None):
    lips = self.lexemeinflectionpattern_set.filter(
      inflection_characteristic=inflection_characteristic)
    tables = [lip.inflection_table(variant, qualifiers=qualifiers)
              for lip in lips]
    table1 = tables[0]
    for table2 in tables[1:]:
      for row1, row2 in zip(table1, table2):
        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 table1

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

  def all_forms(self, affixes=True, 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(
          affixes=affixes, label_filter=label_filter, variant=variant))
    return forms

  def get_root(self, pattern, inflection_characteristic):
    basic_form = self.entry
    pos = self.part_of_speech.symbol
    return get_root(basic_form, pos, pattern, inflection_characteristic)

  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
    set_vocabulary(self, new_owner, True)
    self.owner_vocabulary = new_owner
    set_vocabulary(self, old_owner, False)

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

  def lip_data(self):
    lips = self.lexemeinflectionpattern_set.all()
    patterns = []
    ics = []
    for lip in lips:
      if patterns == [] or lip.pattern.name != patterns[-1]:
        patterns.append(lip.pattern.name)
      if ics == [] or lip.inflection_characteristic.entry != ics[-1]:
        ics.append(lip.inflection_characteristic.entry)
    patterns = '/'.join(patterns)
    ics = '/'.join(ics)
    return {'patterns': patterns, 'inflection_characteristics': ics}

  def fix_homonym_number(self):
    homonym_numbers = (Lexeme.objects.filter(
      deleted=False, entry=self.entry, part_of_speech=self.part_of_speech)
      .exclude(pk=self.pk)).values_list('homonym_number', flat=True)
    for i in range(1, len(homonym_numbers) + 2):
      if i not in homonym_numbers:
        self.homonym_number = i
        break

  def perm(self, user, action):
    if action == 'view':
      vocabs = self.vocabularies.all()
      return bool(vocabs & visible_vocabularies(user))
    elif action == 'change':
      priority = (not (self.responsible and
                       self.responsible.has_perm('dictionary.lexeme_priority'))
                  or user.has_perm('dictionary.lexeme_priority'))
      edit_vocab = self.owner_vocabulary in user.editable_vocabularies.all()
      return edit_vocab and priority

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

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

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

def get_root(basic_form, pos, pattern, ic, use_pattern_ending=False):
  bfl = ic.basic_form_label
  basic_endings = pattern.endings.filter(base_form_label=bfl)
  ends = []
  if use_pattern_ending:
    ends.append(pattern.basic_form_ending)
  if basic_endings:
    ends += [e.string for e in pattern.endings.filter(base_form_label=bfl)]
  if pos == 'ger':
    ends = [end + 'ie' for end in ends]
  if pos == 'pact':
    ends = [end + 'cy' for end in ends]
  if pos in ('ppas', 'appas'):
    ends = [end + 'y' for end in ends]
  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, pos, pattern, ic, use_pattern_ending=True)
    else:
      return None


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')
  # charakterystyka fleksyjna (rodzaj, aspekt)
  inflection_characteristic = ForeignKey(
    InflectionCharacteristic, db_column='charfl', verbose_name=u'char. fleks.')
  # rdzen odmiany przy zastosowaniu danego wzoru
  root = CharField(max_length=64, db_column='rdzen')
  # tu mozna sygnalizowac, ze dany sposob
  # odmiany leksemu jest gorszy, przestarzaly, etc
  qualifiers = ManyToManyField(
    Qualifier, blank=True, db_table='kwalifikatory_odmieniasiow')

  def table_template(self, variant):
    return TableTemplate.objects.get(
      variant=variant, pattern_type=self.pattern.type,
      inflection_characteristic=self.inflection_characteristic)

  def cells(self, variant='1'):
    tt = self.table_template(variant)
    return tt.cell_set.all()

  def inflection_table(self, variant, separated=False, qualifiers=None,
                       edit_view=False):
    tt = self.table_template(variant)
    cells = tt.cell_set.all()
    headers = tt.tableheader_set.all()
    rows = set()
    last_col = 0
    cells = [cell for cell in cells if self.forms(cell)]
    for cell in cells:
      rows.add(cell.tablecell.row)
      col = cell.tablecell.col + cell.tablecell.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
    table = [[{'type': 'empty'}
              for i in range(last_col)] for j in range(len(rows))]
    rows = sorted(rows)
    # słownik: nr rzędu w bazie -> rzeczywisty numer rzędu
    row_translate = dict(zip(rows, range(len(rows))))
    for cell in cells:
      x = cell.tablecell.col - 1
      y = row_translate[cell.tablecell.row]
      assert table[y][x]['type'] != 'span'
      separator = u'·' if separated else u''
      forms = self.forms(cell, separator=separator, qualifiers=qualifiers,
                         edit_view=edit_view)
      if table[y][x]['type'] == 'empty':
        table[y][x] = {
          'type': 'forms',
          'forms': forms,
          'rowspan': cell.tablecell.rowspan,
          'colspan': cell.tablecell.colspan,
        }
        for i in range(cell.tablecell.colspan):
          for j in range(cell.tablecell.rowspan):
            if (i, j) != (0, 0):
              assert table[y+j][x+i]['type'] == 'empty'
              table[y+j][x+i]['type'] = 'span'
      else:
        assert cell.tablecell.rowspan == table[y][x]['rowspan']
        assert cell.tablecell.colspan == table[y][x]['colspan']
        table[y][x]['forms'] += forms
    for header in headers:
      x = header.col - 1
      y = row_translate[header.row]
      assert table[y][x]['type'] == 'empty'
      table[y][x] = {
        'type': 'label',
        'label': [header.label],
        'row_header': header.row_header,
        'rowspan': header.rowspan,
        'colspan': header.colspan,
      }
      for i in range(header.colspan):
        for j in range(header.rowspan):
          if (i, j) != (0, 0):
            assert table[y+j][x+i]['type'] == 'empty'
            table[y+j][x+i]['type'] = 'span'
    return table

  def all_forms(self, separator='', affixes=True, label_filter=None,
                variant='1', qualifiers=None):
    forms = []
    for cell in self.cells(variant=variant):
      forms += self.forms(
        cell, separator, affixes, label_filter=label_filter,
        qualifiers=qualifiers)
    return forms

  def forms(self, cell, separator='', affixes=True, label_filter=None,
            qualifiers=None, edit_view=False):
    if not qualifiers:
      qualifiers = Qualifier.objects.all()
    endings = Ending.objects.filter(
      base_form_label=cell.base_form_label, pattern=self.pattern)
    if label_filter:
      endings = endings.filter(base_form_label__entry__regex=label_filter)
    if not edit_view:
      l_qual = set(self.lexeme.qualifiers.all() & qualifiers)
      # podgląd może być na niezapisanym lipie
      lip_qual = set(self.qualifiers.all() & qualifiers if self.pk else ())
    forms = [
      (
        (cell.index, self.index, ending.index),
        (cell.prefix + self.root + separator + ending.string + cell.suffix
        if affixes else self.root + separator + ending.string),
        #+ '#' + cell.base_form_label.entry,
        combine_qualifiers(l_qual, lip_qual,
                           set(ending.qualifiers.all() & qualifiers))
        if not edit_view else set(ending.qualifiers.all() & qualifiers),
      )
      for ending in endings
    ]
    return forms

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

  def __unicode__(self):
    return '%s : %s/%s : %s' % (
      self.lexeme.entry,
      self.pattern.name,
      self.pattern.type.entry,
      self.inflection_characteristic.entry,
    )

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

def all_forms(pattern, ic, pos, base, variant='1', affixes=True):
  root = get_root(base, pos, pattern, ic)
  tt = TableTemplate.objects.get(
    variant=variant, pattern_type=pattern.type, inflection_characteristic=ic)
  forms = set()
  for cell in tt.cell_set.all():
    endings = Ending.objects.filter(
      base_form_label=cell.base_form_label, pattern=pattern)
    forms |= set(
      cell.prefix + root + ending.string + cell.suffix
      if affixes else root + ending.string
      for ending in endings)
  return forms

def combine_qualifiers(l_qualifiers, lip_qualifiers, e_qualifiers):
  qualifiers = set(l_qualifiers)
  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, lexeme__deleted=False).distinct()

# zakonczenie formy bazowej
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')

  def editable_vocabularies(self, 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 = ['index']


# 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')
  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')
  classifications = ManyToManyField(Classification, blank=True,
                                    related_name='vocabularies')
  # qualifiers = ManyToManyField(Qualifier, 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() |
            User.objects.filter(
              Q(groups__permissions=perm) | Q(user_permissions=perm) |
              Q(is_superuser=True)
            )).distinct()

  def __unicode__(self):
    return self.id

  class Meta:
    db_table = 'slowniki'
    permissions = (
      ('manage_vocabulary', u'Może zarządzać słownikami'),
    )

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

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

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

  class Meta:
    db_table = 'leksemy_w_slownikach'

def set_vocabulary(lexeme, vocabulary, set):
  if set:
    LexemeAssociation.objects.get_or_create(
      lexeme=lexeme, vocabulary=vocabulary)
  else:
    la = lexeme.lexemeassociation_set.filter(vocabulary=vocabulary)
    if la:
      assert(vocabulary != lexeme.owner_vocabulary)
      la.delete()


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

  class Meta:
    db_table = 'typyodsylaczy'


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

  class Meta:
    db_table = 'odsylacze'


class Variant(Model):
  id = CharField(max_length=32, primary_key=True, db_column='wariant')

  def __unicode__(self):
    return self.id

  class Meta:
    db_table = 'warianty'

class TableTemplate(Model):
  variant = ForeignKey(Variant, db_column='wariant')
  pattern_type = ForeignKey(PatternType, db_column='wtyp')
  inflection_characteristic = ForeignKey(
    InflectionCharacteristic, db_column='charfl')

  def __unicode__(self):
    return '%s : %s : %s : %s' % (
      self.variant, self.inflection_characteristic.part_of_speech,
      self.pattern_type.entry, self.inflection_characteristic)

  class Meta:
    db_table = 'szablony_tabel'

#klatka paradygmatu
#(element szablonu tabelki odmiany)
class Cell(Model):
  table_template = ForeignKey(TableTemplate, db_column='st_id')
  #etykieta formy bazowej
  base_form_label = ForeignKey(BaseFormLabel, db_column='efobaz')
  #znacznik docelowego tagsetu
  tag = TextField(blank=True, db_column='tag')
  prefix = CharField(max_length=20, blank=True, db_column='prefiks')
  suffix = CharField(max_length=20, blank=True, db_column='sufiks')
  #kolejnosc klatki w paradygmacie
  index = IntegerField(db_column='kind')

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

class TableCell(Model):
  cell = OneToOneField(Cell, db_column='k_id')
  row = IntegerField()
  col = IntegerField()
  rowspan = IntegerField()
  colspan = IntegerField()

  class Meta:
    db_table = 'komorki_tabel'

class TableHeader(Model):
  table_template = ForeignKey(TableTemplate, db_column='st_id')
  row = IntegerField()
  col = IntegerField()
  rowspan = IntegerField()
  colspan = IntegerField()
  label = CharField(max_length=64, blank=True, db_column='nagl')
  row_header = BooleanField(db_column='wierszowy')

  def __unicode__(self):
    return '%s : %s : %s' % (self.label, self.row, self.col)

  class Meta:
    db_table = 'naglowki_tabel'

# na szybko i brudno
class ParadygmatyWSJP(Model):
  wariant = CharField(max_length=4)
  typr = ForeignKey(PatternType, db_column='typr')
  charfl = ForeignKey(InflectionCharacteristic, 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 LexemeForm(Model):
  lexeme = ForeignKey(Lexeme)
  form = CharField(max_length=128, db_index=True)

class SavedFilter(Model):
  serialized_filter = TextField()
  name = CharField(max_length=64)
  user = ForeignKey(User)
  super = BooleanField() # na razie nieużywane

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

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

# model przeznaczony tylko do odczytu!
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_')
  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)
  pattern = ForeignKey(Pattern, db_column='pattern_id_', null=True, blank=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_')

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