def __call__(self, request, **kwargs): # get the category try: cat = self.get_category(request, kwargs.pop('category', '')) except self.EmptyHomepageException: return self.render(request, {}, self.empty_homepage_template_name) # mark it as being rendered object_rendering.send(sender=Category, request=request, category=cat, publishable=None) object_rendered.send(sender=Category, request=request, category=cat, publishable=None) # if API enabled and active, return a serialized category response = render_as_api(request, cat) if response: return response context = self.get_context(request, cat, **kwargs) # custom view for category if custom_urls.resolver.has_custom_detail(cat): # custom_urls depend on the main rendered object being stored as # 'object' in context context['object'] = cat return custom_urls.resolver.call_custom_detail(request, context) template_name = cat.template archive_template = cat.app_data.ella.archive_template if archive_template and not context.get('is_title_page'): template_name = archive_template return self.render(request, context, self.get_templates(context, template_name))
def __call__(self, request, **kwargs): # get the category try: cat = self.get_category(request, kwargs.pop('category', '')) except self.EmptyHomepageException: return self.render(request, {}, self.empty_homepage_template_name) # mark it as being rendered object_rendering.send(sender=Category, request=request, category=cat, publishable=None) object_rendered.send(sender=Category, request=request, category=cat, publishable=None) # if API enabled and active, return a serialized category response = render_as_api(request, cat) if response: return response context = self.get_context(request, cat, **kwargs) template_name = cat.template archive_template = cat.app_data.ella.archive_template if archive_template and not context.get('is_title_page'): template_name = archive_template return self.render(request, context, self.get_templates(context, template_name))
class ObjectDetail(EllaCoreView): """ Renders a page for publishable. If ``url_remainder`` is specified, tries to locate custom view via :meth:`DetailDispatcher.call_view`. If :meth:`DetailDispatcher.has_custom_detail` returns ``True``, calls :meth:`DetailDispatcher.call_custom_detail`. Otherwise renders a template with context containing: * object: ``Publishable`` instance representing the URL accessed * category: ``Category`` of the ``object`` * content_type_name: slugified plural verbose name of the publishable's content type * content_type: ``ContentType`` of the publishable The template is chosen based on the object in question (the first one that matches is used): * ``page/category/<tree_path>/content_type/<app>.<model>/<slug>/object.html`` * ``page/category/<tree_path>/content_type/<app>.<model>/object.html`` * ``page/category/<tree_path>/object.html`` * ``page/content_type/<app>.<model>/object.html`` * ``page/object.html`` :param request: ``HttpRequest`` from Django :param category: ``Category.tree_path`` (empty if home category) :param year month day: date matching the `publish_from` field of the `Publishable` object :param slug: slug of the `Publishable` :param url_remainder: url after the object's url, used to locate custom views in `custom_urls.resolver` :raises Http404: if the URL is not valid and/or doesn't correspond to any valid `Publishable` """ template_name = 'object.html' class WrongUrl(Http404): pass def __call__(self, request, category, slug, year=None, month=None, day=None, id=None, url_remainder=None): try: context = self.get_context(request, category, slug, year, month, day, id) except self.WrongUrl, e: message, obj = e.args url = obj.get_absolute_url() if url_remainder: url += url_remainder return redirect(url, permanent=True) obj = context['object'] object_rendering.send(sender=context['object'].__class__, request=request, category=context['category'], publishable=context['object']) # check for custom actions if url_remainder: return custom_urls.resolver.call_custom_view(request, obj, url_remainder, context) response = render_as_api(request, obj) if response: return response if custom_urls.resolver.has_custom_detail(obj): return custom_urls.resolver.call_custom_detail(request, context) object_rendered.send(sender=context['object'].__class__, request=request, category=context['category'], publishable=context['object']) return self.render(request, context, self.get_templates(context))
def __call__(self, request, category, slug, year=None, month=None, day=None, id=None, url_remainder=None): try: context = self.get_context(request, category, slug, year, month, day, id) except self.WrongUrl as e: message, obj = e.args url = obj.get_absolute_url() if url_remainder: url += url_remainder return redirect(url, permanent=True) obj = context['object'] object_rendering.send(sender=context['object'].__class__, request=request, category=context['category'], publishable=context['object']) # check for custom actions if url_remainder: return custom_urls.resolver.call_custom_view(request, obj, url_remainder, context) response = render_as_api(request, obj) if response: return response if custom_urls.resolver.has_custom_detail(obj): return custom_urls.resolver.call_custom_detail(request, context) object_rendered.send(sender=context['object'].__class__, request=request, category=context['category'], publishable=context['object']) return self.render(request, context, self.get_templates(context))
def detail(request, category, url_remainder=''): path = category.split('/') # construct all possible tree_paths and get their IDs from redis part = [] pipe = redis.pipeline() for p in path: part.append(p) pipe.get(REDIS_KEY % '/'.join(part)) ids = pipe.execute() # no IDs -> 404 if not any(ids): raise Http404() # get the last ID == the most specific wiki object id = filter(None, ids)[-1] wiki = get_cached_object(Wiki, pk=id) if not (wiki.is_published() or wiki_settings.IS_MODERATOR_FUNC(request.user)): # future publish, render if accessed by moderator raise Http404() # treat the rest as part of custom_urls part leftover = category[len(wiki.tree_path):] object_rendering.send(sender=Wiki, request=request, category=wiki.category, publishable=wiki) context = { 'category': wiki.category, 'object': wiki, 'content_type': wiki.content_type, } # custom urls if leftover or url_remainder: url_remainder = ('%s/%s' % (leftover, url_remainder)). strip('/') + '/' return custom_urls.resolver.call_custom_view(request, wiki, url_remainder, context) # ella API response = render_as_api(request, wiki) if response: return response # custom detail if custom_urls.resolver.has_custom_detail(wiki): return custom_urls.resolver.call_custom_detail(request, context) object_rendered.send(sender=Wiki, request=request, category=wiki.category, publishable=wiki) return TemplateResponse(request, get_templates_from_publishable('object.html', wiki), context)
def get(self, request, *args, **kwargs): if 'slug' not in kwargs: raise Http404 # Compatibility with `paginator` tag. if 'p' in request.GET: self.kwargs.update({'page': request.GET['p']}) self.author = get_cached_object(Author, slug=kwargs['slug']) response = render_as_api(request, self.author) if response: return response return super(AuthorView, self).get(request, *args, **kwargs)