def account(): '''Account details page for updating info.''' report_form = ReportForm() update_form = UpdateInfoForm() user = current_user if update_form.validate_on_submit(): if bcrypt.check_password_hash(user.password, update_form.current_pass.data): user.username = update_form.username.data user.real_name = update_form.real_name.data user.email_personal = update_form.email_personal.data user.email_company = update_form.email_company.data db.session.commit() flash('Update successful.', 'success') return redirect(url_for('main.home')) else: flash('Please check your credentials.', 'warning') return redirect(url_for('user.account')) elif request.method == 'GET': update_form.username.data = user.username update_form.real_name.data = user.real_name update_form.email_personal.data = user.email_personal update_form.email_company.data = user.email_company else: flash('Please check that no fields are empty.', 'warning') return render_template('account.html', title='Account Settings', update_form=update_form, report_form=report_form)
def reports(): '''Display all submitted reports.''' report_form = ReportForm() page = request.args.get('page', 1, type=int) reports = Report.query.order_by(Report.date_posted.desc())\ .paginate(per_page=5, page=page) return render_template('report.html', report_form=report_form, posts=reports)
def find_post(post_id): '''Find and display a specific post.''' report_form = ReportForm() post = Post.query.get_or_404(post_id) previous = request.referrer if previous == None: previous = url_for('main.home') return render_template('post.html', post=post, previous=previous, report_form=report_form)
def blog(): '''Default blog view, shows all posts.''' report_form = ReportForm() page = request.args.get('page', 1, type=int) posts = Post.query.order_by(Post.date_posted.desc()).paginate(per_page=3, page=page) return render_template('blog.html', title='Blog', posts=posts, page=page, report_form=report_form)
def report(): '''Report contact form for website issues.''' report_form = ReportForm(request.form) if report_form.validate_on_submit(): title = report_form.title.data email = report_form.email.data content = report_form.content.data date = datetime.utcnow() report = Report( title = title, email = email, content = content, date_posted = date ) send_report_notification(title, content, email, date) db.session.add(report) db.session.commit() flash('Report submitted succesfully. Thank you!', 'success') else: flash(f'Report submission failed. Please check your email address.', 'warning') return redirect(request.referrer)
def contact(): '''Render the contact page, display contact form.''' report_form = ReportForm() contact_form = ContactForm() if contact_form.validate_on_submit(): name = contact_form.name.data email = contact_form.email.data title = contact_form.title.data content = contact_form.content.data send_contact_notification(name, title, email, content) flash('Message sent!', 'success') return redirect(url_for('main.contact')) return render_template('contact.html', title='Contact', contact_form=contact_form, report_form=report_form)
def edit_post(post_id): '''Edit an existing post.''' report_form = ReportForm() post = Post.query.get_or_404(post_id) if request.method == 'POST': form = request.form if form['title'] and form['tags'] and form['content']: post.title = form['title'] post.content = form['content'] post.tags = form['tags'] db.session.commit() flash('Update succesful.', 'success') return redirect(url_for('post.find_post', post_id=post.id)) else: flash('Please check your input.', 'warning') return redirect(url_for('edit_post', post_id=post.id)) return render_template('editor.html', form=post, report_form=report_form)
def editor(): '''Create new posts, edit existing ones.''' form = 0 report_form = ReportForm() if request.method == 'POST': form = request.form if form['title'] and form['content'] and form['tags']: post = Post(title=form['title'], content=form['content'], tags=form['tags'], author=current_user) db.session.add(post) db.session.commit() flash('New post posted!', 'success') return redirect(url_for('post.blog')) else: flash('Check your content fields!', 'warning') redirect(url_for('post.blog', form=form)) return render_template('editor.html', form=form, report_form=report_form)
def login(): '''Checks given credentials against database, logs user in if correct.''' if current_user.is_authenticated: return redirect(url_for('main.home')) login_form = LoginForm() report_form = ReportForm() if login_form.validate_on_submit(): user = User.query.filter_by(username=login_form.username.data).first() if user and bcrypt.check_password_hash(user.password, login_form.password.data): login_user(user) next_page = request.args.get('next') flash('Welcome back.', 'success') return redirect(next_page) if next_page else redirect( url_for('main.home')) else: flash('Please check your credentials.', 'warning') return render_template('login.html', title='Login', login_form=login_form, report_form=report_form)
def sort_by_tags(): '''Filter blog posts by given tag query parameters.''' report_form = ReportForm() page = request.args.get('page', 1, type=int) month = request.args.get('month', 0, type=int) tags = request.args.get('tags', '', type=str) if month and tags: posts = Post.query.filter(extract('month', Post.date_posted)==month)\ .filter(Post.tags.contains(tags))\ .order_by(Post.date_posted.desc())\ .paginate(per_page=3, page=page) return render_template('blog.html', report_form=report_form, page=page, posts=posts) elif tags and month == 0: posts = Post.query.filter(Post.tags.contains(tags))\ .order_by(Post.date_posted.desc())\ .paginate(per_page=3, page=page) return render_template('blog.html', report_form=report_form, posts=posts) elif month and tags == '': posts = Post.query.filter(extract('month', Post.date_posted)==month)\ .order_by(Post.date_posted.desc())\ .paginate(per_page=3, page=page) return render_template('blog.html', report_form=report_form, page=page, posts=posts) else: posts = Post.query.order_by(Post.date_posted.desc()).paginate( per_page=3, page=page) return render_template('blog.html', report_form=report_form, page=page, posts=posts)
def error_404(error): '''404 error handler''' report_form = ReportForm() return render_template('errors/404.html', report_form=report_form), 404
def error_500(error): '''500 error handler''' report_form = ReportForm() return render_template('errors/500.html', report_form=report_form), 500
def profile(): '''Render the porfolio page.''' report_form = ReportForm() return render_template('profile.html', title='Profile', report_form=report_form)
def home(): '''Render home page.''' page = request.args.get('page', 1, type=int) posts = Post.query.order_by(Post.date_posted.desc()).paginate(per_page=2, page=page) report_form = ReportForm() return render_template('home.html', posts=posts, report_form=report_form), 200