def analyzer_speed_comparison(): user = session.get('user_info') username = user.get('username') str_today = datetime.now().strftime('%Y-%m-%d') key = 'user_data:%s:%s:history.speed' % (username, str_today) history_speed = dict() b_history_speed = r_session.get(key) if b_history_speed is None: history_speed = __get_history_speed_data(username) r_session.setex(key, json.dumps(history_speed), 3600 * 25) else: history_speed = json.loads(b_history_speed.decode('utf-8')) key = 'user_data:%s:%s' % (username, str_today) b_today_data = r_session.get(key) if b_today_data is None: speed_comparison_data = __get_speed_comparison_data(history_speed, [], '2012-10-04 14:39:00') else: today_data = json.loads(b_today_data.decode('utf-8')) speed_comparison_data = __get_speed_comparison_data(history_speed, today_data.get('speed_stat'), today_data.get('updated_time')) return Response(json.dumps(speed_comparison_data), mimetype='application/json')
def get_id_map(username): id_map_key = 'id_map:%s' % username; b_id_map = r_session.get(id_map_key) if b_id_map is not None: return json.loads(b_id_map.decode('utf-8')) user_key = '%s:%s' % ('user', username) user_info = json.loads(r_session.get(user_key).decode('utf-8')) accounts_key = 'accounts:%s' % username id_map = {} for acct in sorted(r_session.smembers(accounts_key)): account_key = 'account:%s:%s' % (username, acct.decode("utf-8")) account_info = json.loads(r_session.get(account_key).decode("utf-8")) account_data_key = account_key + ':data' account_data_value = r_session.get(account_data_key) if account_data_value is not None: account_info['data'] = json.loads(account_data_value.decode("utf-8")) if user_info.get('is_show_byname') == None: user_info['is_show_byname'] = 1 if user_info.get('is_show_byname') == 0: if account_info.get('remark_name') is None: id_map[account_info.get('user_id')]='未备注' else: id_map[account_info.get('user_id')]=account_info.get('remark_name') elif user_info.get('is_show_byname') == 1: if 'data' not in account_info.keys(): id_map[account_info.get('user_id')]='' elif 'phone' not in account_info['data']['privilege'].keys(): id_map[account_info.get('user_id')]='' id_map[account_info.get('user_id')]=account_info['data']['privilege']['phone'] else: id_map[account_info.get('user_id')]=account_info.get('username') r_session.setex(id_map_key, 60*60, json.dumps(id_map)) return id_map
def accounts_count(): count_key = 'count:accounts' b_count_info = r_session.get(count_key) if b_count_info is not None: return dict(accounts_count=json.loads( r_session.get(count_key).decode('utf-8'))) users = r_session.scard('users') accounts = 0 accountsk = 0 for name in r_session.smembers('users'): accounts_key = 'accounts:%s' % name.decode('utf-8') for acct in r_session.smembers(accounts_key): account_key = 'account:%s:%s' % (name.decode('utf-8'), acct.decode("utf-8")) account_data_key = account_key + ':data' account_data_value = r_session.get(account_data_key) if account_data_value is None: continue account_info = json.loads(account_data_value.decode("utf-8")) for i in account_info.get('device_info'): accountsk += 1 accounts += r_session.scard(accounts_key) accounts_count = dict(users=users, accounts=accounts, accountsk=accountsk) r_session.setex(count_key, json.dumps(accounts_count), 120) return dict(accounts_count=accounts_count)
def get_id_map(username): id_map_key = 'id_map:%s' % username; b_id_map = r_session.get(id_map_key) if b_id_map is not None: return json.loads(b_id_map.decode('utf-8')) user_key = '%s:%s' % ('user', username) user_info = json.loads(r_session.get(user_key).decode('utf-8')) accounts_key = 'accounts:%s' % username id_map = {} for acct in sorted(r_session.smembers(accounts_key)): account_key = 'account:%s:%s' % (username, acct.decode("utf-8")) account_info = json.loads(r_session.get(account_key).decode("utf-8")) account_data_key = account_key + ':data' account_data_value = r_session.get(account_data_key) if account_data_value is not None: account_info['data'] = json.loads(account_data_value.decode("utf-8")) if user_info.get('is_show_byname') == None: user_info['is_show_byname'] = 1 if user_info.get('is_show_byname') == 0: if account_info.get('remark_name') is None: id_map[account_info.get('user_id')]='未备注' else: id_map[account_info.get('user_id')]=account_info.get('remark_name') elif user_info.get('is_show_byname') == 1: if 'data' not in account_info.keys(): id_map[account_info.get('user_id')]='' elif 'phone' not in account_info['data']['privilege'].keys(): id_map[account_info.get('user_id')]='' id_map[account_info.get('user_id')]=account_info['data']['privilege']['phone'] else: id_map[account_info.get('user_id')]=account_info.get('username') r_session.setex(id_map_key, json.dumps(id_map), 60*60) return id_map
def accounts_count(): count_key = 'count:accounts' b_count_info = r_session.get(count_key) if b_count_info is not None: return dict(accounts_count=json.loads(r_session.get(count_key).decode('utf-8'))) users = r_session.scard('users') accounts = 0 accountsk = 0 for name in r_session.smembers('users'): accounts_key = 'accounts:%s' % name.decode('utf-8') for acct in r_session.smembers(accounts_key): account_key = 'account:%s:%s' % ( name.decode('utf-8'), acct.decode("utf-8")) account_data_key = account_key + ':data' account_data_value = r_session.get(account_data_key) if account_data_value is None: continue account_info = json.loads(account_data_value.decode("utf-8")) for i in account_info.get('device_info'): accountsk += 1 accounts += r_session.scard(accounts_key) accounts_count = dict(users=users, accounts=accounts, accountsk=accountsk) r_session.setex(count_key, json.dumps(accounts_count), 120) return dict(accounts_count=accounts_count)
def __handshake(): user = session.get('user_info') username = user.get('username') if username is None or not username: username = '' key = 'user:%s:is_online' % username # SETEX(KEY_NAME TIMEOUT VALUE) r_session.setex(key, '1', 120) r_session.sadd('global:online.users', username)
def send_msg(username, subject, content, expire=3600 * 24 * 31, sender='Admin'): if bytes(username, 'utf-8') not in r_session.smembers('users'): return '找不到该用户。' msgs_key = 'user_messages:%s' % username msg_id = str(uuid.uuid1()) msg = dict(id=msg_id, subject=subject, content=content,sender=sender, is_read=False, time=datetime.now().strftime('%Y-%m-%d %H:%M')) msg_key = 'user_message:%s' % msg_id r_session.setex(msg_key, expire, json.dumps(msg)) r_session.lpush(msgs_key, msg_id) return '发送成功'
def get_mid_to_uid(username): mid_to_uid_key = 'mid_to_uid:%s' % username; b_mid_to_uid = r_session.get(mid_to_uid_key) if b_mid_to_uid is not None: return json.loads(b_mid_to_uid.decode('utf-8')) mid_to_uid={} accounts_key = 'accounts:%s' % username for acct in sorted(r_session.smembers(accounts_key)): account_data_key = 'account:%s:%s:data' % (username, acct.decode("utf-8")) b_account_data = r_session.get(account_data_key) if b_account_data is not None: account_data = json.loads(b_account_data.decode("utf-8")) mid_to_uid[account_data.get('privilege').get('mid')]=acct.decode("utf-8") r_session.setex(mid_to_uid_key, json.dumps(mid_to_uid), 24*60*60) return mid_to_uid
def resend_report(): user = session.get('user_info') resend_key = '%s:%s' % ('last_resend', user.get('username')) if r_session.get(resend_key) is not None: session['error_message'] = '您的请求过于频繁,请稍后再尝试。' return redirect(url_for('excavators')) mail_key = '%s:%s' % ('user_income_mail', user.get('username')) b_mail = r_session.get(mail_key) if b_mail is None: session['error_message'] = '您的收益报告尚未生成,请开启自动报告或等待系统生成。' return redirect(url_for('excavators')) r_session.rpush('mail_queue',b_mail) session['info_message'] = '您的收益报告已经加入邮件发送队列,请等待系统发送' r_session.setex(resend_key,datetime.now().strftime('%Y-%m-%d %H:%M:%S'),3600) return redirect(url_for('excavators'))
def get_mid_to_uid(username): mid_to_uid_key = 'mid_to_uid:%s' % username; b_mid_to_uid = r_session.get(mid_to_uid_key) if b_mid_to_uid is not None: return json.loads(b_mid_to_uid.decode('utf-8')) mid_to_uid={} accounts_key = 'accounts:%s' % username for acct in sorted(r_session.smembers(accounts_key)): account_data_key = 'account:%s:%s:data' % (username, acct.decode("utf-8")) b_account_data = r_session.get(account_data_key) if b_account_data is not None: account_data = json.loads(b_account_data.decode("utf-8")) mid_to_uid[account_data.get('privilege').get('mid')]=acct.decode("utf-8") r_session.setex(mid_to_uid_key, 24*60*60, json.dumps(mid_to_uid)) return mid_to_uid
def analyzer_speed_vs_income(): user = session.get('user_info') username = user.get('username') str_today = datetime.now().strftime('%Y-%m-%d') key = 'user_data:%s:%s:%s' % (username, 'speed_vs_income', str_today) data = dict() b_data = r_session.get(key) if b_data is None: data = __seven_day_pdc(username) r_session.setex(key, json.dumps(data), 3600 * 25) else: data = json.loads(b_data.decode('utf-8')) return Response(json.dumps(data), mimetype='application/json')
def analyzer_speed_vs_income(): user = session.get('user_info') username = user.get('username') str_today = datetime.now().strftime('%Y-%m-%d') key = 'user_data:%s:%s:%s' % (username, 'speed_vs_income', str_today) extra_info_key='extra_info:%s' % (username) b_extra_info=r_session.get(extra_info_key) if b_extra_info is None: extra_info={} else: extra_info=json.loads(b_extra_info.decode('utf-8')) data = dict() b_data = r_session.get(key) if b_data is None: data = __seven_day_pdc(username) if 'last_adjust_date' in extra_info.keys() and datetime.strptime(extra_info['last_adjust_date'],'%Y-%m-%d %H:%M:%S').day == datetime.now().day: r_session.setex(key, 3600 * 25, json.dumps(data)) else: data = json.loads(b_data.decode('utf-8')) return Response(json.dumps(data), mimetype='application/json')
def analyzer_speed_vs_income(): user = session.get('user_info') username = user.get('username') str_today = datetime.now().strftime('%Y-%m-%d') key = 'user_data:%s:%s:%s' % (username, 'speed_vs_income', str_today) extra_info_key = 'extra_info:%s' % (username) b_extra_info = r_session.get(extra_info_key) if b_extra_info is None: extra_info = {} else: extra_info = json.loads(b_extra_info.decode('utf-8')) data = dict() b_data = r_session.get(key) if b_data is None: data = __seven_day_pdc(username) if 'last_adjust_date' in extra_info.keys() and datetime.strptime( extra_info['last_adjust_date'], '%Y-%m-%d %H:%M:%S').day == datetime.now().day: r_session.setex(key, json.dumps(data), 3600 * 25) else: data = json.loads(b_data.decode('utf-8')) return Response(json.dumps(data), mimetype='application/json')
def user_register(): email = request.values.get('username') invitation_code = request.values.get('invitation_code') username = request.values.get('username') password = request.values.get('password') re_password = request.values.get('re_password') r = r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)" if re.match(r, email) is None: session['error_message'] = '邮箱地址格式不正确.' return redirect(url_for('register')) if username == '': session['error_message'] = '账号名不能为空。' return redirect(url_for('register')) if r_session.get('%s:%s' % ('user', username)) is not None: session['error_message'] = '该账号名已存在。' return redirect(url_for('register')) if password != re_password: session['error_message'] = '密码输入不一致.' return redirect(url_for('register')) if len(password) < 8: session['error_message'] = '密码必须8位及以上.' return redirect(url_for('register')) if r_session.sismember('email', email): session['error_message'] = '该邮件地址已被注册.' return redirect(url_for('register')) if not r_session.sismember('invitation_codes', invitation_code) and \ not r_session.sismember('public_invitation_codes', invitation_code): session['error_message'] = '无效的邀请码。' return redirect(url_for('register')) email_code = r_session.get('emailcode:%s' % email) if email_code is not None: code_time = json.loads(email_code.decode('utf-8')) if datetime.strptime(code_time, '%Y-%m-%d %H:%M:%S') + timedelta(minutes=5) > datetime.now(): session['error_message'] = '发送邮件过于频繁 请稍候再试.' return redirect(url_for('register')) _chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" key = ''.join(random.sample(_chars, 36)) config_key = '%s:%s' % ('user', 'system') config_info = json.loads(r_session.get(config_key).decode('utf-8')) if 'trial_period' not in config_info.keys(): config_info['trial_period'] = 14 user = dict(username=username, password=hash_password(password), id=str(uuid.uuid1()), active=True, is_admin=False, max_account_no=1, email=email,total_account_point=config_info['trial_period'], created_time=datetime.now().strftime('%Y-%m-%d %H:%M:%S')) days=int(user.get('total_account_point')/user.get('max_account_no')) if days<36500: user['expire_date'] = (datetime.now() + timedelta(days=days)).strftime('%Y-%m-%d') else: user['expire_date'] = (datetime.now() + timedelta(days=36500)).strftime('%Y-%m-%d') r_session.setex('emailcode:%s' % email, json.dumps(user.get('created_time')), 60*5) r_session.setex('activecode:%s' % key, json.dumps(user), 60*30) bytesString = key.encode('utf-8') encodestr = base64.b64encode(bytesString) if user_email(email, encodestr.decode('utf-8')) != True: session['error_message'] = '激活帐户邮件发送失败 邮箱不存在.' return redirect(url_for('register')) r_session.srem('invitation_codes', invitation_code) r_session.srem('public_invitation_codes', invitation_code) session['info_message'] = '激活帐户邮件已发送到您的邮箱.' return redirect(url_for('register'))
def __handshake(): user = session.get('user_info') username = user.get('username') if user.get('username') is not None else '' key = 'user:%s:is_online' % username r_session.setex(key, '1', 120) r_session.sadd('global:online.users', username)
def user_register(): email = request.values.get('username') invitation_code = request.values.get('invitation_code') username = request.values.get('username') password = request.values.get('password') re_password = request.values.get('re_password') r = r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)" if re.match(r, email) is None: session['error_message'] = '邮箱地址格式不正确.' return redirect(url_for('register')) if username == '': session['error_message'] = '账号名不能为空。' return redirect(url_for('register')) if r_session.get('%s:%s' % ('user', username)) is not None: session['error_message'] = '该账号名已存在。' return redirect(url_for('register')) if password != re_password: session['error_message'] = '新密码输入不一致.' return redirect(url_for('register')) if len(password) < 8: session['error_message'] = '密码必须8位及以上.' return redirect(url_for('register')) if r_session.sismember('email', email): session['error_message'] = '该邮件地址已被注册.' return redirect(url_for('register')) if not r_session.sismember('invitation_codes', invitation_code) and \ not r_session.sismember('public_invitation_codes', invitation_code): session['error_message'] = '无效的邀请码。' return redirect(url_for('register')) email_code = r_session.get('emailcode:%s' % email) if email_code is not None: code_time = json.loads(email_code.decode('utf-8')) if datetime.strptime(code_time, '%Y-%m-%d %H:%M:%S') + timedelta(minutes=5) > datetime.now(): session['error_message'] = '发送邮件过于频繁 请稍候再试.' return redirect(url_for('register')) _chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" key = ''.join(random.sample(_chars, 36)) user = dict(username=username, password=hash_password(password), id=str(uuid.uuid1()), active=True, is_admin=False, max_account_no=20, email=email, created_time=datetime.now().strftime('%Y-%m-%d %H:%M:%S')) r_session.setex('emailcode:%s' % email, json.dumps(user.get('created_time')), 60*5) r_session.setex('activecode:%s' % key, json.dumps(user), 60*30) bytesString = key.encode('utf-8') encodestr = base64.b64encode(bytesString) if user_email(email, encodestr.decode('utf-8')) != True: session['error_message'] = '激活帐户邮件发送失败 邮箱不存在.' return redirect(url_for('register')) r_session.srem('invitation_codes', invitation_code) r_session.srem('public_invitation_codes', invitation_code) session['info_message'] = '激活帐户邮件已发送到您的邮箱.' return redirect(url_for('register'))
def __handshake(user): username = user.get('username') r_session.setex('user:%s:is_online' % username, '1', 120) r_session.sadd('global:online.users', username)
def __handshake(): user = session.get("user_info") username = user.get("username") if user.get("username") is not None else "" key = "user:%s:is_online" % username r_session.setex(key, "1", 120) r_session.sadd("global:online.users", username)
def __handshake(): user = session.get('user_info') username = user.get('username') r_session.setex('user:%s:is_online' % username, '1', 120) r_session.sadd('global:online.users', username)