def published(self): tree_q = Q() for model_cls, model_filter in get_q_filters().iteritems(): ct = ContentType.objects.get_for_model(model_cls) if model_filter is not None: object_ids = model_cls.objects.filter(model_filter).values_list('id', flat=True) tree_q |= Q(object_id__in=object_ids, content_type=ct) else: tree_q |= Q(content_type=ct) return self.get_query_set().filter(tree_q)
def render_tag(self, context, instance, children_type, varname): # if instance is not None: if instance == 'root': treeitem = None elif instance == 'guess': # try to guess treeitem = get_treeitem_from_context(context) elif instance is None: treeitem = None else: if isinstance(instance, TreeItem): treeitem = instance else: try: treeitem = instance.tree.get() except AttributeError: raise TemplateSyntaxError('Instance argument must have `tree` attribute') if children_type: ModelClass = None for model_cls in connected_models(): if model_cls._meta.module_name == children_type: ModelClass = model_cls if ModelClass is not None: model_filter = get_q_filters()[ModelClass] if model_filter is not None: queryset = ModelClass.objects.filter(model_filter) else: queryset = ModelClass.objects.all() allowed_ids = TreeItem.objects.published().filter(parent=treeitem, content_type__model=children_type).values_list('object_id', flat=True) queryset = queryset.filter(id__in=allowed_ids) else: # Empty queryset = [] else: queryset = TreeItem.objects.published().filter(parent=treeitem) if varname: context[varname] = queryset return u'' else: self.templates[0:0] = ['%s/children_tag.html' % app_name for app_name in get_data_appnames()] context['children_queryset'] = children_qs return render_to_string(self.templates, context)
def item_view(request, model, slug=None, object_id=None): ''' Render catalog page for object Url variables: model: mdoel name slug: object's slug object_id object's id Required at least one of ``slug`` or ``object_id`` parameters Templates: 'catalog/<app_label>/<model_name>.html' 'catalog/<model_name>.html' '<app_label>/<model_name>_in_catalog.html' 'catalog/treeitem.html', Context taken from ``object_list`` method from ``django.views.generic.list_detail``: object_list list of objects is_paginated are the results paginated? results_per_page number of objects per page (if paginated) has_next is there a next page? has_previous is there a prev page? page the current page next the next page previous the previous page pages number of pages, total hits number of objects, total last_on_page the result number of the last of object in the object_list (1-indexed) first_on_page the result number of the first object in the object_list (1-indexed) page_range: A list of the page numbers (1-indexed). ''' ModelClass = None for model_cls in connected_models(): if model_cls._meta.module_name == model: ModelClass = model_cls if ModelClass is not None: model_filter = get_q_filters()[ModelClass] if model_filter is not None: model_queryset = ModelClass.objects.filter(model_filter) else: model_queryset = ModelClass.objects.all() # select template try: opts = ModelClass._meta t = loader.select_template([ 'catalog/%s/%s.html' % (opts.app_label, opts.module_name), 'catalog/%s.html' % opts.module_name, '%s/%s_in_catalog.html' % (opts.app_label, opts.module_name), 'catalog/treeitem.html', ]) extra_context = { 'template_name': t.name, } except loader.TemplateDoesNotExist: pass if slug is not None: return object_detail(request, model_queryset, slug=slug, **extra_context) elif id is not None: return object_detail(request, model_queryset, object_id=object_id, **extra_context) else: return HttpResponseNotFound(_('No object data specified')) else: return HttpResponseNotFound(_('Model %s does not registered' % model))