Skip to content
This repository has been archived by the owner on Jan 26, 2021. It is now read-only.

Commit

Permalink
add template context processor to list all communities in the navbar …
Browse files Browse the repository at this point in the history
…+ unittest
  • Loading branch information
ana-balica committed Dec 16, 2014
1 parent c9689d1 commit 9d1f16c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
9 changes: 9 additions & 0 deletions systers_portal/community/context_processors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from community.models import Community


def communities_processor(request):
"""Custom template context preprocessor that allows to inject into every
request the list of all communities. This is necessary in order to display
the list of communities in the navigation bar."""
communities = Community.objects.all().order_by("order")
return {'communities': communities}
24 changes: 24 additions & 0 deletions systers_portal/community/tests/test_context_processors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.test import TestCase, Client

from users.models import SystersUser
from community.models import Community


class CommunitiesProcessorTestCase(TestCase):
def setUp(self):
User.objects.create(username='foo', password='foobar')
self.systers_user = SystersUser.objects.get()
self.client = Client()

def test_communities_processor(self):
"""Test the rendering of all communities in the templates"""
Community.objects.create(name="Foo", slug="foo", order=1,
community_admin=self.systers_user)
Community.objects.create(name="Boo", slug="boo", order=2,
community_admin=self.systers_user)
index_url = reverse('index')
response = self.client.get(index_url)
self.assertContains(response, '<a href="/community/foo/">Foo</a>')
self.assertContains(response, '<a href="/community/boo/">Boo</a>')
4 changes: 4 additions & 0 deletions systers_portal/systers_portal/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@
'guardian.backends.ObjectPermissionBackend',
)

TEMPLATE_CONTEXT_PROCESSORS += (
'community.context_processors.communities_processor',
)

ROOT_URLCONF = 'systers_portal.urls'

WSGI_APPLICATION = 'systers_portal.wsgi.application'
Expand Down
3 changes: 3 additions & 0 deletions systers_portal/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Communities <span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">
{% for community in communities %}
<li><a href="{{ community.get_absolute_url }}">{{ community.name }}</a></li>
{% endfor %}
</ul>
</li>
<li><a href="http://systers.org/systers-dev/">Wiki</a></li>
Expand Down

0 comments on commit 9d1f16c

Please sign in to comment.