ajax in clients and more fixes

remotes/origin/yandex
Bachurin Sergey 12 years ago
parent 4324af05d5
commit 5b400d56af
  1. 54
      project/customer/views/clients_ajax.py
  2. 27
      project/static/js/client.commons.js
  3. 23
      project/static/js/client.list.js
  4. 13
      project/static/js/dialogs.js
  5. 3
      project/templates/customer/clients/form.html
  6. 6
      project/templates/customer/clients/list.html

@ -3,11 +3,14 @@ import simplejson as json
from django.shortcuts import get_object_or_404
from django.http import HttpResponseBadRequest, HttpResponse
from django.template import RequestContext
from django.views.decorators.http import require_POST
from django.views.decorators.csrf import csrf_protect
from django.contrib.auth.decorators import login_required
from django.template.loader import render_to_string
from .. import models, forms
from ...docs.models import Invoice, Faktura, AktRabot, AktSverki, Nakladn, Platejka, Dover
from ..decorators import license_required
@ -43,12 +46,16 @@ def clients_add_ajax(request):
new_client_id = None
new_client_str = None
form = form_class(data=request.POST)
new_client_id = None
html = ''
if form.is_valid():
new_client = form.save(commit=False)
new_client.user = request.user
new_client.save()
new_client_id = new_client.id
new_client_str = new_client.name
html = render_to_string('customer/clients/list_item.html',
{'obj': new_client}, RequestContext(request))
non_field_errors = form.non_field_errors()
if not form.is_valid():
@ -59,9 +66,10 @@ def clients_add_ajax(request):
'field_errors': form.errors, # ошибки полей
'form_errors': non_field_errors, # ошибки формы
#'reload': form.is_valid() and 'reload_on_success' in request.GET
'reload': True,
'reload': False,
'id': new_client_id,
'name': new_client_str,
'action': 'client-add',
'row_html': html,
}
return HttpResponse(json.dumps(data), mimetype='application/json')
@ -80,9 +88,11 @@ def clients_edit_ajax(request, id):
form = form_class(data=request.POST, instance=client)
if form.is_valid():
form.save()
client = form.save()
non_field_errors = form.non_field_errors()
html = render_to_string('customer/clients/list_item.html',
{'obj': client}, RequestContext(request))
if not form.is_valid():
non_field_errors.append(u'Заполните/исправьте выделенные поля.')
@ -90,7 +100,13 @@ def clients_edit_ajax(request, id):
'success': form.is_valid(),
'field_errors': form.errors, # ошибки полей
'form_errors': non_field_errors, # ошибки формы
'reload': True,
'reload': False,
'id': client.id,
'name': client.name,
'contact': client.contact_name,
'phone': client.contact_phone,
'action': 'client-edit',
'row_html': html,
}
return HttpResponse(json.dumps(data), mimetype='application/json')
@ -104,12 +120,30 @@ def clients_delete_ajax(request, id):
return HttpResponseBadRequest()
client = get_object_or_404(models.Client, pk=id, user=request.user)
client.delete()
client_docs = []
doc_list = [(Invoice, u'счета'), (Faktura, u'счета-фактуры'), (Nakladn, u'накладные'),
(AktRabot, u'акты выполненных работ'), (Platejka, u'платёжные поручения'),
(Dover, u'доверенности'), (AktSverki, u'акты сверки')]
for doc in doc_list:
docs = doc[0].objects.filter(client=client)
if docs:
client_docs.append(doc[1])
if not client_docs:
client.delete()
success = True
message = {'title': u'Инфо', 'msg': u'Контрагент удалён.'}
del_id = id
else:
success = True
message = {'title': u'Инфо', 'msg': u'Контрагент не удалён. Есть выписанные документы: %s.' % ','.join(client_docs)}
del_id = None
# TODO обработать ошибки удаления
data = {
'success': True,
'message': {'title': 'Инфо', 'msg': 'Контрагент удалён.',},
'reload': True,
'success': success,
'message': message,
'reload': False,
'action': 'client-delete',
'id': del_id,
}
return HttpResponse(json.dumps(data), mimetype='application/json')

@ -1,14 +1,25 @@
function setup_client_edit_form(form) {
var btn1_caption = form.data('dialog-btn-caption') || "Сохранить";
form.dialog({
modal: true,
autoOpen: false,
minWidth: 670
minWidth: 670,
buttons: [
{
text: btn1_caption,
click: function(){
form.submit()
},
},
{
text:"Отмена",
click: function(){
$(this).dialog("close");
},
}
]
});
$('button[name=close-form]', form).click(function() {
form.dialog('close');
return false;
});
}
function setup_client_delete_form(form) {
@ -17,15 +28,11 @@ function setup_client_delete_form(form) {
autoOpen: false
});
$('button[name=close-form]', form).click(function() {
form.dialog('close');
return false;
});
}
function setup_client_edit_links(form, reload_on_success) {
$('table#clients td a.client.edit-link').each(function() {
$(this).click(function() {
$(this).on('click', function() {
var link = $(this);
var form_action = link.attr('href') + 'ajax/'; // url to post form

@ -1,14 +1,17 @@
$(document).ready(function() {
var edit_form = $('#client-edit-form');
if (edit_form) {
setup_client_edit_form(edit_form);
setup_client_edit_links(edit_form, true);
setup_client_add_link(edit_form, true);
}
window.clients_edit_del_update = function() {
var edit_form = $('#client-edit-form');
if (edit_form) {
setup_client_edit_form(edit_form);
setup_client_edit_links(edit_form, true);
setup_client_add_link(edit_form, true);
}
var delete_form = $('#client-delete-form');
if (delete_form) {
setup_client_edit_form(delete_form);
setup_client_delete_links(delete_form, true);
var delete_form = $('#client-delete-form');
if (delete_form) {
setup_client_edit_form(delete_form);
setup_client_delete_links(delete_form, true);
}
}
window.clients_edit_del_update();
});

@ -41,6 +41,19 @@ $(document).ready(function() {
dlg_msg.dialog('open');
}
}
if (data.action){
if (data.action == 'client-add'){
$(data.row_html).insertAfter('#clients_header_row');
window.clients_edit_del_update();
}
if (data.action == 'client-edit'){
$("tr#client_" + data.id).replaceWith(data.row_html);
window.clients_edit_del_update();
}
if (data.action == 'client-delete'){
$("tr#client_" + data.id).remove();
}
}
else if (data.reload) {
window.location.reload(true);
}

@ -206,9 +206,6 @@
{% if not is_ajax %}
<input type="submit" name="submit" value="Сохранить" />
<input type="submit" name="_cancel" value="Отмена" />
{% else %}
<button type="submit" role="button" name="submit" id='add_client'>Сохранить</button>
<button class="close-form" type="button" role="close" name="close-form">Отмена</button>
{% endif %}
</div>
</form>

@ -15,7 +15,7 @@
<br /><br />
<table id="clients" class="list">
<tr>
<tr id='clients_header_row'>
<th style="width: 50%">Контрагент</th>
<th style="width: 18%">Контактное лицо</th>
<th style="width: 14%">Телефон</th>
@ -35,12 +35,10 @@
{% block dialogs %}
{% include "customer/clients/form.html" with form=client_form is_ajax="True" %}
<form id="client-delete-form" action="" method="post" title="Удалить контрагента">
<form id="client-delete-form" action="" method="post" title="Удалить контрагента" data-dialog-btn-caption="Да, я уверен">
{% csrf_token %}
<div class="errors-layout"></div>
<p>Вы уверены, что хотите удалить контрагента <span class='client'></span>?</p>
<button type="submit" role="button" name="submit">Да, я уверен</button>
<button class="close-form" type="button" role="close" name="close-form">Отмена</button>
</form>
{% endblock %}

Loading…
Cancel
Save