Class-based generic views

The module media_tree.contrib.views contains class-based generic views that enable you to access FileNode objects through public URLs. Please see below for specific examples. Of course you can also extend the generic view classes to create views that suit your specific requirements.

Note

As with any public views, you may want to restrict the objects that should be publicly visible by passing an appropriately filtered queryset when implementing a view. For instance, you may not want users to see the internal folder structure of your FileNode objects, hence using a FileNodeListingView with a QuerySet such as FileNode.objects.all() would be a bad idea.

List Views

class media_tree.contrib.views.listing.FileNodeListingView(**kwargs)

View class for implementing views that render a file listing. This class is based on Django’s generic ListView. Please refer to the respective Django documentation on subclassing and customizing Class-based generic views.

The following example urls.py would implement a view listing a folder with the path some/folder and its immediate children, but not descendants deeper down the hierarchy:

from media_tree.models import FileNode
from media_tree.contrib.views.listing import FileNodeListingView
from django.conf.urls.defaults import *

urlpatterns = patterns('',
    (r'^listing/$', FileNodeListingView.as_view(
        # notice that queryset can be any iterable, for instance a list:
        queryset=[FileNode.objects.get(path='some/folder')],
        list_max_depth=2
    )),
)

The folder tree that is rendered does not have to be identical to the actual tree in your media library. Instead, you can group arbitrary nodes, or output a merged (flat) list. The following example would display all descendants of two folders in a merged, one-dimensional list, as if they were all in the same folder:

from media_tree.models import FileNode
from media_tree.contrib.views.listing import FileNodeListingView, LISTING_MERGED
from django.conf.urls.defaults import *

urlpatterns = patterns('',
    (r'^listing/$', FileNodeListingView.as_view(
        queryset=FileNode.objects.filter(pk__in=(1, 2)),
        list_type=LISTING_MERGED
    )),
)
classmethod as_view(**initkwargs)

Main entry point for a request-response process.

get_allow_empty()

Returns True if the view should display empty lists, and False if a 404 should be raised instead.

get_context_object_name(object_list)

Get the name of the item to be used in the context.

get_paginate_by(queryset)

Get the number of items to paginate by, or None for no pagination.

get_paginator(queryset, per_page, orphans=0, allow_empty_first_page=True)

Return an instance of the paginator for this view.

get_queryset()

Get the list of items for this view. This must be an interable, and may be a queryset (in which qs-specific behavior will be enabled).

get_template_names()

Return a list of template names to be used for the request. Must return a list. May not be called if get_template is overridden.

paginate_queryset(queryset, page_size)

Paginate the queryset, if needed.

render_to_response(context, **response_kwargs)

Returns a response with a template rendered with the given context.

context_object_name = 'node_list'

Designates the name of the variable to use in the context.

include_descendants = True

Toggles the inclusion of all descendants of objects in queryset. Note that this is True by default, meaning a full nested folder and file tree will be rendered.

list_filter_media_types = None

An iterable containing media types to filter for.

list_max_depth = None

Max depth of the tree for descendants to be included.

list_type = 'N'

Type of the listing, either LISTING_NESTED or LISTING_MERGED.

template_name = 'media_tree/filenode_list.html'

Name of the template.

class media_tree.contrib.views.listing.FileNodeListingFilteredByFolderView(**kwargs)

Extended listing View class for implementing a file listing that can be filtered by parent folder and render a folder tree next to the list of files in the currently selected folder.

classmethod as_view(**initkwargs)

Main entry point for a request-response process.

get_allow_empty()

Returns True if the view should display empty lists, and False if a 404 should be raised instead.

get_context_object_name(object_list)

Get the name of the item to be used in the context.

get_paginate_by(queryset)

Get the number of items to paginate by, or None for no pagination.

get_paginator(queryset, per_page, orphans=0, allow_empty_first_page=True)

Return an instance of the paginator for this view.

get_template_names()

Return a list of template names to be used for the request. Must return a list. May not be called if get_template is overridden.

paginate_queryset(queryset, page_size)

Paginate the queryset, if needed.

render_to_response(context, **response_kwargs)

Returns a response with a template rendered with the given context.

auto_merge_single_folder = True

Specifies whether the folder list should be suppressed and the list merged if there is only one level of hierarchy in queryset.

folder_pk_param_name = 'folder'

Name of the parameter added to the query string for the folder filter.

Detail Views

class media_tree.contrib.views.detail.FileNodeDetailView(**kwargs)

View class for implementing detail views for FileNode objects. This class is based on Django’s generic DetailView. Please refer to the respective Django documentation on subclassing and customizing Class-based generic views.

The following example urls.py would implement a detail view capable of displaying all files with the extension .txt, with URLs containing the full path of the FileNode object, for instance http://yourdomain.com/files/some/folder/ReadMe.txt:

from media_tree.models import FileNode
from media_tree.contrib.views.detail import FileNodeDetailView
from django.conf.urls.defaults import *

urlpatterns = patterns('',
    (r'^files/(?P<path>.+)/$', FileNodeDetailView.as_view(
        queryset=FileNode.objects.filter(extension='txt')
    )),
)
model

alias of FileNode

classmethod as_view(**initkwargs)

Main entry point for a request-response process.

get_context_object_name(obj)

Get the name to use for the object.

get_object(queryset=None)

Returns the object the view is displaying.

By default this requires self.queryset and a pk or path argument in the URLconf, but subclasses can override this to return any object.

get_template_names()

Return a list of template names to be used for the request. Must return a list. May not be called if get_template is overridden.

render_to_response(context, **response_kwargs)

Returns a response with a template rendered with the given context.

context_object_name = 'node'

Designates the name of the variable to use in the context.

filter_media_types = None

An iterable containing media types to filter for.

filter_node_types = (200,)

An iterable containing node types to filter for. By default this is (FileNode.FILE,)

template_name = 'media_tree/filenode_detail.html'

Name of the template.

class media_tree.contrib.views.detail.image.ImageNodeDetailView(**kwargs)

View class for implementing image detail views for FileNode objects. This class is based on Django’s generic DetailView. Please refer to the respective Django documentation on subclassing and customizing Class-based generic views.

The following example urls.py would implement a detail view capable of displaying all image files under a folder node with the path some/folder:

from media_tree.models import FileNode
from media_tree.contrib.views.detail.image import ImageNodeDetailView
from django.conf.urls.defaults import *

urlpatterns = patterns('',
    (r'^images/(?P<pk>\d+)/$', ImageNodeDetailView.as_view(
        queryset=FileNode.objects.get(path='some/folder').get_descendants()
    )),
)
model

alias of FileNode

classmethod as_view(**initkwargs)

Main entry point for a request-response process.

get_context_object_name(obj)

Get the name to use for the object.

get_object(queryset=None)

Returns the object the view is displaying.

By default this requires self.queryset and a pk or path argument in the URLconf, but subclasses can override this to return any object.

get_template_names()

Return a list of template names to be used for the request. Must return a list. May not be called if get_template is overridden.

render_to_response(context, **response_kwargs)

Returns a response with a template rendered with the given context.

context_object_name = 'image_node'

Designates the name of the variable to use in the context.

height = None

Maximum height of the thumbnail. If not set, default values will be used.

template_name = 'media_tree/image_detail.html'

Name of the template.

width = None

Maximum width of the thumbnail. If not set, default values will be used.

Project Versions

Table Of Contents

Previous topic

Using FileNodes in templates

Next topic

Extending Django Media Tree

This Page