Commit 55e69a66ee49c66f296b7bb497b73f5f082c00bd

Authored by janek37
1 parent 78cef100

dokończone utrzymywanie dwustronnych odsyłaczy

common/util.py
... ... @@ -113,7 +113,10 @@ def cut_end(s, end):
113 113  
114 114 def error_messages(form):
115 115 return '\n'.join(
116   - '%s: %s' % (form.fields[k].label, ' '.join(v)) if k != '__all__'
  116 + '%s: %s' % (
  117 + force_unicode(form.fields[k].label),
  118 + ' '.join(v))
  119 + if k != '__all__'
117 120 else ' '.join(v) for k, v in form.errors.iteritems())
118 121  
119 122  
... ...
dictionary/ajax_lexeme_view.py
... ... @@ -339,11 +339,12 @@ def update_lexeme(request, form_data):
339 339 for cr_pk in form_dict['deleted_cr']:
340 340 cr = CrossReference.objects.get(pk=cr_pk)
341 341 for rev_cr in cr.reversed():
342   - proposed_crs['delete'].append({
343   - 'cr_id': rev_cr.id,
344   - 'entry': rev_cr.from_lexeme.entry,
345   - 'type_desc': rev_cr.type.desc,
346   - })
  342 + if cr.from_lexeme.editable_vocabularies(request.user):
  343 + proposed_crs['delete'].append({
  344 + 'cr_id': rev_cr.id,
  345 + 'entry': rev_cr.from_lexeme.entry,
  346 + 'type_desc': rev_cr.type.desc,
  347 + })
347 348 cr.delete()
348 349 submitted_lips = []
349 350 submitted_crs = []
... ... @@ -380,16 +381,17 @@ def update_lexeme(request, form_data):
380 381 cr_form = CrossReferenceForm(data=form_dict, prefix=prefix)
381 382 if cr_form.is_valid():
382 383 cr = cr_form.save()
383   - if not cr.reversed():
384   - rev_type = cr.type.reversed()
385   - if rev_type:
386   - proposed_crs['create'].append({
387   - 'type_id': rev_type.id,
388   - 'from_lexeme': cr.to_lexeme.id,
389   - 'to_lexeme': cr.from_lexeme.id,
390   - 'entry': cr.to_lexeme.entry,
391   - 'type_desc': rev_type.desc,
392   - })
  384 + if cr.to_lexeme.editable_vocabularies(request.user):
  385 + if not cr.reversed():
  386 + rev_type = cr.type.reversed()
  387 + if rev_type:
  388 + proposed_crs['create'].append({
  389 + 'type_id': rev_type.id,
  390 + 'from_lexeme': cr.to_lexeme.id,
  391 + 'to_lexeme': cr.from_lexeme.id,
  392 + 'entry': cr.to_lexeme.entry,
  393 + 'type_desc': rev_type.desc,
  394 + })
393 395 else:
394 396 raise AjaxError(error_messages(cr_form))
395 397 if len(submitted_lips) == 0 and l.is_public():
... ... @@ -485,6 +487,27 @@ def create_derivatives(request, lexeme_id, chosen_derivatives):
485 487 return {}
486 488  
487 489  
  490 +@ajax(method='post')
  491 +def update_back_references(request, deleted_cr, created_cr):
  492 + user = request.user
  493 + for cr_data in deleted_cr:
  494 + cr = CrossReference.objects.get(id=int(cr_data['cr_id']))
  495 + if cr.from_lexeme.perm(user, 'change'):
  496 + cr.delete()
  497 + else:
  498 + raise AjaxError('access denied')
  499 + for cr_data in created_cr:
  500 + from_lexeme = Lexeme.objects.get(id=int(cr_data['from_lexeme']))
  501 + to_lexeme = Lexeme.objects.get(id=int(cr_data['to_lexeme']))
  502 + cr_type = CrossReferenceType.objects.get(id=cr_data['type_id'])
  503 + if from_lexeme.perm(user, 'change'):
  504 + CrossReference.objects.create(
  505 + from_lexeme=from_lexeme, to_lexeme=to_lexeme, type=cr_type)
  506 + else:
  507 + raise AjaxError('access denied')
  508 + return {}
  509 +
  510 +
488 511 def update_lexeme_qualifiers(lexeme, user, form_dict, form_data):
489 512 owner = lexeme.owner_vocabulary
490 513 editable_vocabs = editable_vocabularies(user).exclude(pk=owner.pk)
... ...
dictionary/models.py
... ... @@ -761,8 +761,9 @@ class Lexeme(Model):
761 761  
762 762 def perm(self, user, action):
763 763 if action == 'view':
764   - vocabs = self.vocabularies.all()
765   - return bool(vocabs & visible_vocabularies(user))
  764 + if not user.is_authenticated() and not self.is_public():
  765 + return False
  766 + return bool(self.visible_vocabularies(user))
766 767 elif action == 'change':
767 768 priority = user.has_perm('dictionary.lexeme_priority')
768 769 priority |= not (
... ...
dictionary/static/js/edit.js
... ... @@ -73,6 +73,12 @@ var edit = {
73 73 edit.busy_ctrl = null;
74 74 },
75 75  
  76 + cleanup: function() {
  77 + "use strict";
  78 + edit.changed = false;
  79 + edit.busy_off();
  80 + },
  81 +
76 82 confirm_discard: function() {
77 83 "use strict";
78 84 return window.confirm(
... ... @@ -127,7 +133,7 @@ $(function() {
127 133 "use strict";
128 134 $(document).on(
129 135 'submit', '#' + edit.form_id, function() {
130   - $(this).find('button[type=submit]').button('disable');
  136 + $('.' + edit.form_submit_class).button('disable');
131 137 var this_form = $(this);
132 138 var form_data = this_form.serializeArray();
133 139 edit.form_submit(form_data);
... ...
dictionary/static/js/lexeme-edit.js
... ... @@ -195,6 +195,26 @@ $.extend(edit, {
195 195 }
196 196 ]
197 197 });
  198 + $('#back-references-dialog').dialog({
  199 + autoOpen: false,
  200 + width: 'auto',
  201 + modal: true,
  202 + buttons: [
  203 + {
  204 + text: gettext("Anuluj"),
  205 + click: function() {
  206 + $(this).dialog('close');
  207 + }
  208 + },
  209 + {
  210 + text: gettext("Zastosuj"),
  211 + click: function() {
  212 + $(this).dialog('close');
  213 + update_back_references();
  214 + }
  215 + }
  216 + ]
  217 + });
198 218 $('#action-button').click(function () {
199 219 $('#group-action-dialog').dialog('open');
200 220 });
... ... @@ -420,44 +440,50 @@ $.extend(edit, {
420 440 } else {
421 441 edit.load_content(edit_id);
422 442 }
423   - if (data.derivatives.length > 0) {
424   - auto_derivatives = data.derivatives;
425   - var ul = $('#auto-derivatives-list');
426   - ul.empty();
427   - $.each(data.derivatives, function(i, der_data) {
428   - var li = $('<li/>');
429   - var name = 'der' + i;
430   - var input = $('<input/>').attr('type', 'checkbox')
431   - .attr('name', name).attr('id', name);
432   - var label = $('<label/>').attr('for', name);
433   - li.append(input).append(label).appendTo(ul);
434   - if (der_data.entry) {
435   - label.text(der_data.cr_type + ', ' + der_data.entry);
436   - } else {
437   - var der_input = $('<input/>').attr('type', 'text')
438   - .addClass('derivative-input');
439   - label.text(der_data.cr_type + ', ');
440   - li.append(der_input);
441   - }
442   - });
443   - $('#auto-derivatives-dialog').dialog('open');
444   - }
  443 + suggest_back_references(data.proposed_crs);
  444 + suggest_derivatives(data.derivatives);
445 445 },
446 446 error_callback: function (xhr, status, error) {
447 447 common.error_alert(status + ': ' + error);
448   - edit.busy_off();
  448 + edit.cleanup();
449 449 },
450 450 bad_data_callback: function () {
451   - edit.busy_off();
  451 + edit.cleanup();
452 452 return true;
453 453 }
454 454 });
455 455 if (result === false) {
456   - edit.busy_off();
  456 + edit.cleanup();
457 457 }
458 458 }
459 459 });
460 460  
  461 +function suggest_derivatives(derivatives) {
  462 + "use strict";
  463 + if (derivatives.length > 0) {
  464 + auto_derivatives = derivatives;
  465 + var ul = $('#auto-derivatives-list');
  466 + ul.empty();
  467 + $.each(derivatives, function (i, der_data) {
  468 + var li = $('<li/>');
  469 + var name = 'der' + i;
  470 + var input = $('<input/>').attr('type', 'checkbox')
  471 + .attr('name', name).attr('id', name);
  472 + var label = $('<label/>').attr('for', name);
  473 + li.append(input).append(label).appendTo(ul);
  474 + if (der_data.entry) {
  475 + label.text(der_data.cr_type + ', ' + der_data.entry);
  476 + } else {
  477 + var der_input = $('<input/>').attr('type', 'text')
  478 + .addClass('derivative-input');
  479 + label.text(der_data.cr_type + ', ');
  480 + li.append(der_input);
  481 + }
  482 + });
  483 + $('#auto-derivatives-dialog').dialog('open');
  484 + }
  485 +}
  486 +
461 487 function create_auto_derivatives() {
462 488 "use strict";
463 489 // zebrać numery derywatów do utworzenia
... ... @@ -492,6 +518,66 @@ function create_auto_derivatives() {
492 518 });
493 519 }
494 520  
  521 +function suggest_back_references(proposed_crs) {
  522 + "use strict";
  523 + if (proposed_crs.delete.length > 0 || proposed_crs.create.length > 0) {
  524 + var cr_container = $('#proposed-crs'), ul;
  525 + cr_container.empty();
  526 + if (proposed_crs.delete.length > 0) {
  527 + var delete_label = $('<p/>').text(gettext("Do usunięcia:"));
  528 + ul = $('<ul/>').attr('id', 'delete-cr');
  529 + $.each(proposed_crs.delete, function(i, cr_data) {
  530 + var li = $('<li/>');
  531 + var name = 'del_cr' + cr_data.cr_id;
  532 + var input = $('<input/>').attr('type', 'checkbox')
  533 + .attr('name', name).attr('id', name)
  534 + .data('cr_data', cr_data);
  535 + var label = $('<label/>').attr('for', name)
  536 + .text(cr_data.entry + ', ' + cr_data.type_desc);
  537 + li.append(input).append(label).appendTo(ul);
  538 + });
  539 + cr_container.append(delete_label).append(ul);
  540 + }
  541 + if (proposed_crs.create.length > 0) {
  542 + var create_label = $('<p/>').text(gettext("Do dodania:"));
  543 + ul = $('<ul/>').attr('id', 'create-cr');
  544 + $.each(proposed_crs.create, function(i, cr_data) {
  545 + var li = $('<li/>');
  546 + var name = 'new_cr' + cr_data.type_id + '-' +
  547 + cr_data.from_lexeme;
  548 + var input = $('<input/>').attr('type', 'checkbox')
  549 + .attr('name', name).attr('id', name)
  550 + .data('cr_data', cr_data);
  551 + var label = $('<label/>').attr('for', name)
  552 + .text(cr_data.entry + ', ' + cr_data.type_desc);
  553 + li.append(input).append(label).appendTo(ul);
  554 + });
  555 + cr_container.append(create_label).append(ul);
  556 + }
  557 + $('#back-references-dialog').dialog('open');
  558 + }
  559 +}
  560 +
  561 +function update_back_references() {
  562 + "use strict";
  563 + var chosen_delete = [], chosen_create = [];
  564 + $('#delete-cr').find(':checked').each(function(i, elem) {
  565 + chosen_delete.push($(elem).data('cr_data'));
  566 + });
  567 + $('#create-cr').find(':checked').each(function(i, elem) {
  568 + chosen_create.push($(elem).data('cr_data'));
  569 + });
  570 + $.ajaxJSON({
  571 + method: 'post',
  572 + url: $dj.ajax_update_back_references,
  573 + data: {
  574 + deleted_cr: chosen_delete,
  575 + created_cr: chosen_create
  576 + },
  577 + description: gettext("Zaktualizowanie zwrotnych derywatów")
  578 + });
  579 +}
  580 +
495 581 var deleted, deleted_cr;
496 582  
497 583 function get_pos() {
... ...
dictionary/templates/lexeme_view.html
... ... @@ -193,4 +193,7 @@
193 193 <table id="search-by-form-table">
194 194 </table>
195 195 </div>
  196 + <div id="back-references-dialog" title="{% trans 'Zwrotne odsyłacze' %}">
  197 + <div id="proposed-crs"></div>
  198 + </div>
196 199 {% endblock %}
... ...
dictionary/urls.py
... ... @@ -43,6 +43,7 @@ urlpatterns += patterns(
43 43 url(r'^ajax/dynamic-fields/$', 'dynamic_action_fields'),
44 44 url(r'^ajax/execute-actions/$', 'execute_group_actions'),
45 45 url(r'^ajax/create-derivatives/$', 'create_derivatives'),
  46 + url(r'^ajax/update-back-references/$', 'update_back_references'),
46 47 url(r'^ajax/clone-lexeme/$', 'clone_lexeme'),
47 48 url(r'^ajax/get-entry/$', 'get_entry'),
48 49 )
... ...
dictionary/views.py
... ... @@ -147,6 +147,7 @@ def lexeme_view(request):
147 147 'ajax_dynamic_fields': reverse('dynamic_action_fields'),
148 148 'ajax_execute_actions': reverse('execute_group_actions'),
149 149 'ajax_create_derivatives': reverse('create_derivatives'),
  150 + 'ajax_update_back_references': reverse('update_back_references'),
150 151 'ajax_clone_lexeme': reverse('clone_lexeme'),
151 152 'vocabs': vocabs,
152 153 'bs_options': [
... ...
patterns/static/js/pattern-edit.js
... ... @@ -157,7 +157,7 @@ $.extend(edit, {
157 157 });
158 158 form_data.push({name: 'ending_list', value: ending_list});
159 159 edit.busy_on();
160   - $.ajaxJSON({
  160 + var result = $.ajaxJSON({
161 161 method: 'post',
162 162 url: $dj.ajax_update_pattern,
163 163 data: {
... ... @@ -171,13 +171,16 @@ $.extend(edit, {
171 171 },
172 172 error_callback: function (xhr, status, error) {
173 173 common.error_alert(status + ': ' + error);
174   - edit.busy_off();
  174 + edit.cleanup();
175 175 },
176 176 bad_data_callback: function () {
177   - edit.busy_off();
  177 + edit.cleanup();
178 178 return true;
179 179 }
180 180 });
  181 + if (result === false) {
  182 + edit.cleanup();
  183 + }
181 184 }
182 185 });
183 186  
... ...