def handle_shipper(form): suppinn = form.cleaned_data['suppinn'].strip() shipper = Org.objects.filter(inn=suppinn).first() if not (shipper): # not found > create shipper = Org( inn = suppinn, name = form.cleaned_data['suppname'].strip(), fullname = form.cleaned_data['suppfull'].strip() ) shipper.save() return shipper
def scan_edit(request, id): ''' ''' scan = models.Scan.objects.get(pk=int(id)) if request.method == 'POST': form = forms.ScanEditForm(request.POST) if form.is_valid(): suppinn = form.cleaned_data['suppinn'].strip() shipper = Org.objects.filter(inn=suppinn).first() if not (shipper): # not found > create shipper = Org( inn = suppinn, name = form.cleaned_data['suppname'].strip(), fullname = form.cleaned_data['suppfull'].strip() ) shipper.save() scan.place = form.cleaned_data['place'].strip() scan.subject = form.cleaned_data['subject'].strip() scan.depart = form.cleaned_data['depart'].strip() scan.payer = form.cleaned_data['payer'].strip() scan.shipper = shipper scan.supplier = shipper.name scan.no = form.cleaned_data['no'].strip() scan.date = form.cleaned_data['date'] scan.sum = form.cleaned_data['sum'] scan.save() return redirect('scan_view', scan.pk) else: form = forms.ScanEditForm(initial={ 'place': scan.place, 'subject': scan.subject, 'depart': scan.depart, 'payer': scan.payer, 'suppinn': scan.shipper.inn if scan.shipper else '', 'suppname': scan.shipper.name if scan.shipper else scan.supplier, 'suppfull': scan.shipper.fullname if scan.shipper else '', 'no': scan.no, 'date': scan.date, 'sum': scan.sum, }) return render_to_response('scan/form.html', context_instance=RequestContext(request, { 'form': form, 'object': scan, }))
def scan_edit(request, pk): scan = models.Scan.objects.get(pk=int(pk)) if request.method == 'POST': form = forms.ScanEditForm(request.POST) if form.is_valid(): suppinn = form.cleaned_data['suppinn'].strip() shipper = Org.objects.filter(inn=suppinn).first() if not shipper: # not found > create shipper = Org(inn=suppinn, name=form.cleaned_data['suppname'].strip(), fullname=form.cleaned_data['suppfull'].strip()) shipper.save() scan.place = form.cleaned_data['place'].strip() scan.subject = form.cleaned_data['subject'].strip() scan.depart = form.cleaned_data['depart'].strip() scan.payer = form.cleaned_data['payer'].strip() scan.shipper = shipper scan.supplier = shipper.name scan.no = form.cleaned_data['no'].strip() scan.date = form.cleaned_data['date'] scan.sum = form.cleaned_data['sum'] scan.save() return redirect('scan_view', scan.pk) else: form = forms.ScanEditForm( initial={ 'place': scan.place, 'subject': scan.subject, 'depart': scan.depart, 'payer': scan.payer, 'suppinn': scan.shipper.inn if scan.shipper else '', 'suppname': scan.shipper.name if scan.shipper else scan.supplier, 'suppfull': scan.shipper.fullname if scan.shipper else '', 'no': scan.no, 'date': scan.date, 'sum': scan.sum, }) return render(request, 'invarch/form.html', { 'form': form, 'object': scan, })