def search(): form0 = SearchProfileForm() if form0.validate_on_submit(): user = User.query.filter_by(username=form0.username.data).first() if user is not None: return redirect(url_for('user', username=user.username)) flash('Invalid Username') return redirect(request.referrer)
def messages_section(username): form0 = SearchProfileForm() page = request.args.get('page', 1, type=int) convos = Conversation.query.filter_by(author=current_user).union( Conversation.query.filter_by(profile=current_user)).order_by( Conversation.timestamp.desc()).paginate( page, app.config['CONVERSATIONS_PER_PAGE'], False) next_url = url_for('messages_section', username = username, page = convos.next_num) \ if convos.has_next else None prev_url = url_for('messages_section', username = username, page = convos.prev_num) \ if convos.has_prev else None convos_with = [] for convo in convos.items: if convo.author == current_user: convos_with.append(convo.profile) else: convos_with.append(convo.author) return render_template('messages_section.html', convos_with=convos_with, next_url=next_url, prev_url=prev_url, badge_colour=badge_colour, form0=form0, Message=Message)
def comments(post_id): post = Post.query.filter_by(id=post_id).first_or_404() form = CommentForm() form0 = SearchProfileForm() if form.validate_on_submit(): comment = Comment(body=form.comment.data, author=current_user, post=post) db.session.add(comment) db.session.commit() flash('Your comment is now live!') return redirect(url_for('comments', post_id=post.id)) page = request.args.get('page', 1, type=int) comments = Comment.query.order_by(Comment.timestamp.desc()).filter_by( post=post).paginate(page, app.config['COMMENTS_PER_PAGE'], False) next_url = url_for('comments', post_id=post.id, page=posts.next_num) \ if comments.has_next else None prev_url = url_for('comments', post_id=post.id, page=posts.prev_num) \ if comments.has_prev else None return render_template('comments_section.html', title='Comments', upvote=Upvote, badge_colour=badge_colour, form=form, form0=form0, post=post, comments=comments.items)
def leaderboard(): form0 = SearchProfileForm() form = EmptyForm() page = request.args.get('page', 1, type=int) posts = Post.query.order_by(Post.votes.desc()).filter_by( banned=0).paginate(page, app.config['POSTS_PER_PAGE'], False) #Getting all posts next_url = url_for('leaderboard', page=posts.next_num) \ if posts.has_next else None prev_url = url_for('leaderboard', page=posts.prev_num) \ if posts.has_prev else None page_num = request.args.get('page', '1') start_num = int(page_num) * app.config['POSTS_PER_PAGE'] - app.config[ 'POSTS_PER_PAGE'] + 1 if current_user.banned: return render_template('banned.html') return render_template('leaderboard.html', title='Leaderboard', form=form, form0=form0, posts=posts.items, upvote=Upvote, badge_colour=badge_colour, next_url=next_url, prev_url=prev_url, start_num=start_num)
def messages(username): msg_to = User.query.filter_by(username=username).first_or_404() if msg_to.id == 1 or msg_to == current_user: #Prevent chatting with admin or self return render_template('admin_restricted.html') form0 = SearchProfileForm() form = MessageForm() if form.validate_on_submit(): message = Message(body=form.message.data, author=current_user, profile=msg_to) db.session.add(message) flash('Message sent!') ##add change to table prev_convo = None new_convo = None try: print('curstart') current_start = Conversation.query.filter_by(author=current_user, profile=msg_to) print('msgstart') msg_to_start = Conversation.query.filter_by(author=msg_to, profile=current_user) print('union') prev_convo = current_start.union(msg_to_start).first() print('set_tm') prev_convo.timestamp = message.timestamp print('finish try') except: print('new convo') new_convo = Conversation(author=current_user, profile=msg_to) db.session.add(new_convo) db.session.commit() return redirect(url_for('messages', username=username)) page = request.args.get('page', 1, type=int) messages = current_user.msgs_btw(username).paginate( page, app.config['MESSAGES_PER_PAGE'], False) for message in messages.items: if message.author != current_user: message.seen = 1 db.session.commit() next_url = url_for('messages', username = username, page = messages.next_num) \ if messages.has_next else None prev_url = url_for('messages', username = username, page = messages.prev_num) \ if messages.has_prev else None return render_template('messages.html', title='Messages', messages=messages.items, form0=form0, form=form, user=msg_to, badge_colour=badge_colour, prev_url=prev_url, next_url=next_url)
def general_report(): form0 = SearchProfileForm() form = ReportForm() if form.validate_on_submit(): prev_url = request.args.get('prev') if url_parse(prev_url).netloc != '': return redirect(url_for('index')) report = Report(reason=form.reason.data, page_of_report=prev_url, author=current_user) db.session.add(report) db.session.commit() flash('Your report has been submitted. Thank you for your feedback!') return redirect(prev_url) return render_template('general_report.html', form=form, form0=form0, title='Report')
def index(): #Submit post form0 = SearchProfileForm() form = EmptyForm() form2 = PostForm() if form.validate_on_submit(): file = form2.dare.data post = Post(body=form2.post.data, author=current_user) db.session.add(post) post = Post.query.order_by(Post.timestamp.desc()).first() filename = secure_filename("{}_{}_{}".format(str(post.author.id), str(post.author), str(post.id))) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) #path = str(app.config['UPLOAD_FOLDER'] + "/" + filename) post.dare = filename db.session.commit() flash('Your post is now live!') #Standard practice to respond to POST requests with redirect #as web browsers refresh by re-issuing the last request. #Without redirecting, refresh will resubmit the form. return redirect(url_for('index')) #Page navigation page = request.args.get('page', 1, type=int) posts = current_user.followed_posts().filter_by(banned=0).paginate( page, app.config['POSTS_PER_PAGE'], False) next_url = url_for('index', page=posts.next_num) \ if posts.has_next else None prev_url = url_for('index', page=posts.prev_num) \ if posts.has_prev else None if current_user.banned: return render_template('banned.html') return render_template('index.html', title='Home', form=form, form2=form2, form0=form0, posts=posts.items, upvote=Upvote, badge_colour=badge_colour, next_url=next_url, prev_url=prev_url)
def reported_general(): form0 = SearchProfileForm() form = EmptyForm() if current_user.id == 1: page = request.args.get('page', 1, type=int) reports = Report.query.filter(Report.page_of_report != None).paginate( page, app.config['POSTS_PER_PAGE'], False) next_url = url_for('reported_general', page = reports.next_num) \ if reports.has_next else None prev_url = url_for('reported_general', page = reports.prev_num)\ if reports.has_prev else None return render_template('reported_general.html', reports=reports.items, form=form, prev_url=prev_url, next_url=next_url, badge_colour=badge_colour, title='Reported General', form0=form0) return render_template('admin_restricted.html')
def reported_users(): form0 = SearchProfileForm() form = EmptyForm() form2 = BanForm() if current_user.id == 1: page = request.args.get('page', 1, type=int) users = User.query.filter(User.reports_on_me != None).paginate( page, app.config['POSTS_PER_PAGE'], False) next_url = url_for('reported_users', page=users.next_num) \ if users.has_next else None prev_url = url_for('reported_users', page=users.prev_num) \ if users.has_prev else None return render_template('reported_users.html', title='Reported Users', badge_colour=badge_colour, users=users.items, form=form, form2=form2, next_url=next_url, prev_url=prev_url, form0=form0) return render_template('admin_restricted.html')
def reported_comments(): form0 = SearchProfileForm() form = EmptyForm() form2 = BanForm() if current_user.id == 1: page = request.args.get('page', 1, type=int) comments = Comment.query.filter(Comment.reports != None).paginate( page, app.config['POSTS_PER_PAGE'], False) next_url = url_for('reported_cases', page=comments.next_num) \ if comments.has_next else None prev_url = url_for('reported_cases', page=comments.prev_num) \ if comments.has_prev else None return render_template('reported_comments.html', title='Reported Posts', comments=comments.items, form=form, form2=form2, form0=form0, badge_colour=badge_colour, next_url=next_url, prev_url=prev_url) return render_template('admin_restricted.html')
def user(username): form0 = SearchProfileForm() form = EmptyForm() #Follow/Unfollow button form2 = BanForm() user = User.query.filter_by(username=username).first_or_404() page = request.args.get('page', 1, type=int) posts = user.own_posts().paginate(page, app.config['POSTS_PER_PAGE'], False) next_url = url_for('user', username=user.username, page=posts.next_num) \ if posts.has_next else None prev_url = url_for('user', username=user.username, page=posts.prev_num) \ if posts.has_prev else None if user.banned and current_user.id != 1: return render_template('banned.html') return render_template('user.html', user=user, form=form, form2=form2, form0=form0, posts=posts.items, upvote=Upvote, badge_colour=badge_colour, next_url=next_url, prev_url=prev_url)