Commit f0f51bbc1a8da1c83b5fb425c50e389e34dbac8e

Authored by janek37
1 parent 9f6e006d

trochę refaktoryzacji

dictionary/forms.py
... ... @@ -547,4 +547,20 @@ ACTION_FIELDS = (
547 547 class ActionFieldForm(Form):
548 548 field = ChoiceField(
549 549 choices=[(id, data[0]) for id, data in ACTION_FIELDS], label=u'',
550   - widget=Select(attrs={'class': 'field-choice'}))
551 550 \ No newline at end of file
  551 + widget=Select(attrs={'class': 'field-choice'}))
  552 +
  553 +class CellRestrictionsForm(Form):
  554 + pattern_types = ModelMultipleChoiceField(
  555 + queryset=PatternType.objects.none(), label='')
  556 + # zniknie dla nierzeczowników...
  557 + inflection_characteristics = ModelMultipleChoiceField(
  558 + queryset=InflectionCharacteristic.objects.none(), label='')
  559 + # TODO atrybuty, części mowy?
  560 +
  561 + # wybrane/dostępne typy wzorów/charfle
  562 + def __init__(self, sel_pt, av_pt, sel_ic, av_ic, **kwargs):
  563 + super(CellRestrictionsForm, self).__init__(**kwargs)
  564 + self.fields['pattern_types'].queryset = av_pt
  565 + self.fields['pattern_types'].initial = sel_pt
  566 + self.fields['inflection_characteristics'].queryset = av_ic
  567 + self.fields['inflection_characteristics'].initial = sel_ic
552 568 \ No newline at end of file
... ...
dictionary/management/commands/import_morfologik.py
... ... @@ -576,11 +576,11 @@ def check_sgjp(lc_sym, entry, form_set, forms, **extra):
576 576 if lc_sym == 'adj' and lexeme.refs_to.filter(type='nieadj'):
577 577 continue
578 578 if lc_sym == 'subst' and extra['tantum'] == 'sg':
579   - sgjp_forms = lexeme.all_forms(affixes=False, label_filter=r'sg:')
  579 + sgjp_forms = lexeme.all_forms(label_filter=r'sg:')
580 580 elif lexeme.part_of_speech.symbol == 'appas':
581   - sgjp_forms = lexeme.all_forms(affixes=True)
  581 + sgjp_forms = lexeme.all_forms()
582 582 else:
583   - sgjp_forms = lexeme.all_forms(affixes=False)
  583 + sgjp_forms = lexeme.all_forms()
584 584 if sgjp_forms == form_set:
585 585 matched_lexemes.append(lexeme)
586 586 continue
... ... @@ -591,11 +591,10 @@ def check_sgjp(lc_sym, entry, form_set, forms, **extra):
591 591 inflection_characteristic__symbol__in=(
592 592 'm1', 'p1')).exists():
593 593 # depr
594   - exceptions = lexeme.all_forms(affixes=False,
595   - label_filter=r'^pl:nom$')
  594 + exceptions = lexeme.all_forms(label_filter=r'^pl:nom$')
596 595 elif lc_sym == 'adj':
597 596 # -o
598   - exceptions = lexeme.all_forms(affixes=False, label_filter=r'^0$')
  597 + exceptions = lexeme.all_forms(label_filter=r'^0$')
599 598 if form_set.issubset(sgjp_forms) and diff.issubset(exceptions):
600 599 matched_lexemes.append(lexeme)
601 600 if len(matched_lexemes) > 1:
... ...
dictionary/management/commands/import_resztki.py
... ... @@ -305,11 +305,11 @@ def check_sgjp(lc_sym, entry, form_set, **extra):
305 305 if lc_sym == 'adj' and lexeme.refs_to.filter(type='nieadj'):
306 306 continue
307 307 if lc_sym == 'subst' and extra['tantum'] == 'sg':
308   - sgjp_forms = lexeme.all_forms(affixes=False, label_filter=r'sg:')
  308 + sgjp_forms = lexeme.all_forms(label_filter=r'sg:')
309 309 elif lexeme.part_of_speech.symbol == 'appas':
310   - sgjp_forms = lexeme.all_forms(affixes=True)
  310 + sgjp_forms = lexeme.all_forms()
311 311 else:
312   - sgjp_forms = lexeme.all_forms(affixes=False)
  312 + sgjp_forms = lexeme.all_forms()
313 313 if sgjp_forms == form_set:
314 314 matched_lexemes.append(lexeme)
315 315 continue
... ... @@ -320,11 +320,10 @@ def check_sgjp(lc_sym, entry, form_set, **extra):
320 320 inflection_characteristic__symbol__in=(
321 321 'm1', 'p1')).exists():
322 322 # depr
323   - exceptions = lexeme.all_forms(affixes=False,
324   - label_filter=r'^pl:nom$')
  323 + exceptions = lexeme.all_forms(label_filter=r'^pl:nom$')
325 324 elif lc_sym == 'adj':
326 325 # -o
327   - exceptions = lexeme.all_forms(affixes=False, label_filter=r'^0$')
  326 + exceptions = lexeme.all_forms(label_filter=r'^0$')
328 327 if form_set.issubset(sgjp_forms) and diff.issubset(exceptions):
329 328 matched_lexemes.append(lexeme)
330 329 if len(matched_lexemes) > 1:
... ...
dictionary/management/commands/import_warszawa.py
... ... @@ -43,7 +43,7 @@ def process_forms(forms, base, pos, patterns):
43 43 # # sprawdzić wygenerowane formy...
44 44 # p_forms = set()
45 45 # for pattern in k_patterns:
46   - # p_forms |= all_forms(pattern, ic, pos, base, affixes=False)
  46 + # p_forms |= all_forms(pattern, ic, pos, base)
47 47 # w_forms = set(form for form, tag in forms)
48 48 # if p_forms != w_forms:
49 49 # patterns_ok = False
... ...
dictionary/models.py
1 1 #-*- coding:utf-8 -*-
2 2  
3   -from django.db.models import *
4 3 from django.contrib.auth.models import User, Permission
  4 +from django.db.models import Manager, Model, CharField, ForeignKey, \
  5 + IntegerField, BooleanField, ManyToManyField, TextField, DateTimeField, \
  6 + OneToOneField
5 7 from common.util import no_history, GroupDict
6 8 from accounts.util import users_with_perm
7 9  
... ... @@ -234,7 +236,7 @@ class PatternType(Model):
234 236 symbol = CharField(
235 237 max_length=32, blank=True, db_column='wtyp', verbose_name=u'typ wzoru')
236 238  
237   - # TODO to powinno być raczej w bazie
  239 + # FIXME to powinno być raczej w bazie
238 240 def base_form_labels(self):
239 241 #bfl_ids = set(self.tabletemplate_set.distinct()
240 242 #.values_list('cell__base_form_label', flat=True))
... ... @@ -287,6 +289,19 @@ class Pattern(Model):
287 289 base_form_label=inflection_characteristic.basic_form_label)[
288 290 0].string
289 291  
  292 + def base_endings(self, label_filter=None):
  293 + bfls = self.type.base_form_labels()
  294 + endings = Ending.objects.filter(
  295 + base_form_label__in=bfls, pattern=self) \
  296 + .select_related('base_form_label').prefetch_related('qualifiers')
  297 + if label_filter is not None:
  298 + endings = endings.filter(
  299 + base_form_label__symbol__regex=label_filter)
  300 + bfl_dict = GroupDict((bfl, []) for bfl in bfls)
  301 + for ending in endings:
  302 + bfl_dict.add(ending.base_form_label, ending)
  303 + return bfl_dict
  304 +
290 305 def __unicode__(self):
291 306 return self.name
292 307  
... ... @@ -427,14 +442,12 @@ class Lexeme(Model):
427 442 for form in self.all_forms():
428 443 self.lexemeform_set.add(LexemeForm(form=form))
429 444  
430   - def all_forms(self, affixes=True, label_filter=None, variant='1'):
  445 + def all_forms(self, label_filter=None, variant='1'):
431 446 forms = set()
432 447 for lip in self.lexemeinflectionpattern_set.all():
433 448 forms |= set(
434 449 form for (indexes, form, qualifiers)
435   - in lip.all_forms(
436   - affixes=affixes, label_filter=label_filter,
437   - variant=variant))
  450 + in lip.all_forms(label_filter=label_filter, variant=variant))
438 451 return forms
439 452  
440 453 def get_root(self, pattern, inflection_characteristic):
... ... @@ -667,35 +680,8 @@ class LexemeInflectionPattern(Model):
667 680 tt = self.table_template(variant)
668 681 return tt.cell_set.select_related('base_form_label', 'tablecell')
669 682  
670   - def table_cells(self, variant='1'):
671   - tt = self.new_table_template(variant)
672   - # na razie olewamy atrybuty
673   - # TODO
674   - return NewTableCell.objects.filter(
675   - cell__table_template=tt,
676   - inflection_characteristics=self.inflection_characteristic,
677   - pattern_types=self.pattern.type).select_related(
678   - 'cell__base_form_label')
679   -
680   - def table_headers(self, variant='1'):
681   - tt = self.new_table_template(variant)
682   - # TODO uwzględniać atrybuty
683   - return tt.headers.filter(
684   - inflection_characteristics=self.inflection_characteristic,
685   - pattern_types=self.pattern.type)
686   -
687 683 def base_endings(self, label_filter=None):
688   - bfls = self.pattern.type.base_form_labels()
689   - endings = Ending.objects.filter(
690   - base_form_label__in=bfls, pattern=self.pattern) \
691   - .select_related('base_form_label').prefetch_related('qualifiers')
692   - if label_filter is not None:
693   - endings = endings.filter(
694   - base_form_label__symbol__regex=label_filter)
695   - bfl_dict = GroupDict((bfl, []) for bfl in bfls)
696   - for ending in endings:
697   - bfl_dict.add(ending.base_form_label, ending)
698   - return bfl_dict
  684 + return self.pattern.base_endings(label_filter)
699 685  
700 686 def inflection_table(self, variant, separated=False, qualifiers=None,
701 687 edit_view=False):
... ... @@ -768,83 +754,24 @@ class LexemeInflectionPattern(Model):
768 754  
769 755 def new_inflection_table(self, variant, separated=False, qualifiers=None,
770 756 edit_view=False):
771   - table_cells = self.table_cells(variant)
772   - headers = self.table_headers(variant)
773   - rows = set()
774   - last_col = 0
775   - for table_cell in table_cells:
776   - rows.add(table_cell.row)
777   - col = table_cell.col + table_cell.colspan - 1
778   - if col > last_col:
779   - last_col = col
780   - for header in headers:
781   - rows.add(header.row)
782   - col = header.col + header.colspan - 1
783   - if col > last_col:
784   - last_col = col
785   - table = [[{'type': 'empty'}
786   - for i in xrange(last_col)] for j in xrange(len(rows))]
787   - rows = sorted(rows)
788   - # słownik: nr rzędu w bazie -> rzeczywisty numer rzędu
789   - row_translate = dict(zip(rows, xrange(len(rows))))
790   - base_endings = self.base_endings()
791   - for tc in table_cells:
792   - x = tc.col - 1
793   - y = row_translate[tc.row]
794   - table_cell = table[y][x]
795   - assert table_cell['type'] != 'span'
796   - separator = u'·' if separated else u''
797   - forms = self.new_forms(
798   - tc, base_endings, separator=separator, qualifiers=qualifiers,
799   - edit_view=edit_view)
800   - if not forms:
801   - continue
802   - if table_cell['type'] == 'empty':
803   - table[y][x] = {
804   - 'type': 'forms',
805   - 'forms': forms,
806   - 'rowspan': tc.rowspan,
807   - 'colspan': tc.colspan,
808   - }
809   - for i in xrange(tc.colspan):
810   - for j in xrange(tc.rowspan):
811   - if (i, j) != (0, 0):
812   - assert table[y + j][x + i]['type'] == 'empty'
813   - table[y + j][x + i]['type'] = 'span'
814   - else:
815   - assert tc.rowspan == table_cell['rowspan']
816   - assert tc.colspan == table_cell['colspan']
817   - table_cell['forms'] += forms
818   - for header in headers:
819   - x = header.col - 1
820   - y = row_translate[header.row]
821   - assert table[y][x]['type'] == 'empty'
822   - table[y][x] = {
823   - 'type': 'label',
824   - 'label': [header.label],
825   - 'css_class': header.css_class,
826   - 'rowspan': header.rowspan,
827   - 'colspan': header.colspan,
828   - }
829   - for i in xrange(header.colspan):
830   - for j in xrange(header.rowspan):
831   - if (i, j) != (0, 0):
832   - assert table[y + j][x + i]['type'] == 'empty'
833   - table[y + j][x + i]['type'] = 'span'
834   - return [row for row in table
835   - if not all(cell['type'] == 'empty' for cell in row)]
  757 + tt = self.new_table_template(variant)
  758 + lip_qualifiers = self.qualifiers.all() if not edit_view else []
  759 + return tt.render(
  760 + self.pattern, self.inflection_characteristic,
  761 + separated, qualifiers, edit_view,
  762 + self.index, lip_qualifiers, self.root)
836 763  
837   - def all_forms(self, separator='', affixes=True, label_filter=None,
  764 + def all_forms(self, separator=u'', label_filter=None,
838 765 variant='1', qualifiers=None):
839 766 forms = []
840 767 base_endings = self.base_endings(label_filter)
841 768 for cell in self.cells(variant=variant):
842 769 forms += self.forms(
843   - cell, base_endings, separator, affixes, qualifiers=qualifiers)
  770 + cell, base_endings, separator, qualifiers=qualifiers)
844 771 return forms
845 772  
846   - def forms(self, cell, base_endings, separator='',
847   - affixes=True, qualifiers=None, edit_view=False):
  773 + def forms(self, cell, base_endings, separator=u'', qualifiers=None,
  774 + edit_view=False):
848 775 if qualifiers:
849 776 qualifiers_set = set(qualifiers)
850 777  
... ... @@ -858,51 +785,19 @@ class LexemeInflectionPattern(Model):
858 785 if not edit_view:
859 786 #l_qual = set(self.lexeme.qualifiers.all()) & set(qualifiers)
860 787 lip_qual = filter_quals(self.qualifiers.all())
861   - forms = [
  788 + else:
  789 + lip_qual = set()
  790 + return [
862 791 (
863 792 (cell.index, self.index, ending.index),
864   - (
865   - cell.prefix + self.root + separator + ending.string + cell.suffix
866   - if affixes else self.root + separator + ending.string),
867   - #+ '#' + cell.base_form_label.symbol,
  793 + (cell.prefix + self.root + separator + ending.string +
  794 + cell.suffix),
868 795 combine_qualifiers(lip_qual,
869 796 filter_quals(ending.qualifiers.all()))
870 797 if not edit_view else filter_quals(ending.qualifiers.all()),
871 798 )
872 799 for ending in endings
873 800 ]
874   - return forms
875   -
876   - def new_forms(self, table_cell, base_endings, separator='',
877   - affixes=True, qualifiers=None, edit_view=False):
878   - cell = table_cell.cell
879   - if qualifiers:
880   - qualifiers_set = set(qualifiers)
881   -
882   - def filter_quals(quals):
883   - if not qualifiers:
884   - return set(quals)
885   - else:
886   - return set(quals) & qualifiers_set
887   -
888   - endings = base_endings[cell.base_form_label]
889   - if not edit_view:
890   - #l_qual = set(self.lexeme.qualifiers.all()) & set(qualifiers)
891   - lip_qual = filter_quals(self.qualifiers.all())
892   - forms = [
893   - (
894   - (table_cell.index, self.index, ending.index),
895   - (
896   - cell.prefix + self.root + separator + ending.string + cell.suffix
897   - if affixes else self.root + separator + ending.string),
898   - #+ '#' + cell.base_form_label.symbol,
899   - combine_qualifiers(lip_qual,
900   - filter_quals(ending.qualifiers.all()))
901   - if not edit_view else filter_quals(ending.qualifiers.all()),
902   - )
903   - for ending in endings
904   - ]
905   - return forms
906 801  
907 802 def editable_vocabularies(self, user):
908 803 return self.lexeme.editable_vocabularies(user)
... ... @@ -920,8 +815,7 @@ class LexemeInflectionPattern(Model):
920 815 unique_together = ('lexeme', 'index')
921 816 ordering = ['index']
922 817  
923   -
924   -def all_forms(pattern, ic, pos, base, variant='1', affixes=True):
  818 +def all_forms(pattern, ic, pos, base, variant='1'):
925 819 root = get_root(base, pos, pattern, ic)
926 820 tt = TableTemplate.objects.get(
927 821 variant=variant, pattern_type=pattern.type,
... ... @@ -932,13 +826,14 @@ def all_forms(pattern, ic, pos, base, variant='1', affixes=True):
932 826 base_form_label=cell.base_form_label, pattern=pattern)
933 827 forms |= set(
934 828 cell.prefix + root + ending.string + cell.suffix
935   - if affixes else root + ending.string
936   - for ending in endings)
  829 + for ending in endings)
937 830 return forms
938 831  
939 832  
940 833 def combine_qualifiers(lip_qualifiers, e_qualifiers):
941 834 #qualifiers = set(l_qualifiers)
  835 + if not lip_qualifiers:
  836 + return e_qualifiers
942 837 qualifiers = set()
943 838 for q in list(lip_qualifiers) + list(e_qualifiers):
944 839 if q.exclusion_class:
... ... @@ -1197,6 +1092,86 @@ class NewTableTemplate(Model):
1197 1092 attributes = ManyToManyField(LexemeAttribute)
1198 1093 attribute_values = ManyToManyField(LexemeAttributeValue)
1199 1094  
  1095 + def table_cells(self, pattern, ic):
  1096 + # TODO uwzględniać atrybuty
  1097 + return NewTableCell.objects.filter(
  1098 + cell__table_template=self,
  1099 + inflection_characteristics=ic,
  1100 + pattern_types=pattern.type).select_related(
  1101 + 'cell__base_form_label')
  1102 +
  1103 + def table_headers(self, pattern, ic):
  1104 + # TODO uwzględniać atrybuty
  1105 + return self.headers.filter(
  1106 + inflection_characteristics=ic, pattern_types=pattern.type)
  1107 +
  1108 + def render(self, pattern, ic, separated, qualifiers, edit_view,
  1109 + lip_index, lip_qualifiers, root):
  1110 + table_cells = self.table_cells(pattern, ic)
  1111 + headers = self.table_headers(pattern, ic)
  1112 + base_endings = pattern.base_endings()
  1113 + rows = set()
  1114 + last_col = 0
  1115 + for table_cell in table_cells:
  1116 + rows.add(table_cell.row)
  1117 + col = table_cell.col + table_cell.colspan - 1
  1118 + if col > last_col:
  1119 + last_col = col
  1120 + for header in headers:
  1121 + rows.add(header.row)
  1122 + col = header.col + header.colspan - 1
  1123 + if col > last_col:
  1124 + last_col = col
  1125 + table = [[{'type': 'empty'}
  1126 + for i in xrange(last_col)] for j in xrange(len(rows))]
  1127 + rows = sorted(rows)
  1128 + # słownik: nr rzędu w bazie -> rzeczywisty numer rzędu
  1129 + row_translate = dict(zip(rows, xrange(len(rows))))
  1130 + for tc in table_cells:
  1131 + x = tc.col - 1
  1132 + y = row_translate[tc.row]
  1133 + table_cell = table[y][x]
  1134 + assert table_cell['type'] != 'span'
  1135 + separator = u'·' if separated else u''
  1136 + forms = tc.cell.forms(base_endings, separator, root, lip_qualifiers,
  1137 + lip_index, tc.index, qualifiers=qualifiers, edit_view=edit_view)
  1138 + if not forms:
  1139 + continue
  1140 + if table_cell['type'] == 'empty':
  1141 + table[y][x] = {
  1142 + 'type': 'forms',
  1143 + 'forms': forms,
  1144 + 'rowspan': tc.rowspan,
  1145 + 'colspan': tc.colspan,
  1146 + }
  1147 + for i in xrange(tc.colspan):
  1148 + for j in xrange(tc.rowspan):
  1149 + if (i, j) != (0, 0):
  1150 + assert table[y + j][x + i]['type'] == 'empty'
  1151 + table[y + j][x + i]['type'] = 'span'
  1152 + else:
  1153 + assert tc.rowspan == table_cell['rowspan']
  1154 + assert tc.colspan == table_cell['colspan']
  1155 + table_cell['forms'] += forms
  1156 + for header in headers:
  1157 + x = header.col - 1
  1158 + y = row_translate[header.row]
  1159 + assert table[y][x]['type'] == 'empty'
  1160 + table[y][x] = {
  1161 + 'type': 'label',
  1162 + 'label': [header.label],
  1163 + 'css_class': header.css_class,
  1164 + 'rowspan': header.rowspan,
  1165 + 'colspan': header.colspan,
  1166 + }
  1167 + for i in xrange(header.colspan):
  1168 + for j in xrange(header.rowspan):
  1169 + if (i, j) != (0, 0):
  1170 + assert table[y + j][x + i]['type'] == 'empty'
  1171 + table[y + j][x + i]['type'] = 'span'
  1172 + return [row for row in table
  1173 + if not all(cell['type'] == 'empty' for cell in row)]
  1174 +
1200 1175  
1201 1176 class NewCell(Model):
1202 1177 table_template = ForeignKey(NewTableTemplate, related_name='cells')
... ... @@ -1208,6 +1183,36 @@ class NewCell(Model):
1208 1183 #inflection_characteristics = ManyToManyField(InflectionCharacteristic)
1209 1184 #attribute_values = ManyToManyField(LexemeAttributeValue)
1210 1185  
  1186 + def forms(self, base_endings, separator=u'', root=u'',
  1187 + lip_qualifiers=None, lip_index=0, table_index=0,
  1188 + qualifiers=None, edit_view=False):
  1189 + if qualifiers:
  1190 + qualifiers_set = set(qualifiers)
  1191 +
  1192 + def filter_quals(quals):
  1193 + if not qualifiers:
  1194 + return set(quals)
  1195 + else:
  1196 + return set(quals) & qualifiers_set
  1197 +
  1198 + if not edit_view:
  1199 + #l_qual = filter_quals(lexeme_qualifiers)
  1200 + lip_qual = filter_quals(lip_qualifiers)
  1201 + else:
  1202 + lip_qual = set()
  1203 + endings = base_endings[self.base_form_label]
  1204 + forms = [
  1205 + (
  1206 + (table_index, lip_index, ending.index),
  1207 + (self.prefix + root + separator + ending.string +
  1208 + self.suffix),
  1209 + combine_qualifiers(
  1210 + lip_qual, filter_quals(ending.qualifiers.all())),
  1211 + )
  1212 + for ending in endings
  1213 + ]
  1214 + return forms
  1215 +
1211 1216  
1212 1217 class NewTableCell(Model):
1213 1218 cell = ForeignKey(NewCell, related_name='table_cells')
... ... @@ -1227,7 +1232,6 @@ class NewExportCell(Model):
1227 1232 pattern_types = ManyToManyField(PatternType)
1228 1233 inflection_characteristics = ManyToManyField(InflectionCharacteristic)
1229 1234 # attribute_values = ManyToManyField(LexemeAttributeValue)
1230   - # parts of speech
1231 1235 tag_template = TextField()
1232 1236  
1233 1237  
... ...
dictionary/templates/table_view.html 0 → 100644
  1 +{% extends "base.html" %}
  2 +{% load format_date pagination_tags %}
  3 +
  4 +{% block extrahead %}
  5 + <link rel="stylesheet" type="text/css"
  6 + href="{{ MEDIA_URL }}css/table_view.css"/>
  7 + <script type="text/javascript"
  8 + src="{{ MEDIA_URL }}js/table-view.js"></script>
  9 +{% endblock %}
  10 +
  11 +{% block title %}Historia{% endblock %}
  12 +
  13 +{% block content %}
  14 + <h3>Historia zmian</h3>
  15 + <div class="tabs">
  16 + <ul>
  17 + <li><a href="#lexeme_items">Leksemy</a></li>
  18 + </ul>
  19 + <div id="lexeme_items">
  20 + {% paginated_list_panel "lexeme_items" %}
  21 + {% paginated_list "lexeme_items" %}
  22 + </div>
  23 + </div>
  24 +{% endblock %}
0 25 \ No newline at end of file
... ...
dictionary/views.py
... ... @@ -409,7 +409,9 @@ def show_new_tables(request):
409 409 'cells__table_cells')
410 410 }
411 411  
412   -def edit_tables(request):
  412 +@render('table_view.html')
  413 +def table_view(request):
  414 +
413 415 # muszą być formularze do wybierania typów wzorów i charfli
414 416 # i współrzędnych w tabeli
415 417 # i dodawania nowych form (prefiks, efobaz, sufiks)
... ...
media/css/table_view.css 0 → 100644
media/js/table-view.js 0 → 100644