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
4 changes: 4 additions & 0 deletions development.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ use = egg:springboard
unicore.content_repo_url = https://github.com/universalcore/unicore-cms-content-gem-tanzania
; thumbor.security_key = ''

[celery]
CELERY_TASK_SERIALIZER = json
CELERY_ALWAYS_EAGER = True

###
# wsgi server configuration
###
Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ pytest-xdist
Sphinx
sphinx_rtd_theme
sphinx-argparse
mock
-r requirements.txt
1 change: 1 addition & 0 deletions springboard/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ def includeme(config):
config.add_route('flat_page', '/{slug}/')
config.add_route('api_notify', '/api/notify/', request_method='POST')
config.scan(".views")
config.scan(".events")
3 changes: 0 additions & 3 deletions springboard/defaults.ini
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,3 @@ jinja2.filters =
format_date = springboard.filters:format_date_filter
thumbor = springboard.filters:thumbor_filter
markdown = springboard.filters:markdown_filter

[celery]
CELERY_TASK_SERIALIZER = json
31 changes: 31 additions & 0 deletions springboard/events.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from uuid import uuid4

from pyramid.events import NewResponse
from pyramid.events import subscriber

from unicore.google.tasks import pageview


ONE_YEAR = 31556952


@subscriber(NewResponse)
def new_request(event):
request = event.request
registry = request.registry
response = event.response

profile_id = registry.settings.get('ga.profile_id')
if not profile_id:
return

client_id = request.cookies.get('ga_client_id', uuid4().hex)
response.set_cookie('ga_client_id', value=client_id, max_age=ONE_YEAR)
pageview.delay(profile_id, client_id, {
'path': request.path,
'uip': request.remote_addr,
'dr': request.referer or '',
'dh': request.domain,
'user_agent': request.user_agent,
'ul': request.accept_language,
})
9 changes: 6 additions & 3 deletions springboard/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@


@contextfilter
def format_date_filter(ctx, date_string, format):
dt = parser.parse(date_string)
return dt.strftime(format)
def format_date_filter(ctx, timestamp, format):
if isinstance(timestamp, basestring):
timestamp = parser.parse(timestamp)
return timestamp.strftime(format)


@contextfilter
Expand All @@ -28,4 +29,6 @@ def thumbor_filter(ctx, image, width, height=None):

@contextfilter
def markdown_filter(ctx, content):
if not content:
return content
return markdown(content)
56 changes: 56 additions & 0 deletions springboard/tests/test_events.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from datetime import datetime

import mock

from springboard.tests.base import SpringboardTestCase


class TestEvents(SpringboardTestCase):

def setUp(self):
self.workspace = self.mk_workspace()
self.app = self.mk_app(self.workspace, settings={
'ga.profile_id': 'UA-some-id',
})

@mock.patch('unicore.google.tasks.pageview.delay')
def test_ga_pageviews(self, mock_task):
[category] = self.mk_categories(self.workspace, count=1)
[page] = self.mk_pages(self.workspace, count=1,
primary_category=category.uuid,
created_at=datetime.now().isoformat())
self.workspace.save(page, 'Add page')
self.workspace.refresh_index()

self.app.get('/', status=200, extra_environ={
'HTTP_HOST': 'some.site.com',
'REMOTE_ADDR': '192.0.0.1',
})
mock_task.assert_called_once()
((profile_id, gen_client_id, data), _) = mock_task.call_args_list[0]
self.assertEqual(profile_id, 'UA-some-id')
self.assertEqual(data['path'], '/')
self.assertEqual(data['uip'], '192.0.0.1')
self.assertEqual(data['dh'], 'some.site.com')
self.assertEqual(data['dr'], '')

page_url = '/page/%s/' % page.uuid
headers = {
'User-agent': 'Mozilla/5.0',
}
self.app.get(page_url, status=200, headers=headers, extra_environ={
'HTTP_REFERER': '/',
'HTTP_HOST': 'some.site.com',
'REMOTE_ADDR': '192.0.0.1',
})
((profile_id, client_id, data), _) = mock_task.call_args_list[1]

self.assertEqual(profile_id, 'UA-some-id')
self.assertEqual(data['path'], page_url)
self.assertEqual(data['dr'], '/')
self.assertEqual(data['uip'], '192.0.0.1')
self.assertEqual(data['dh'], 'some.site.com')
self.assertEqual(data['user_agent'], 'Mozilla/5.0')

# # ensure cid is the same across calls
self.assertEqual(gen_client_id, client_id)
13 changes: 12 additions & 1 deletion springboard/tests/test_filters.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from datetime import datetime

from pyramid import testing

from libthumbor import CryptoURL
Expand All @@ -12,11 +14,16 @@ class TestFilters(SpringboardTestCase):
def setUp(self):
self.workspace = self.mk_workspace()

def test_format_date_filter(self):
def test_format_date_filter_string(self):
self.assertEqual(
format_date_filter({}, '2012-12-31', '%d-%m-%y'),
'31-12-12')

def test_format_date_filter_date(self):
self.assertEqual(
format_date_filter({}, datetime(2012, 12, 31), '%d-%m-%y'),
'31-12-12')

def test_thumbor_filter(self):
testing.setUp(settings={
'thumbor.security_key': 'foo',
Expand All @@ -42,3 +49,7 @@ def test_markdown_filter(self):
self.assertEqual(
markdown_filter({}, '*foo*'),
'<p><em>foo</em></p>')

def test_markdown_filter_none(self):
self.assertEqual(markdown_filter({}, None), None)
self.assertEqual(markdown_filter({}, ''), '')
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ use = egg:{{cookiecutter.app_name}}
unicore.content_repo_url = {{cookiecutter.unicore_content_repo_url}}
; thumbor.security_key = '{{cookiecutter.thumbor_security_key}}'

[celery]
CELERY_TASK_SERIALIZER = json
CELERY_ALWAYS_EAGER = True

###
# wsgi server configuration
###
Expand Down