def new_blog(): if not current_user.is_admin: return render_extensions('401.html') if request.method == 'POST': try: content = str(request.form['content']) except Exception: content = '' try: slug = str(request.form['slug']) except Exception: slug = '' try: title = str(request.form['title']) except Exception: title = '' post = Post(title=title, body=content, slug=slug) post.save() current_user.posts.append(post) current_user.save() return render_extensions('admin/new_blog.html')
def edit_blog(blog_id): if not current_user.is_admin: return render_extensions('401.html') if request.method == 'POST': try: content = str(request.form['content']) except Exception: content = '' try: slug = str(request.form['slug']) except Exception: slug = '' try: title = str(request.form['title']) except Exception: title = '' post = Post(title=title, body=content, slug=slug) post.save() current_user.posts.append(post) current_user.save() post_obj = Post.query.filter_by(id=int(blog_id)).first() post_content = { 'title': str(post_obj.title), 'slug': str(post_obj.slug), 'body': str(post_obj.body), } return render_extensions('admin/edit_blog.html', post=post_content)
def blog_page(page=None): """ :param page: :return: """ page = int(page) _page_size = 3 # TODO: move into settings if page is None or page <= 0: next_page = 0 prev_page = 1 current = True else: next_page = page - 1 prev_page = page + 1 current = False posts = get_page(_page_size, page) return render_extensions("blog/blog_page.html", posts=posts, next_page=next_page, prev_page=prev_page, current=current)
def blog_detail(pk): """ :param pk: :return: """ post = get_post_detail(int(pk)) return render_extensions("blog/blog_detail.html", post=post)
def change_username(): form = UsernameForm() if form.validate_on_submit(): current_user.username = form.username.data current_user.save() return redirect(url_for('user.profile')) else: flash_errors(form) return render_extensions('users/change_username.html', resetform=form)
def change_password(): form = PasswordForm() if form.validate_on_submit(): current_user.set_password(form.password.data) current_user.save() return redirect(url_for('user.profile')) else: flash_errors(form) return render_extensions('users/change_password.html', resetform=form)
def home(): form = LoginForm(request.form) # Handle logging in if request.method == 'POST': if form.validate_on_submit(): login_user(form.user) flash("You are logged in.", 'success') redirect_url = request.args.get("next") or url_for("user.profile") return redirect(redirect_url) else: flash_errors(form) return render_extensions("public/home.html", form=form)
def register(): form = RegisterForm(request.form, csrf_enabled=False) if form.validate_on_submit(): new_user = User.create(username=form.username.data, first_name=form.first_name.data, last_name=form.last_name.data, email=form.email.data, password=form.password.data, active=True) flash("Thank you for registering. You can now log in.", 'success') return redirect(url_for('public.home')) else: flash_errors(form) return render_extensions('public/register.html', form=form)
def reset_with_token(token): try: from dice.settings import Config ts = URLSafeTimedSerializer(Config.SECRET_KEY) email = ts.loads(token, salt="recover-key", max_age=86400) except: return render_template("404.html") form = PasswordForm() if form.validate_on_submit(): emailuser = User.query.filter_by(email=email).first_or_404() emailuser.set_password(form.password.data) emailuser.save() return redirect(url_for('public.home')) else: flash_errors(form) return render_extensions('users/reset_with_token.html', resetform=form, token=token)
def reset(): form = EmailForm() if form.validate_on_submit(): emailuser = User.query.filter_by(email=form.email.data).first_or_404() subject = "Password reset requested" from dice.settings import Config ts = URLSafeTimedSerializer(Config.SECRET_KEY) token = ts.dumps(emailuser.email, salt='recover-key') recover_url = url_for('user.reset_with_token', token=token, _external=True) html = render_template('email/recover.html', recover_url=recover_url) msg = Message(html=html, recipients=[emailuser.email], subject=subject) mail.send(msg) return redirect(url_for('public.home')) else: flash_errors(form) return render_extensions('users/reset.html', resetform=form)
def about(): form = LoginForm(request.form) return render_extensions("public/about.html", form=form)
def unsubscribe(): return render_extensions('users/unsubscribe.html')
def profile(): return render_extensions("users/profile.html")