Commit 23aa3c4cdf485a71bab36b5b4ac9116ab827c576

Authored by janek37
1 parent b9327834

działa ostrzeganie przed usunięciem atrybutów

dictionary/ajax_lexeme_view.py
... ... @@ -27,9 +27,9 @@ def get_inflection_tables(request, variant, lexeme_id):
27 27 return {'tables': tables}
28 28  
29 29 @ajax(method='get', template='inflection_table.html')
30   -def table_preview(request, id, pattern, inflection_characteristic, lip_id,
31   - entry=None, pos=None):
32   - lexeme = Lexeme.all_objects.get(pk=id)
  30 +def table_preview(request, lexeme_id, pattern, inflection_characteristic,
  31 + lip_id, entry=None, pos=None):
  32 + lexeme = Lexeme.all_objects.get(pk=lexeme_id)
33 33 if not lexeme.perm(request.user, 'view'):
34 34 raise AjaxError('access denied')
35 35 if entry is None:
... ... @@ -92,6 +92,15 @@ def extra_attributes(request, lexeme_id, pos, ics):
92 92 l, part_of_speech=part_of_speech, ics=ics),
93 93 }
94 94  
  95 +@ajax(method='get')
  96 +def check_attributes(request, lexeme_id, pos, ics):
  97 + l = Lexeme.objects.get(pk=lexeme_id)
  98 + part_of_speech = PartOfSpeech.objects.get(symbol=pos)
  99 + ics = InflectionCharacteristic.objects.filter(
  100 + part_of_speech=part_of_speech, entry__in=ics)
  101 + attrs = list(l.attributes(part_of_speech, ics).values_list('pk', flat=True))
  102 + return {'attrs': attrs}
  103 +
95 104 @render_template('lexeme_edit_form.html')
96 105 @ajax(method='get', template='lexeme_edit_form.html')
97 106 def lexeme_edit_form(request, id):
... ... @@ -279,8 +288,6 @@ def update_lexeme(request, form_data, mask=''):
279 288 cr_form.save()
280 289 else:
281 290 raise AjaxError(error_messages(cr_form))
282   - # TODO trzeba porównywać z poprzednią wartością
283   - # i jeśli się różni to do usuwać ze starej i dodawać do nowej
284 291 if name.startswith('attr') and prefix not in submitted_attrs:
285 292 submitted_attrs.append(prefix)
286 293 attr_id = prefix[len('attr'):]
... ... @@ -356,9 +363,9 @@ def get_list(form_data, name):
356 363 return [pair['value'] for pair in form_data if pair['name'] == name]
357 364  
358 365 @ajax(method='post')
359   -def delete_lexeme(request, id):
360   - id = int(id)
361   - l = Lexeme.objects.get(pk=id)
  366 +def delete_lexeme(request, lexeme_id):
  367 + lexeme_id = int(lexeme_id)
  368 + l = Lexeme.objects.get(pk=lexeme_id)
362 369 if not l.perm(request.user, 'change'):
363 370 raise AjaxError('access denied')
364 371 l.deleted = True
... ... @@ -366,8 +373,8 @@ def delete_lexeme(request, id):
366 373 key_list = cache.get('key_list', [])
367 374 for key in key_list:
368 375 pk_list = cache.get(key)
369   - if pk_list and id in pk_list:
370   - pk_list.remove(id)
  376 + if pk_list and lexeme_id in pk_list:
  377 + pk_list.remove(lexeme_id)
371 378 cache.set(key, pk_list)
372 379 return {}
373 380  
... ... @@ -501,11 +508,12 @@ def add_vocabulary(request, name):
501 508 vocab.managers.add(request.user) #add
502 509 return {}
503 510  
  511 +# nieużywane
504 512 @ajax(method='get')
505   -def vocabulary_privileges(request, name):
  513 +def vocabulary_permissions(request, vocab_id):
506 514 if not request.user.has_perm('dictionary.manage_vocabulary'):
507 515 raise AjaxError('access denied')
508   - vocab = Vocabulary.objects.get(id=name)
  516 + vocab = Vocabulary.objects.get(id=vocab_id)
509 517 return {
510 518 'managers': list(vocab.all_managers().values_list('pk', flat=True)),
511 519 'viewers': list(vocab.all_viewers().values_list('pk', flat=True)),
... ... @@ -513,18 +521,18 @@ def vocabulary_privileges(request, name):
513 521 }
514 522  
515 523 @ajax(method='post')
516   -def set_vocabulary_privilege(request, name, id, type, set):
  524 +def set_vocabulary_permission(request, name, vocab_id, perm, on):
517 525 if not request.user.has_perm('dictionary.manage_vocabulary'):
518 526 raise AjaxError('access denied')
519 527 vocab = Vocabulary.objects.get(id=name)
520   - user = User.objects.get(pk=id)
521   - if type == 'view':
  528 + user = User.objects.get(pk=vocab_id)
  529 + if perm == 'view':
522 530 related_manager = vocab.viewers
523   - elif type == 'change':
  531 + elif perm == 'change':
524 532 related_manager = vocab.editors
525 533 else: # type == 'manage'
526 534 related_manager = vocab.managers
527   - if set:
  535 + if on:
528 536 related_manager.add(user) #add
529 537 else:
530 538 related_manager.remove(user) #add
... ...
dictionary/views.py
... ... @@ -70,6 +70,7 @@ def lexeme_view(request):
70 70 'ajax_create_lexeme': reverse('create_lexeme'),
71 71 'ajax_classification_forms': reverse('classification_forms'),
72 72 'ajax_extra_attributes': reverse('extra_attributes'),
  73 + 'ajax_check_attributes': reverse('check_attributes'),
73 74 'ajax_homonym_count': reverse('homonym_count'),
74 75 'ajax_cr_homonyms': reverse('cr_homonyms'),
75 76 'ajax_new_action_row': reverse('new_action_row'),
... ...
media/js/common.js
... ... @@ -121,18 +121,30 @@ $.ajaxJSON = function(params) {
121 121 var encoded_data = {};
122 122 if (params.save && !before_save())
123 123 return false;
  124 + if (params.async === undefined)
  125 + params.async = true;
124 126 $.each(params.data, function(key, value) {
125 127 encoded_data[key] = $.toJSON(value);
126 128 });
127   - $.ajax({
128   - type: params.method,
129   - url: params.url,
130   - dataType: 'json',
131   - data: encoded_data,
132   - success: parse_result(params),
133   - error: params.error_callback
134   - });
135   - return true;
  129 + if (params.async === true) {
  130 + $.ajax({
  131 + type: params.method,
  132 + url: params.url,
  133 + dataType: 'json',
  134 + data: encoded_data,
  135 + success: parse_result(params),
  136 + error: params.error_callback
  137 + });
  138 + return true;
  139 + } else {
  140 + return $.parseJSON($.ajax({
  141 + type: params.method,
  142 + url: params.url,
  143 + dataType: 'json',
  144 + data: encoded_data,
  145 + async: false
  146 + }).responseText);
  147 + }
136 148 };
137 149  
138 150 function blank(str) {
... ...
media/js/lexeme-view.js
... ... @@ -264,7 +264,6 @@ function init_form_widgets() {
264 264 $('#cr-list').append(new_row);
265 265 jqgrid.show_changed();
266 266 });
267   - $(document).on('change', '#id_part_of_speech', check_pos);
268 267  
269 268 var update_preview = function() {
270 269 var lip_row = $(this).closest('.lip-row');
... ... @@ -362,10 +361,9 @@ function init_form_widgets() {
362 361 $(document).on('click', '#move-lexeme .label', function() {
363 362 $('#move-lexeme').toggleClass('move-hidden');
364 363 });
  364 + $(document).on('change', '#id_part_of_speech', check_pos);
365 365 $(document).on('change', '#id_new_owner', reload_classifications);
366   - // TODO
367   - //$(document).on('change', '#id_part_of_speech', reload_classifications);
368   - $(document).on('change', '#id_part_of_speech', reload_attributes);
  366 + // TODO trzeba też uwzględniać usunięcie odmieniasia
369 367 $(document).on('change', '.inflection-characteristic', reload_attributes);
370 368 $(document).on('keyup', '#id_entry', show_homonym_count);
371 369 }
... ... @@ -633,7 +631,7 @@ var set_active_lip_row = function() {
633 631 $(this).addClass('lip-row-active ui-state-active');
634 632 // tabelka
635 633 var data = {
636   - id: lexeme_id(),
  634 + lexeme_id: lexeme_id(),
637 635 entry: get_entry(),
638 636 pattern: $('input.pattern', this).value(),
639 637 inflection_characteristic:
... ... @@ -658,7 +656,7 @@ function delete_lexeme() {
658 656 $.ajaxJSON({
659 657 method: 'post',
660 658 url: $dj.ajax_delete_lexeme,
661   - data: {id: id},
  659 + data: {lexeme_id: id},
662 660 description: 'Usunięcie leksemu',
663 661 callback: function() {
664 662 jqgrid.grid.trigger("reloadGrid");
... ... @@ -838,7 +836,7 @@ var set_active_prompter_row = function() {
838 836 $(this).addClass('prompter-row-active');
839 837 // tabelka
840 838 var data = {
841   - id: lexeme_id(),
  839 + lexeme_id: lexeme_id(),
842 840 entry: get_entry(),
843 841 pos: get_pos(),
844 842 pattern: $('.pattern', this).text(),
... ... @@ -871,42 +869,65 @@ function input_prompter_pattern() {
871 869 var check_pos = function() {
872 870 "use strict";
873 871 var pos = get_pos();
874   - var bad_lips = [], confirmed;
  872 + var bad_lips = [], good_ics = [], stale_attrs = [], confirmed;
875 873 var lip_row_elems = $('.lip-row'), cr_row_elems = $('.cr-row');
876 874 lip_row_elems.each(function() {
877 875 var ic = $(this).find('select.inflection-characteristic').val();
878 876 var ic_entry = $(this).find(
879 877 'select.inflection-characteristic :selected').text();
880 878 var pattern = $(this).find('input.pattern').val();
881   - var result = $.ajax({
882   - type: 'get',
  879 + var answer = $.ajaxJSON({
  880 + method: 'get',
883 881 url: $dj.ajax_check_pos,
884 882 data: {pos_id: pos, ic_id: ic},
885 883 async: false
886   - }).responseText;
887   - if ($.parseJSON(result).answer !== 'yes')
  884 + }).answer;
  885 + if (answer !== 'yes')
888 886 bad_lips.push({ic: ic_entry, pattern: pattern});
  887 + else
  888 + good_ics.push(ic_entry);
  889 + });
  890 + var new_attrs = $.ajaxJSON({
  891 + method: 'get',
  892 + url: $dj.ajax_check_attributes,
  893 + data: {lexeme_id: lexeme_id(), pos: pos, ics: good_ics},
  894 + async: false
  895 + }).attrs;
  896 + var old_attrs = $('#extra-attributes').find('select').map(function(i, el) {
  897 + return parseInt(el.id.split('-')[0].substring('id_attr'.length), 10);
889 898 });
890   - if (bad_lips || cr_row_elems.length > 0) {
891   - var lips_text, cr_text = '', lips = [];
  899 + $.each(old_attrs, function(i, attr) {
  900 + if (new_attrs.indexOf(attr) === -1)
  901 + stale_attrs.push(attr);
  902 + });
  903 + if (bad_lips.length > 0 || cr_row_elems.length > 0 || stale_attrs.length > 0) {
  904 + var lips_text = '', cr_text = '', lips = [];
892 905 if (bad_lips.length > 0) {
893 906 $.each(bad_lips, function(i, t) {
894 907 lips.push('(' + t.ic + ', ' + t.pattern + ')');
895 908 });
896 909 }
897   - lips_text = " sposoby odmiany: " + lips.join(', ');
  910 + if (lips.length > 0) {
  911 + lips_text = "sposoby odmiany: " + lips.join(', ') + '\n';
  912 + }
898 913 if (cr_row_elems.length > 0) {
899   - if (lips_text)
900   - cr_text = ' i';
901   - cr_text += ' wszystkie odsyłacze';
  914 + cr_text += "wszystkie odsyłacze\n";
902 915 }
903   - confirmed = window.confirm('Zostaną usunięte' + lips_text + cr_text +
904   - '.\nKontynuować?');
  916 + // TODO ostrzegać przed znikaniem klasyfikacji [kiedyś]
  917 + var attr_text = '';
  918 + if (stale_attrs.length > 0) {
  919 + attr_text = "atrybuty: " + $.map(stale_attrs, function(attr) {
  920 + return $("[for='id_attr" + attr + "-value']").text().replace(':', '');
  921 + }).join(', ') + '\n';
  922 + }
  923 + confirmed = window.confirm(
  924 + 'Zostaną usunięte:\n' + lips_text + cr_text + attr_text + 'Kontynuować?');
905 925 if (!confirmed) {
906 926 this.selectedIndex = pos_index;
907 927 }
908 928 }
909   - if (confirmed || !(bad_lips || cr_row_elems.length > 0)) {
  929 + if (confirmed || !(bad_lips.length > 0 || cr_row_elems.length > 0 ||
  930 + stale_attrs.length > 0)) {
910 931 pos_index = this.selectedIndex;
911 932 var result = $.ajax({ // znowu brzydko...
912 933 type: 'get',
... ... @@ -948,6 +969,10 @@ var check_pos = function() {
948 969 deleted_cr.push(Number(li[0].id.split('-')[1]));
949 970 li.remove();
950 971 });
  972 + reload_attributes();
  973 + // TODO
  974 + // reload_classifications
  975 +
951 976 }
952 977 };
953 978  
... ...
media/js/manager-view.js
1 1 $(function() {
2 2 "use strict";
3   - /*$('#vocabulary-select').change(function() {
4   - $('#user-privileges').find('input[type=checkbox]').prop('checked', false);
5   - $.ajaxJSON({
6   - method: 'get',
7   - url: $dj.ajax_get_privileges,
8   - data: {name: $(this).value()},
9   - callback: function(data) {
10   - $.each(data.viewers, function(i, id) {
11   - $('#view-'+id).prop('checked', true);
12   - });
13   - $.each(data.editors, function(i, id) {
14   - $('#change-'+id).prop('checked', true);
15   - });
16   - $.each(data.managers, function(i, id) {
17   - $('#manage-'+id).prop('checked', true);
18   - });
19   - }
20   - });
21   - });*/
22 3 $('#add-vocabulary').click(function() {
23 4 var new_vocab = $('#new-vocabulary');
24 5 var name = new_vocab.value();
... ... @@ -43,9 +24,9 @@ $(function() {
43 24 url: $dj.ajax_set_privilege,
44 25 data: {
45 26 name: vocab_name,
46   - id: id,
47   - type: type,
48   - set: $t.is(':checked')
  27 + vocab_id: id,
  28 + perm: type,
  29 + on: $t.is(':checked')
49 30 }
50 31 });
51 32 var view_checkbox = $('#view-'+id), change_checkbox = $('#change-'+id);
... ...
... ... @@ -33,8 +33,8 @@ urlpatterns += patterns('dictionary.ajax_lexeme_view',
33 33 url(r'^ajax/new_lip_row/$', 'new_lip_edit_row'),
34 34 url(r'^ajax/save_columns/$', 'save_columns'),
35 35 url(r'^ajax/add_vocabulary/$', 'add_vocabulary'),
36   - url(r'^ajax/get_privileges/$', 'vocabulary_privileges'),
37   - url(r'^ajax/set_privilege$', 'set_vocabulary_privilege'),
  36 + url(r'^ajax/get_privileges/$', 'vocabulary_permissions'),
  37 + url(r'^ajax/set_privilege$', 'set_vocabulary_permission'),
38 38 url(r'^ajax/new_cr_row/$', 'new_cross_reference_row'),
39 39 url(r'^ajax/delete_lexeme/$', 'delete_lexeme'),
40 40 url(r'^ajax/check-pos/$', 'check_pos'),
... ... @@ -43,6 +43,7 @@ urlpatterns += patterns('dictionary.ajax_lexeme_view',
43 43 url(r'^ajax/create-lexeme/$', 'create_lexeme'),
44 44 url(r'^ajax/classification-forms/$', 'classification_forms'),
45 45 url(r'^ajax/extra-attributes/$', 'extra_attributes'),
  46 + url(r'^ajax/check-attributes/$', 'check_attributes'),
46 47 url(r'^ajax/homonym-count/$', 'homonym_count'),
47 48 url(r'^ajax/cr-homonyms/$', 'cr_homonyms'),
48 49 url(r'^ajax/new-action-row/$', 'new_action_row'),
... ...