def bill_cash_add(request): lang,user,fio,method = get_info(request) if request.method == 'POST': # Порядок действия таков: # 1) Создаём Cash # 2) Создаём Payment с этим Cash # 3) Создаём Garanty - если её ещё нет. Если есть - добавляем к имеющейся # 4) Итерируем по элементам в форме от 1 до макс добавляя активы в список активов # 5) Если всё прошло хорошо - активы из списка сохраняем bill_date = request.POST.get('date','') if bill_date: a=[int(a) for a in bill_date.split('.')] a.reverse() bill_date=datetime.datetime(*a) else: bill_date = datetime.datetime.now() # print bill_date # raise ImportError cash = Cash(date = bill_date, contractor = Contractor.objects.get(id=request.POST.get('contractor_id')), bill_number = request.POST.get('bill_number'), ) cash.save() payment = Payment(cash = cash, ) payment.save() try: garanty = Garanty.objects.get(number = request.POST.get('garanty')) except Garanty.DoesNotExist: garanty = Garanty(number = request.POST.get('garanty')) garanty.save() for item_number in range(1,int(request.POST.get('max_asset_form_number'))+1): sitem_number = str(item_number) if sitem_number+'_model' in request.POST: for count in range(0,int(request.POST.get('count_of_asset'+sitem_number))): a=Asset(asset_type = Asset_type.objects.get(id=request.POST.get(sitem_number+'_asset_type')), payment = payment, garanty = garanty, model = request.POST.get(sitem_number+'_model'), status = Status.objects.get(id=request.POST.get(sitem_number+'_status')), guarantee_period = request.POST.get(sitem_number+'_guarantee_period'), note = request.POST.get(sitem_number+'_note'), price = request.POST.get(sitem_number+'_price'), ) a.save() cur_place=Place_Asset(installation_date = bill_date, asset = a, place = Place.objects.get(id=request.POST.get(sitem_number+'_current_place')), ) cur_place.save() return (False,HttpResponseRedirect('/all_bills/')) # Создаём новый счёт, значит теперь надо номер новой гарантии try: garanty_number = int(Garanty.objects.all().order_by('-number')[0].number)+1 except IndexError: # значит, гарантий ещё в базе нет garanty_number = 0 contractors_list = assets.api.get_contractors_list(request,internal=True) asset_types_list = assets.api.get_asset_type_list(request,internal=True) return (True,('new_bill.html', {'NewCashBillForm':{'garanty':garanty_number}},{'contractors_list':contractors_list,'asset_types_list':asset_types_list, 'method':method},request,app))
def bill_add(request): lang,user,fio,method = get_info(request) if request.method == 'POST': # Порядок действия таков: # 1) Создаём Cash # 2) Создаём Payment с этим Cash # 3) Создаём Garanty - если её ещё нет. Если есть - добавляем к имеющейся # 4) Итерируем по элементам в форме от 1 до макс добавляя активы в список активов # 5) Если всё прошло хорошо - активы из списка сохраняем bill_date = request.POST.get('date','') if bill_date: a=[int(a) for a in bill_date.split('.')] a.reverse() bill_date=datetime.datetime(*a) else: bill_date = datetime.datetime.now() # print bill_date # raise ImportError cash = Cash(date = bill_date, contractor = Contractor.objects.get(id=request.POST.get('contractor_id')), bill_number = request.POST.get('bill_number'), ) cash.save() payment = Payment(cash = cash, ) payment.save() try: garanty = Garanty.objects.get(number = request.POST.get('garanty')) except Garanty.DoesNotExist: garanty = Garanty(number = request.POST.get('garanty')) garanty.save() for item_number in range(1,int(request.POST.get('max_asset_form_number'))+1): sitem_number = str(item_number) if sitem_number+'_model' in request.POST: for count in range(0,int(request.POST.get('count_of_asset'+sitem_number))): a=Asset(asset_type = Asset_type.objects.get(id=request.POST.get(sitem_number+'_asset_type')), payment = payment, garanty = garanty, model = request.POST.get(sitem_number+'_model'), status = Status.objects.get(id=request.POST.get(sitem_number+'_status')), guarantee_period = request.POST.get(sitem_number+'_guarantee_period'), note = request.POST.get(sitem_number+'_note'), price = request.POST.get(sitem_number+'_price'), ) a.save() cur_place=Place_Asset(installation_date = bill_date, asset = a, place = Place.objects.get(id=request.POST.get(sitem_number+'_current_place')), ) cur_place.save() return (False,HttpResponseRedirect('/tasks/')) # else: # print "FOrm is not valid??" contractors_list = assets.api.get_contractors_list(request,internal=True) asset_types_list = assets.api.get_asset_type_list(request,internal=True) # Translators: This message appears on the home page only message=_('Message') print message print request.LANGUAGE_CODE return (True,('new_bill.html', {'NewCashBillForm':{}},{'contractors_list':contractors_list,'asset_types_list':asset_types_list, 'method':method,'message':message},request,app))