def load_user(id): """ :param id: :return: """ return User.objects(id=id, is_deleted=False).first()
def reset_password(): """ 重置密码 :return: """ email = request.json.email user = User.objects(account__email=email).first() if user: user.account.activation_key = str(uuid4()) user.save() url = 'http://m.maybe.cn/account/confirm_reset_password?activation_key=%s&email=%s' % ( user.account.activation_key, user.account.email) html = render_template('admin/user/_reset_password.html', project=current_app.config['PROJECT'], username=user.name, url=url) message = Message(subject=gettext('reset your password in ') + 'maybe', html=html, recipients=[user.account.email]) message.sender = '*****@*****.**' mail.send(message) return jsonify( message='OK', desc=gettext( 'please see your email for instructions on how to access your account' )) else: return jsonify( message='Failed', desc=gettext('sorry, not found user for that email address'))
def pay(self, order, amount, coin_type=COIN_TYPE.COIN): if coin_type == COIN_TYPE.COIN and amount > self.balance: current_app.logger.error( 'order coin exceed wallet balance. order: {}, amount: {}, balance: {}' .format(order.id, amount, self.balance)) amount = self.balance if coin_type == COIN_TYPE.CASH and amount > self.cash: current_app.logger.error( 'order cash exceed balance. order: {}, amount: {}, balance: {}' .format(order.id, amount, self.cash)) amount = self.cash time = datetime.utcnow() reason = COIN_TRADE_REASON.PAY kind = COIN_TRADE_TYPE.OUTCOME user = User.objects(id=order.customer_id).first() trade = CoinTrade.create(user=user, amount=amount, time=time, kind=kind, reason=reason, wallet=self, reason_id=str(order.id), coin_type=coin_type) return trade
def forget_password(): """ :return: """ email = request.json.get('email', '') if not email: return jsonify(message='Failed', error=gettext('please correct the email format')) user = User.objects(account__email=email).first() if not user: return jsonify( message='Failed', error=gettext('sorry, no user found for that email address')) user.account.activation_key = str(uuid4()) user.save() url = 'http://account.maybe.cn/account/confirm_reset_password?activation_key=%s&email=%s' % ( user.account.activation_key, user.account.email) html = render_template('admin/user/_reset_password.html', username=user.name, url=url) jobs.notification.send_mail.delay([user.account.email], gettext('reset your password in ') + 'maybe', html=html) return jsonify(message='OK')
def logs(self, ltype, lid): """ :param ltype: :param lid: :return: """ if ltype == 'express': logistics = Logistic.objects(id=lid).first().express_tracking return self.render('admin/logistic/express.html', logistics=logistics) elif ltype == 'logistic': logistics = LogisticLog.objects(logistic_id=lid, log_type__ne='API') user = lambda i: getattr(User.objects(id=i).first(), 'name', '' ) if i and i != 'system' else i return self.render('admin/logistic/logs.html', logistics=logistics, user=user) elif ltype == 'print': logistic = Logistic.objects(id=lid).first() if logistic.is_closed: return Response('this logistics id has been closed.') return self.render('admin/logistic/print_page.html', logistic=logistic)
def reset_password(): """ 重置密码 :return: """ form = RecoverPasswordForm() if form.validate_on_submit(): user = User.objects(account__email=form.email.data).first() if user: flash( gettext( 'please see your email for instructions on how to access your account' ), 'success') user.account.activation_key = str(uuid.uuid4()) user.save() # send recover password html # TODO: change project name url = 'http://bigbang.maybe.cn/admin/confirm_reset_password?activation_key=%s&email=%s' % ( user.account.activation_key, user.account.email) html = render_template('admin/user/_reset_password.html', project=current_app.config['PROJECT'], username=user.name, url=url) jobs.notification.send_mail.delay( [user.account.email], gettext('reset your password in ') + 'Maybe', html) return render_template('admin/user/reset_password.html', form=form) else: flash(gettext('sorry, no user found for that email address'), 'error') return render_template('admin/user/reset_password.html', form=form)
def user_info(user_id): """ 获取用户信息 :param user_id: :return: """ user = User.objects(id=user_id).first_or_404() return jsonify(message='OK', user=json_templ.user_json(user))
def unfollow(follow_id): """ :param follow_id: :return: """ follow_user = User.objects(id=follow_id).first_or_404() current_user.unfollow(follow_user) return jsonify(message='OK')
def confirm_reset_password(): """ 确认密码 :return: """ if request.method == 'GET': if current_user.is_authenticated: if not login_fresh(): return login_manager.needs_refresh() user = current_user elif 'activation_key' in request.args and 'email' in request.args: activation_key = request.args.get('activation_key') email = request.args.get('email') user = User.objects( Q(account__activation_key=activation_key) & Q(account__email=email)).first() else: return Response('邮件已失效') form = ConfirmResetPasswordForm( activation_key=user.account.activation_key, email=user.account.email) return render_template('admin/user/confirm_reset_password.html', form=form) if request.method == 'POST': form = ConfirmResetPasswordForm() activation_key = form.activation_key.data email = form.email.data user = User.objects( Q(account__activation_key=activation_key) & Q(account__email=email)).first() # 修改密码成功 if form.validate_on_submit(): user.account.password = form.password.data user.account.activation_key = None user.save() flash( gettext('your password has been changed, please login again'), 'success') return render_template('admin/user/success_reset_password.html') # 修改密码失败 flash(gettext('fail, please confirm your password'), 'success') return render_template('admin/user/confirm_reset_password.html', form=form)
def follow(follow_id): """ :param follow_id: :return: """ follow_user = User.objects(id=follow_id).first_or_404() if follow_user.id == current_user.id: return jsonify(message='Failed', error='Can not follow yourself') current_user.follow(follow_user) return jsonify(message='OK')
def bind_email(): """ :return: """ email = request.json.get('email') user_id = request.json.get('user_id') if not email: return jsonify(message='Failed', error=gettext('no email')) if User.objects(account__email=email): return jsonify(message='Failed', error=gettext('the email alreadly exists')) user = User.objects(id=user_id).first() user.account.email = email user.account.is_email_verified = True user.save() login_user(user, remember=True) return jsonify(message='OK', user=json_templ.get_user_info(user), remember_token=user.generate_auth_token())
def user_followers(): """ :return: """ data = request.args user_id = data.get('user_id') page = int(data.get('page', 0)) per_page = int(data.get('per_page', 20)) user = User.objects(id=user_id).first_or_404() followers = user.followers users = paginate(followers, page, per_page) return jsonify(message='OK', users=[json_templ.user_json(u) for u in users])
def email_signup(): """ 邮箱注册 :return: """ data = request.json name = data.get('name') email = data.get('email', '') password = data.get('password', '') if not password: return jsonify(message='Failed', error=gettext('please fill in.')) if User.objects(account__email=email): return jsonify(message='Failed', error=gettext('this name has been registered.')) if not name: name = 'maybe' + str(time.time()).replace('.', '') user = User.create(name=name, email=email, password=password) login_user(user, remember=True) return jsonify(message='OK', user=json_templ.get_user_info(user), remember_token=user.generate_auth_token())
def customer(self): return User.objects(id=self.customer_id).first()
def validate_email(self, field): if User.objects(account__email=field.data).first() is not None: raise ValidationError('此邮箱已被注册')
def validate_name(self, field): if User.objects(name=field.data).first() is not None: raise ValidationError('此用户名已存在')