def user(username): if re.match("^[a-zA-Z0-9_]+$", username) == None: return render_template('500.html'), 500 user = load_user_by_username(username) if user == -1: flash('Something error!') return render_template('500.html'), 500 if user == 0: flash('User is not exists') return redirect(url_for('index')) page = request.args.get('page', 1, type=int) page = page if page > 0 else 1 all_posts = current_user.followed_posts() post_per_page = app.config['POSTS_PER_PAGE'] posts = all_posts[(page - 1) * post_per_page:page * post_per_page if len(all_posts) >= page * post_per_page else len(all_posts)] next_url = url_for('user', username=user.username, page=page + 1) \ if len(all_posts) > page * post_per_page else None prev_url = url_for('user', username=user.username, page=page - 1) \ if (page > 1 and len(all_posts) > (page - 1) * post_per_page) else None usernames = [] for i in posts: usernames.append(load_user(i[2])) return render_template('user.html', user=user, posts=posts, usernames=usernames, next_url=next_url, prev_url=prev_url, izip=izip, avatars=avatar, dt=datetime.strptime)
def login(): if current_user.is_authenticated: return redirect(url_for('index')) form = LoginForm() if form.validate_on_submit(): user = load_user_by_username(form.username.data) if user == -1: flash('Something error!') return render_template('500.html'), 500 if user == 0: flash('Invalid username or password') return redirect(url_for('login')) login_user(user, remember=form.remember_me.data) next_page = request.args.get('next') if not next_page or url_parse(next_page).netloc != '': next_page = url_for('index') return redirect(next_page) return render_template('login.html', title='Sign In', form=form)
def unfollow(username): if re.match("^[a-zA-Z0-9_]+$", username) == None: return render_template('500.html'), 500 user = load_user_by_username(username) if user == -1: flash('Something error!') return render_template('500.html'), 500 if user == 0: flash('User is not exists') return redirect(url_for('index')) if user == current_user: flash('You cannot unfollow yourself!') return redirect(url_for('user', username=username)) if current_user.unfollow(user): flash('You are not following {}.'.format(username)) else: flash('Failed!') return redirect(url_for('user', username=username))