def detail(request, slug, template_name='news/detail.html', \ template_name_ajax='news/detail_ajax.html'): """ News item detail. In the template, we show the title and the body of the News item and links to all its' all available translations. :param django.http.HttpRequest request: :param string slug: Foo item slug. :param string template_name: :return django.http.HttpResponse: """ layout = get_layout(as_instance=True) language = get_language_from_request(request) if language is not None: translation.activate(language) results_kwargs = {'slug': slug} try: queryset = NewsItem._default_manager.filter(**results_kwargs) item = queryset.get(**results_kwargs) except Exception as e: raise Http404 context = {'layout': layout, 'item': item} if request.is_ajax(): template_name = template_name_ajax return render_to_response(template_name, context, context_instance=RequestContext(request))
def detail(request, slug, template_name="news/detail.html", template_name_ajax="news/detail_ajax.html"): """ News item detail. In the template, we show the title and the body of the News item and links to all its' all available translations. :param django.http.HttpRequest request: :param string slug: Foo item slug. :param string template_name: :return django.http.HttpResponse: """ layout = get_layout(as_instance=True) language = get_language_from_request(request) if language is not None: translation.activate(language) results_kwargs = {"slug": slug} try: queryset = NewsItem._default_manager.filter(**results_kwargs) item = queryset.get(**results_kwargs) except Exception as e: raise Http404 context = {"layout": layout, "item": item} if request.is_ajax(): template_name = template_name_ajax return render_to_response(template_name, context, context_instance=RequestContext(request))
def detail(request, slug, template_name='foo/detail.html'): """ Foo item detail. In the template, we show the title and the body of the FooItem and links to all its' all available translations. :param django.http.HttpRequest request: :param str slug: Foo item slug. :param str template_name: :return django.http.HttpResponse: """ language = get_language_from_request(request) if language is not None: translation.activate(language) try: item = FooItem._default_manager.all().prefetch_related('translations') \ .select_related('translation_of').get(slug=slug) except Exception as e: raise Http404 context = {'item': item} return render_to_response(template_name, context, context_instance=RequestContext(request))
def render(self, context): # Try to get request.LANGUAGE_CODE. If fail, use default one. request = context['request'] language = get_language_from_request(request, default=None) if not language: language = smart_resolve(self.language, context) if not language in get_languages_keys(): language = default_language translation.activate(language) return ''
def post_processor(self): """ Getting news items for the current active language. """ results_kwargs = {} language = get_language_from_request(self.request) if language is not None: results_kwargs.update({'language': language}) self.data.news_items = NewsItem._default_manager.filter(**results_kwargs) \ .order_by('-date_published')[:self.data.max_items]
def post_processor(self): """ Getting news items for the current active language. """ results_kwargs = {} language = get_language_from_request(self.request) if language is not None: results_kwargs.update({'language': language}) self.data.news_items = NewsItem._default_manager.filter(**results_kwargs) \ .order_by('-date_published')[:self.data.max_items]
def browse(request, template_name='foo/browse.html'): """ In the template, we show all available FooItems for current language. :param django.http.HttpRequest request: :param str template_name: :return django.http.HttpResponse: """ language = get_language_from_request(request) results_kwargs = {} if language is not None: translation.activate(language) results_kwargs.update({'language': language}) queryset = FooItem._default_manager.filter(**results_kwargs).order_by('-date_published') context = {'items': queryset} return render_to_response(template_name, context, context_instance=RequestContext(request))
def render(self, context): ret_val = None obj = smart_resolve(self.obj, context) try: is_multilingual = obj.is_multilingual except AttributeError as e: is_multilingual = False raise template.TemplateSyntaxError( _("Invalid usage of ``get_translated_object_for``. Translated object shall be multilingual.") ) try: request = context['request'] except: request = None if language is None and request is None: raise template.TemplateSyntaxError( _("Invalid usage of ``get_translated_object_for``. Can't retrieve ``HttpRequest`` object from " "template context, while ``language`` attribute is not specified.") ) if language is None: language = get_language_from_request(request) else: language = smart_resolve(self.language, context) if language: language = str(language) else: language = default_language ret_val = obj.get_translation_for(language) if self.as_var: context[self.as_var] = ret_val return '' return ret_val
def browse(request, template_name="news/browse.html", template_name_ajax="news/browse_ajax.html"): """ In the template, we show all available NewsItems for current language. :param django.http.HttpRequest request: :param string template_name: :return django.http.HttpResponse: """ # Getting dashboard settings for the user. Then get users' layout. dashboard_settings = get_or_create_dashboard_settings(request.user) layout = get_layout(layout_uid=dashboard_settings.layout_uid, as_instance=True) language = get_language_from_request(request) results_kwargs = {} if language is not None: translation.activate(language) results_kwargs.update({"language": language}) queryset = NewsItem._default_manager.filter(**results_kwargs).order_by("-date_published") page = request.GET.get(PAGE_URL_PARAM, 1) # import ipdb; ipdb.set_trace() num_posts = request.GET.get(NUM_POSTS_URL_PARAM, DEFAULT_MAX_NEWS_ITEMS) try: num_posts = int(num_posts) except Exception as e: num_posts = DEFAULT_MAX_NEWS_ITEMS if num_posts < 1 or num_posts > MAX_NUM_POSTS_LISTING: num_posts = DEFAULT_MAX_NEWS_ITEMS paginator = Paginator(queryset, num_posts, allow_empty_first_page=False) try: page_number = int(page) except ValueError as e: if "last" == page: page_number = paginator.num_pages else: raise Http404(_("Invalid page!")) try: page_obj = paginator.page(page_number) except InvalidPage as e: raise Http404(_("Invalid page!")) context = { "layout": layout, "PAGE_URL_PARAM": PAGE_URL_PARAM, "NUM_POSTS_URL_PARAM": NUM_POSTS_URL_PARAM, # Pagination specific "paginator": paginator, "page_obj": page_obj, "results_per_page": paginator.per_page, "has_next": page_obj.has_next(), "has_previous": page_obj.has_previous(), "page": page_obj.number, "next": page_obj.next_page_number() if page_obj.has_next() and page_obj.next_page_number() is not None else "", "previous": page_obj.previous_page_number() if page_obj.has_previous() and page_obj.previous_page_number() is not None else "", "first_on_page": page_obj.start_index(), "last_on_page": page_obj.end_index(), "pages": paginator.num_pages, "hits": paginator.count, "page_range": paginator.page_range, "items": page_obj.object_list, "request_path": request.path, } if request.is_ajax(): template_name = template_name_ajax return render_to_response(template_name, context, context_instance=RequestContext(request))
def browse(request, template_name='news/browse.html', \ template_name_ajax='news/browse_ajax.html'): """ In the template, we show all available NewsItems for current language. :param django.http.HttpRequest request: :param string template_name: :return django.http.HttpResponse: """ # Getting dashboard settings for the user. Then get users' layout. dashboard_settings = get_or_create_dashboard_settings(request.user) layout = get_layout(layout_uid=dashboard_settings.layout_uid, as_instance=True) language = get_language_from_request(request) results_kwargs = {} if language is not None: translation.activate(language) results_kwargs.update({'language': language}) queryset = NewsItem._default_manager.filter(**results_kwargs) \ .order_by('-date_published') page = request.GET.get(PAGE_URL_PARAM, 1) #import ipdb; ipdb.set_trace() num_posts = request.GET.get(NUM_POSTS_URL_PARAM, DEFAULT_MAX_NEWS_ITEMS) try: num_posts = int(num_posts) except Exception as e: num_posts = DEFAULT_MAX_NEWS_ITEMS if num_posts < 1 or num_posts > MAX_NUM_POSTS_LISTING: num_posts = DEFAULT_MAX_NEWS_ITEMS paginator = Paginator(queryset, num_posts, allow_empty_first_page=False) try: page_number = int(page) except ValueError as e: if 'last' == page: page_number = paginator.num_pages else: raise Http404(_("Invalid page!")) try: page_obj = paginator.page(page_number) except InvalidPage as e: raise Http404(_("Invalid page!")) context = { 'layout': layout, 'PAGE_URL_PARAM': PAGE_URL_PARAM, 'NUM_POSTS_URL_PARAM': NUM_POSTS_URL_PARAM, # Pagination specific 'paginator': paginator, 'page_obj': page_obj, 'results_per_page': paginator.per_page, 'has_next': page_obj.has_next(), 'has_previous': page_obj.has_previous(), 'page': page_obj.number, 'next': page_obj.next_page_number() if page_obj.has_next() and page_obj.next_page_number() is not None else '', 'previous': page_obj.previous_page_number() if page_obj.has_previous() and page_obj.previous_page_number() is not None else '', 'first_on_page': page_obj.start_index(), 'last_on_page': page_obj.end_index(), 'pages': paginator.num_pages, 'hits': paginator.count, 'page_range': paginator.page_range, 'items': page_obj.object_list, 'request_path': request.path } if request.is_ajax(): template_name = template_name_ajax return render_to_response(template_name, context, context_instance=RequestContext(request))