def follow(username): user = MongoClient().blog.User.find_one({'username': username}) if user is None: flash('此用户不存在.') return redirect(url_for('.index')) very = False temp = MongoClient().blog.User.find_one({ 'username': current_user.username }).get('following') for i in range(temp.__len__()): if temp[i][0] == username: very = True break if very: flash('您已经关注过了他,不能重复关注.') return redirect(url_for('.user', username=username)) followers = user.get('followers') time = datetime.utcnow() follow = [current_user.username, time] followers.append(follow) MongoClient().blog.User.update({'username': username}, {'$set': { 'followers': followers }}) post2 = MongoClient().blog.User.find_one( {'username': current_user.username}) following = post2.get('following') follow = [user.get('username'), time] following.append(follow) MongoClient().blog.User.update({'username': current_user.username}, {'$set': { 'following': following }}) flash('您成功关注了 %s.' % username) return redirect(url_for('.user', username=username))
def signin(): if current_user is not None and current_user.is_authenticated: return redirect(url_for('index')) form = LoginForm(request.form) if form.validate_on_submit(): user = User.query.filter_by(email=form.email.data).first() if user is None: form.email.errors.append('User does not exist') return render_template('signin.html', signinpage_form=form) if user.password != hash_password(form.password.data): return render_template('signin.html', signinpage_form=form) login_user(user, remember=form.remember_me.data) session['signed'] = True session['username'] = user.username if session.get('next'): next_page = session.get('next') session.pop('next') return redirect(next_page) else: return redirect(url_for('index')) else: session['next'] = request.args.get('next') return render_template('signin.html', signinpage_form=LoginForm(), page_title='this is Login route')
def post(id): post = MongoClient().blog.Aritical.find({'_id': ObjectId(id)}) form = CommentForm() if form.validate_on_submit(): comments = post[0].get('comments') body = form.body.data comments.append([body, current_user.username, datetime.utcnow()]) MongoClient().blog.Aritical.update({'_id': ObjectId(id)}, {'$set': { 'comments': comments }}) flash('评论发布成功.') return redirect(url_for('.post', id=id, page=-1)) page = request.args.get('page', 1, type=int) pagination = PaginateComments(page, id) comments = pagination.items comment = (post[0].get('username') != current_user.username) return render_template('post.html', posts=post, form=form, i=0, comments=comments, pagination=pagination, author=comment, id=id)
def edit_profile_admin(id): user = MongoClient().blog.User.find_one({'_id': ObjectId(id)}) if user is None: return abort(404) user_temp = Temp(id=user.get('_id'), username=user.get('username'), email=user.get('email'), password=user.get('password'), activate=user.get('activate'), role=user.get('role'), name=user.get('name'), location=user.get('location'), about_me=user.get('about_me'), last_since=user.get('last_since'), member_since=user.get('member_since')) form = EditProfileAdminForm(user=user_temp) if form.validate_on_submit(): MongoClient().blog.User.update({'email': user_temp.email}, {'$set': { 'name': form.name.data }}) MongoClient().blog.User.update( {'email': user_temp.email}, {'$set': { 'username': form.username.data }}) MongoClient().blog.User.update({'email': user_temp.email}, {'$set': { 'email': form.email.data }}) MongoClient().blog.User.update( {'email': user_temp.email}, {'$set': { 'activate': form.activate.data }}) MongoClient().blog.User.update({'email': user_temp.email}, {'$set': { 'role': form.role.data }}) MongoClient().blog.User.update( {'email': user_temp.email}, {'$set': { 'location': form.location.data }}) MongoClient().blog.User.update( {'email': user_temp.email}, {'$set': { 'about_me': form.about_me.data }}) flash('The profile has been updated.') return redirect(url_for('.user', username=user_temp.username)) form.email.data = user_temp.email form.username.data = user_temp.username form.activate.data = user_temp.activate form.role.data = user_temp.role.name form.name.data = user_temp.name form.location.data = user_temp.location form.about_me.data = user_temp.about_me return render_template('edit_profile.html', form=form, user=user_temp)
def facebook_authorized(resp): next_url = flask_login.request.args.get('next') or url_for('root') if resp is None: # The user likely denied the request flask_login.flash(u'There was a problem logging in.') return flask_login.redirect(next_url) flask_login.session['oauth_token'] = (resp['access_token'], '') user_data = facebook.get('/me').data user = User.query.filter(User.email == user_data['email']).first() if user is None: new_user = User(email=user_data['email'], first_name=user_data['first_name'], last_name=user_data['last_name']) db_session.add(new_user) db_session.commit() flask_login.login_user(new_user) else: flask_login.login_user(user) return flask_login.redirect(next_url)
def account_createtable(): form = CreateTableForm(request.form) if form.validate(): tableid = DB.add_table(form.tablenumber.data, current_user.get_id()) new_url = BH.shorten_url(config.base_url + "newrequest/" + tableid) DB.update_table(tableid, new_url) return redirect(url_for('account')) return render_template("account.html", createtableform=form, tables=DB.get_tables(current_user.get_id()))
def unfollow(username): user = MongoClient().blog.User.find_one({'username': username}) if user is None: flash('此用户不存在.') return redirect(url_for('.index')) very = False temp = MongoClient().blog.User.find_one({ 'username': current_user.username }).get('following') for i in range(temp.__len__()): if temp[i][0] == username: very = True break if not very: flash('您没有关注这个用户.') return redirect(url_for('.user', username=username)) followers = user.get('followers') for i in range(followers.__len__()): if followers[i][0] == current_user.username: followers.remove(followers[i]) break MongoClient().blog.User.update({'username': username}, {'$set': { 'followers': followers }}) post2 = MongoClient().blog.User.find_one( {'username': current_user.username}) following = post2.get('following') for i in range(following.__len__()): if following[i][0] == username: following.remove(following[i]) break MongoClient().blog.User.update({'username': current_user.username}, {'$set': { 'following': following }}) flash('您取消关注了 %s.' % username) return redirect(url_for('.user', username=username))
def login(): form = LoginForm(request.form) if form.validate(): stored_user = DB.get_user(form.loginemail.data) if stored_user and PH.validate_password(form.loginpassword.data, stored_user['salt'], stored_user['hashed']): user = User(form.loginemail.data) login_user(user, remember=True) return redirect(url_for('account')) form.loginemail.errors.append("Email or password invalid") return render_template("home.html", loginform=form, registrationform=RegistrationForm())
def following(username): user = MongoClient().blog.User.find_one({'username': username}) if user is None: flash('此用户不存在.') return redirect(url_for('.index')) page = request.args.get('page', 1, type=int) pagination = PaginateFollowing(page=page, username=username) follows = pagination.item return render_template('followers.html', user=user, title='关注的人', title1='', title2='关注的人', endpoint='.following', pagination=pagination, follows=follows)
def edit(id): post = MongoClient().blog.Aritical.find_one({'_id': ObjectId(id)}) if current_user.id != post.get('user_id') and \ not current_user.can(Permission.ADMINISTER): abort(403) form = EditPostForm() if form.validate_on_submit(): MongoClient().blog.Aritical.update({'_id': ObjectId(id)}, { '$set': { 'body': form.body.data, 'body_html': body_html(form.body.data) } }) flash('修改成功') return redirect(url_for('.post', id=post.get('_id'))) form.body.data = post.get('body') return render_template('edit_post.html', form=form)
def delete(id): user = MongoClient().blog.Aritical.find({'_id': ObjectId(id)}) if not current_user.username == user[0].get( 'username') and not current_user.is_administrator(): abort(304) timedata = request.args.get('data') comments = user[0].get('comments') for i in range(comments.__len__()): time = str(comments[i][2]) if time == timedata: del comments[i] break MongoClient().blog.Aritical.update({'_id': ObjectId(id)}, {'$set': { 'comments': comments }}) return redirect(url_for('.post', id=id))
def index(): form = PostForm() if current_user.can(Permission.WRITE_ARTICLES) and \ form.validate_on_submit(): Post(body=form.body.data).new_article() return redirect(url_for('.index')) page = request.args.get('page', 1, type=int) show_followed = False if current_user.is_authenticated: show_followed = bool(request.cookies.get('show_followed', '')) if show_followed: pagination = Paginate(page, 1) else: pagination = Paginate(page, 0) posts = pagination.item return render_template('index.html', form=form, posts=posts, pagination=pagination, show_followed=show_followed)
def get_rooms(user): from models.models import ActivatedUsers, session q = ActivatedUsers.query.filter_by(user_id=user).first() if q is None: from flask_login import redirect, url_for del session['user_active'] return redirect(url_for('main.index_page')) if q.rooms is not None: arr = q.rooms.split('/') s = [] for x in arr: if x: s.append(x) session['rooms'] = s
def signout(): session.pop('username') session.pop('signed') logout_user() return redirect(url_for('index'))
def account_deletetable(): tableid = request.args.get("tableid") DB.delete_table(tableid) return redirect(url_for('account'))
def logout(): if current_user.is_authenticated(): logout_user() return redirect('/')
def dashboard_resolve(): request_id = request.args.get("request_id") DB.delete_request(request_id) return redirect(url_for('dashboard'))
def show_followed(): resp = make_response(redirect(url_for('.index'))) resp.set_cookie('show_followed', '1', max_age=30 * 24 * 60 * 60) return resp
def logout(): logout_user() return redirect(url_for("home"))
def before_request(): if not (current_user.is_authenticated and current_user.admin): return redirect(url_for('home.signin'))
def unauthorized(): return redirect(url_for('login', next=request.path))