2017-06-17 19 views
1

私は、異なる日付の予定を通過させるだけのPostメソッドのチェックを追加しましたが、エラーmsgを返す方法はわかりません。コードはこちらFlask-resfulでエラーメッセージを返すにはどうすればよいですか?

from flask_restful import Resource, Api, request 
from package.model import conn 


class Appointments(Resource): 

    def get(self): 
     appointment = conn.execute("SELECT p.*,d.*,a.* from appointment a LEFT JOIN patient p ON a.pat_id = p.pat_id LEFT JOIN doctor d ON a.doc_id = d.doc_id ORDER BY appointment_date DESC").fetchall() 
     return appointment 

    def post(self): 
     appointment = request.get_json(force=True) 
     pat_id = appointment['pat_id'] 
     doc_id = appointment['doc_id'] 
     appointment_date = appointment['appointment_date'] 

     a = conn.execute("SELECT count(*) From appointment WHERE doc_id =? 
     AND appointment_date=?",(doc_id,appointment_date,)).fetchone() 
     if a['count(*)'] == 0: 
      appointment['app_id'] = conn.execute('''INSERT INTO appointment(pat_id,doc_id,appointment_date)VALUES(?,?,?)''', (pat_id, doc_id,appointment_date)).lastrowid 
      conn.commit() 
      return appointment 
     else: 
      pass 

pass文の代わりに返すものは何ですか?

PSは:コンテキストの場合、私はそれは、クライアントに特別なHTTPコードとメッセージとHTTPExceptionを上げることができます、https://github.com/tushariscoolster/HospitalManagementSystem

答えて

1

フラスコ-のRestfulはabort機能を提供して改善しようとしています。

だから、あなたは以下のようなコードを変更しようとすることができます:

from flask_restful import abort 

class Appointments(Resource): 
    def post(self): 
     # ignore some code 
     if a['count(*)'] == 0: 
      # ignore some code 
     else: 
      abort(403, error_message='just accept an appointment on special date') 

、その後、クライアントは403、以下のような有効なJSON文字列受け取ります:

{"error_message":"just accept an appointment on special date"} 

最後に、クライアントをエラーメッセージを適切に処理する必要があります。

関連する問題