How to use post_form method in Splinter

Best Python code snippet using splinter

post.py

Source:post.py Github

copy

Full Screen

1from datetime import datetime2import flask3from flask_security import login_required, current_user4from sqlalchemy import func5from slugify import slugify6from flask_babelex import gettext as _7from app.model.models import Post, Category, Tag, Archive, Collection, Comment, About8from app.form.forms import PostForm, OperateForm, CreateForm, CreateTopicForm9from app.controller.extensions import db10from app.utils.common import redirect_back11from app.utils.decorator import permission_required12from app.controller.signals import post_visited13from app.controller.md_ext import md14posts_bp = flask.Blueprint('posts', __name__, url_prefix='/')15@posts_bp.route('/', methods=['GET'])16def posts():17 page = int(flask.request.args.get('page', 1))18 pagination = Post.query.filter_by(is_draft=False).\19 order_by(Post.is_top.desc()).\20 order_by(Post.timestamp.desc()).\21 paginate(page=page, per_page=10)22 count_post_nums = db.session.query(func.count(Post.id)).filter_by(is_draft=False).scalar()23 if pagination.page == 1:24 posts_template = "index.html"25 else:26 posts_template = "posts.html"27 return flask.render_template(28 posts_template,29 pagination=pagination,30 posts=pagination.items,31 count_post_nums=count_post_nums32 )33@posts_bp.route('/archives', methods=['GET'])34def archives():35 page = int(flask.request.args.get('page', 1))36 pagination = Archive.query.paginate(page=page, per_page=5)37 return flask.render_template(38 "archive.html",39 pagination=pagination,40 archives=pagination.items41 )42@posts_bp.route('/tags', methods=['GET'])43def tags():44 tags = Tag.query.all()45 return flask.render_template("tags.html", tags=tags)46@posts_bp.route('/topics', methods=['GET'])47def topics():48 page = int(flask.request.args.get('page', 1))49 pagination = Collection.query.paginate(page=page, per_page=6)50 return flask.render_template(51 "topics.html",52 pagination=pagination,53 topics=pagination.items54 )55@posts_bp.route('/topic/<int:topic_id>', methods=['GET'])56def topic(topic_id):57 search_topic = Collection.query.get(topic_id)58 if not search_topic:59 flask.abort(404)60 page = int(flask.request.args.get('page', 1))61 pagination = Post.query.filter_by(is_draft=False, collection_id=topic_id). \62 order_by(Post.is_top.desc()). \63 order_by(Post.timestamp.desc()). \64 paginate(page=page, per_page=10)65 count_post_nums = db.session.query(func.count(Post.id)).filter_by(is_draft=False, collection_id=topic_id).scalar()66 return flask.render_template(67 "topic.html",68 pagination=pagination,69 posts=pagination.items,70 topic=search_topic,71 count_post_nums=count_post_nums72 )73@posts_bp.route('/tag/<int:tag_id>', methods=['GET'])74def tag(tag_id):75 search_tag = Tag.query.get(tag_id)76 if not search_tag:77 flask.abort(404)78 page = int(flask.request.args.get('page', 1))79 pagination = Post.query.filter_by(is_draft=False).filter(Post.tags.contains(search_tag)). \80 order_by(Post.is_top.desc()). \81 order_by(Post.timestamp.desc()). \82 paginate(page=page, per_page=10)83 count_post_nums = db.session.query(func.count(Post.id)).filter_by(is_draft=False).\84 filter(Post.tags.contains(search_tag)).scalar()85 posts_template = "posts.html"86 return flask.render_template(87 posts_template,88 pagination=pagination,89 posts=pagination.items,90 tag=search_tag,91 count_post_nums=count_post_nums92 )93@posts_bp.route('/category/<int:category_id>', methods=['GET'])94def category(category_id):95 search_category = Category.query.get(category_id)96 if not search_category:97 flask.abort(404)98 page = int(flask.request.args.get('page', 1))99 pagination = Post.query.filter_by(is_draft=False).filter_by(category=search_category). \100 order_by(Post.is_top.desc()). \101 order_by(Post.timestamp.desc()). \102 paginate(page=page, per_page=10)103 count_post_nums = db.session.query(func.count(Post.id)).filter_by(is_draft=False).\104 filter_by(category=search_category).scalar()105 posts_template = "posts.html"106 return flask.render_template(107 posts_template,108 pagination=pagination,109 posts=pagination.items,110 category=search_category,111 count_post_nums=count_post_nums112 )113@posts_bp.route('/archive/<int:archive_id>', methods=['GET'])114def archive(archive_id):115 search_archive = Archive.query.get(archive_id)116 if not search_archive:117 flask.abort(404)118 page = int(flask.request.args.get('page', 1))119 pagination = Post.query.filter_by(is_draft=False).filter_by(archive_id=archive_id). \120 order_by(Post.is_top.desc()). \121 order_by(Post.timestamp.desc()). \122 paginate(page=page, per_page=10)123 count_post_nums = db.session.query(func.count(Post.id)).filter_by(is_draft=False).\124 filter_by(archive_id=archive_id).scalar()125 posts_template = "posts.html"126 return flask.render_template(127 posts_template,128 pagination=pagination,129 posts=pagination.items,130 archive=search_archive,131 count_post_nums=count_post_nums132 )133@posts_bp.route('/drafts', methods=['GET'])134@login_required135@permission_required('ADMINISTER')136def draft():137 operate_form = OperateForm()138 page = int(flask.request.args.get('page', 1))139 pagination = Post.query.filter_by(is_draft=True).paginate(page=page, per_page=10)140 return flask.render_template(141 "drafts.html",142 pagination=pagination,143 drafts=pagination.items,144 operate_form=operate_form145 )146@posts_bp.route('/post/<post_slug>', methods=['GET'])147def post(post_slug):148 operate_form = OperateForm()149 post = Post.query.filter_by(slug=post_slug, is_draft=False).first_or_404()150 if not current_user.is_admin and post.is_privacy:151 flask.abort(403)152 post_visited.send(flask.current_app._get_current_object(), post=post)153 return flask.render_template(154 'post.html',155 post=post,156 operate_form=operate_form157 )158@posts_bp.route('/search', methods=['GET'])159def search_post():160 q = flask.request.args.get('q')161 page = int(flask.request.args.get('page', 1))162 pagination = Post.query.whooshee_search(q, order_by_relevance=0).paginate(page=page, per_page=10)163 count_post_nums = Post.query.whooshee_search(q, order_by_relevance=0).count()164 return flask.render_template(165 "posts.html",166 pagination=pagination,167 posts=pagination.items,168 count_post_nums=count_post_nums169 )170@posts_bp.route('/post/create', methods=['GET', 'POST'])171@login_required172@permission_required('ADMINISTER')173def create_post():174 post_form = PostForm()175 create_category_form = CreateForm()176 create_collection_form = CreateTopicForm()177 create_tag_form = CreateForm()178 if post_form.validate_on_submit():179 title = post_form.title.data180 category = post_form.category.data181 collection = post_form.collection.data182 background = post_form.background_image_url.data183 tag_names = post_form.tags.data184 deny_comment = post_form.deny_comment.data185 privacy = post_form.privacy.data186 top = post_form.top.data187 is_markdown = post_form.is_markdown.data188 if is_markdown:189 content = post_form.body.data190 body = md.convert(content)191 markdown = True192 else:193 body = post_form.body.data194 markdown = False195 slug = slugify(title, max_length=100)196 if Post.query.filter_by(slug=slug).first():197 flask.flash(_("This post name already exists, choose an other name."), category="warning")198 return redirect_back()199 post = Post(200 title=title,201 slug=slug,202 category=Category.query.filter_by(name=category).first(),203 collection=Collection.query.filter_by(name=collection).first(),204 tags=[Tag.query.filter_by(name=item).first() for item in tag_names],205 deny_comment=deny_comment,206 is_privacy=privacy,207 is_top=top,208 is_markdown=markdown,209 background=background,210 body=body,211 timestamp=datetime.now(),212 author=current_user213 )214 if is_markdown:215 post.content = content216 db.session.add(post)217 db.session.commit()218 if post_form.save_submit.data:219 post.is_draft = True220 db.session.commit()221 flask.flash(_("The Post has been saved as a Draft Successful!"), category="success")222 return flask.redirect(flask.url_for('posts.posts'))223 elif post_form.publish_submit.data:224 flask.flash(_("Create A New Post Successful!"), category="success")225 return flask.redirect(flask.url_for('posts.post', post_slug=slug))226 return flask.render_template(227 'create.html',228 form=post_form,229 create_category_form=create_category_form,230 create_collection_form=create_collection_form,231 create_tag_form=create_tag_form232 )233@posts_bp.route('/post/edit/<post_slug>', methods=['GET', 'POST'])234@login_required235@permission_required('ADMINISTER')236def edit_post(post_slug):237 post_form = PostForm()238 create_category_form = CreateForm()239 create_collection_form = CreateTopicForm()240 create_tag_form = CreateForm()241 search_post = Post.query.filter_by(slug=post_slug).first_or_404()242 is_markdown = search_post.is_markdown243 post_form.title.data = search_post.title244 old_slug = search_post.slug245 if search_post.category:246 post_form.category.data = search_post.category.name247 else:248 if Category.query.first():249 post_form.category.data = Category.query.first().name250 if search_post.collection:251 post_form.collection.data = search_post.collection.name252 else:253 post_form.collection.data = ""254 if search_post.tags:255 post_form.tags.data = [item.name for item in search_post.tags]256 else:257 if Tag.query.first():258 post_form.tags.data = [Tag.query.first().name]259 post_form.background_image_url.data = search_post.background260 post_form.deny_comment.data = search_post.deny_comment261 post_form.privacy.data = search_post.is_privacy262 post_form.top.data = search_post.is_top263 post_form.is_markdown.data = is_markdown264 if is_markdown:265 post_form.body.data = search_post.content266 else:267 post_form.body.data = search_post.body268 if post_form.validate_on_submit():269 title = flask.request.form.get('title')270 category_name = flask.request.form.get('category')271 collection_name = flask.request.form.get('collection')272 tag_names = flask.request.form.getlist('tags')273 background = flask.request.form.get('background_image_url')274 privacy = True if flask.request.form.get('privacy') else False275 top = True if flask.request.form.get('top') else False276 if is_markdown:277 content = flask.request.form.get('body')278 body = md.convert(content)279 else:280 body = flask.request.form.get('body')281 new_slug = slugify(title, max_length=100)282 if new_slug != old_slug and Post.query.filter_by(slug=new_slug).first():283 flask.flash(_("This post name already exists, choose an other name."), category="warning")284 return redirect_back()285 search_post.title = title286 search_post.slug = new_slug287 category = Category.query.filter_by(name=category_name).first()288 if category:289 search_post.category = category290 collection = Collection.query.filter_by(name=collection_name).first()291 if collection:292 search_post.collection = collection293 search_post.tags = [Tag.query.filter_by(name=item).first() for item in tag_names]294 search_post.background = background295 search_post.is_privacy = privacy296 search_post.is_top = top297 search_post.body = body298 # search_post.timestamp = datetime.now()299 if is_markdown:300 search_post.content = content301 if post_form.save_submit.data:302 search_post.is_draft = True303 db.session.commit()304 flask.flash(_("The Post has been saved as a Draft Successful!"), category="success")305 return flask.redirect(flask.url_for('posts.posts'))306 elif post_form.publish_submit.data:307 search_post.is_draft = False308 db.session.commit()309 flask.flash(_("Update and Publish The Post Successful!"), category="success")310 return flask.redirect(flask.url_for('posts.post', post_slug=new_slug))311 return flask.render_template(312 'edit.html',313 form=post_form,314 post=search_post,315 create_category_form=create_category_form,316 create_collection_form=create_collection_form,317 create_tag_form=create_tag_form318 )319@posts_bp.route('/post/delete/<post_slug>', methods=['POST'])320@login_required321@permission_required('ADMINISTER')322def delete_post(post_slug):323 search_post = Post.query.filter_by(slug=post_slug).first_or_404()324 db.session.delete(search_post)325 flask.flash(_("Delete The Post Successful!"), category="success")326 return flask.redirect(flask.url_for('posts.posts'))327@posts_bp.route('/post/category/new', methods=['POST'])328@login_required329@permission_required('ADMINISTER')330def create_category():331 category_name = flask.request.form.get('name')332 if Category.query.filter_by(name=category_name).first():333 flask.flash(_("This Category already exists!"), category="warning")334 return redirect_back()335 category = Category(name=category_name)336 db.session.add(category)337 db.session.commit()338 return redirect_back()339@posts_bp.route('/post/collection/new', methods=['POST'])340@login_required341@permission_required('ADMINISTER')342def create_collection():343 collection_name = flask.request.form.get('name')344 collection_description = flask.request.form.get('description')345 collection_background = flask.request.form.get('background')346 if Collection.query.filter_by(name=collection_name).first():347 flask.flash(_("This topic already exists!"), category="warning")348 return redirect_back()349 collection = Collection(350 name=collection_name,351 description=collection_description,352 background=collection_background353 )354 db.session.add(collection)355 db.session.commit()356 return redirect_back()357@posts_bp.route('/post/tag/new', methods=['POST'])358@login_required359@permission_required('ADMINISTER')360def create_tag():361 tag_name = flask.request.form.get('name')362 if Tag.query.filter_by(name=tag_name).first():363 flask.flash(_("This Tag already exists!"), category="warning")364 return redirect_back()365 tag = Tag(name=tag_name)366 db.session.add(tag)367 db.session.commit()368 return redirect_back()369@posts_bp.route('/post/create_comment/<post_slug>', methods=['POST'])370def create_comment(post_slug):371 search_post = Post.query.filter_by(slug=post_slug).first_or_404()372 author = flask.request.form.get("author")373 email = flask.request.form.get("email")374 body = flask.request.form.get("body")375 comment = Comment(376 author=author,377 email=email,378 body=body,379 post_id=search_post.id380 )381 db.session.add(comment)382 db.session.commit()383 return flask.redirect(flask.url_for('posts.post', post_slug=search_post.slug))384@posts_bp.route('/post/delete-comment/<comment_id>', methods=['POST'])385@login_required386@permission_required('ADMINISTER')387def delete_comment(comment_id):388 search_comment = Comment.query.get(comment_id)389 post_slug = search_comment.post.slug390 db.session.delete(search_comment)391 flask.flash(_("Delete The Comment Successful!"), category="success")392 return flask.redirect(flask.url_for('posts.post', post_slug=post_slug))393@posts_bp.route('/about')394def about():395 about = About.query.first()...

Full Screen

Full Screen

views.py

Source:views.py Github

copy

Full Screen

1from django.shortcuts import render, redirect2from django.core.paginator import Paginator3from django.db.models import Q4from .models import *5from django.utils.timezone import now6from .forms import *7from django.core.exceptions import ObjectDoesNotExist8def index(request):9 if request.GET.get('search'):10 search = request.GET.get('search')11 news_list = News.objects.filter(12 Q(content__icontains=search) | Q(author__username__contains=search) | Q(headline__icontains=search))[::-1]13 else:14 news_list = News.objects.all()[::-1]15 paginator = Paginator(news_list, 3)16 page = request.GET.get('page')17 news = paginator.get_page(page)18 context = {19 'news_list': news,20 }21 return render(request, 'blog/index.html', context)22def blog(request, blog_id):23 news = News.objects.get(id=blog_id)24 if request.method == 'POST':25 comment_form = CommentForm(request.POST)26 if comment_form.is_valid():27 comment = Comment(user=request.user,28 text=comment_form.cleaned_data['text'],29 news=news,30 pub_date=now())31 Comment.save(comment)32 else:33 comment_form = CommentForm()34 news_list = News.objects.order_by('-pub_date')[:9]35 comments = Comment.objects.select_related().filter(news=blog_id)36 context = {'news': news,37 'news_list': news_list,38 'comments': comments,39 'comment_form': comment_form}40 return render(request, 'blog/news.html', context)41def blogger(request, author_username):42 try:43 UserProfile.objects.get(user__username=author_username)44 except ObjectDoesNotExist:45 return redirect('index')46 blogger_profile = UserProfile.objects.get(user__username=author_username)47 post_list = News.objects.filter(author=blogger_profile.user)48 context = {49 'blogger': blogger_profile,50 'post_list': post_list,51 }52 return render(request, 'blog/blogger.html', context)53def bloggers(request):54 bloggers_list = UserProfile.objects.all()55 context = {56 'bloggers_list': bloggers_list57 }58 return render(request, 'blog/bloggers.html', context)59def profile(request):60 try:61 UserProfile.objects.get(user=request.user)62 except ObjectDoesNotExist:63 new_profile = UserProfile(user=request.user)64 new_profile.save()65 if request.method == 'POST' and 'bio_form' in request.POST:66 bio_form = BioForm(request.POST or None)67 if bio_form.is_valid():68 user_profile = UserProfile.objects.get(user=request.user)69 user_profile.bio = bio_form.cleaned_data['bio']70 user_profile.save()71 else:72 bio_form = BioForm()73 if request.method == 'POST' and 'avatar_form' in request.POST:74 avatar_form = AvatarForm(request.POST, request.FILES or None)75 if avatar_form.is_valid():76 user_profile = UserProfile.objects.get(user=request.user)77 user_profile.avatar = avatar_form.cleaned_data['avatar']78 user_profile.save()79 else:80 avatar_form = AvatarForm()81 context = {82 'bio_form': bio_form,83 'avatar_form': avatar_form,84 }85 return render(request, 'blog/profile.html', context)86def create_post(request):87 if request.method == 'POST':88 post_form = PostForm(request.POST, request.FILES)89 if post_form.is_valid():90 post = News(author=request.user,91 content=post_form.cleaned_data['content'],92 headline=post_form.cleaned_data['headline'],93 image=post_form.cleaned_data['image'],94 category=post_form.cleaned_data['category'],95 pub_date=now())96 News.save(post)97 return redirect('index')98 else:99 post_form = PostForm()100 content = {101 'post_form': post_form,102 }...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Splinter automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful