def update_local(request, supplier_id): local_supplier = get_object_or_404(LocalSupplier, pk=supplier_id) initial_data = { 'city': local_supplier.city.name, } if request.method == 'POST': contacts = local_supplier.contact_list.post_dict(request.POST) form = LocalSupplierForm(request.POST, instance=local_supplier) if form.is_valid(): past_supplier = LocalSupplier.objects.get(pk=supplier_id) updated_supplier = form.save() msg = updated_supplier.contact_list.update_contacts(contacts) if msg: messages.warning(request, msg) History.updated_history(past_supplier, updated_supplier, request.user) messages.success(request, 'Local Supplier updated') else: try: contacts = local_supplier.contact_list.get_dict() except AttributeError: local_supplier.save() contacts = {} form = LocalSupplierForm(initial=initial_data, instance=local_supplier) data = { 'contacts': contacts, 'form': form, 'local_supplier': local_supplier, } return render_to_response( 'suppliers/local/update.html', data, context_instance=RequestContext(request), )
def create_local(request): if request.method == 'POST': contacts = ContactList.post_dict(request.POST) form = LocalSupplierForm(request.POST) if form.is_valid(): local_supplier = form.save() msg = local_supplier.contact_list.update_contacts(contacts) if msg: messages.warning(request, msg) History.created_history(local_supplier, request.user) messages.success(request, 'Local Supplier created.') if '_popup' in request.GET: popup_data = { 'obj': escape(local_supplier), 'pk_value': escape(local_supplier.id), } return render_to_response( 'home/close_popup.html', popup_data, context_instance=RequestContext(request), ) return redirect('suppliers:update-local', local_supplier.pk) else: contacts = {} form = LocalSupplierForm() data = { 'contacts': contacts, 'form': form, } return render_to_response( 'suppliers/local/create.html', data, context_instance=RequestContext(request), )