def wechat_login(self, jsonInfo): ''' 微信用户登录 第一步:# 获取唯一的open_id 第二步: 判断用户是否是第一次登录,如果是,则创建用户信息,否则,查看是否需要变更用户信息 :param info: :return: {'data': '', 'status': 'SUCCESS'} ''' info = json.loads(jsonInfo) raw_data = info['rawData'].encode('utf-8') signature = info['signature'] code = info['code'] url = GET_OPENID_URL payload = {'appid': APP_ID, 'secret': APP_SECRET, 'js_code': code, 'grant_type': 'authorization_code'} r = requests.get(url=url, params=payload) ret = json.loads(r.text) open_id = ret['openid'] session_key = ret['session_key'].encode('utf-8') new_signature = hashlib.sha1(raw_data + session_key).hexdigest() if signature == new_signature: query = db.session.query(UserInfo).filter(UserInfo.open_id == open_id) result = query.first() user_info = {} raw_data_dict = json.loads(info['rawData']) avatar_url = raw_data_dict['avatarUrl'] user_name = raw_data_dict['nickName'] user_info['imgpath'] = avatar_url user_info['open_id'] = open_id user_info['user_name'] = user_name if result: user_info['user_id'] = result.user_id user_info['foreign_id'] = user_info['user_id'] self._update_user_info(user_info) else: user_info['user_id'] = self.generate_id(open_id) user_info['imgpath_id'] = self.generate_id(avatar_url) user_info['foreign_id'] = user_info['user_id'] user_info['createtime'] = datetime.now() ImgPath.create(info=user_info) UserInfo.create(info=user_info) tokenManager = TokenManager() user_info['validity'] = VALIDITY (status, token_id) = tokenManager.update_token(info=user_info) db.session.commit() return (True, token_id) else: return (False, None)
def _create_user(self, info): ''' 后台,创建用户 第一步: 验证管理员身份 第二步: 创建Token, Imgpath, UserInfo信息 :param info: {'token_id': '', 'user_name': '', 'imgpath': '', 'user_id': '', 'open_id': ''} :return: {'data': '', 'status': 'SUCCESS'} ''' info['imgpath_id'] = self.generate_id(info['imgpath']) info['imgpath'] = info['imgpath'] info['foreign_id'] = info['user_id'] ImgPath.create(info=info) UserInfo.create(info=info) db.session.commit() return (True, info['user_id'])
def get_score_list(self, info): ''' 积分列表 第一步: 验证用户身份 第二步: 获取积分列表 :param info: {'token_id': '', 'user_id': '', 'activity_id': ''} :return: {'data': '', 'status': 'SUCCESS'} ''' user_id = info['user_id'] start_index = info['start_index'] page_count = info['page_count'] query = db.session.query(UserInfo) all_result = query.order_by(desc(UserInfo.user_score)).offset(start_index).limit(page_count).all() callback_data = [] imagemanager = ImageManager() img_request = {} for result in all_result: (status, user_info) = UserInfo.generate(result=result) count = db.session.query( func.count(UserInfo.user_id) ).filter( UserInfo.user_score > user_info['user_score'] ).first() user_info['index_number'] = count[0] + 1 img_request['foreign_id'] = result.user_id if result.open_id is None: (status, image_info) = imagemanager.get_image(info=img_request) else: imgpath = db.session.query(ImgPath).filter(ImgPath.foreign_id == result.user_id).first() (status, image_info) = ImgPath.generate(result=imgpath) user_info.update(image_info) callback_data.append(user_info) return (True, callback_data)
def get_score_list(self, info): ''' 积分列表 第一步: 验证用户身份 第二步: 获取积分列表 :param info: {'token_id': '', 'user_id': '', 'activity_id': ''} :return: {'data': '', 'status': 'SUCCESS'} ''' user_id = info['user_id'] start_index = info['start_index'] page_count = info['page_count'] query = db.session.query(UserInfo) all_result = query.order_by(desc( UserInfo.user_score)).offset(start_index).limit(page_count).all() callback_data = [] imagemanager = ImageManager() img_request = {} for result in all_result: (status, user_info) = UserInfo.generate(result=result) count = db.session.query(func.count(UserInfo.user_id)).filter( UserInfo.user_score > user_info['user_score']).first() user_info['index_number'] = count[0] + 1 img_request['foreign_id'] = result.user_id if result.open_id is None: (status, image_info) = imagemanager.get_image(info=img_request) else: imgpath = db.session.query(ImgPath).filter( ImgPath.foreign_id == result.user_id).first() (status, image_info) = ImgPath.generate(result=imgpath) user_info.update(image_info) callback_data.append(user_info) return (True, callback_data)
def test_mod_user_by_id(app, db, check_ui): if app.user.count() == 0: app.user.create(UserInfo(firstname="Test", lastname="TEST")) old_users = db.get_userinfo_list() user = random.choice(old_users) user_mod = UserInfo(firstname="EDITED4466", lastname="EDITED4466") app.user.mod_user_by_id(user.id, user_mod) new_users = db.get_userinfo_list() #assert len(old_users) == len(new_users) pos = old_users.index(user) old_users[pos] = user_mod assert old_users == new_users if check_ui: assert sorted(old_users, key=UserInfo.id_or_max) == sorted(new_users, key=UserInfo.id_or_max)
def get_score_info(self, info): ''' 获取活动成绩 :param info: {'token_id': '', 'user_id': '', 'activity_id': ''} :return: {'data': '', 'status': 'SUCCESS'} ''' activity_id = info['activity_id'] part_query = db.session.query(Participate, UserInfo).outerjoin( UserInfo, Participate.user_id == UserInfo.user_id ).filter(and_(Participate.activity_id == activity_id, Participate.score != SCORE_EMPTY)) all_result = part_query.order_by(desc(Participate.score)).all() callback_data = [] img_request = {} imagemanager = ImageManager() for score_result in all_result: user = score_result.UserInfo participate = score_result.Participate img_request['foreign_id'] = participate.user_id if user.open_id is None: (status, image_info) = imagemanager.get_image(info=img_request) else: imgpath = db.session.query(ImgPath).filter(ImgPath.foreign_id == user.user_id).first() (status, image_info) = ImgPath.generate(result=imgpath) (status, part_info) = Participate.generate(result=participate) (status, user_info) = UserInfo.generate(result=user) part_info.update(user_info) part_info.update(image_info) callback_data.append(part_info) return (True, callback_data)
def get_club_score_list(self, info): ''' 俱乐部积分列表 第一步: 验证用户身份 第二步: 获取积分最多的俱乐部积分列表 :param info: {'token_id': '', 'user_id': '', 'activity_id': ''} :return: {'data': '', 'status': 'SUCCESS'} ''' user_id = info['user_id'] start_index = info['start_index'] page_count = info['page_count'] if info['club_id'] == '-1': club_query = db.session.query(ClubScore).filter( ClubScore.user_id == user_id) club_result = club_query.order_by(desc( ClubScore.club_number)).first() else: club_result = db.session.query(ClubScore).filter( ClubScore.club_id == info['club_id']).first() print(club_result) if club_result: club_id = club_result.club_id query = db.session.query(UserInfo, ClubScore).outerjoin( ClubScore, UserInfo.user_id == ClubScore.user_id).filter( ClubScore.club_id == club_id) all_result = query.order_by(desc(ClubScore.club_number)).offset( start_index).limit(page_count).all() callback_data = [] imagemanager = ImageManager() img_request = {} for result in all_result: user = result.UserInfo clubscore = result.ClubScore (status, _club) = ClubScore.generate(result=clubscore) (status, _user_info) = UserInfo.generate(result=user) count = db.session.query(func.count( ClubScore.score_id)).filter( and_(ClubScore.club_id == club_id, ClubScore.club_number > _club['club_number'])).first() _club['index_number'] = count[0] + 1 img_request['foreign_id'] = user.user_id if user.open_id is None: (status, image_info) = imagemanager.get_image(info=img_request) else: imgpath = db.session.query(ImgPath).filter( ImgPath.foreign_id == user.user_id).first() (status, image_info) = ImgPath.generate(result=imgpath) _club.update(image_info) _club.update(_user_info) callback_data.append(_club) return (True, callback_data) else: return (False, None)
def create_user_background(self, info): ''' 后台,创建用户 第一步: 验证管理员身份 第二步: 创建Token, Imgpath, UserInfo信息 :param info: {'token_id': '', 'user_name': '', 'imgpath': '', 'user_id': '', 'open_id': ''} :return: {'data': '', 'status': 'SUCCESS'} ''' info['user_id'] = self.generate_id(info['user_name']) info['token_id'] = self.generate_id(info['user_id']) info['createtime'] = datetime.now() info['validity'] = VALIDITY Token.create(info) info['imgpath_id'] = self.generate_id(info['imgpath']) info['imgpath'] = info['imgpath'] info['foreign_id'] = info['user_id'] ImgPath.create(info=info) UserInfo.create(info=info) db.session.commit() return (True, info['user_id'])
def get_club_score_list(self, info): ''' 俱乐部积分列表 第一步: 验证用户身份 第二步: 获取积分最多的俱乐部积分列表 :param info: {'token_id': '', 'user_id': '', 'activity_id': ''} :return: {'data': '', 'status': 'SUCCESS'} ''' user_id = info['user_id'] start_index = info['start_index'] page_count = info['page_count'] if info['club_id'] == '-1': club_query = db.session.query(ClubScore).filter(ClubScore.user_id == user_id) club_result = club_query.order_by(desc(ClubScore.club_number)).first() else: club_result = db.session.query(ClubScore).filter(ClubScore.club_id == info['club_id']).first() print(club_result) if club_result: club_id = club_result.club_id query = db.session.query(UserInfo, ClubScore).outerjoin( ClubScore, UserInfo.user_id == ClubScore.user_id ).filter(ClubScore.club_id == club_id) all_result = query.order_by(desc(ClubScore.club_number)).offset(start_index).limit(page_count).all() callback_data = [] imagemanager = ImageManager() img_request = {} for result in all_result: user = result.UserInfo clubscore = result.ClubScore (status, _club) = ClubScore.generate(result=clubscore) (status, _user_info) = UserInfo.generate(result=user) count = db.session.query( func.count(ClubScore.score_id) ).filter( and_( ClubScore.club_id == club_id, ClubScore.club_number > _club['club_number'] ) ).first() _club['index_number'] = count[0] + 1 img_request['foreign_id'] = user.user_id if user.open_id is None: (status, image_info) = imagemanager.get_image(info=img_request) else: imgpath = db.session.query(ImgPath).filter(ImgPath.foreign_id == user.user_id).first() (status, image_info) = ImgPath.generate(result=imgpath) _club.update(image_info) _club.update(_user_info) callback_data.append(_club) return (True, callback_data) else: return (False, None)
def test_add_user_to_group(app, orm): if app.user.count() == 0: app.user.create(UserInfo(firstname="Test", lastname="TEST")) if app.group.count() == 0: app.group.create(Group(name="Test", header="", footer="")) all_users = orm.get_userinfo_list() user = random.choice(all_users) ##Выберем случайную группу из существующих groups = orm.get_group_list() group_id = random.choice(groups).id app.user.add_user_to_group(user.id, group_id) new_users = orm.get_userinfo_in_group(Group(id=group_id)) assert user in new_users
def test_del_user_by_id(app, db, check_ui): if app.user.count() == 0: app.user.create(UserInfo(firstname="Test", lastname="")) old_users = db.get_userinfo_list() user = random.choice(old_users) app.user.del_user_by_id(user.id) new_users = db.get_userinfo_list() assert len(old_users) - 1 == len(new_users) old_users.remove(user) assert old_users == new_users if check_ui: assert sorted(old_users, key=UserInfo.id_or_max) == sorted(new_users, key=UserInfo.id_or_max)
def get_userinfo_list(self): list = [] cursor = self.connection.cursor() try: cursor.execute( "select id, firstname, lastname from addressbook where deprecated='0000-00-00 00:00'" ) for row in cursor: (id, firstname, lastname) = row list.append( UserInfo(id=str(id), firstname=firstname, lastname=lastname)) finally: cursor.close() return list
def get_room_detail(self, info): ''' 活动详情 第一步: 验证用户身份 第二步: 判断活动是否存在,是否已经加入,是否已经满员 第三步: 自动分配活动位置, 创建参加记录 :param info: {'token_id': '', 'user_id': '', 'activity_id': ''} :return: {'data': '', 'status': 'SUCCESS'} ''' activity_id = info['activity_id'] user_id = info['user_id'] (status, activity_detail) = self._get_activity_detail(info) (status, judge_info) = self._get_activity_judge(info) part_query = db.session.query(Participate, UserInfo).outerjoin( UserInfo, Participate.user_id == UserInfo.user_id).filter( Participate.activity_id == activity_id) all_result = part_query.order_by(Participate.location).all() role_list = [] callback_data = {} my_info = {} img_request = {} imagemanager = ImageManager() for result in all_result: participate = result.Participate user = result.UserInfo img_request['foreign_id'] = participate.user_id (status, part_info) = Participate.generate(result=participate) if user.open_id is None: (status, image_info) = imagemanager.get_image(info=img_request) else: imgpath = db.session.query(ImgPath).filter( ImgPath.foreign_id == user.user_id).first() (status, image_info) = ImgPath.generate(result=imgpath) (status, user_info) = UserInfo.generate(result=user) part_info.update(image_info) part_info.update(user_info) if part_info['user_id'] == user_id: my_info.update(part_info) role_list.append(part_info) callback_data['role_list'] = role_list callback_data['activity_detail'] = activity_detail callback_data['judge_info'] = judge_info callback_data['my_info'] = my_info return (True, callback_data)
def get_room_detail(self, info): ''' 活动详情 第一步: 验证用户身份 第二步: 判断活动是否存在,是否已经加入,是否已经满员 第三步: 自动分配活动位置, 创建参加记录 :param info: {'token_id': '', 'user_id': '', 'activity_id': ''} :return: {'data': '', 'status': 'SUCCESS'} ''' activity_id = info['activity_id'] user_id = info['user_id'] (status, activity_detail) = self._get_activity_detail(info) (status, judge_info) = self._get_activity_judge(info) part_query = db.session.query(Participate, UserInfo).outerjoin( UserInfo, Participate.user_id == UserInfo.user_id ).filter(Participate.activity_id == activity_id) all_result = part_query.order_by(Participate.location).all() role_list = [] callback_data = {} my_info = {} img_request = {} imagemanager = ImageManager() for result in all_result: participate = result.Participate user = result.UserInfo img_request['foreign_id'] = participate.user_id (status, part_info) = Participate.generate(result=participate) if user.open_id is None: (status, image_info) = imagemanager.get_image(info=img_request) else: imgpath = db.session.query(ImgPath).filter(ImgPath.foreign_id == user.user_id).first() (status, image_info) = ImgPath.generate(result=imgpath) (status, user_info) = UserInfo.generate(result=user) part_info.update(image_info) part_info.update(user_info) if part_info['user_id'] == user_id: my_info.update(part_info) role_list.append(part_info) callback_data['role_list'] = role_list callback_data['activity_detail'] = activity_detail callback_data['judge_info'] = judge_info callback_data['my_info'] = my_info return (True, callback_data)
def test_del_user_from_group(app, orm): if app.user.count() == 0: app.user.create(UserInfo(firstname="Test", lastname="TEST")) if app.group.count() == 0: app.group.create(Group(name="Test", header="", footer="")) ##Выберем случайную группу из существующих groups = orm.get_group_list() group = random.choice(groups) ##Найден в ней всех пользователей users_in_group = orm.get_userinfo_in_group(group) ##Если пользователей в группе нет, добавим нового if len(users_in_group) == 0: all_users = orm.get_userinfo_list() user = random.choice(all_users) app.user.add_user_to_group(user.id, group.id) users_in_group = orm.get_userinfo_in_group(group) user_to_del = random.choice(users_in_group) app.user.del_user_from_group(user_to_del.id, group.id) new_users = orm.get_userinfo_in_group(group) assert user_to_del not in new_users
def get_users_list(self): if self.users_cache is None: wd = self.app.wd self.app.open_home_page() self.users_cache = [] for element in wd.find_elements_by_css_selector("tr[name=entry]"): cells = element.find_elements_by_tag_name("td") firstname = cells[2].text lastname = cells[1].text id = element.find_element_by_name("selected[]").get_attribute( "value") all_phones = cells[5].text all_emails = cells[4].text self.users_cache.append( UserInfo(firstname=firstname, lastname=lastname, all_phones_from_homepage=all_phones, all_emails=all_emails, id=id)) return list(self.users_cache)
def get_userinfo_tel_list(self): list = [] cursor = self.connection.cursor() try: cursor.execute( "select id, firstname, lastname, home, mobile, work, phone2 from addressbook where deprecated='0000-00-00 00:00'" ) for row in cursor: (id, firstname, lastname, home, mobile, work, phone2) = row list.append( UserInfo(id=str(id), firstname=firstname, lastname=lastname, homephone=home, mobilephone=mobile, workphone=work, phone2=phone2)) finally: cursor.close() return list
def get_user_info_from_edit_page(self, index): wd = self.app.wd self.select_user_by_index_for_mod(index) firstname = wd.find_element_by_name("firstname").get_attribute("value") lastname = wd.find_element_by_name("lastname").get_attribute("value") id = wd.find_element_by_name("id").get_attribute("value") home = wd.find_element_by_name("home").get_attribute("value") mobile = wd.find_element_by_name("mobile").get_attribute("value") work = wd.find_element_by_name("work").get_attribute("value") phone2 = wd.find_element_by_name("phone2").get_attribute("value") email = wd.find_element_by_name("email").get_attribute("value") email2 = wd.find_element_by_name("email2").get_attribute("value") email3 = wd.find_element_by_name("email3").get_attribute("value") return UserInfo(firstname=firstname, lastname=lastname, homephone=home, mobilephone=mobile, workphone=work, phone2=phone2, email=email, email2=email2, email3=email3, id=id)
elif o == "-f": f = a def random_string(prefix, maxlen): symbols = string.ascii_letters + string.digits # + string.punctuation + " "*10 #Отключение небезопасных символов return prefix + ''.join( [random.choice(symbols) for i in range(random.randrange(maxlen))]) testdata = [ UserInfo( firstname=random_string("firstname", 15), lastname=random_string("lastname", 15), homephone=random_string("homephone", 15), mobilephone=random_string("mobilephone", 15), workphone=random_string("workphone", 15), fax=random_string("fax", 15), phone2=random_string("phone2", 15), email=random_string("email", 15), email2=random_string("email2", 15), email3=random_string("email3", 15), ) for i in range(n) ] file = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", f) with open(file, "w") as out: jsonpickle.set_encoder_options("json", indent=2) out.write(jsonpickle.encode(testdata))
def convert(userinfo): return UserInfo(id=str(userinfo.id), firstname=userinfo.firstname, lastname=userinfo.lastname)