Tutorial: Sorting galleries through rolls

Prerequisites

Here we’ll assume you have a working Tutorial: Quickstart setup.

Goals

We’ll add rolls, which are groups of Elements.

Updating settings

settings.py

Add the following in INSTALLED_APPS:

"mg.orga.roll",

Of course, we have to update the database:

user@host quickstart $ ./manage.py syncdb

Updating the templates

In templates/element/element_detail.html, we’ll assume we now have an extra template variable roll.

  • In header:

    {% load mg_roll %}
    
  • In block title, add roll name:

    {% block title %}{{object.name}} - {{roll.name}}{% endblock %}
    
  • In block content, add a link to parent roll, and surrounding elements:

    Roll: <a href="{% url roll-detail roll.slug %}">{{roll.name}}</a>
    {% get_roll_prev object as prev %}
    {% if prev %}
    Prev: <a href="{% url element roll.slug prev.pk %}">{{prev.name}}</a>
    {% endif %}
    {% get_roll_next object as next %}
    {% if next %}
    Next: <a href="{% url element roll.slug next.pk %}">{{next.name}}</a>
    {% endif %}
    

Code should look like:

{% extends "base.html" %}
{% load mg_element %}
{% load mg_roll %}

{% block title %}{{object.name}} - {{roll.name}}{% endblock %}

{% block content %}
<p>
 <a href="/">Back to roll list</a>
 Roll: <a href="{% url roll-detail roll.slug %}">{{roll.name}}</a>
 {% get_roll_prev object as prev %}
 {% if prev %}
 Prev: <a href="{% url element roll.slug prev.pk %}">{{prev.name}}</a>
 {% endif %}
 {% get_roll_next object as next %}
 {% if next %}
 Next: <a href="{% url element roll.slug next.pk %}">{{next.name}}</a>
 {% endif %}
</p>

Moreover, we have to create a new template for roll list. Create a new templates/roll/roll_list.html containing:

{% extends "base.html" %}
{% load mg_element %}

{% block title %}List of rolls{% endblock %}

{% block content %}
<p>
  {% for object in object_list %}
  <a href="{{ object.slug }}/">
   {% elem_preview object.get_random_element %}<br />
   {{object.name}}
  </a>
  {% endfor %}
</p>
{% endblock %}

And we have to add a roll view, which lists contained elements. In templates/roll/roll_detail.html, put:

{% extends "base.html" %}
{% load mg_element %}

{% block title %}Roll {{roll.name}}{% endblock %}

{% block content %}
<p>
  <a href="/">Back to list of rolls</a>
</p>
<p>
  {% for object in roll.children.all %}
  <a href="{{ object.pk }}/">
   {% elem_preview object %}
  </a>
  {% endfor %}
</p>
{% endblock %}

Putting our photos in a roll

We’ll use the python shell to take all photos from the library, and put them in a new roll named “Foo”. Run the shell with:

user@host quickstart $ ./manage.py shell

Import the models:

>>> from mg.core.element.models import *
>>> from mg.orga.roll.models import *

Create and save the new roll:

>>> r = Roll(slug = "foo", name = "Foo")
>>> r.save()

Take all elements as children:

>>> r.children = list(Element.objects.all())

Update the urls.py

We have to expose our new views. Let’s change the urls.py to have:

from django.conf.urls.defaults import *

from mg.orga.roll.models import Roll
from django.conf import settings

urlpatterns = patterns(
    'django.views.generic.list_detail',

    url(r'^$', 'object_list', dict(
        queryset = Roll.objects.all(),
        ),
    ),
) + patterns(
    'django.views.static',

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

    # Will take care of the following urls:
    # /roll_slug/
    # /roll_slug/elem_pk/
    url(r'^', include('mg.orga.roll.urls')),
)

We’re done !

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

user@host quickstart $ ./manage.py runserver

What’s next ?

You may want to: