def search(request): query = request.GET.get('q') page = request.GET.get('page') or 1 from django.db.models import Q import operator words = query.split(' ') qs = [] for word in words: qs.append(Q(title__icontains=word)) qs.append(Q(content__icontains=word)) posts = Post.objects.filter(reduce(operator.or_, qs)) paginator = Paginator(posts, 5) try: p = paginator.page(page) except (EmptyPage, InvalidPage): p = paginator.page(paginator.num_pages) results = p.object_list for result in results: result.permalink = make_permalink(result, absolute=True, request=request) meta = {'query': query} return render('search.html', {'results': results, 'meta': meta, 'paginator': p}, context_instance=RequestContext(request))
def render(template_name, context={}, **kwargs): # Generate the navigation options based on what's in the navigation tables. # Loop through the items and add them to the navigation dict, which is passed # with the context in the global variable. This can be used in your templates # by accessing global.navigation.menuslug (no spaces, dots or dashes in the slugs). navigation = {} available_menus = Menu.objects.all() for menu in available_menus: navigation[menu.slug] = [] menu.populate() # Populates the menu permalinks from linked objects for item in menu.items: navigation[menu.slug].append({ 'caption': item.caption, 'permalink': item.permalink, }) # Form the global context here, these variables are passed to each and every template # rendered via juice.front.views.render(). You can add additional variables which can # be used in your base template or children. context['global'] = { 'title': 'Juice', 'home': make_permalink(), 'navigation': navigation, 'tags': Term.objects.filter(taxonomy='category') } # Render the final response based on the JUICE_THEME Django setting. Note that this structure # is mandatory for Juice templates to work correctly. If you're looking to change URL styles # to access dynamic and static data, use the permalinks.py file and urls.py respectively. return render_to_response(template_name, context, **kwargs)
def single(request, post_slug): p = Post.objects.get(slug=post_slug, published__lte=datetime.now()) p.ctype = ContentType.objects.get_for_model(Post) # Apply the shortcodes to the post content p.content = shortcodes.apply(p.content, request) # read the relations with posts and terms rel_tags = TermRelation.objects.filter(content_type__pk=p.ctype.id, object_id=p.id, term__taxonomy='tag') rel_categories = TermRelation.objects.filter(content_type__pk=p.ctype.id, object_id=p.id, term__taxonomy='category') p.tags = [] p.categories = [] for tag in rel_tags: p.tags.append(tag.term) for category in rel_categories: p.categories.append(category.term) # set the permalinks p.permalink = make_permalink(p) for c in p.categories: c.permalink = make_permalink(c) for t in p.tags: t.permalink = make_permalink(t) # comments p.comments = Comment.tree.filter(content_type__pk=p.ctype.id, object_id=p.id) for c in p.comments: c.permalink = make_permalink(c) c.populate() p.comments_count = p.comments.count() CommentForm = FormsAPI.by_slug('comment-form') comment_form, feedback = FormsAPI.process_form(CommentForm, request, content_object=p, feedback=True) for entry in feedback: action, arg = entry if action == "redirect_to": return HttpResponseRedirect(arg) return render('post.html', {'post': p, 'comment_form': comment_form}, context_instance=RequestContext(request))
def page(request, page_slug, page_id=False): if page_id: page = Page.objects.get(id=page_id) else: page = Page.objects.get(slug=page_slug) page.content = shortcodes.apply(page.content, request) page.permalink = make_permalink(page) return render('page.html', {'page': page}, context_instance=RequestContext(request))
def index(request, page=1): posts_list = Post.objects.filter(published__lte=datetime.now()).order_by('-published') paginator = Paginator(posts_list, 5) try: p = paginator.page(page) except (EmptyPage, InvalidPage): p = paginator.page(paginator.num_pages) posts = p.object_list p.next_link = reverse('juice.front.views.index', kwargs={'page': p.next_page_number()}) p.previous_link = reverse('juice.front.views.index', kwargs={'page': p.previous_page_number()}) posts_ctype = ContentType.objects.get_for_model(Post) for post in posts: post.permalink = make_permalink(post) post.permalink_abs = make_permalink(post, absolute=True, request=request) post.comments_count = Comment.objects.filter(content_type__pk=posts_ctype.id, object_id=post.id).count() post.content = shortcodes.apply(post.content, request) return render('home.html', {'posts': posts, 'paginator': p}, context_instance=RequestContext(request))
def create_menu(menu): menu.items = MenuItem.objects.filter(menu__id=menu.id).order_by('order') for item in menu.items: item.permalink = make_permalink(item) return menu
def populate(self): self.items = MenuItem.objects.filter(menu__id=self.id).order_by('order') for item in self.items: if item.content_object: item.permalink = make_permalink(item.content_object)