def log_data(u_id, d_id): if g.current_user.id != u_id or Device.query.get( d_id).user_id != g.current_user.id: abort(403) data = request.get_json() or {} if 'value' not in data: return bad_request('must include a value') logging = Data(device=Device.query.get(d_id)) logging.date = datetime.utcnow() logging.from_dict(data) db.session.add(logging) db.session.commit() response = jsonify(logging.to_dict()) response.status_code = 201 response.headers['Location'] = url_for('api.get_device', id=u_id) return response
def new_data_auto(): params_dict = { 'data_id': request.json.get('data_id', None), 'sn': request.json.get('sn', None), 'patient_id': request.json.get('patient_id',None), 'glucose': request.json.get('glucose', None), 'id_number': request.json.get('id_number', None), 'time': request.json.get('time', None), 'date': request.json.get('date', None), 'hidden': request.json.get('hidden', None) } try: DataValidation().load(params_dict) except ValidationError as e: return jsonify({ 'status': 'fail', 'reason': str(e) }) if request.json['sn'] not in sn_numbers: data = Data() for k in request.json: if hasattr(data, k): setattr(data, k, request.json[k]) if 'date' not in request.json: data.date = datetime.utcnow().date() if 'time' not in request.json: data.time = str(datetime.datetime.now().time())[0:8] if 'time' in request.json: time = request.json['time'] if len(time) < 8: time = time[0:5] + ':00' data.time = time accuchek = Accuchek.query.filter(Accuchek.sn == request.json['sn']).first() if accuchek is None: return jsonify({ 'status':'fail', 'reason':'血糖仪不存在' }) bed = accuchek.bed if accuchek is not None else None patient = bed.patient if bed is not None else None id_number = patient.id_number if patient is not None else None patient_id = patient.patient_id if patient is not None else None data.id_number = id_number data.patient_id = patient_id try: db.session.add(data) db.session.commit() except IntegrityError as e: raise InvalidUsage(message=str(e), status_code=500) return jsonify({ 'patient':data.patient.to_json_patient(), 'data': data.to_json_without_patient(), 'status': 'success', 'reason': '数据已经被添加了' }) else: data = SpareData() for k in request.json: if hasattr(data, k): setattr(data, k, request.json[k]) if 'date' not in request.json: data.date = datetime.utcnow().date() if 'time' not in request.json: data.time = str(datetime.datetime.now().time())[0:8] if 'time' in request.json: time = request.json['time'] if len(time) < 8: time = time[0:5] + ':00' data.time = time try: db.session.add(data) db.session.commit() except IntegrityError as e: raise InvalidUsage(message=str(e), status_code=500) return jsonify({ 'data': data.to_full_json(), 'status': 'success', 'reason': '数据已经被添加了' })
def new_data_artificial(): params_dict = { 'data_id': request.json.get('data_id', None), 'sn': request.json.get('sn', None), 'glucose': request.json.get('glucose', None), 'id_number': request.json.get('id_number', None), 'time': request.json.get('time', None), 'date': request.json.get('date', None), 'hidden': request.json.get('hidden', None), 'patient_id': request.json.get('patient_id', None), 'patient_name': request.json.get('patient_name', None), 'sex': request.json.get('sex', None), 'tel': request.json.get('tel', None), 'age': request.json.get('age', None), 'doctor_name': request.json.get('doctor_name', None) } try: DataArtificialValidation().load(params_dict) except ValidationError as e: return jsonify({ 'status': 'fail', 'reason': str(e) }) accuchek = Accuchek.query.filter(Accuchek.sn == request.json.get('sn')).first() if accuchek is None: return jsonify({ 'status': 'fail', 'reason': '血糖仪不存在' }) data = Data() for k in request.json: if hasattr(data, k): setattr(data, k, request.json[k]) if 'date' not in request.json: data.date = datetime.utcnow().date() if 'time' not in request.json: data.time = str(datetime.datetime.now().time())[0:8] if 'time' in request.json: time = request.json['time'] if len(time)<8: time = time[0:5]+':00' data.time = time patient_id = request.json['patient_id'] patient = Patient.query.filter(Patient.patient_id == patient_id).first() if patient is None: patient = Patient() for k in request.json: if hasattr(patient, k): setattr(patient, k, request.json[k]) try: db.session.add(patient) db.session.commit() except IntegrityError as e: raise InvalidUsage(message=str(e), status_code=500) try: db.session.add(data) db.session.commit() except IntegrityError as e: raise InvalidUsage(message=str(e), status_code=500) return jsonify({ 'data': data.to_json_without_patient(), 'status': 'success', 'reason': '数据已经被添加了' })