From 8308a7adecfacef0de65b5afa0cbdace746668c7 Mon Sep 17 00:00:00 2001 From: Bachurin Sergey Date: Fri, 29 Nov 2013 01:20:19 +0200 Subject: [PATCH] autocomplete, code-name ajax --- project/docs/admin.py | 2 + project/docs/autocomplete_light_registry.py | 12 ++++ project/docs/consts.py | 4 +- project/docs/forms/faktura.py | 5 +- project/docs/urls.py | 6 +- project/docs/views/__init__.py | 1 + project/docs/views/ajax.py | 47 +++++++++++++++ project/settings.py | 1 + project/static/js/formset-events.js | 12 ++-- project/templates/base.html | 1 + .../docs/parts/faktura_form_tbl_items.html | 2 +- project/templates/docs/stub_js.html | 57 +++++++++++++++++++ project/urls.py | 5 +- requirements-dev.txt | 3 +- requirements.txt | 3 +- 15 files changed, 146 insertions(+), 15 deletions(-) create mode 100644 project/docs/autocomplete_light_registry.py create mode 100644 project/docs/views/ajax.py diff --git a/project/docs/admin.py b/project/docs/admin.py index 18c42bd..c194bda 100644 --- a/project/docs/admin.py +++ b/project/docs/admin.py @@ -5,3 +5,5 @@ import models admin.site.register(models.Currency) +admin.site.register(models.Country) +admin.site.register(models.Measure) diff --git a/project/docs/autocomplete_light_registry.py b/project/docs/autocomplete_light_registry.py new file mode 100644 index 0000000..4a4c8f5 --- /dev/null +++ b/project/docs/autocomplete_light_registry.py @@ -0,0 +1,12 @@ +# -*- coding: utf-8 -*- +import autocomplete_light +from django.db.models import Q + +from models import (Country, Currency, Measure) + +autocomplete_light.register(Country, name='ACCountry') +autocomplete_light.register(Currency, name='ACCurrency') +autocomplete_light.register(Measure, name='ACMeasure') + + + diff --git a/project/docs/consts.py b/project/docs/consts.py index 3d60337..f44d913 100644 --- a/project/docs/consts.py +++ b/project/docs/consts.py @@ -20,8 +20,8 @@ NDS_TYPE_CHOICES = ( # ставка НДС NDS_VALUE_0 = 0 -NDS_VALUE_10 = 0.1 -NDS_VALUE_18 = 0.18 +NDS_VALUE_10 = 10 +NDS_VALUE_18 = 18 NDS_VALUE_CHOICES = ( (NDS_VALUE_0, u'Без НДС'), diff --git a/project/docs/forms/faktura.py b/project/docs/forms/faktura.py index be34285..12f7f47 100644 --- a/project/docs/forms/faktura.py +++ b/project/docs/forms/faktura.py @@ -1,8 +1,9 @@ # -*- coding: utf-8 -*- from django import forms -from project.commons.forms import MyBaseModelForm +import autocomplete_light +from project.commons.forms import MyBaseModelForm from .base_forms import BaseModelForm from ..models import Faktura, FakturaItem, PlatDoc @@ -41,6 +42,8 @@ class FakturaAdminForm(FakturaForm): class FakturaItemForm(MyBaseModelForm): """Форма редактирования табличной части фактуры.""" + units = forms.CharField(widget=autocomplete_light.TextWidget('ACMeasure'),) + country_name = forms.CharField(widget=autocomplete_light.TextWidget('ACCountry'),) class Meta: model = FakturaItem exclude = ['parent'] diff --git a/project/docs/urls.py b/project/docs/urls.py index 3789bdc..2a4282b 100644 --- a/project/docs/urls.py +++ b/project/docs/urls.py @@ -2,7 +2,7 @@ from django.conf.urls import * from .views import (InvoiceViews, AktRabotViews, AktSverkiViews, DoverViews, PlatejkaViews, NakladnViews, FakturaViews) -from .views import getview, index +from .views import getview, index, get_pair, get_invoices, get_tbl_items urlpatterns = patterns('docs.views', @@ -56,6 +56,10 @@ for name, klass in klasses: # отправить pdf/xls на email - AJAX url(r'^%s/(?P\d+)/email/ajax/$' % name, getview, {'klass': klass, 'oper': 'email_ajax',}, name='docs_%s_email_ajax' % name), + + url(r'^ajax_get_pair/(?P\w+)/(?P\w+)/(?P\w+)/(?P\w+)/$', get_pair, name='ajax_get_pair'), + url(r'^ajax_get_invoices/(?P\d+)/$', get_invoices, name='ajax_get_invoices'), + url(r'^ajax_get_tbl_items/(?P\d+)/$', get_tbl_items, name='ajax_get_tbl_items'), ) # доп. обработчики: создать Документ по Счету diff --git a/project/docs/views/__init__.py b/project/docs/views/__init__.py index 2c554d7..b39ca8e 100644 --- a/project/docs/views/__init__.py +++ b/project/docs/views/__init__.py @@ -11,6 +11,7 @@ from .dover import DoverViews from .platejka import PlatejkaViews from .nakladn import NakladnViews from .faktura import FakturaViews +from .ajax import get_pair, get_invoices, get_tbl_items #from .sfv import SfvViews diff --git a/project/docs/views/ajax.py b/project/docs/views/ajax.py new file mode 100644 index 0000000..2c8fedd --- /dev/null +++ b/project/docs/views/ajax.py @@ -0,0 +1,47 @@ +# -*- coding: utf-8 -*- +import simplejson as json + +from django.shortcuts import render, get_object_or_404, redirect +from django.http import HttpResponseBadRequest, HttpResponse +from django.core import serializers +from django.db.models import get_model +from django.views.decorators.csrf import csrf_protect + +from ..models import Invoice + + +def get_pair(request, model, param1, val1, param2): + if not request.is_ajax(): + return HttpResponseBadRequest() + + kwargs = {param1: val1} + + try: + q_model = get_model('docs', model) + val2 = getattr(q_model.objects.get(**kwargs), param2) + except: + val2 = None + data = { + 'val': val2, + } + return HttpResponse(json.dumps(data), mimetype='application/json') + + +def get_invoices(request, client_id): + if not request.is_ajax(): + return HttpResponseBadRequest() + + invoices = Invoice.objects.filter(client__id=client_id) + invoices = {invoice.id: '№ %s от %s' % (invoice.doc_num, invoice.doc_date) for invoice in invoices} + + return HttpResponse(json.dumps(invoices), mimetype='application/json') + + +def get_tbl_items(request, invoice_id): + if not request.is_ajax(): + return HttpResponseBadRequest() + + invoice = Invoice.objects.get(pk=invoice_id) + data = serializers.serialize('json', invoice.invoice_items.all()) + + return HttpResponse(json.dumps(data), mimetype='application/json') \ No newline at end of file diff --git a/project/settings.py b/project/settings.py index de76f09..7890082 100644 --- a/project/settings.py +++ b/project/settings.py @@ -140,6 +140,7 @@ INSTALLED_APPS = ( 'pytils', 'django_filters', + 'autocomplete_light', # my apps 'project.commons', diff --git a/project/static/js/formset-events.js b/project/static/js/formset-events.js index d7cc250..fd072be 100644 --- a/project/static/js/formset-events.js +++ b/project/static/js/formset-events.js @@ -1,4 +1,4 @@ -$(function($) { +$(function() { var calc_summa = function (e) { var $this = $(this); var qty = parseFloat($this.closest('tr.row').find('td.qty input').val()); @@ -8,6 +8,10 @@ $(function($) { $this.closest('tr.row').find('td.total_price input').val(qty * price); } } + var calc_itogo_nds = function() { + $('#itogo_nds_text').html('НДС ' + $('#id_nds_value option:selected').text()); + $('#itogo_nds').html(parseFloat($('#id_nds_value').val()) * parseFloat($('#itogo').html()) / 100); + } window.calc_itogo = function (e) { var itogo_sum = 0; $('#tbl_items').find('.total_price input:visible').each(function(){ @@ -15,12 +19,8 @@ $(function($) { itogo_sum += itogo_sum_row; }) $('#itogo').html(itogo_sum); + calc_itogo_nds(); } - var calc_itogo_nds = function() { - $('#itogo_nds_text').html('НДС ' + $('#id_nds_value option:selected').text()); - $('#itogo_nds').html($('#id_nds_value').val() * parseInt($('#itogo').html())); - } - window.set_events = function set_events() { $("#tbl_items tr.row td.qty input").on('keyup', calc_summa); $("#tbl_items tr.row td.price input").on('keyup', calc_summa); diff --git a/project/templates/base.html b/project/templates/base.html index 9f7394a..5a30c73 100644 --- a/project/templates/base.html +++ b/project/templates/base.html @@ -67,6 +67,7 @@ + {% include 'autocomplete_light/static.html' %} {% endblock %} {% block js %}{% endblock %} diff --git a/project/templates/docs/parts/faktura_form_tbl_items.html b/project/templates/docs/parts/faktura_form_tbl_items.html index 81bb9d4..6e95ecc 100644 --- a/project/templates/docs/parts/faktura_form_tbl_items.html +++ b/project/templates/docs/parts/faktura_form_tbl_items.html @@ -21,7 +21,7 @@ {{ iform.units.errors }} {{ iform.units }} - + {{ iform.units_kod.errors }} {{ iform.units_kod }} diff --git a/project/templates/docs/stub_js.html b/project/templates/docs/stub_js.html index 4e3bc1d..c77ad20 100644 --- a/project/templates/docs/stub_js.html +++ b/project/templates/docs/stub_js.html @@ -7,5 +7,62 @@ $('tr.row_tbl_items').formset({ prefix: '{{ formset.prefix }}' }); + var get_pair = function(model, class1, class2, name1, name2) { + $('body').on('focusout', '.' + class1 + ' input', function() { + var $this = $(this); + var $code_input = $this.closest('.row_tbl_items').find('.' + class2 + ' input'); + var code_input_val = $code_input.val(); + + $.get('/my/docs/ajax_get_pair/' + model + '/' + name1 + '/' + name2 + '/' + $this.val() + '/', function(data){ + if (data['val']) { + $code_input.val(data['val']); + if (($code_input.val() != '')&&(($code_input.val() != code_input_val))) { + $code_input.css('color', 'red') + } else { + $code_input.css('color', 'black') + } + $this.css('color', 'black'); + } + }); + }) + } + get_pair('Country', 'country_code', 'country_name', 'code', 'name'); + get_pair('Country', 'country_name', 'country_code', 'name', 'code'); + get_pair('Measure', 'units', 'units_kod', 'name', 'code'); + get_pair('Measure', 'units_kod', 'units', 'code', 'name'); + + $('#id_client').on('change', function() { + var client_id = $(this).val(); + $.get('/my/docs/ajax_get_invoices/' + client_id, function(data) { + var select = $('#id_invoice'); + if(select.prop) { + var options = select.prop('options'); + } + else { + var options = select.attr('options'); + } + $('option', select).remove(); + options[options.length] = new Option('--', null); + + $.each(data, function(val, text) { + options[options.length] = new Option(text, val); + }); + }) + }) + + $('#id_invoice').on('change', function() { + var invoice_id = $(this).val(); + $.get('/my/docs/ajax_get_tbl_items/' + invoice_id, function(data) { + var items = JSON.parse(data); + console.log(items.length); + $.each(items, function(index, item){ + var name = item['fields']['name']; + var units = item['fields']['units']; + var qty = item['fields']['qty']; + var price = item['fields']['price']; + var total_price = item['fields']['total_price']; + }) + }) + }) }); \ No newline at end of file diff --git a/project/urls.py b/project/urls.py index 2455ce7..f2bf327 100644 --- a/project/urls.py +++ b/project/urls.py @@ -1,9 +1,10 @@ # -*- coding: utf-8 -*- from django.conf.urls import patterns, include, url +import autocomplete_light -# Uncomment the next two lines to enable the admin: from django.contrib import admin admin.autodiscover() +autocomplete_light.autodiscover() urlpatterns = patterns('', @@ -16,7 +17,7 @@ urlpatterns = patterns('', # Uncomment the next line to enable the admin: url(r'^admin/', include(admin.site.urls)), - + url(r'autocomplete/', include('autocomplete_light.urls')), url(r'^$', 'project.pages.views.site_index', name='site_index'), url(r'^my/', include('project.customer.urls')), diff --git a/requirements-dev.txt b/requirements-dev.txt index c2938f7..67fff20 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -13,9 +13,10 @@ django-filter==0.7 pytils==0.2.3 psycopg2 flup +django-autocomplete-light==2.0.0pre # dev django-devserver django-eml-email-backend django-debug-toolbar -ipython +ipython \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index a2f5cac..5ebd26d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -13,9 +13,10 @@ django-filter==0.7 pytils==0.2.3 psycopg2 flup +django-autocomplete-light==2.0.0pre # dev #django-devserver #django-eml-email-backend django-debug-toolbar -ipython +ipython \ No newline at end of file