def test_required_fields_make_form_valid(self): """Tests that form is valid iff required fields are valid.""" form = SearchForm(order_by='title') self.assertTrue(form.validate()) # Testing that form with optional fields filled out is valid. cat = Category('Test Cat') author = User('Test Author') tag = Tag('Test Tag') form = SearchForm(query='Test Query', category=[cat], author=[author], tags=[tag], order_by='title') self.assertTrue(form.validate())
def show_home(): """ Display home page to visitors and show front page articles. """ form = SearchForm() frontpage_filter = Post.query.filter(Tag.name == "frontpage") posts = frontpage_filter.order_by("created DESC").all() cats = Category.query.all() tags = Tag.query.all() author_filter = User.query.filter(User.role < 2) authors = author_filter.order_by("name ASC").all() page = int(request.args.get('page')) if request.args.get('page') else 1 pagination = Pagination(posts, per_page=4, total=len(posts), page=page) return render_template('home.html', posts=posts, form=form, pagination=pagination, tags=tags, cats=cats, authors=authors)
def show_all(): """ Display All articles by recency """ # Request details req_cat = None req_auth = None req_tags = None search_term = None order = None # If there is no request, list posts by recency if len(request.args) == 0: posts = Post.query.order_by('created DESC').all() else: # Otherwise, get posts that fit requests search_term = "%" + (request.args.get('q') or "") + "%" categories = [] req_cat = request.args.get('c') category_list = (req_cat.split(',') if req_cat is not None else ([cat.id for cat in Category.query.all()])) for category in category_list: categories.append(Post.category_id == category) category_filter = or_(*categories) authors = [] req_auth = request.args.get('a') author_list = (req_auth.split(',') if req_auth is not None else ([user.id for user in User.query.all()])) for author in author_list: authors.append(Post.author_id == author) author_filter = or_(*authors) # Generate list of tags to look for in relationship table. req_tags = request.args.get('t') tag_list = ([int(tag_id) for tag_id in req_tags.split(',')] if req_tags is not None else ([tag.id for tag in Tag.query.all()])) tags = Post.tags.any(Tag.id.in_(tag_list)) order = request.args.get('order') or 'created DESC' """ To be clear, this query looks for anything like the search term inside of the title or content of all posts and narrows it down to the selected authors, the selected categories, and the selected tags. """ posts = Post.query.join(Category).join(User).filter( or_(Post.title.like(search_term), Post.gfm_content.like(search_term)), author_filter, category_filter, tags).order_by(order).all() form = SearchForm() if form.validate_on_submit(): args = '?q=' + form.query.data if valid_args(form.category.data): args += '&c=' + ilist_to_string(form.category.data) if valid_args(form.author.data): args += '&a=' + ilist_to_string(form.author.data) if valid_args(form.tags.data): args += '&t=' + ilist_to_string(form.tags.data) args += '&order=' + form.order_by.data return redirect(url_for('articles.show_all') + args) page = int(request.args.get('page')) if request.args.get('page') else 1 pagination = Pagination(posts, per_page=4, total=len(posts), page=page, link_size='large') return render_template('articles/articles.html', posts=posts, form=form, query=search_term, cats=req_cat, authors=req_auth, tags=req_tags, order=order, pagination=pagination)
def show_all(): """ Display All articles by recency """ # Request details req_cat = None req_auth = None req_tags = None search_term = None order = None # If there is no request, list posts by recency if len(request.args) == 0: posts = Post.query.order_by('created DESC').all() else: # Otherwise, get posts that fit requests search_term = "%" + (request.args.get('q') or "") + "%" categories = [] req_cat = request.args.get('c') category_list = (req_cat.split(',') if req_cat is not None else ([cat.id for cat in Category.query.all()])) for category in category_list: categories.append(Post.category_id == category) category_filter = or_(*categories) authors = [] req_auth = request.args.get('a') author_list = (req_auth.split(',') if req_auth is not None else ([user.id for user in User.query.all()])) for author in author_list: authors.append(Post.author_id == author) author_filter = or_(*authors) # Generate list of tags to look for in relationship table. req_tags = request.args.get('t') tag_list = ([int(tag_id) for tag_id in req_tags.split(',')] if req_tags is not None else ([tag.id for tag in Tag.query.all()])) tags = Post.tags.any(Tag.id.in_(tag_list)) order = request.args.get('order') or 'created DESC' """ To be clear, this query looks for anything like the search term inside of the title or content of all posts and narrows it down to the selected authors, the selected categories, and the selected tags. """ posts = Post.query.join(Category).join(User).filter( or_(Post.title.like(search_term), Post.gfm_content.like(search_term)), author_filter, category_filter, tags).order_by(order).all() form = SearchForm() if form.validate_on_submit(): args = '?q=' + form.query.data if valid_args(form.category.data): args += '&c=' + ilist_to_string(form.category.data) if valid_args(form.author.data): args += '&a=' + ilist_to_string(form.author.data) if valid_args(form.tags.data): args += '&t=' + ilist_to_string(form.tags.data) args += '&order=' + form.order_by.data return redirect(url_for('articles.show_all') + args) page = int(request.args.get('page')) if request.args.get('page') else 1 pagination = Pagination(posts, per_page=4, total=len(posts), page=page) return render_template('articles/articles.html', posts=posts, form=form, query=search_term, cats=req_cat, authors=req_auth, tags=req_tags, order=order, pagination=pagination)