Tutorial: Sorting rolls through themes

Prerequisites

Here we’ll assume you have a working Tutorial: Sorting galleries through rolls setup.

Goals

We’ll add themes, which are groups of Rolls.

Updating settings

settings.py

Add the following in INSTALLED_APPS:

"mg.orga.theme",

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 theme.

  • In header:

    {% load mg_theme %}
    
  • In block title, add theme name:

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

    Theme: <a href="{% url theme-detail theme.slug %}">{{theme.name}}</a>
    Roll <a href="{% url roll theme.slug roll.slug%}">{{roll.name}}/</a>
    

    Element links (prev/ext url tags) also have to be updated to add the theme slug.

Code should look like:

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

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

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

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

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

{% block title %}List of themes{% 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 theme view, which lists contained rolls. In templates/theme/theme_detail.html, put:

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

{% block title %}Theme {{theme.name}}{% endblock %}

{% block content %}
<p>
  <a href="/">Back to list of themes</a>
</p>
<p>
  {% for object in theme.roll.all %}
  <a href="{{ object.slug }}/">
   {% elem_preview object.get_random_element %}<br />
   {{object.name}}
 </a>
  {% endfor %}
</p>
{% endblock %}

Putting our rolls in a theme

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

user@host quickstart $ ./manage.py shell

Import the models:

>>> from mg.orga.theme.models import *
>>> from mg.orga.roll.models import *

Create and save the new theme:

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

Take all rolls as children:

>>> r.roll = list(Rolls.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.theme.models import Theme
from django.conf import settings

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

    url(r'^$', 'object_list', dict(
        queryset = Theme.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:
    # /theme_slug/
    # /theme_slug/roll_slug/
    # /theme_slug/roll_slug/elem_pk/
    url(r'^', include('mg.orga.theme.urls')),
)

We’re done !

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

user@host quickstart $ ./manage.py runserver

What’s next ?

You may want to: