Ubuntu12.04でPython2.7を使用してDjango1.4を使用しています。
フォームの投稿から情報を取得するビューがあります。最初に行うことは、フォームに指定された情報に基づいてクライアント情報を見つけようとすることです。
テスト中、私は偽の情報を入力していますが、それで問題ないようです。例外がスローされることを期待しているのでDoesNotExist
、元のビューを呼び出して、ユーザーにフォームを再度表示します。できませんか?
2つのビューは次のとおりです。
def input_new_client(request):
"""
.. function:: input_new_client()
Add the client based on the addClientInfo form
:param request: Django Request object
"""
## Create a logging object
path = os.path.join(os.path.dirname(__file__), 'logs/')
filename = '{0}inputNewClient.log'.format(path)
logfile = open(filename, 'w')
now = datetime.datetime.now()
logfile.write('\n --------------------- {0}\n'.format(now))
if (request.method == "POST"):
tkz_ys_api_id = request.POST.get("tkz_ys_api_id")
tkz_ys_trans_key = request.POST.get("tkz_ys_trans_key")
## Validate the YS authentication information
try:
clientInfo = ClientInfo.objects.get(tkz_api_id = tkz_ys_api_id, tkz_trans_key = tkz_ys_trans_key)
except ClientInfo.DoesNotExist or ClientInfo.MultipleObjectsReturned:
logfile.write('{0}\n\n'.format(tkz_ys_api_id))
logfile.write('{0}\n\n'.format(tkz_ys_trans_key))
logfile.write('{0}\n\n'.format(traceback.format_exc()))
logfile.close()
state = "Invalid YS Authentication! Please try again."
add_new_client(request, state)
else:
if (clientInfo.name != "Your Solutions"):
state = "Invalid YS Authentication! Please try again."
add_new_client(request, state)
## Generate a Tokeniz API ID and Trans Key for the new client
(tkz_api_id, tkz_trans_key) = generate_credentials()
## Create a new client
new_client = ClientInfo(name = request.POST.get("tkz_client_name"),
tkz_api_id = tkz_api_id,
tkz_trans_key = tkz_trans_key,
default_gateway_id = request.POST.get("tkz_gateway")
)
new_client.save()
## Validate the YS authentication information
try:
clientInfo = ClientInfo.objects.get(tkz_api_id = tkz_api_id, tkz_trans_key = tkz_trans_key)
foriegn_key = clientInfo.id
except ClientInfo.DoesNotExist or ClientInfo.MultipleObjectsReturned:
output = "Invalid YS Authentication! Please try again."
logfile.write('\n{0}\n'.format(output))
logfile.write('{0}\n\n'.format(traceback.format_exc()))
logfile.close()
## Setup the new clients gateway information
new_gateway_info = GatewayInfo(client = foriegn_key,
api_id = request.POST.get("tkz_gateway_api_id"),
trans_key = request.POST.get("tkz_gateway_trans_key"),
gateway_id = request.POST.get("tkz_gateway"),
)
new_gateway_info.save()
data = {}
data.update(csrf(request))
data.update({ 'tkz_api_id' : tkz_api_id })
data.update({ 'tkz_trans_key' : tkz_trans_key })
data.update({ 'client_name' : request.POST.get("tkz_client_name")})
return render_to_response("updatedClientCredentials.html", data)
そして、フォームを提示する元のビュー:
def add_new_client(request, state = None):
"""
.. function:: add_new_client()
Provide a form for entering new client information
:param request: Django Request object
:param state: A message representing the state of the addition of a client
"""
## Create a logging object
path = os.path.join(os.path.dirname(__file__), 'logs/')
filename = '{0}addNewClient.log'.format(path)
logfile = open(filename, 'a')
now = datetime.datetime.now()
logfile.write('\n --------------------- {0}\n'.format(now))
if (state is None):
state = "Enter information for adding a new client to Tokeniz."
try:
form = AddClientInfo()
except:
output = "Handle Error: Cannot create a valid form"
logfile.write('{0}\n'.format(output))
logfile.write('{0}\n\n'.format(traceback.format_exc()))
logfile.close()
return HttpResponse(output)
try:
data = {}
data.update(csrf(request))
data.update({ 'form' : form })
data.update({ 'state' : state })
except:
output = "Handle Error: Cannot generate CSRF token"
logfile.write('{0}\n'.format(output))
logfile.write('{0}\n\n'.format(traceback.format_exc()))
logfile.close()
return HttpResponse(output)
logfile.close()
return render_to_response("addNewClientInfo.html", data)
clientInfo = ClientInfo.objects.get(tkz_api_id = tkz_ys_api_id, tkz_trans_key = tkz_ys_trans_key)
のリクエストで例外input_new_client
がスローされることを期待していDoesNotExist
ます(これを確実にするために偽のデータを入力したため)。
add_new_client
だから、私は例外の中からビューを呼び出すつもりでした。
問題は、input_new_client
がnew_client.save()
ラインに到達することですが、これは明らかな理由で失敗します。
何かご意見は?
編集1:
input_new_client
次の例外ステートメントを持つようにビューを変更しました。
if (request.method == "POST"):
tkz_ys_api_id = request.POST.get("tkz_ys_api_id")
tkz_ys_trans_key = request.POST.get("tkz_ys_trans_key")
tkz_gateway_id = int(request.POST.get("tkz_gateway"))
## Validate the YS authentication information
try:
clientInfo = ClientInfo.objects.get(tkz_api_id = tkz_ys_api_id, tkz_trans_key = tkz_ys_trans_key)
except (ClientInfo.DoesNotExist, ClientInfo.MultipleObjectsReturned):
logfile.write('tkz_ys_api_id = {0}\n\n'.format(tkz_ys_api_id))
logfile.write('tkz_ys_trans_key = {0}\n\n'.format(tkz_ys_trans_key))
logfile.write('tkz_gateway_id = {0}\n\n'.format(tkz_gateway_id))
logfile.write('{0}\n\n'.format(traceback.format_exc()))
logfile.close()
state = "Invalid YS Authentication! Please try again."
add_new_client(request, state)
else:
if (clientInfo.name != "Your Solutions"):
state = "Invalid YS Authentication! Please try again."
add_new_client(request, state)
私はそれが例外に入るのを知っています-私はログ情報とトレースバックで確認することができます。ただし、には進みませんadd_new_client
。ClientInfo
代わりに、先にスキップして、誤ったデータを含む新しいエントリをモデルに挿入しようとします。
なぜへの呼び出しをスキップするのadd_new_client
ですか?