コード例 #1
0
def record(id):
    auth_header = request.headers.get('Authorization')
    if auth_header:
        auth_token = auth_header.split(" ")[1]
    else:
        responseObject = {
            'status': 'failed',
            'message': 'No authorization header found.'
        }
        return make_response(jsonify(responseObject)), 401

    user_id = User.decode_auth_token(auth_token)
    user = User.get(User.id == user_id)
    from models.record import Record
    if (user_id == id) and user:
        post_data = request.get_json()
        record = Record(weight=post_data['weight'],
                        height=post_data['height'],
                        bmi=post_data['bmi'],
                        user=user.id)
        if record.save():
            responseObject = {
                'status': 'success',
                'message': 'Record successfully saved.'
            }
            return make_response(jsonify(responseObject)), 201
        else:
            responseObject = {
                'status': 'failed',
                'message': 'Something happened,try again later.'
            }
            return make_response(jsonify(responseObject)), 400
    else:
        responseObject = {
            'status': 'failed',
            'message': 'Authentication failed'
        }
        return make_response(jsonify(responseObject)), 401
コード例 #2
0
def create():
    appointment_id = request.form.get("appointment_id")
    a = Appointment.get_or_none(Appointment.id == appointment_id)
    if a.record:
        return jsonify({
            "message": "This appointment already has an existing record.",
            "status": "fail"
        })
    else:
        pass
    
    cholestrol_level = request.form.get("cholestrol_level")
    sugar_level = request.form.get("sugar_level")
    systolic_blood_pressure = request.form.get("systolic_blood_pressure")
    diastolic_blood_pressure = request.form.get("diastolic_blood_pressure")

    online_user = get_jwt_identity()
    user = User.get_or_none(User.id == online_user['id'])

    if (user) and ("patient" in user.role): #need to be updated if allow guardian to create record
        patient_record = Record(cholestrol_level=cholestrol_level, sugar_level=sugar_level, systolic_blood_pressure=systolic_blood_pressure, diastolic_blood_pressure=diastolic_blood_pressure, appointment_id=appointment_id)
        if patient_record.save():
            response = {
                "message": f"Successfully created record.",
                "status": "success",
                "cholestrol level" : patient_record.cholestrol_level,
                "sugar level" : patient_record.sugar_level,
                "systolic_blood_pressure" : patient_record.systolic_blood_pressure,
                "diastolic_blood_pressure" : patient_record.diastolic_blood_pressure,
                "appointment_id": patient_record.appointment_id
            }
        else:
            return jsonify({
                "message": "Add record failed, please try again",
                "status": "fail"
            })
            # Image Upload Start
        images = []
        for i in range(int(request.form.get('image_count'))):
            image = request.files['image_files' + str(i)]
            caption = request.form.get('caption' + str(i))

            if 'image' not in image.mimetype:
                patient_record.delete_instance()
                return jsonify({
                    "message": "One or more of the uploaded files is not an image. Please try again",
                    "status": "fail"
                })
            else:
                file_extension = image.mimetype
                file_extension = file_extension.replace('image/', '.')
                image.filename = str(datetime.now()) + file_extension
                image.filename = secure_filename(image.filename)
                image_url = upload_file_to_s3(image, user.ic_number)
                upload_image = Patient_Photo(record_id=patient_record.id, image_url=image_url, caption=caption)

                if upload_image.save():
                    images.append({
                        "image_url": upload_image.full_image_url,
                        "caption": caption
                    })
                else:
                    patient_record.delete_instance()
                    return jsonify({
                        "message": "Image upload failed, please try again",
                        "status": "fail"
                    })
        response["images"] = images
    else:
        response = {
            "message": "User not found/ Only patient is allowed to create record.",
            "status": "fail"
        }
    return jsonify(response)