Пример #1
0
def get_models(request):
    models = []
    for model_cls in connected_models():
        opts = model_cls._meta
        url = urlresolvers.reverse('admin:%s_%s_add' % (opts.app_label, opts.module_name))
        models.append({'app_label': opts.app_label, 'model_name': unicode(opts.verbose_name), 'url': url})
    return models
Пример #2
0
    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)
Пример #3
0
    def __init__(self, site):
        '''
        Creates column model singletone. 
        Reads data from django models loader and given site registry
        '''
        self.model_cache = loading.cache
        self.admin_registry = site._registry
        self.fields = {}

        for model_cls in connected_models():
            admin_cls = self.admin_registry[model_cls]

            list_display = list(admin_cls.list_display)
            list_display.remove('action_checkbox')

            # Run over all 'list_display' properties and fill columns
            for i, field_name in enumerate(list_display):
                new_field = Column(field_name, model_cls, admin_cls, i)
                if field_name not in self.fields:
                    self.fields[field_name] = new_field
                else:
                    self.fields[field_name].merge(new_field)
Пример #4
0
        self.content_object.delete()
        super(TreeItem, self).delete(*args, **kwds)
    delete.alters_data = True


def insert_in_tree(sender, instance, **kwrgs):
    '''
    Insert newly created object in catalog tree.
    If no parent provided, insert object in tree root 
    '''
    # to avoid recursion save, process only for new instances
    created = kwrgs.pop('created', False)

    if created:
        parent = getattr(instance, 'parent', None)
        if parent is None:
            tree_item = TreeItem(parent=None, content_object=instance)
        else:
            tree_item = TreeItem(parent=parent, content_object=instance)
        tree_item.save()
        instance.save()

for model_cls in connected_models():
    if model_cls is None:
        import warnings
        warnings.warn('Can not import model %s from app %s, check CATALOG_MODELS setting, **YOU MAY LOSE DATA!**' % (model_cls.__name__, model_cls._meta.app_label))
    # set post_save signals on connected objects:
    # for each connected model connect 
    # automatic TreeItem creation for catalog models
    post_save.connect(insert_in_tree, model_cls)
Пример #5
0
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))