Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ install:
- pip install -e .
script:
- flake8 springboard_iogt
- py.test springboard_iogt --cov springboard
- py.test springboard_iogt --cov springboard_iogt
after_success:
- coveralls
deploy:
Expand Down
2 changes: 2 additions & 0 deletions springboard_iogt/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ def main(global_config, **settings):
config = Configurator(settings=defaults)

# add to springboard routes
config.add_route('content_section', '/section/{slug}/')
config.add_route('personae', '/persona/')
config.add_route('skip_persona_selection', '/persona/skip/')
config.add_route('select_persona', '/persona/{slug}/')
config.add_tween('springboard_iogt.views.persona_tween_factory')
config.scan('.views')
config.scan('.events')

config.include('springboard.config')
config.override_asset(
Expand Down
9 changes: 9 additions & 0 deletions springboard_iogt/events.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from pyramid.events import BeforeRender, subscriber

from springboard_iogt.utils import ContentSection


@subscriber(BeforeRender)
def add_content_section_context(event):
event['content_sections'] = zip(ContentSection.SLUGS,
ContentSection.TITLES)
19 changes: 11 additions & 8 deletions springboard_iogt/templates/base.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,22 @@
{% include 'auth.jinja2' %}
</div>
<div class="push"></div><!--/PUSH-->
<div id="submenu">
<a href="/">{{ gettext("Home") }}</a>
{% for nav in main_navigation_items %}
<span>
l <a href="{{'category'|route_url(uuid=nav.uuid)}}">{{nav.title}}</a>
</span>
{% endfor %}
</div>

<div id="content">
{% block content %}
{% endblock %}
</div>

<div id="submenu">
<h2>{% trans %}Explore our sites{% endtrans %}</h2>
<ul>
<li><a href="{{ 'home'|route_url }}">{{ _("Home") }}</a></li>
{% for slug, title in content_sections %}
<li><a href="{{ 'content_section'|route_url(slug=slug) }}">{{ title }}</a>
</li>
{% endfor %}
</ul>
</div>

</body>
</html>
44 changes: 44 additions & 0 deletions springboard_iogt/templates/content_section.jinja2
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{% extends "base.jinja2" %}
{% from "molecules/section_banner.jinja2" import section_banner %}

{% block content %}
<div>
<div class="h1">
{{ section.title }}
</div>

{% set localisation=section.localisations.filter(locale=language)[0] %}
{{ section_banner(section, localisation) }}

{% set featured_pages=section.pages.filter(language=language, featured=True).order_by('-created_at')[:2] %}
{% if featured_pages %}
<div class="articles latest">
<div class="h1">{{ _('Latest') }}</div>
{% for page in featured_pages %}
{# TODO: use page_listing macro #}
<div class="h2">{{ page.title }}</div>
{% endfor %}
</div>
{% endif %}

{% for category in section.categories.filter(language=language) %}
{% set category_pages=section.pages.filter(primary_category=category.uuid).order_by('-created_at')[:1] %}
{% if category_pages %}
<div class="articles">
<div class="h1">
<a href="{{ 'category'|route_url(uuid=category.uuid) }}">{{ category.title }}</a>
</div>
{% for page in category_pages %}
{# TODO: use page_listing macro #}
<div class="h2">{{ page.title }}</div>
{% endfor %}
<div class="clr"></div>
<div class="pagination">
<a href="{{ 'category'|route_url(uuid=category.uuid) }}">{{ _('See all in category') }}</a>
</div>
</div>
{% endif %}
{% endfor %}
</div>
</div>
{% endblock %}
12 changes: 12 additions & 0 deletions springboard_iogt/templates/molecules/section_banner.jinja2
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% macro section_banner(section, localisation) -%}
<div id="banner">
{% if localisation and localisation.image_host and localisation.image %}
<img alt="{% trans owner=section.owner %}Brought to you by {{ owner }}{% endtrans %}" src="{{ localisation.image_host }}{{ localisation|thumbor(320) }}"/>
{% else %}
<img alt="{% trans owner=section.owner %}Brought to you by {{ owner }}{% endtrans %}" src="{{ ('springboard_iogt:static/img/banner_%s.png' % section.slug)|static_url }}" />
{% endif %}
<span>
{% trans owner=section.owner %}Brought to you by {{ owner }}{% endtrans %}
</span>
</div>
{%- endmacro %}
34 changes: 34 additions & 0 deletions springboard_iogt/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from pyramid import testing

from springboard.tests import SpringboardTestCase

from springboard_iogt.utils import ContentSection
from springboard_iogt.views import IoGTViews


class TestUtils(SpringboardTestCase):

def tearDown(self):
testing.tearDown()

def test_content_section(self):
workspace1 = self.mk_workspace(name='barefootlaw')
workspace2 = self.mk_workspace(name='ffl')
testing.setUp(settings={
'unicore.repos_dir': self.working_dir,
'unicore.content_repo_urls': '\n'.join([workspace1.working_dir,
workspace2.working_dir]),
})
views = IoGTViews(self.mk_request())

self.assertTrue(ContentSection.exists(
'barefootlaw', views.all_pages.get_indexes()))
section_obj = ContentSection(
'ffl',
views.all_pages,
views.all_categories,
views.all_localisations)
self.assertEqual(section_obj.index, 'ffl-master')
self.assertEqual(section_obj.pages.get_indexes(), ['ffl-master'])
self.assertEqual(section_obj.title, 'Health advice')
self.assertEqual(section_obj.owner, 'Facts For Life')
24 changes: 24 additions & 0 deletions springboard_iogt/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from springboard_iogt.views import (
PERSONAE, PERSONA_COOKIE_NAME, PERSONA_SKIP_COOKIE_VALUE)
from springboard_iogt.application import main
from springboard_iogt.utils import ContentSection


class TestIoGTViews(SpringboardTestCase):
Expand Down Expand Up @@ -116,3 +117,26 @@ def test_personae(self):
self.assertTrue(all(querystring in tag['href']
for tag in persona_url_tags))
self.assertTrue(querystring in skip_url_tags[0]['href'])

def test_content_section(self):
ffl_workspace = self.mk_workspace(name='ffl')
[page] = self.mk_pages(
ffl_workspace, count=1,
created_at=datetime.utcnow().isoformat())
app = self.mk_app(self.workspace, main=main, settings={
'unicore.content_repo_urls': '\n'.join([self.workspace.working_dir,
ffl_workspace.working_dir])
})
app.set_cookie(PERSONA_COOKIE_NAME, PERSONA_SKIP_COOKIE_VALUE)

response = app.get('/section/doesnotexist/', expect_errors=True)
self.assertEqual(response.status_int, 404)
response = app.get('/section/ffl/')
self.assertEqual(response.status_int, 200)

def test_content_section_listing(self):
app = self.mk_app(self.workspace, main=main)
html = app.get('/does/not/exists/', expect_errors=True).html
section_url_tags = html.find_all('a', href=re.compile(
r'/section/(%s)/' % '|'.join(ContentSection.SLUGS)))
self.assertEqual(len(section_url_tags), len(ContentSection.SLUGS))
45 changes: 45 additions & 0 deletions springboard_iogt/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,53 @@
from pyramid.interfaces import IRoutesMapper
from pyramid.i18n import TranslationStringFactory

from unicore.hub.client.utils import same_origin


translation_string_factory = TranslationStringFactory(None)
_ = translation_string_factory


class ContentSection(object):
SLUGS = [
'barefootlaw',
'ffl',
'mariestopes',
'straighttalk',
'ureport'
]
OWNERS = [
_('Barefoot Law'),
_('Facts For Life'),
_('Marie Stopes'),
_('Straight Talk'),
_('U-report'),
]
TITLES = [
_('Your rights'),
_('Health advice'),
_('Youth'),
_('Sexual health'),
_('U-report'),
]

def __init__(self, slug, pages, categories, localisations):
self.slug = slug
i = self.__class__.SLUGS.index(slug)
self.owner = self.__class__.OWNERS[i]
self.title = self.__class__.TITLES[i]
indexes = pages.get_indexes()
[self.index] = filter(lambda index: slug in index, indexes)
self.pages = pages.indexes(self.index)
self.categories = categories.indexes(self.index)
self.localisations = localisations.indexes(self.index)

@classmethod
def exists(cls, slug, indexes):
return (slug in cls.SLUGS and
any([index for index in indexes if slug in index]))


def get_redirect_url(request, param_name='next', default_route='home'):
redirect_url = request.GET.get(param_name)
if redirect_url and same_origin(redirect_url, request.current_route_url()):
Expand Down
18 changes: 17 additions & 1 deletion springboard_iogt/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

from springboard.views.base import SpringboardViews

from springboard_iogt.utils import get_redirect_url, get_matching_route
from springboard_iogt.utils import (
get_redirect_url, get_matching_route, ContentSection)


ONE_YEAR = 31536000
Expand Down Expand Up @@ -56,3 +57,18 @@ def skip_persona_selection(self):
PERSONA_COOKIE_NAME, value=PERSONA_SKIP_COOKIE_VALUE,
max_age=ONE_YEAR)
return response

@view_config(route_name='content_section',
renderer='springboard_iogt:templates/content_section.jinja2')
def content_section(self):
slug = self.request.matchdict['slug']
indexes = self.all_pages.get_indexes()
if not ContentSection.exists(slug, indexes):
raise HTTPNotFound

return self.context(section=ContentSection(
slug,
pages=self.all_pages,
categories=self.all_categories,
localisations=self.all_localisations
))