def update_client(request): status = 'Error' address = '' phone = '' if request.method == 'POST': json_client = json.loads(request.body.decode('utf-8')) print json_client username = json_client.get('username') firstname = json_client.get('firstname') lastname = json_client.get('lastname') email = json_client.get('email') address = json_client.get('address') phone = json_client.get('phone') try: userModel = User.objects.get(username=username) userModel.first_name = firstname userModel.last_name = lastname userModel.email = email userModel.save() try: client = Client.objects.get(user__username=username) client.address = address client.phone = phone client.active = 1 client.save() except ObjectDoesNotExist: if(address <> "" or phone <> ""): client = Client() client.user = userModel client.address = address client.phone = phone client.active = 1 client.save() else: print 'No existe info adicional de cliente.' status = 'OK' except ObjectDoesNotExist: status = 'Usuario no existe.' else: status = 'Metodo no POST.' return JsonResponse({'status': status})
def addClient(request): if not request.user.is_authenticated(): return HttpResponseRedirect("/") if request.method == 'POST': # If the form has been submitted... form = ClientForm(request.POST) # A form bound to the POST data if form.is_valid(): # All validation rules pass # Process the data in form.cleaned_data firstName = form.cleaned_data['firstName'] middleName = form.cleaned_data['middleName'] lastName = form.cleaned_data['lastName'] dateOfBirth = form.cleaned_data['dateOfBirth'] street = form.cleaned_data['street'] city = form.cleaned_data['city'] stateID = form.cleaned_data['state'] zipCode = form.cleaned_data['zipCode'] phone = form.cleaned_data['phone'] email = form.cleaned_data['email'] clientAddress = Address() clientAddress.street = street clientAddress.city = city theState = State.objects.filter(id=stateID) if len(theState)==1: theState = theState[0] clientAddress.state = theState clientAddress.zipCode = zipCode clientAddress.save() aClient = Client() aClient.firstName = firstName aClient.middleName = middleName aClient.lastName = lastName aClient.dateOfBirth = dateOfBirth aClient.address = clientAddress aClient.phone = phone aClient.email = email aClient.save() return HttpResponseRedirect('/clients/') # Redirect after POST else: form = ClientForm() # An unbound form context = { 'form': form, } return render(request, 'addClient.html', context)