def test_user_name_unique(session): user1, user2 = User(name='first', password='******'), User(name='first', password='******') session.add_all((user1, user2)) with pytest.raises(IntegrityError): session.commit()
def create_db(): try: User.drop_table() Member.drop_table() Commodity.drop_table() Category.drop_table() UserOrder.drop_table() OrderItem.drop_table() CommodityImage.drop_table() CommodityComment.drop_table() Address.drop_table() OrderStatus.drop_table() Announcement.drop_table() Feedback.drop_table() except: pass User.create_table() Member.create_table() Commodity.create_table() Category.create_table() UserOrder.create_table() OrderItem.create_table() CommodityImage.create_table() CommodityComment.create_table() Address.create_table() OrderStatus.create_table() Announcement.create_table() Feedback.create_table()
def confirm_all(order_id): order = get_object_or_404(UserOrder, id=order_id) status = get_object_or_404(OrderStatus, name="confirmed") UserOrder.update(is_confirmed=True, status=status, confirm_date=dt.today()).where(id=order_id).execute() point = g.user.point point += order.total_price() User.update(point=point).where(id=g.user.id).execute() return render_template("cart/success.html", user=g.user, order=order)
def test_user_set_password(session): user = User() user.set_password('password') assert user.password_hash is not None assert user.password_hash != 'password' user2 = User(password='******') assert user2.password_hash is not None assert user2.password_hash != 'password'
def init_db(): # init status global status_list global memberships global category_list global user_list s_init = OrderStatus.create(name="init") s_confirmed = OrderStatus.create(name="confirmed") s_sent = OrderStatus.create(name="sent") s_complete = OrderStatus.create(name="complete") s_canceled = OrderStatus.create(name="canceled") status_list += [s_init, s_confirmed, s_sent, s_complete, s_canceled] member0 = Member.create(name="normal", point=1000, discount=100) member1 = Member.create(name="vip1", point=2000, discount=90) member2 = Member.create(name="vip2", point=3000, discount=80) member3 = Member.create(name="vip3", point=4000, discount=70) member4 = Member.create(name="vip4", point=5000, discount=60) member5 = Member.create(name="vip5", point=-1, discount=50) memberships += [member1, member2, member3, member4] # init user User.create(username="******", password="******", admin=True) # user = User.create(username="******", password="******", admin=True) # user.set_password("zhang") # user.save() Announcement.create(title="Hello", content="World") Announcement.create(title="Hello", content="World") Announcement.create(title="Hello", content="World") for i in range(100): random_user() # init Commodity cat = Category.create(name="All") category_list.append(cat) for i in range(10): random_cat() for i in range(100): random_co() for u in user_list: random_order(u) for i in range(20): random_feed()
def _update_group(bot, update=False): session = db.session for g in bot.groups(update): group = session.query(Group).get(g.puid) if not group: group = Group.create(id=g.puid, nick_name=g.nick_name) local_ids = set([u.id for u in group.members]) wx_ids = set([u.puid for u in g.members]) need_add = wx_ids.difference(local_ids) if need_add: for u in g.members: if u.puid in need_add: user = User.create( id=u.puid, **{field: getattr(u, field) for field in USER_FIELD}) group.add_member(user) _, path, need_update = gen_avatar_path(u.puid) if need_update: try: u.get_avatar(path) except (ResponseError, KeyError): print('No member: {}'.format(u.puid)) need_del = local_ids.difference(wx_ids) if need_del: for u in group.members: if u.id in need_del: group.del_member(u) _, path, need_update = gen_avatar_path(g.puid) if need_update: g.get_avatar(path) session.commit()
def _update_contact(bot, update=False): session = db.session myself = _get_self(bot) wx_friends = bot.friends(update) local_ids = set([u.id for u in myself.friends.all()]) wx_ids = set([u.puid for u in wx_friends]) need_add = wx_ids.difference(local_ids) if need_add: for u in wx_friends: if u.puid in need_add: user = User.create( id=u.puid, **{field: getattr(u, field) for field in USER_FIELD}) myself.add_friend(user) _, path, need_update = gen_avatar_path(u.puid) if need_update: try: u.get_avatar(path) except ResponseError: print('No member: {}'.format(u.puid)) need_del = local_ids.difference(wx_ids) if need_del: for u in myself.friends: if u.id in need_del: myself.del_friend(u) session.commit()
def _get_self(bot): myself = db.session.query(User).get(bot.self.puid) if myself is None: myself = User.create( id=bot.self.puid, **{field: getattr(bot.self, field) for field in USER_FIELD}) return myself
def before_request(): from models.core import User if 'user_pk' in session: try: g.user = User.select().get(id=int(session['user_pk'])) except: g.user = None else: g.user = None
def random_user(): global memberships global user_list username = "******".join(random_name(2)) password = "******" user = User.create(username=username, password=password, membership=random.choice(memberships)) # user.set_password("zhang") # user.save() user_list.append(user) return user
def _update_mp(bot, update=False): session = db.session myself = session.query(User).get(bot.self.puid) wx_mps = bot.mps() local_ids = set([m.id for m in myself.mps]) wx_ids = set([u.puid for u in wx_mps]) need_add = wx_ids.difference(local_ids) if need_add: for m in wx_mps: if m.puid in need_add: User.create(id=m.puid, **{field: getattr(m, field) for field in MP_FIELD}) # wxpy还不支持公众号的头像下载 need_del = local_ids.difference(wx_ids) if need_del: for mp in myself.mps: if mp.id in need_del: db.session.delete(mp) session.commit()
def register(): if request.method == 'GET': return render_template('core/register.html', register=True) else: username = request.form.get('username', None) password = request.form.get('password', None) passwordr = request.form.get('passwordr', None) if not username or not password or not passwordr: return render_template('core/register.html', register=True, error='Can not be empty') if password != passwordr: return render_template('core/register.html', register=True, error='Password Not The Same') if User.select().filter(username=username).exists(): return render_template('core/register.html', register=True, error='User has exist') user = User.create(username=username, password=password) #user.set_password(password) #user.save() session['logged_in'] = True session['user_pk'] = user.get_pk() session.permanent = True g.user = user return redirect(url_for('core.index'))
def login(): post = request.json username = post.get("username", None) password = post.get("password", None) user = User.select().filter(username=username).execute().first() if user: if user.check_password(password): session['logged_in'] = True session['user_pk'] = user.get_pk() session.permanent = True g.user = user return jsonify(success=True, is_admin=user.admin) else: return jsonify(success=False, msg="Wrong Password") else: return jsonify(success=False, msg="No such user")
def _update_group(bot, update=False): session = db.session wx_groups = bot.groups(update) myself = _get_self(bot) wx_ids = set([g.puid for g in wx_groups]) groups = session.query(Group).filter(Group.owner_id == bot.self.puid).all() local_ids = set([g.id for g in groups]) need_del = local_ids.difference(wx_ids) for group in groups: if group.id in need_del: myself.groups.remove(group) db.session.delete(group) for g in wx_groups: group = session.query(Group).get(g.puid) if not group: group = Group.create(id=g.puid, nick_name=g.nick_name, owner_id=bot.self.puid) local_ids = set([u.id for u in group.members]) wx_ids = set([u.puid for u in g.members if u.puid]) need_add = wx_ids.difference(local_ids) if need_add: for u in g.members: if u.puid in need_add: user = User.create( id=u.puid, **{field: getattr(u, field) for field in USER_FIELD}) group.add_member(user) _, path, need_update = gen_avatar_path(u.puid) if need_update: try: u.get_avatar(path) except (ResponseError, KeyError): print('No member: {}'.format(u.puid)) need_del = local_ids.difference(wx_ids) if need_del: for u in group.members: if u.id in need_del: group.del_member(u) _, path, need_update = gen_avatar_path(g.puid) if need_update: g.get_avatar(path) session.commit()
def login_page(): if request.method == 'GET': return render_template('core/login.html', login=True, next=request.args.get('next', None)) else: username = request.form.get("username", None) password = request.form.get("password", None) next_url = request.form.get("next", None) user = User.select().filter(username=username).execute().first() if user: if user.check_password(password): session['logged_in'] = True session['user_pk'] = user.get_pk() session.permanent = True g.user = user if next_url: return redirect(next_url) return redirect(url_for('core.index')) else: return render_template('core/login.html', login=True, error='Password error') else: return render_template('core/login.html', login=True, error='No such user')
def test_user_password_non_null(session): user = User(name='garrett') session.add(user) with pytest.raises(IntegrityError): session.commit()
def retrieve_data(update=False): bot = get_bot() session = db.session # update group for g in bot.groups(update): group = session.query(Group).get(g.puid) if not group: group = Group.create(id=g.puid, nick_name=g.nick_name) local_ids = set([u.id for u in group.members]) wx_ids = set([u.puid for u in g.members]) need_add = wx_ids.difference(local_ids) if need_add: for u in g.members: if u.puid in need_add: user = User.create(id=u.puid, **{field: getattr(u, field) for field in USER_FIELD}) group.add_member(user) _, path, need_update = gen_avatar_path(u.puid) if need_update: try: u.get_avatar(path) except (ResponseError, KeyError): print('No member: {}'.format(u.puid)) need_del = local_ids.difference(wx_ids) if need_del: for u in group.members: if u.id in need_del: group.del_member(u) _, path, need_update = gen_avatar_path(g.puid) if need_update: g.get_avatar(path) # update mp myself = session.query(User).get(bot.self.puid) wx_mps = bot.mps() local_ids = set([m.id for m in myself.mps]) wx_ids = set([u.puid for u in wx_mps]) need_add = wx_ids.difference(local_ids) if need_add: for m in wx_mps: if m.puid in need_add: User.create(id=m.puid, **{field: getattr(m, field) for field in MP_FIELD}) # wxpy还不支持公众号的头像下载 need_del = local_ids.difference(wx_ids) if need_del: for mp in myself.mps: if mp.id in need_del: db.session.delete(mp) # update contact myself = session.query(User).get(bot.self.puid) wx_friends = bot.friends() local_ids = set([u.id for u in myself.friends.all()]) wx_ids = set([u.puid for u in wx_friends]) need_add = wx_ids.difference(local_ids) if need_add: for u in wx_friends: if u.puid in need_add: user = User.create(id=u.puid, **{field: getattr(u, field) for field in USER_FIELD}) myself.add_friend(user) _, path, need_update = gen_avatar_path(u.puid) if need_update: try: u.get_avatar(path) except ResponseError: print('No member: {}'.format(u.puid)) need_del = local_ids.difference(wx_ids) if need_del: for u in myself.friends: if u.id in need_del: myself.del_friend(u) session.commit()
def retrieve_data(update=False): bot = get_bot() session = db.session # update group for g in bot.groups(update): group = session.query(Group).get(g.puid) if not group: group = Group.create(id=g.puid, nick_name=g.nick_name) local_ids = set([u.id for u in group.members]) wx_ids = set([u.puid for u in g.members]) need_add = wx_ids.difference(local_ids) if need_add: for u in g.members: if u.puid in need_add: user = User.create( id=u.puid, **{field: getattr(u, field) for field in USER_FIELD}) group.add_member(user) _, path, need_update = gen_avatar_path(u.puid) if need_update: try: u.get_avatar(path) except (ResponseError, KeyError): print('No member: {}'.format(u.puid)) need_del = local_ids.difference(wx_ids) if need_del: for u in group.members: if u.id in need_del: group.del_member(u) _, path, need_update = gen_avatar_path(g.puid) if need_update: g.get_avatar(path) # update mp myself = session.query(User).get(bot.self.puid) wx_mps = bot.mps() local_ids = set([m.id for m in myself.mps]) wx_ids = set([u.puid for u in wx_mps]) need_add = wx_ids.difference(local_ids) if need_add: for m in wx_mps: if m.puid in need_add: User.create(id=m.puid, **{field: getattr(m, field) for field in MP_FIELD}) # wxpy还不支持公众号的头像下载 need_del = local_ids.difference(wx_ids) if need_del: for mp in myself.mps: if mp.id in need_del: db.session.delete(mp) # update contact myself = session.query(User).get(bot.self.puid) wx_friends = bot.friends() local_ids = set([u.id for u in myself.friends.all()]) wx_ids = set([u.puid for u in wx_friends]) need_add = wx_ids.difference(local_ids) if need_add: for u in wx_friends: if u.puid in need_add: user = User.create( id=u.puid, **{field: getattr(u, field) for field in USER_FIELD}) myself.add_friend(user) _, path, need_update = gen_avatar_path(u.puid) if need_update: try: u.get_avatar(path) except ResponseError: print('No member: {}'.format(u.puid)) need_del = local_ids.difference(wx_ids) if need_del: for u in myself.friends: if u.id in need_del: myself.del_friend(u) session.commit()
def test_user_name_non_null(session): user = User(password='******') session.add(user) with pytest.raises(IntegrityError): session.commit()