Tutorial: Putting elements in a tree

Prerequisites

Here we’ll assume you have a working Tutorial: Quickstart setup, without roll support.

Goals

We’ll add tree hierarchy models, which adds semantics of a filesystem to the media gallery.

In order to achive semantics of a filesystem, a new Directory model is added. It inherits from Element, therefore a directory is an element much like a photo. This way, Elements are nodes of our filesystem, and nodes can be Directory, Photo, ...

A directory can contain arbitrary elements, elements can be parented in more than one directory (think about hardlinks). Directories can also be parented in more than one directory, this is unusual.

Updating settings

settings.py

Add the following in INSTALLED_APPS:

"mg.orga.tree",

We have to update the database:

user@host quickstart $ ./manage.py syncdb

urls.py

Like for Roll-based and Theme-based hierarchy, the Tree hierarchy provides some built-in views. Set the new urls.py:

from django.conf.urls.defaults import *
from django.conf import settings

urlpatterns = patterns(
    'django.views.static',

    url(r'^media/(?P<path>.*)$', 'serve', dict(
        document_root = settings.MEDIA_ROOT,
        )
    ),
) + patterns(
    '',

    # Will take care of the following urls:
    # /dir/dir/.../element
    url(r'^', include('mg.orga.tree.urls')),
)

Tweaking the hierarchy

The tree application provides a powerful management command which provides a shell prompt acting on the media gallery hierarchy. See Managing Directories for reference.

Let’s say we imported some photos the usual way, without putting them in any organizational model:

$ ./manage.py mg_import --files ~/data/*.jpg

Now we want to edit the tree to move them to some intelligent place. We’ll use the built-in shell. It has all the usual shell behaviours, even completion and line edition:

$ ./manage.py mg_tree_shell
MgTree / # ls
Listing /
IMG_1140                                   photo      nipo         2010-12-31 19:41:42
IMG_1141                                   photo      nipo         2010-12-31 19:41:53
IMG_1142                                   photo      nipo         2010-12-31 19:42:08
IMG_1143                                   photo      nipo         2010-12-31 19:42:22
IMG_1144                                   photo      nipo         2010-12-31 19:42:29
IMG_1145                                   photo      nipo         2010-12-31 19:42:40

MgTree / # mkdir Test
MgTree / # mkdir Test/Even
MgTree / # mkdir Test/Odd
MgTree / # mv IMG_114[024]* Test/Even/
Move /IMG_1140 to /Test/Even
Move /IMG_1142 to /Test/Even
Move /IMG_1144 to /Test/Even
MgTree / # mv IMG_114* Test/Odd/
Move /IMG_1141 to /Test/Odd
Move /IMG_1143 to /Test/Odd
Move /IMG_1145 to /Test/Odd
MgTree / # ls
Listing /
Test                                       directory  nipo         2011-01-16 16:28:24.777003

MgTree / # cd Test/
MgTree /Test # ls *
Listing /Test/Even
IMG_1140                                   photo      nipo         2010-12-31 19:41:42
IMG_1142                                   photo      nipo         2010-12-31 19:42:08
IMG_1144                                   photo      nipo         2010-12-31 19:42:29

Listing /Test/Odd
IMG_1141                                   photo      nipo         2010-12-31 19:41:53
IMG_1143                                   photo      nipo         2010-12-31 19:42:22
IMG_1145                                   photo      nipo         2010-12-31 19:42:40

MgTree /Test # ^D

We’re done !

That’s it, you successfully added tree support. You can see the result running the server:

user@host quickstart $ ./manage.py runserver

What’s next ?

You may want to:

  • express your HTML design skills and create better templates.