def post_index(page_num=1): query = Post.query search = request.args.get('q') if search: query = query \ .filter((Post.body.contains(search)) | (Post.title.contains(search))) posts = query \ .filter(Post.status == Post.STATUS_PUBLIC) \ .order_by(Post.created_timestamp.desc()) \ .paginate(page_num, current_app.config['RABIANG_POSTS_PER_PAGE'], False) title = gettext('Blog') + ' - ' + \ current_app.config['RABIANG_SITE_NAME'] breadcrumbs = [{ 'text': gettext('Home'), 'href': url_for('main.index'), }, { 'text': gettext('Blog'), 'href': False, }] sidebar = sidebar_data() return render_template( current_app.config['RABIANG_SITE_THEME'] + '/page/post_index.html', posts=posts, title=title, breadcrumbs=breadcrumbs, sidebar=sidebar)
def tag_detail(tag_name, page_num=1): tag = Tag.query \ .filter(Tag.name == tag_name) \ .first_or_404() posts = tag.posts \ .order_by(Post.created_timestamp.desc()) \ .paginate(page_num, current_app.config['RABIANG_POSTS_PER_PAGE'], False) title = gettext('Tag') + ' - ' + tag_name + ' - ' + \ current_app.config['RABIANG_SITE_NAME'] breadcrumbs = [{ 'text': gettext('Home'), 'href': url_for('main.index'), }, { 'text': gettext('Blog'), 'href': url_for('page.post_index'), }, { 'text': gettext('Tag'), 'href': url_for('page.tag_index'), }, { 'text': tag_name, 'href': False, }] sidebar = sidebar_data() return render_template( current_app.config['RABIANG_SITE_THEME'] + '/page/tag_name.html', posts=posts, title=title, breadcrumbs=breadcrumbs, sidebar=sidebar)
def tag_index(): tags = Tag.query \ .add_columns(db.func.count(Tag.id)) \ .join(post_tag) \ .join(Post) \ .filter(Post.status == Post.STATUS_PUBLIC) \ .group_by(Tag.id) \ .order_by(Tag.name) \ .all() title = gettext('Tag') + ' - ' + \ current_app.config['RABIANG_SITE_NAME'] breadcrumbs = [{ 'text': gettext('Home'), 'href': url_for('main.index'), }, { 'text': gettext('Blog'), 'href': url_for('page.post_index'), }, { 'text': gettext('Tag'), 'href': False, }] sidebar = sidebar_data() return render_template( current_app.config['RABIANG_SITE_THEME'] + '/page/tag.html', tags=tags, title=title, breadcrumbs=breadcrumbs, sidebar=sidebar)
def post_index(page_num=1): query = Post.query search = request.args.get('q') if search: query = query \ .filter((Post.body.contains(search)) | (Post.title.contains(search))) posts = query \ .filter(Post.status == Post.STATUS_PUBLIC) \ .order_by(Post.created_timestamp.desc()) \ .paginate(page_num, current_app.config['RABIANG_POSTS_PER_PAGE'], False) title = gettext('Blog') + ' - ' + \ current_app.config['RABIANG_SITE_NAME'] breadcrumbs = [{ 'text': gettext('Home'), 'href': url_for('main.index'), }, { 'text': gettext('Blog'), 'href': False, }] sidebar = sidebar_data() return render_template(current_app.config['RABIANG_SITE_THEME'] + '/page/post_index.html', posts=posts, title=title, breadcrumbs=breadcrumbs, sidebar=sidebar)
def category_index(): categories = build_tree_dictionary(PageCategory) title = gettext('Category') + ' - ' + \ current_app.config['RABIANG_SITE_NAME'] breadcrumbs = [{ 'text': gettext('Home'), 'href': url_for('main.index'), }, { 'text': gettext('Blog'), 'href': url_for('page.post_index'), }, { 'text': gettext('Category'), 'href': False, }] sidebar = sidebar_data() return render_template(current_app.config['RABIANG_SITE_THEME'] + '/page/category_index.html', categories=categories, title=title, breadcrumbs=breadcrumbs, sidebar=sidebar)
def post_month_index(year, month, page_num=1): posts = Post.query \ .filter((db.func.extract('year', Post.created_timestamp) == year) & (db.func.extract('month', Post.created_timestamp) == month)) \ .order_by(Post.created_timestamp.desc()) \ .paginate(page_num, current_app.config['RABIANG_POSTS_PER_PAGE'], False) title = gettext('Blog Archives') + ' - ' + \ current_app.config['RABIANG_SITE_NAME'] breadcrumbs = [{ 'text': gettext('Home'), 'href': url_for('main.index'), }, { 'text': gettext('Blog'), 'href': url_for('page.post_index'), }, { 'text': gettext('Blog Archives'), 'href': False, }] sidebar = sidebar_data() return render_template(current_app.config['RABIANG_SITE_THEME'] + '/page/post_month_index.html', posts=posts, title=title, breadcrumbs=breadcrumbs, sidebar=sidebar)
def post_month_index(year, month, page_num=1): posts = Post.query \ .filter((db.func.extract('year', Post.created_timestamp) == year) & (db.func.extract('month', Post.created_timestamp) == month)) \ .order_by(Post.created_timestamp.desc()) \ .paginate(page_num, current_app.config['RABIANG_POSTS_PER_PAGE'], False) title = gettext('Blog Archives') + ' - ' + \ current_app.config['RABIANG_SITE_NAME'] breadcrumbs = [{ 'text': gettext('Home'), 'href': url_for('main.index'), }, { 'text': gettext('Blog'), 'href': url_for('page.post_index'), }, { 'text': gettext('Blog Archives'), 'href': False, }] sidebar = sidebar_data() return render_template( current_app.config['RABIANG_SITE_THEME'] + '/page/post_month_index.html', posts=posts, title=title, breadcrumbs=breadcrumbs, sidebar=sidebar)
def category_index(): categories = build_tree_dictionary(PageCategory) title = gettext('Category') + ' - ' + \ current_app.config['RABIANG_SITE_NAME'] breadcrumbs = [{ 'text': gettext('Home'), 'href': url_for('main.index'), }, { 'text': gettext('Blog'), 'href': url_for('page.post_index'), }, { 'text': gettext('Category'), 'href': False, }] sidebar = sidebar_data() return render_template( current_app.config['RABIANG_SITE_THEME'] + '/page/category_index.html', categories=categories, title=title, breadcrumbs=breadcrumbs, sidebar=sidebar)
def post_detail_slug(slug): post = Post.query \ .filter(Post.slug == slug) \ .first_or_404() if post.status != Post.STATUS_PUBLIC \ and (not current_user.is_authenticated or current_user.id != post.author_id): return render_template(current_app.config['RABIANG_SITE_THEME'] + '/404.html'), 404 form = CommentForm() if form.validate_on_submit(): comment = Comment() comment.name = form.name.data comment.email = form.email.data comment.body = form.body.data comment.ip_address = request.remote_addr comment.post_id = post.id db.session.add(comment) db.session.commit() flash(gettext('Your comment has been published.'), 'success') return redirect(url_for('page.post_detail_slug', slug=post.slug)) comments = post.comments \ .order_by(Comment.created_timestamp.asc()) \ .all() title = post.title + ' - ' + \ current_app.config['RABIANG_SITE_NAME'] breadcrumbs = [{ 'text': gettext('Home'), 'href': url_for('main.index'), }, { 'text': gettext('Blog'), 'href': url_for('page.post_index'), }, { 'text': post.title, 'href': False, }] sidebar = sidebar_data() return render_template(current_app.config['RABIANG_SITE_THEME'] + '/page/post_detail.html', post=post, Post=Post, form=form, comments=comments, title=title, breadcrumbs=breadcrumbs, sidebar=sidebar)
def post_detail_slug(slug): post = Post.query \ .filter(Post.slug == slug) \ .first_or_404() if post.status != Post.STATUS_PUBLIC \ and (not current_user.is_authenticated or current_user.id != post.author_id): return render_template(current_app.config['RABIANG_SITE_THEME'] + '/404.html'), 404 form = CommentForm() if form.validate_on_submit(): comment = Comment() comment.name = form.name.data comment.email = form.email.data comment.body = form.body.data comment.ip_address = request.remote_addr comment.post_id = post.id db.session.add(comment) db.session.commit() flash(gettext('Your comment has been published.'), 'success') return redirect(url_for('page.post_detail_slug', slug=post.slug)) comments = post.comments \ .order_by(Comment.created_timestamp.asc()) \ .all() title = post.title + ' - ' + \ current_app.config['RABIANG_SITE_NAME'] breadcrumbs = [{ 'text': gettext('Home'), 'href': url_for('main.index'), }, { 'text': gettext('Blog'), 'href': url_for('page.post_index'), }, { 'text': post.title, 'href': False, }] sidebar = sidebar_data() return render_template( current_app.config['RABIANG_SITE_THEME'] + '/page/post_detail.html', post=post, Post=Post, form=form, comments=comments, title=title, breadcrumbs=breadcrumbs, sidebar=sidebar)
def post_edit(post_id): post = Post.query.get_or_404(post_id) if not current_user.is_authenticated or current_user.id != post.author_id: return render_template(current_app.config['RABIANG_SITE_THEME'] + '/404.html'), 404 form = PostForm(obj=post) form.category.choices = build_tree_tuple_list(PageCategory, prefix=True) if form.validate_on_submit(): post.title = form.title.data post.slug = form.slug.data post.body = clean_and_linkify(form.body.data) post.status = form.status.data # post.format = current_app.config['RABIANG_POST_HTML_FORMAT'] post.category_id = form.category.data post.tags = form.tags.data post.author = current_user db.session.add(post) db.session.commit() flash(gettext('You edited your post.'), 'success') return redirect(url_for('page.post_detail_slug', slug=post.slug)) # Set default category option when written form.category.data = post.category_id if post.category_id else 0 title = gettext('Edit') + ' - ' + \ current_app.config['RABIANG_SITE_NAME'] breadcrumbs = [{ 'text': gettext('Home'), 'href': url_for('main.index'), }, { 'text': gettext('Blog'), 'href': url_for('page.post_index'), }, { 'text': '{} - {}'.format(gettext('Edit'), post.title), 'href': False, }] sidebar = sidebar_data() return render_template(current_app.config['RABIANG_SITE_THEME'] + '/page/post_edit.html', form=form, post=post, Post=Post, title=title, breadcrumbs=breadcrumbs, sidebar=sidebar)
def post_edit(post_id): post = Post.query.get_or_404(post_id) if not current_user.is_authenticated or current_user.id != post.author_id: return render_template(current_app.config['RABIANG_SITE_THEME'] + '/404.html'), 404 form = PostForm(obj=post) form.category.choices = build_tree_tuple_list(PageCategory, prefix=True) if form.validate_on_submit(): post.title = form.title.data post.slug = form.slug.data post.body = clean_and_linkify(form.body.data) post.status = form.status.data # post.format = current_app.config['RABIANG_POST_HTML_FORMAT'] post.category_id = form.category.data post.tags = form.tags.data post.author = current_user db.session.add(post) db.session.commit() flash(gettext('You edited your post.'), 'success') return redirect(url_for('page.post_detail_slug', slug=post.slug)) # Set default category option when written form.category.data = post.category_id if post.category_id else 0 title = gettext('Edit') + ' - ' + \ current_app.config['RABIANG_SITE_NAME'] breadcrumbs = [{ 'text': gettext('Home'), 'href': url_for('main.index'), }, { 'text': gettext('Blog'), 'href': url_for('page.post_index'), }, { 'text': '{} - {}'.format(gettext('Edit'), post.title), 'href': False, }] sidebar = sidebar_data() return render_template( current_app.config['RABIANG_SITE_THEME'] + '/page/post_edit.html', form=form, post=post, Post=Post, title=title, breadcrumbs=breadcrumbs, sidebar=sidebar)
def category_detail(category_name, page_num=1): category = PageCategory.query \ .filter(PageCategory.name == category_name) \ .first_or_404() categories = build_tree_list(PageCategory, category) # Lookup posts with specified category and its descendants. category_names = [category.name] category_names.extend([category.name for category in categories]) posts = Post.query \ .filter(Post.status == Post.STATUS_PUBLIC) \ .join(PageCategory) \ .filter(PageCategory.name.in_(category_names)) \ .order_by(Post.created_timestamp.desc()) \ .paginate(page_num, current_app.config['RABIANG_POSTS_PER_PAGE'], False) title = gettext('Category') + ' - ' + \ current_app.config['RABIANG_SITE_NAME'] breadcrumbs = [{ 'text': gettext('Home'), 'href': url_for('main.index'), }, { 'text': gettext('Blog'), 'href': url_for('page.post_index'), }, { 'text': gettext('Category'), 'href': url_for('page.category_index'), }, { 'text': category_name, 'href': False, }] sidebar = sidebar_data() return render_template( current_app.config['RABIANG_SITE_THEME'] + '/page/category_detail.html', title=title, posts=posts, breadcrumbs=breadcrumbs, sidebar=sidebar)
def category_detail(category_name, page_num=1): category = PageCategory.query \ .filter(PageCategory.name == category_name) \ .first_or_404() categories = build_tree_list(PageCategory, category) # Lookup posts with specified category and its descendants. category_names = [category.name] category_names.extend([category.name for category in categories]) posts = Post.query \ .filter(Post.status == Post.STATUS_PUBLIC) \ .join(PageCategory) \ .filter(PageCategory.name.in_(category_names)) \ .order_by(Post.created_timestamp.desc()) \ .paginate(page_num, current_app.config['RABIANG_POSTS_PER_PAGE'], False) title = gettext('Category') + ' - ' + \ current_app.config['RABIANG_SITE_NAME'] breadcrumbs = [{ 'text': gettext('Home'), 'href': url_for('main.index'), }, { 'text': gettext('Blog'), 'href': url_for('page.post_index'), }, { 'text': gettext('Category'), 'href': url_for('page.category_index'), }, { 'text': category_name, 'href': False, }] sidebar = sidebar_data() return render_template(current_app.config['RABIANG_SITE_THEME'] + '/page/category_detail.html', title=title, posts=posts, breadcrumbs=breadcrumbs, sidebar=sidebar)
def post_delete(post_id): post = Post.query.get_or_404(post_id) if not current_user.is_authenticated or current_user.id != post.author_id: return render_template(current_app.config['RABIANG_SITE_THEME'] + '/404.html'), 404 form = DeletePostForm() if form.validate_on_submit(): post.status = Post.STATUS_DELETED db.session.add(post) db.session.commit() flash(gettext('You deleted your post.'), 'success') return redirect(url_for('page.post_index')) title = gettext('Delete') + ' - ' + \ current_app.config['RABIANG_SITE_NAME'] breadcrumbs = [{ 'text': gettext('Home'), 'href': url_for('main.index'), }, { 'text': gettext('Blog'), 'href': url_for('page.post_index'), }, { 'text': '{} - {}'.format(gettext('Delete'), post.title), 'href': False, }] sidebar = sidebar_data() return render_template(current_app.config['RABIANG_SITE_THEME'] + '/page/post_delete.html', form=form, post=post, title=title, breadcrumbs=breadcrumbs, sidebar=sidebar)
def post_delete(post_id): post = Post.query.get_or_404(post_id) if not current_user.is_authenticated or current_user.id != post.author_id: return render_template(current_app.config['RABIANG_SITE_THEME'] + '/404.html'), 404 form = DeletePostForm() if form.validate_on_submit(): post.status = Post.STATUS_DELETED db.session.add(post) db.session.commit() flash(gettext('You deleted your post.'), 'success') return redirect(url_for('page.post_index')) title = gettext('Delete') + ' - ' + \ current_app.config['RABIANG_SITE_NAME'] breadcrumbs = [{ 'text': gettext('Home'), 'href': url_for('main.index'), }, { 'text': gettext('Blog'), 'href': url_for('page.post_index'), }, { 'text': '{} - {}'.format(gettext('Delete'), post.title), 'href': False, }] sidebar = sidebar_data() return render_template( current_app.config['RABIANG_SITE_THEME'] + '/page/post_delete.html', form=form, post=post, title=title, breadcrumbs=breadcrumbs, sidebar=sidebar)
def post_user_index(username, page_num=1): author = User.query \ .filter(User.username == username) \ .first_or_404() query = author.posts if current_user.is_authenticated and current_user.id == author.id: query = query.filter((Post.status == Post.STATUS_PUBLIC) | (Post.status == Post.STATUS_DRAFT)) else: query = query.filter(Post.status == Post.STATUS_PUBLIC) posts = query \ .order_by(Post.created_timestamp.desc()) \ .paginate(page_num, current_app.config['RABIANG_POSTS_PER_PAGE'], False) title = username + ' - ' + \ current_app.config['RABIANG_SITE_NAME'] breadcrumbs = [{ 'text': gettext('Home'), 'href': url_for('main.index'), }, { 'text': gettext('Blog'), 'href': url_for('page.post_index'), }, { 'text': username, 'href': False, }] sidebar = sidebar_data() return render_template(current_app.config['RABIANG_SITE_THEME'] + '/page/post_user_index.html', posts=posts, title=title, breadcrumbs=breadcrumbs, sidebar=sidebar)
def post_user_index(username, page_num=1): author = User.query \ .filter(User.username == username) \ .first_or_404() query = author.posts if current_user.is_authenticated and current_user.id == author.id: query = query.filter((Post.status == Post.STATUS_PUBLIC) | (Post.status == Post.STATUS_DRAFT)) else: query = query.filter(Post.status == Post.STATUS_PUBLIC) posts = query \ .order_by(Post.created_timestamp.desc()) \ .paginate(page_num, current_app.config['RABIANG_POSTS_PER_PAGE'], False) title = username + ' - ' + \ current_app.config['RABIANG_SITE_NAME'] breadcrumbs = [{ 'text': gettext('Home'), 'href': url_for('main.index'), }, { 'text': gettext('Blog'), 'href': url_for('page.post_index'), }, { 'text': username, 'href': False, }] sidebar = sidebar_data() return render_template( current_app.config['RABIANG_SITE_THEME'] + '/page/post_user_index.html', posts=posts, title=title, breadcrumbs=breadcrumbs, sidebar=sidebar)
def category_delete(category_id): page_category = PageCategory.query.get_or_404(category_id) form = DeleteCategoryForm() if form.validate_on_submit(): db.session.delete(page_category) db.session.commit() flash(gettext('You deleted the category.'), 'success') return redirect(url_for('page.category_create')) title = gettext('Delete') + ' - ' + \ current_app.config['RABIANG_SITE_NAME'] breadcrumbs = [{ 'text': gettext('Home'), 'href': url_for('main.index'), }, { 'text': gettext('Blog'), 'href': url_for('page.post_index'), }, { 'text': '{} - {}'.format(gettext('Delete a category'), page_category.name), 'href': False, }] sidebar = sidebar_data() return render_template(current_app.config['RABIANG_SITE_THEME'] + '/page/category_delete.html', form=form, page_category=page_category, title=title, breadcrumbs=breadcrumbs, sidebar=sidebar)
def post_upload(): form = PhotoForm() if form.validate_on_submit(): filename = secure_filename(form.photo.data.filename) form.photo.data.save( os.path.join(current_app.config['RABIANG_IMAGE_FOLDER'], filename)) flash(gettext('You uploaded a file.'), 'success') return redirect(url_for('page.post_upload')) else: filename = None title = gettext('Image Upload') + ' - ' + \ current_app.config['RABIANG_SITE_NAME'] breadcrumbs = [{ 'text': gettext('Home'), 'href': url_for('main.index'), }, { 'text': gettext('Blog'), 'href': url_for('page.post_index'), }, { 'text': gettext('Upload'), 'href': False, }] sidebar = sidebar_data() return render_template( current_app.config['RABIANG_SITE_THEME'] + '/page/upload.html', form=form, filename=filename, title=title, breadcrumbs=breadcrumbs, sidebar=sidebar)
def post_upload(): form = PhotoForm() if form.validate_on_submit(): filename = secure_filename(form.photo.data.filename) form.photo.data.save( os.path.join(current_app.config['RABIANG_IMAGE_FOLDER'], filename)) flash(gettext('You uploaded a file.'), 'success') return redirect(url_for('page.post_upload')) else: filename = None title = gettext('Image Upload') + ' - ' + \ current_app.config['RABIANG_SITE_NAME'] breadcrumbs = [{ 'text': gettext('Home'), 'href': url_for('main.index'), }, { 'text': gettext('Blog'), 'href': url_for('page.post_index'), }, { 'text': gettext('Upload'), 'href': False, }] sidebar = sidebar_data() return render_template(current_app.config['RABIANG_SITE_THEME'] + '/page/upload.html', form=form, filename=filename, title=title, breadcrumbs=breadcrumbs, sidebar=sidebar)
def category_delete(category_id): page_category = PageCategory.query.get_or_404(category_id) form = DeleteCategoryForm() if form.validate_on_submit(): db.session.delete(page_category) db.session.commit() flash(gettext('You deleted the category.'), 'success') return redirect(url_for('page.category_create')) title = gettext('Delete') + ' - ' + \ current_app.config['RABIANG_SITE_NAME'] breadcrumbs = [{ 'text': gettext('Home'), 'href': url_for('main.index'), }, { 'text': gettext('Blog'), 'href': url_for('page.post_index'), }, { 'text': '{} - {}'.format(gettext('Delete a category'), page_category.name), 'href': False, }] sidebar = sidebar_data() return render_template( current_app.config['RABIANG_SITE_THEME'] + '/page/category_delete.html', form=form, page_category=page_category, title=title, breadcrumbs=breadcrumbs, sidebar=sidebar)