def role_revoke(id, role): role = Role.find(role) user = User.find(id).roles old_roles = [(lambda y: y.name)(x) for x in user] if role.name in old_roles: db.table('roles_users').where('user_id', id).where('role_id', role).delete() user_old = User.find(id) updated_user = User.find(id).roles updated_roles = [(lambda y: y.name)(x) for x in updated_user] encoded = {'email': user_old.email, 'name': user_old.name, 'weight': user_old.weight, 'role': updated_roles} data = jwt.encode(encoded, key=key, algorithm='HS256') user_old.token = data; save = user_old.save() if save: return jsonify({ 'message': 'Access controls have been updated' }), 202 else: return jsonify({ 'message': 'Some Tragedy has happen try again later' }), 404
def myvids(): if request.method == 'POST': db.table('videos').insert(request.get_json()) return jsonify({'message': 'upload success'}), 200 elif request.method == 'GET': videos = db.table('videos').get() return jsonify({'videos': videos.serialize()}), 200
def get_user_summary(id): user = db.table('users')\ .where('users.id',id)\ .select('users.id as Id','users.email as email','users.name as name','users.weight as Weight')\ .first().serialize() summary = db.table('user_orders')\ .where('user_id',id).get().map(lambda x:{ 'nextDate':x['nextDate'], 'purchased':x['purchased'], 'created_at':x['created_at'], 'id':x['id'], 'summary':[{ 'blood_test':y['blood_test'], 'dignosis':y['dignosis'], 'treatment':y['treatment'], 'disease':y['disease'] } for y in json.loads(x['summary'])] }) attachment = db.table('user_attachments')\ .where('user_id',id)\ .select('id','attachment')\ .get().serialize() user['reports'] = [{ 'attachment': b['attachment'], 'history': a } for a, b in zip(summary, attachment)] return jsonify({'summary': user})
def myvidsdel(id): if request.method == 'POST': db.table('videos').where('id', id).delete() return jsonify({'message': 'removed successully'}), 200 elif request.method == 'PUT': videos = db.table('videos').where('id', id).update(request.get_json()) return jsonify({'message': 'updated'}), 200
def about_update(): db.table('about').where('id', 1).update({ 'text':request.get_json()['about'] }) return jsonify({ "message":"ok" })
def update_Unit(id): if request.is_json: db.table('units').where('id',id).update(request.get_json()) return jsonify({ 'message':'updated' }),202 else: return jsonify({ 'message':'bad request' }),400
def create_Unit(): if request.is_json: db.table('units').insert(request.get_json()) return jsonify({ 'message':'created' }),201 else: return jsonify({ 'message':'bad request' }),400
def save_pop(id): if request.method == 'POST': it = db.table('popup').where('pageId', id).update(request.get_json()) if it: return jsonify({'message': 'Popup updated'}), 200 else: return jsonify({'message': 'no changed were done'}), 200 elif request.method == 'GET': pop = db.table('popup').where('pageId', id).first() return jsonify({'popup': pop}) else: return jsonify({'message': 'Operation not supported'})
def make_indicator_equation(): if request.method == 'GET': indicators = db.table('indicators').get() return jsonify({'indicators': indicators.serialize()}) elif request.method == 'POST': payload = request.get_json() indicatorId = request.args.get('flag') if db.table('indicators').where('id', indicatorId).update( request.get_json()): return jsonify({'message': 'modified'}) else: return jsonify( {'message': 'updation failed may be wrong indicator'})
def check_for_retest(id): nextTime = db.table('user_orders').latest().first() today = datetime.datetime.now() ntt = nextTime['nextDate'] delta = today - ntt print(delta.days) return jsonify({'time': today})
def edit_indicator(id): category = db.table('categories').where('id',id).update(request.get_json()['category']) indicator = request.get_json()['indicators'] ilist= ["CALL edit_indicator('{id}','{name}','{variable}','{description}')".format(**x) for x in indicator] for x in ilist: db.select(x) return jsonify({'message':'updated'}),200
def get_plot_data(id): summary = db.table('user_orders')\ .where('user_id',id)\ .order_by('id','desc')\ .limit(1)\ .get()\ .pluck('summary')\ .map(lambda u:json.loads(u))\ .collapse()\ .pluck('blood_test')\ .collapse()\ .unique('name') weight = summary.filter(lambda z: z['name'] == 'weight') bloods = summary.filter(lambda z: not z['name'] == 'weight') labels = bloods.pluck('name') values = bloods.pluck('number') return jsonify({ 'plot': { 'yAxis': weight.serialize(), 'xAxis': { 'labels': labels.serialize(), 'values': values.serialize() } } })
def upload_pic_user(id): user = db.table('users').where('id', id).first() if request.method == 'POST': if user is not None: db.table('users').where('id', id).update( {'profile_pic': request.get_json()['image']}) return jsonify({'message': 'success'}) elif request.method == 'GET': print user if user is not None: return send_from_directory(current_app.config['UPLOAD_FOLDER'], user.profile_pic) else: return jsonify({'message': 'No Data Available'})
def uptae_other_param(id): if request.is_json: bar = db.table('other_health').where('id', id).update(request.get_json()) return jsonify({'message': 'updated'}), 200 else: return jsonify({'message': 'Invalid Syntax'}), 400
def make_indicator_test(): test = db.table('indicators').get().transform( lambda x: identify_colors(x['equation'],x['variable'],205) )\ .all() return jsonify({'results': test})
def get_default_units(id): unit = db.table('user_defaults_units').where('user_id', id).first() if unit is not None: return jsonify({'defaults': unit}), 200 else: return jsonify({'message': 'units not set for you'})
def create_public_param(): if request.is_json: d = db.table('public_health').where('id', id).insert(request.get_json()) return jsonify(d) else: return jsonify({'message': 'Invalid Syntax'}), 400
def del_diagnostixi(id): if request.is_json: d = db.table('dignoses').where('id',id).delete() return jsonify({ 'message': "ok" }), 204 else: return jsonify({ 'message':'Invalid Syantax' }), 200
def create_diagnostix(): if request.is_json: d = db.table('dignoses').insert(request.get_json()) return jsonify({ 'message':'created successfully' }), 201 else: return jsonify({ 'message':'Invalid Syantax' }), 200
def all_question(): if request.is_json: data = db.table('questions')\ .select('questions.question as question','questions.answer as answer','questions.status as status','users.name as name', 'questions.id as id')\ .join('users','questions.user_id','=','users.id')\ .get() return jsonify(data) else: return jsonify({}), 404
def activate_user(email): user = db.table('users').where('email', email).first() if user is not None: verfication = db.table('verifications').where('user_id', user.id).first() if verfication is not None: if verfication.code == request.get_json()['code']: db.select('START TRANSACTION') db.select( 'update users set active= "1" where users.id= {}'.format( user.id)) db.select('COMMIT') return jsonify({'token': user.token}) else: return jsonify({'message': 'wrong verification code'}) else: return jsonify({'message': 'Something wrong happens'}) else: return jsonify({'message': 'No user exists'})
def login_handler(args): email = args['email'] passw = b"{}".format(args['password']) user = db.table('users').where('email', email).first() or db.table("users").where( 'username', email).first() print email if user is None: return jsonify({'message': 'Username or EmailId not Exists'}), 404 else: if user.active == "0": return jsonify({'isActive': user.active}) elif bcrypt.checkpw(passw, str(user.password)): return jsonify({'email': user.email, 'token': str(user.token)}) else: print(passw) print(bcrypt.hashpw(passw, bcrypt.gensalt(15))) print(user.password) return jsonify({"message": "Invalid Credentials"})
def edit_diagnostix(id): if request.is_json: d = db.table('dignoses').where('id',id).update(request.get_json()) return jsonify({ 'message': "ok" }), 203 else: return jsonify({ 'message':'Invalid Syantax' }), 200
def pay_for_treatments(id, status): userid = id, pay = status summary = request.get_json()['summary'] sdata = json.dumps(summary) catId = summary[0]['disease']['category_id'] tdump = json.dumps(summary[0]['treatment']) category = db.table('categories').where('id', catId).first() nextDate = datetime.datetime.now() + datetime.timedelta( days=category.nextTime) print category.nextTime print nextDate x = db.table('user_orders').insert({ 'user_id': userid, 'purchased': pay, 'summary': sdata, 'nextDate': nextDate }) return jsonify({'message': 'success'}), 200
def all(): if request.method == 'GET': d = db.table('dignoses')\ .join('diseases','dignoses.disease_id','=','diseases.id')\ .select('dignoses.id as id','dignoses.name as name','dignoses.pricetag as pricetag','dignoses.summary as summary','dignoses.image as image','diseases.name as disease','dignoses.created_at as created_at')\ .get() return jsonify({ 'diagnosis': d.serialize() }), 200 else: return jsonify({ 'message':'Invalid Syantax' }), 200
def indivisual_history(id): x= db.table('injuries').where('user_id',id)\ .join('users','injuries.user_id','=','users.id')\ .join('treatments','injuries.treatment_id','=','treatments.id')\ .join('diseases','injuries.disease_id','=','diseases.id')\ .select('users.name as name', 'treatments.price as price', 'diseases.name as disease', 'treatments.description as prescription', 'injuries.status as status', 'injuries.tsummary as summary')\ .get().serialize() return jsonify(x), 200
def make_single_user_report(id, disease): user = db.table('users').where('id', id).first() if user is not None: reports = db.table('user_diseases').where('user_id', id).where( 'disease_id', disease).get() if reports is not None: report = { 'user':user.name, 'reports':reports.map(lambda y: { 'date':y['created_at'], 'report':{ 'labels':Collection(json.loads(y['bloodtest'])).pluck('name').all(), 'values':Collection(json.loads(y['bloodtest'])).pluck('number').all(), 'colors':Collection(json.loads(y['bloodtest']))\ .transform(lambda x:identify_colors(x['equation'],x['variable'],x["number"])).all() } }).all() } return jsonify({'reports': report}) else: return jsonify({'message': 'No Reports Available for User'}) else: return jsonify({'message': 'user doesnt exists'})
def make_reports_for_user(id): user = db.table('users')\ .where('users.id',id)\ .select('users.id as Id','users.email as email','users.name as name','users.weight as Weight','users.mobile as contact')\ .first().serialize() summary = db.table('user_orders')\ .where('user_id',id).get().map(lambda x:{ 'nextDate':x['nextDate'], 'purchased':x['purchased'], 'created_at':x['created_at'], 'id':x['id'], 'summary':[{ 'blood_test':y['blood_test'], 'dignosis':y['dignosis'], 'treatment':y['treatment'], 'disease':y['disease'] } for y in json.loads(x['summary'])] }) attachment = db.table('user_attachments')\ .where('user_id',id)\ .select('id','attachment')\ .get().serialize() report = [{ 'attachment': b['attachment'], 'history': a } for a, b in zip(summary, attachment)] html = render_template('report.html', user=user, report=report, js0n=json, enum=zip) return {'html': html}
def update_password(email): user = db.table('users').where('email', email).first() old = request.get_json()['old'] new = request.get_json()['new'] if user is not None: if bcrypt.checkpw(b"{}".format(old), str(user.password)): check = makepassword(new, user.id) if check == True: return jsonify({'message': 'password changed succesfully'}) else: return jsonify({'message': 'Some Error Occured'}) else: return jsonify({'message': ' Old password not matchs'}) else: return jsonify({'Wrong email Id'})
def forgotten_password(email): user = db.table('users').where('email', email).first() if user is not None: new = id_generator() changed = makepassword(new, user.id) if changed: message = reset_template.format(new) msg = Message(subject="password reset info", html=message, sender="*****@*****.**", recipients=[user.email]) mail.send(msg) return jsonify({ 'message': 'your password has been reset successfully ...check your email' }) else: return jsonify({'message': 'wrong user name'})