def change_user_ignore(update): with db_session: user_id = update.message.from_user.id user = User.get(tg_user_id=user_id) if not user: insert_user_by_update(update) user = User.get(tg_user_id=user_id) user.tg_user_ignore = 1 ^ user.tg_user_ignore commit() return user.tg_user_ignore
def note_index_post_page(): """ 添加用户笔记 """ editor = g.curr_user data = {} form = NoteForm() data['is_error'] = False uid = form.uid.data user = None if uid: user = User.get(uid) if not user: user = editor if form.validate(): note = add_note(form.title.data, form.content.data, user, editor, x=form.x.data, y=form.y.data, z=form.z.data) to_redirect = redirect(url_for('note_page.note_index_page', uid=uid)) return to_redirect else: data['is_error'] = True data['form'] = form notes, pages = get_user_recent_notes(user, limit=60) data['notes'] = notes data['pages'] = pages return render('/note/index.html', **data)
def inc_coupon_count(db: directives.PeeweeSession, user_name: hug.types.text, increment: hug.types.number): """ [--user_name] <string> [--increment] <number>; increment the user coupon_count, to decrement give a negative number """ with db.atomic(): user = User.get(User.user_name == user_name) user.coupons += increment user.save()
def set_coupon_count(db: directives.PeeweeSession, user_name: hug.types.text, value: hug.types.number): """ [--user_name] <string> [--value] <number>; set the user coupon_count to <value> """ with db.atomic(): user = User.get(User.user_name == user_name) user.coupons = value user.save()
def get_user(user_name: str, context: PeeweeContext): with context.db.atomic(): try: return User.get(User.user_name == user_name) except DoesNotExist: log.warning("user not found: %s", user_name) return False except DatabaseError: log.exception("unknown error logging in: %s", user_name) return False
def get_or_create_anon_user(context: PeeweeContext): name = "unregistered_user" with context.db.atomic(): try: user = User.get(User.user_name == name) return user except DoesNotExist: user = User.create(user_name=name, role=UserRoles.ANON, salt="", password="", coupons=4) user.save() return user
def before_request(): """ Do sth before each request. """ # var init. g.curr_user = None g.req_start_time = time.time() user_id = get_user_id_from_cookie() if user_id > 0: #todo(myth) 根据cookie查询数据,获取用户名 curr_user = User.get(user_id) if curr_user: g.curr_user = curr_user
def change_user_pw(db: directives.PeeweeSession, username: hug.types.text, password: hug.types.text, for_real: hug.types.smart_boolean = False): if not for_real: print( f"this would change {username}'s pw to {password}. Run with --for_real if you're sure.") sys.exit(1) with db.atomic(): name = username.lower() salt = get_random_string(2) secret_password = password hashed_password = hash_pw(name, salt, secret_password) user = User.get(User.user_name == username) user.salt = salt user.password = hashed_password user.save() print(f"{user.user_name}'s pw successfully changed.")
def patch_user(db: PeeweeSession, user_name: hug.types.text, coupons: hug.types.number): with db.atomic(): try: user = User.get(User.user_name == user_name) if coupons < 0: coupons = 0 user.coupons = coupons user.save() return {"user_name": user.user_name, "coupons": user.coupons} except DoesNotExist as e: raise hug.HTTPBadRequest except ValueError as e: raise hug.HTTPBadRequest except AssertionError as e: raise hug.HTTPBadRequest
def get_or_create_auto_user(db: PeeweeSession, role: str, name: str): coupons = 4 if ( role == UserRoles.ANON ) else config.Ldap.user_coupon_number if role == UserRoles.USER else 1 with db.atomic(): try: user = User.get(User.user_name == name) return user except DoesNotExist: user = User.create(user_name=name, role=role, salt="", password="", coupons=coupons) user.save() return user
def verify_user(user_name, user_password, context: PeeweeContext): name = normalize_user(user_name) with context.db.atomic(): try: user = User.get(User.user_name == name) salt = user.salt hashed = hash_pw(name, salt, user_password) if hashed == user.password: return user log.warning("invalid credentials for user: %s", user_name) return False except DoesNotExist: log.warning("user not found: %s", user_name) return False except DatabaseError: log.exception("unknown error logging in: %s", user_name) return False
def before_request(): """ Do sth before each request. """ clean_resource() # var init. g.curr_user = None g.req_start_time = time.time() user_id = get_user_id_from_cookie() if user_id > 0: # todo(myth) 根据cookie查询数据,获取用户名 curr_user = User.get(user_id) if curr_user: g.curr_user = curr_user
def note_index_page(): """ 用户的笔记 """ data = {} editor = g.curr_user uid = request.args.get('uid', type=int, default=0) user = None if uid: user = User.get(uid) if not user: user = editor form = NoteForm() form.uid.data = user.id data['form'] = form notes, pages = get_user_recent_notes(user, limit=60) data['notes'] = notes data['pages'] = pages return render('/note/index.html', **data)
def inc_coupon_count(db: directives.PeeweeSession, user_name: hug.types.text, increment: hug.types.number): with db.atomic(): user = User.get(User.user_name == user_name) user.coupons += increment user.save()
def set_coupon_count(db: directives.PeeweeSession, user_name: hug.types.text, value: hug.types.number): with db.atomic(): user = User.get(User.user_name == user_name) user.coupons = value user.save()