def otp_send(): print "inside sendotp" if not request.json.get('mobile'): abort(400) else: user_id = request.json.get('mobile') otp = pyotp.TOTP('base32secret3232').now() try: act_rec = Account.query.filter_by(user_id=user_id).first() if act_rec: act_rec.otp = otp act_rec.last_updated_on = ctime() session_commit() res = jsonify({'result': 'modified'}) else: act = Account(user_id, otp) account_added = add_row(act) if account_added: res = jsonify({'result': 'created'}) else: return make_response(jsonify({'result': 'failed'}), 501) email_otp(user_id, otp) return make_response(res, 200) except Exception, e: logging.error(str(e)) abort(400)
def otp_validate(): print request.json if request.json.get('mobile') is None or request.json.get('otp') is None: abort(400) else: user_id = request.json.get('mobile') otp = request.json.get('otp') try: act_rec = Account.query.filter_by(user_id=user_id).first() if act_rec and act_rec.otp == otp: time_difference = datetime.strptime( ctime(), "%a %b %d %H:%M:%S %Y") - act_rec.last_updated_on if time_difference.seconds > 600: return make_response(jsonify({'result': 'otp expired'}), 502) access_token = pyotp.random_base32() act_rec.access_token = access_token act_rec.last_updated_on = ctime() session_commit() #map_products(user_id, request.json.get('products')) products = Product.query.all() res = { 'products': [product.as_dict() for product in products], 'access_token': access_token } return make_response(jsonify(res), 200) else: return make_response(jsonify({'result': 'invalid otp'}), 501) except Exception, e: logging.error(str(e)) abort(404)
def set_calendar(product_id): try: user_id = request.authorization.get('username') calendar_json = request.json.get('calendar') act_rec = AccountProduct.query.filter_by( user_id=user_id, product_id=product_id).first() if act_rec and act_rec.role == 'PROVIDER': cal_rec = AccountProductCalendar.query.filter_by( user_id=user_id, product_id=product_id).first() if not cal_rec: calendar = AccountProductCalendar(user_id, product_id, calendar_json, None) add_row(calendar) return make_response( jsonify({'result': 'success. Calendar added'}), 200) else: cal_rec.calendar = calendar_json session_commit() return make_response( jsonify({'result': 'success. Calendar modified'}), 200) else: return make_response( jsonify({'result': 'failed. User is not a PROVIDER'}), 501) except Exception, e: logging.error(str(e)) abort(400)
def otp_validate(): print request.json if request.json.get('mobile') is None or request.json.get('otp') is None : abort(400) else: user_id = request.json.get('mobile') otp = request.json.get('otp') try: act_rec = Account.query.filter_by(user_id=user_id).first() if act_rec and act_rec.otp == otp: time_difference = datetime.strptime(ctime(), "%a %b %d %H:%M:%S %Y") - act_rec.last_updated_on if time_difference.seconds > 600: return make_response(jsonify({'result': 'otp expired'}), 502) access_token = pyotp.random_base32() act_rec.access_token = access_token act_rec.last_updated_on = ctime() session_commit() #map_products(user_id, request.json.get('products')) products = Product.query.all() res= {'products' : [product.as_dict() for product in products], 'access_token': access_token} return make_response(jsonify(res), 200) else: return make_response(jsonify({'result': 'invalid otp'}), 501) except Exception, e: logging.error(str(e)) abort(404)
def offline(): print "inside offline" user_id = request.authorization.get('username') act_rec = Account.query.filter_by(user_id=user_id).first() if act_rec: act_rec.online = False act_rec.last_updated_on = ctime() session_commit() return make_response(jsonify({'result': 'success'}), 200) else: abort(400)
def map_products(user_id, products): if products: for product_id, prod in request.json.get('products').iteritems(): role = prod.get("role") act_prod_rec = AccountProduct.query.filter_by(user_id=user_id, product_id=product_id).first() if not act_prod_rec: account_product = AccountProduct(user_id, product_id, role) add_row(account_product) else: act_prod_rec.role = role session_commit()
def offline(): print "inside offline" user_id = request.authorization.get('username') act_rec = Account.query.filter_by(user_id=user_id).first() if act_rec: act_rec.online = False act_rec.last_updated_on = ctime() session_commit() return make_response(jsonify({'result':'success'}), 200) else: abort(400)
def map_products(user_id, products): if products: for product_id, prod in request.json.get('products').iteritems(): role = prod.get("role") act_prod_rec = AccountProduct.query.filter_by( user_id=user_id, product_id=product_id).first() if not act_prod_rec: account_product = AccountProduct(user_id, product_id, role) add_row(account_product) else: act_prod_rec.role = role session_commit()
def socket_connection(): print "inside socket" user_id = request.authorization.get('username') act_rec = Account.query.filter_by(user_id=user_id).first() if act_rec and request.json.get('connection_id'): act_rec.connection_id = request.json.get('connection_id') act_rec.online = True act_rec.last_updated_on = ctime() session_commit() return make_response(jsonify({'result': 'success'}), 200) else: abort(400)
def set_calendar(product_id): try: user_id = request.authorization.get('username') calendar_json = request.json.get('calendar') act_rec = AccountProduct.query.filter_by(user_id=user_id, product_id=product_id).first() if act_rec and act_rec.role == 'PROVIDER': cal_rec = AccountProductCalendar.query.filter_by(user_id=user_id, product_id=product_id).first() if not cal_rec: calendar = AccountProductCalendar(user_id, product_id, calendar_json, None) add_row(calendar) return make_response(jsonify({'result': 'success. Calendar added'}), 200) else: cal_rec.calendar = calendar_json session_commit() return make_response(jsonify({'result': 'success. Calendar modified'}), 200) else: return make_response(jsonify({'result': 'failed. User is not a PROVIDER'}), 501) except Exception, e: logging.error(str(e)) abort(400)
def update_image_url(): print request.json if request.json.get('url') is None or request.json.get('type') is None : abort(400) else: user_id = request.authorization.get('username') img_type = request.json.get('type') url = request.json.get('url') try: act_rec = Account.query.filter_by(user_id=user_id).first() if act_rec: if img_type == 'logo': act_rec.logo_url = url elif img_type == 'profilepic': act_rec.profilepic_url = url act_rec.last_updated_on = ctime() session_commit() return make_response(jsonify({'result': 'success'}), 200) else: return make_response(jsonify({'result': 'Account does not exist'}), 501) except Exception, e: logging.error(str(e)) abort(404)
def update_image_url(): print request.json if request.json.get('url') is None or request.json.get('type') is None: abort(400) else: user_id = request.authorization.get('username') img_type = request.json.get('type') url = request.json.get('url') try: act_rec = Account.query.filter_by(user_id=user_id).first() if act_rec: if img_type == 'logo': act_rec.logo_url = url elif img_type == 'profilepic': act_rec.profilepic_url = url act_rec.last_updated_on = ctime() session_commit() return make_response(jsonify({'result': 'success'}), 200) else: return make_response( jsonify({'result': 'Account does not exist'}), 501) except Exception, e: logging.error(str(e)) abort(404)