Why is the below function is not retunring value of date in response? Please check if…
- User
Why is the below function is not retunring value of date in response? Please check if the date needs to be formatted before being able to show in respone. @app.route('/viewGrossMotorForm', methods=['GET','POST']) def viewGrossMotorForm(): session=Session() try: if(request.method == "POST"): if('Authorization' in request.headers): token= request.headers.get('Authorization')
if not token: return jsonify({'MSG':'Token is missing'}) data = jwt.decode(token,app.config['SECRET_KEY'], algorithms=['HS256', ]) if(data): request1= request.get_json() pid = request1.get('pid') queryresult= Common_Function.CommonFun.convertToJson( Constant.constant.constant.viewGrossMotorForm, session.query(Model.models.Application.M_CKGrossmotor.CKGID.label('ID'), Model.models.Application.M_CKGrossmotor.M_AppointmentID.label('Appointment ID'), Model.models.Application.M_CKGrossmotor.grossmotor3yes.label('0-3 Months'), Model.models.Application.M_CKGrossmotor.grossmotor3no.label('grossmotor03no'), Model.models.Application.M_CKGrossmotor.grossmotor6yes.label('3-6 Months'), Model.models.Application.M_CKGrossmotor.grossmotor6no.label('grossmotor36no'), Model.models.Application.M_CKGrossmotor.grossmotor9yes.label('6-9 Months'), Model.models.Application.M_CKGrossmotor.grossmotor9no.label('grossmotor69no'), Model.models.Application.M_CKGrossmotor.grossmotor12yes.label('9-12 Months'), Model.models.Application.M_CKGrossmotor.grossmotor12no.label('grossmotor12no'), Model.models.Application.M_CKGrossmotor.grossmotor18yes.label('12-18 Months'), Model.models.Application.M_CKGrossmotor.grossmotor18no.label('grossmotor1218no'), Model.models.Application.M_CKGrossmotor.grossmotor24yes.label('18-24 Months'), Model.models.Application.M_CKGrossmotor.grossmotor24no.label('grossmotor1824no'), Model.models.Application.M_CKGrossmotor.grossmotor30yes.label('24-30 Months'), Model.models.Application.M_CKGrossmotor.grossmotor30no.label('grossmotor2430no'), Model.models.Application.M_CKGrossmotor.grossmotor36yes.label('30-36 Months'), Model.models.Application.M_CKGrossmotor.grossmotor36no.label('grossmotor3036no'), Model.models.Application.M_CKGrossmotor.grossmotor42yes.label('36-42 Months'), Model.models.Application.M_CKGrossmotor.grossmotor42no.label('grossmotor3642no'), Model.models.Application.M_CKGrossmotor.grossmotor48yes.label('42-48 Months'), Model.models.Application.M_CKGrossmotor.grossmotor48no.label('grossmotor4248no'), Model.models.Application.M_CKGrossmotor.grossmotor54yes.label('48-54 Months'), Model.models.Application.M_CKGrossmotor.grossmotor54no.label('grossmotor4854no'), Model.models.Application.M_CKGrossmotor.grossmotor60yes.label('54-60 Months'), Model.models.Application.M_CKGrossmotor.grossmotor60no.label('grossmotor5460no'), Model.models.Application.M_CKGrossmotor.AddDate.label('Date'), ).filter_by(M_Patient_MPID=pid,IsActive=1,IsDeleted=0 ).order_by(Model.models.Application.M_CKGrossmotor.CKGID.desc()).all()) return jsonify(result=queryresult) else: return jsonify({'err':'Token is expired'}) else: return jsonify({'err':'Please Login'}) except Exception as e: return jsonify({'err':str(e)}) finally: session.close()Below is the function which is used to create entry in the m_ckgrossmotor table: @app.route('/submitGrossMotorForm', methods=['GET','POST']) def submitGrossMotorForm():
session=Session() try: if(request.method == "POST"): if('Authorization' in request.headers): token= request.headers.get('Authorization') if not token: return jsonify({'MSG':'Token is missing'}) data = jwt.decode(token,app.config['SECRET_KEY'], algorithms=['HS256', ]) if(data): request_json = request.get_json() question1 = request_json.get('question1') question2 = request_json.get('question2') question3 = request_json.get('question3') question4 = request_json.get('question4') question5 = request_json.get('question5') question6 = request_json.get('question6') question7 = request_json.get('question7') question8 = request_json.get('question8') question9 = request_json.get('question9') question10 = request_json.get('question10') question11 = request_json.get('question11') question12 = request_json.get('question12') question13 = request_json.get('question13') question14 = request_json.get('question14') question15 = request_json.get('question15') question16 = request_json.get('question16') question17 = request_json.get('question17') question18 = request_json.get('question18') question19 = request_json.get('question19') question20 = request_json.get('question20') question21 = request_json.get('question21') question22 = request_json.get('question22') question23 = request_json.get('question23') question24 = request_json.get('question24') question25 = request_json.get('question25') question26 = request_json.get('question26') question27 = request_json.get('question27') question28 = request_json.get('question28') question29 = request_json.get('question29') question30 = request_json.get('question30') question31 = request_json.get('question31') question32 = request_json.get('question32') question33 = request_json.get('question33') question34 = request_json.get('question34') question4B = request_json.get('question4B') question4C = request_json.get('question4C') grossmotor3yes = request_json.get('grossMotor0To3Yes') grossmotor3no = request_json.get('grossMotor0To3No') grossmotor6yes = request_json.get('grossMotor3To6Yes') grossmotor6no = request_json.get('grossMotor3To6No') grossmotor9yes = request_json.get('grossMotor6To9Yes') grossmotor9no = request_json.get('grossMotor6To9No') grossmotor12yes = request_json.get('grossMotor9To12Yes') grossmotor12no = request_json.get('grossMotor9To12No') grossmotor18yes = request_json.get('grossMotor12To18Yes') grossmotor18no = request_json.get('grossMotor12To18No') grossmotor24yes = request_json.get('grossMotor18To24Yes') grossmotor24no = request_json.get('grossMotor18To24No') grossmotor30yes = request_json.get('grossMotor24To30Yes') grossmotor30no = request_json.get('grossMotor24To30No') grossmotor36yes = request_json.get('grossMotor30To36Yes') grossmotor36no = request_json.get('grossMotor30To36No') grossmotor42yes = request_json.get('grossMotor36To42Yes') grossmotor42no = request_json.get('grossMotor36To42No') grossmotor48yes = request_json.get('grossMotor42To48Yes') grossmotor48no = request_json.get('grossMotor42To48No') grossmotor54yes = request_json.get('grossMotor48To54Yes') grossmotor54no = request_json.get('grossMotor48To54No') grossmotor60yes = request_json.get('grossMotor54To60Yes') grossmotor60no = request_json.get('grossMotor54To60No') Aid = request_json.get('Aid') PID = request_json.get('pid') Id = request_json.get('Id') Insert=Model.models.Application.M_CKGrossmotor() Insert.M_Patient_MPID=PID Insert.M_AppointmentID=Aid Insert.canlifttheheadup=question1 Insert.triestostabilizehead=question2 Insert.lessroundingofback=question3 Insert.canstabiliseheadfully=question4 Insert.Rollsfromfronttoback=question5 Insert.Cansitwithoutsupport=question6 Insert.Bearswholebodyweightonlegs=question7 Insert.Standswellwitharmshigh=question8 Insert.Cruisesfurnitureusinonehand=question9 Insert.Walkswithonehandheld=question10 Insert.Standsononefootwithslight=question11 Insert.Seatsselfinsmallchair=question12 Insert.Throwsballwhilestanding=question13 Insert.Walksdownstairsholdingrail=question14 Insert.Kicksballwithoutdemonstration=question15 Insert.Squatsinplay=question16 Insert.Walkupstairswithrail=question17 Insert.Jumpsinplace=question18 Insert.Standswithbothfeetonbalance=question19 Insert.Balancesononefootfor3seconds=question20 Insert.Goesupstairsnorails=question21 Insert.Pedalstricycle=question22 Insert.Balancesononefoot4to8second=question23 Insert.Hopononefoottwotothreetimes=question24 Insert.Standingbroadjump1to2feet=question25 Insert.Gallops=question26 Insert.Throwsballoverhand10feet=question27 Insert.Catchesbouncedball=question28 Insert.Walksdownstairswithrail=question29 Insert.Balanceononefoot8seconds=question30 Insert.Hopononefoot15times=question31 Insert.Canskip=question32 Insert.Runbroadjumpapproximately2to3feet=question33 Insert.Walksbackwardheeltoe=question34 Insert.Rollsfrombacktofront=question4B Insert.Sittingsupportstarts=question4C Insert.grossmotor3yes=grossmotor3yes Insert.grossmotor3no =grossmotor3no Insert.grossmotor6yes=grossmotor6yes Insert.grossmotor6no=grossmotor6no Insert.grossmotor9yes=grossmotor9yes Insert.grossmotor9no=grossmotor9no Insert.grossmotor12yes=grossmotor12yes Insert.grossmotor12no=grossmotor12no Insert.grossmotor18yes=grossmotor18yes Insert.grossmotor18no=grossmotor18no Insert.grossmotor24yes=grossmotor24yes Insert.grossmotor24no=grossmotor24no Insert.grossmotor30yes=grossmotor30yes Insert.grossmotor30no=grossmotor30no Insert.grossmotor36yes=grossmotor36yes Insert.grossmotor36no=grossmotor36no Insert.grossmotor42yes=grossmotor42yes Insert.grossmotor42no=grossmotor42no Insert.grossmotor48yes=grossmotor48yes Insert.grossmotor48no=grossmotor48no Insert.grossmotor54yes=grossmotor54yes Insert.grossmotor54no=grossmotor54no Insert.grossmotor60yes=grossmotor60yes Insert.grossmotor60no=grossmotor60no Insert.AddDate = datetime.datetime.now() Insert.AddIP= flask.request.remote_addr session.add(Insert) session.commit() return jsonify({'msg':'CK Developmental Added Successfully'}) else: return jsonify({'err':'Token is expired'}) else: return jsonify({'err':'Please Login'}) except Exception as e: return jsonify({'err':str(e)}) finally: session.close() - Assistant
From the code you provided, the possible reasons the date might not be returning properly could include:
-
The date format retrieved from the database might be in a format that is not JSON serializable. DateTime objects need to be converted to a string in a specific format before being sent through JSON.
-
If the date is stored in the database as a string and in a particular format, it should be directly serializable unless the format contains characters that need escaping in JSON.
-
If the
AddDatefield has null values or it's not being populated correctly in the database, then it would also lead to the date not showing up in the response.
It's not clear from the code you've provided exactly how the date is being parsed and sent back in the
jsonifycall. But generally, you can ensure that the date is returned in the response by formatting it to a string usingstrftime(). Here's an example of how you can do that:# ... within your query and response construction Model.models.Application.M_CKGrossmotor.AddDate.label('Date'), # ...Then, before returning the result, iterate over your
queryresultto format the date:for record in queryresult: if 'Date' in record and record['Date']: # If 'Date' is a datetime object, format it to a string record['Date'] = record['Date'].strftime("%Y-%m-%d %H:%M:%S")After doing this, ensure that your dates are being serialized correctly when calling
jsonify(result=queryresult). If dates are still not showing up in your response, you might need to debug the code to see if the date is being retrieved properly from the database and that yourconvertToJsonfunction fromCommon_Function.CommonFunhandles datetime objects appropriately.Remember also to import
datetimein the function where you are formatting, if you haven't already.Lastly, make sure that when you are creating an entry with
submitGrossMotorForm(), theAddDatefield is being populated correctly, for which you are usingdatetime.datetime.now()which is fine as long asdatetimeis imported. -
- User
What would the paylod for the below api would be, please give example. @app.route('/showParticularPatientPackage',methods=['POST']) def showParticularPatientPackage(): try: if(request.method == "POST"): if('Authorization' in request.headers): token= request.headers.get('Authorization')
if not token: return jsonify({'MSG':'Token is missing'}) data = jwt.decode(token,app.config['SECRET_KEY'], algorithms=['HS256', ]) if(data): session=Session() # request_json = request.get_json(force = True) # Id=request_json.get('id') #Id=request.data Id=request.get_json() if(Id != '' and Id != None): queryresult= Common_Function.CommonFun.convertToJson( Constant.constant.constant.getAppointment, session.query(Model.models.Application.M_Appointment.MAID.label('ID'), Model.models.Application.M_Branch.MB_Name.label('Branch'), Model.models.Application.M_Patient.MP_Name.label('Patient'), Model.models.Application.M_Service.MS_CategoryName.label('Procedure'), Model.models.Application.M_Appointment.MA_Date.label('Date') ).filter_by(MP_IsActive=1,MP_IsDeleted=0,M_Patient_MPID=Id ).join(Model.models.Application.M_Branch, Model.models.Application.M_Branch.MBID==Model.models.Application.M_Appointment.M_Branch_MBID ).join(Model.models.Application.M_Patient, Model.models.Application.M_Patient.MPID==Model.models.Application.M_Appointment.M_Patient_MPID ).join(Model.models.Application.M_Service, Model.models.Application.M_Service.MSID==Model.models.Application.M_Appointment.MP_Procedure).all()) session.commit() return jsonify(result=queryresult) else: return jsonify({'err':'something went wrong please try again'}) else: return jsonify({'err':'Token is expired'}) else: return jsonify({'err':'Please Login'}) except Exception as e: return jsonify({'err':str(e)}) finally: session.close() - Assistant
The endpoint
/showParticularPatientPackageis expecting a POST request with a JSON payload containing anid. Since theidvariable is being used to filter a patient's appointment from the database, it is likely thatidrefers to the patient's ID.Based on the structure of the code and the expected JSON format, the payload should look something like this:
{ "id": "1234" }You also need to include the
Authorizationheader with the API request, which should contain the JWT token required for authentication.Here is an example of how you might use
curlfrom the command line to make a request to this API with the payload and authorization token:curl -X POST \ -H "Authorization: Bearer your_token_here" \ -H "Content-Type: application/json" \ -d '{"id":"1234"}' \ http://your_api_domain.com/showParticularPatientPackageMake sure to replace
your_token_herewith the actual JWT token andhttp://your_api_domain.comwith the actual domain where your API is hosted. Also, replace1234with the actual patient ID you want to query for.In your Python code using
requestslibrary, it would look something like this:import requests url = "http://your_api_domain.com/showParticularPatientPackage" headers = { "Authorization": "Bearer your_token_here", "Content-Type": "application/json" } payload = { "id": "1234" } response = requests.post(url, json=payload, headers=headers) print(response.text)Again, you would replace
your_token_here,http://your_api_domain.com, and1234with your specific token, API endpoint, and patient ID respectively. - User
Why is the below API giving "something went wrong error" and does'nt seem to hit. @app.route(‘/showParticularPatientPackage’,methods=[‘POST’]) def showParticularPatientPackage(): try: if(request.method == “POST”): if(‘Authorization’ in request.headers): token= request.headers.get(‘Authorization’)
if not token: return jsonify({‘MSG’:‘Token is missing’}) data = jwt.decode(token,app.config[‘SECRET_KEY’], algorithms=[‘HS256’, ]) if(data): session=Session()
request_json = request.get_json(force = True)
Id=request_json.get(‘id’)
#Id=request.data Id=request.get_json() if(Id != ‘’ and Id != None): queryresult= Common_Function.CommonFun.convertToJson( Constant.constant.constant.getAppointment, session.query(Model.models.Application.M_Appointment.MAID.label(‘ID’), Model.models.Application.M_Branch.MB_Name.label(‘Branch’), Model.models.Application.M_Patient.MP_Name.label(‘Patient’), Model.models.Application.M_Service.MS_CategoryName.label(‘Procedure’), Model.models.Application.M_Appointment.MA_Date.label(‘Date’) ).filter_by(MP_IsActive=1,MP_IsDeleted=0,M_Patient_MPID=Id ).join(Model.models.Application.M_Branch, Model.models.Application.M_Branch.MBID==Model.models.Application.M_Appointment.M_Branch_MBID ).join(Model.models.Application.M_Patient, Model.models.Application.M_Patient.MPID==Model.models.Application.M_Appointment.M_Patient_MPID ).join(Model.models.Application.M_Service, Model.models.Application.M_Service.MSID==Model.models.Application.M_Appointment.MP_Procedure).all()) session.commit() return jsonify(result=queryresult) else: return jsonify({‘err’:‘something went wrong please try again’}) else: return jsonify({‘err’:‘Token is expired’}) else: return jsonify({‘err’:‘Please Login’}) except Exception as e: return jsonify({‘err’:str(e)}) finally: session.close() Payload using: { "id": "1234" }
- Assistant
There are several reasons why the
/showParticularPatientPackageAPI may return a "something went wrong please try again" error message. Let's analyze each part of the request handling and see where it could potentially fail.-
Authorization Token Check: The first thing the function does is check for an Authorization token. Ensure you are passing the correct header with the "Bearer" prefix followed by space and then the token string.
-
Token Decoding: The API tries to decode the provided JWT token using
app.config['SECRET_KEY']. Make sure that the token was encoded with the same secret key that you're using to decode it. If the decoding fails or the token has expired (or not provided), you will receive an error related to that. -
Getting JSON Data: The way the ID is retrieved from the JSON payload could be problematic. The line
Id=request.get_json()may fail if the content type of the request is not set toapplication/json, or the request data is not valid JSON. It is also assuming that the JSON payload is raw and not a dictionary. If it's a dictionary, you should access theidfield asId=request.get_json().get('id'). -
Database Query: There could be an issue with the database query itself, such as:
- A filter condition may not be met (
MP_IsActive,MP_IsDeleted, andM_Patient_MPID). - There may be a problem with the database joins or the database schema doesn't match what's coded.
- A database timeout or disconnection could occur.
- A filter condition may not be met (
-
Error Handling: The error message is generic and arises from any exception caught by the
except Exception as eblock. This could be improved by using more descriptive error messages based on different caught exceptions or adding logging to determine the exact reason for failure.
Given that you are receiving a "something went wrong please try again" error, the exception is likely occurring either when parsing the input
Idor within the database query logic. To further investigate the issue, add logging or print statements within thetryblock to check which part of the code is failing.Let's summarize the troubleshooting checklist:
- Make sure the client sends a properly formatted JSON payload with content type
application/json. - The
idshould be correctly extracted from the payload. Check by replacingId=request.get_json()withId=request.get_json().get('id'). - Log the actual exception message in the
exceptblock to get more information on the error. - The token should be valid and not expired.
- Ensure all database conditions match actual data and the database operations are executed correctly.
- The secret key must match the one used for encoding the JWT token.
Try adding a log/print inside each if-else block to see where the code execution is redirecting and where it might be failing. The lack of more specific error handling makes it harder to identify the exact issue.
-