-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement automatic loading of new micro-updates using AJAX calls
- Loading branch information
Showing
11 changed files
with
277 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# -*- coding: utf-8 -*- | ||
from .view import View # noqa | ||
from .add_microupdate import AddMicroUpdateView # noqa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# -*- coding: utf-8 -*- | ||
from collective.liveblog import _ | ||
from collective.liveblog.adapters import IMicroUpdateContainer | ||
from collective.liveblog.adapters import MicroUpdate | ||
from collective.liveblog.interfaces import IBrowserLayer | ||
from collective.liveblog.interfaces import ILiveblog | ||
from five import grok | ||
from plone import api | ||
from zope.event import notify | ||
from zope.lifecycleevent import ObjectModifiedEvent | ||
|
||
grok.templatedir('templates') | ||
|
||
|
||
class AddMicroUpdateView(grok.View): | ||
|
||
"""Add a micro-update to the Liveblog.""" | ||
|
||
grok.context(ILiveblog) | ||
grok.layer(IBrowserLayer) | ||
grok.name('add-microupdate') | ||
grok.require('collective.liveblog.AddMicroUpdate') | ||
|
||
def render(self): | ||
title = self.request.form.get('title', None) | ||
text = self.request.form.get('text', None) | ||
if not text: | ||
msg = _(u'There were some errors. Required input is missing.') | ||
api.portal.show_message(msg, self.request, type='error') | ||
else: | ||
adapter = IMicroUpdateContainer(self.context) | ||
adapter.add(MicroUpdate(title, text)) | ||
|
||
# notify the Liveblog has a new micro-update | ||
notify(ObjectModifiedEvent(self.context)) | ||
msg = _(u'Item published.') | ||
api.portal.show_message(msg, self.request) | ||
|
||
self.request.response.redirect(self.context.absolute_url()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<configure | ||
xmlns="http://namespaces.zope.org/zope" | ||
xmlns:browser="http://namespaces.zope.org/browser" | ||
xmlns:i18n="http://namespaces.zope.org/i18n" | ||
i18n_domain="collective.liveblog"> | ||
|
||
</configure> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" | ||
xmlns:tal="http://xml.zope.org/namespaces/tal" | ||
xmlns:i18n="http://xml.zope.org/namespaces/i18n" | ||
tal:omit-tag="" | ||
i18n:domain="collective.liveblog"> | ||
<body tal:omit-tag=""> | ||
<div id="micro-updates" tal:omit-tag=""> | ||
<div class="microupdate" data-timestamp="" | ||
tal:repeat="update view/updates_since_timestamp" | ||
tal:attributes="data-timestamp update/timestamp"> | ||
<p class="microupdate-title"> | ||
<strong tal:content="update/title" /> | ||
</p> | ||
<div class="microupdate-text" tal:content="structure update/text" /> | ||
<p class="microupdate-byline" i18n:translate=""> | ||
By <span tal:content="update/creator" /> | ||
<span tal:content="update/time_only" tal:attributes="title update/created" /> | ||
</p> | ||
</div> | ||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# -*- coding: utf-8 -*- | ||
from collective.liveblog.interfaces import IBrowserLayer | ||
from collective.liveblog.interfaces import ILiveblog | ||
from five import grok | ||
from plone.memoize import ram | ||
from time import time | ||
|
||
grok.templatedir('templates') | ||
|
||
|
||
class Updates(grok.View): | ||
|
||
"""Helper view for Liveblog.""" | ||
|
||
grok.context(ILiveblog) | ||
grok.layer(IBrowserLayer) | ||
grok.require('zope2.View') | ||
|
||
def _updates_since_timestamp(self): | ||
"""Return the list of micro-updates since the specified timestamp.""" | ||
timestamp = self.request.get('timestamp', None) | ||
|
||
# timestamp should a string representing a float | ||
try: | ||
float(timestamp) | ||
except (TypeError, ValueError): | ||
return [] | ||
|
||
updates = self.context.restrictedTraverse('view').updates() | ||
updates = [u for u in updates if u['timestamp'] > timestamp] | ||
return updates | ||
|
||
@ram.cache(lambda *args: time() // 20) | ||
def updates_since_timestamp(self): | ||
"""Return the list of micro-updates since the specified timestamp; | ||
the list is cached for 20 seconds. | ||
""" | ||
return self._updates_since_timestamp() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/collective/liveblog/tests/test_add_microupdate_view.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# -*- coding: utf-8 -*- | ||
from collective.liveblog.interfaces import IBrowserLayer | ||
from collective.liveblog.testing import FUNCTIONAL_TESTING | ||
from plone import api | ||
from Products.statusmessages.interfaces import IStatusMessage | ||
from zope.interface import alsoProvides | ||
|
||
import unittest | ||
|
||
|
||
class AddMicroUpdateViewTestCase(unittest.TestCase): | ||
|
||
layer = FUNCTIONAL_TESTING | ||
|
||
def setUp(self): | ||
self.portal = self.layer['portal'] | ||
self.request = self.layer['request'] | ||
alsoProvides(self.request, IBrowserLayer) | ||
with api.env.adopt_roles(['Manager']): | ||
self.liveblog = api.content.create( | ||
self.portal, 'Liveblog', 'liveblog') | ||
|
||
def test_add_microupdate_no_parameters(self): | ||
# invoke the view and check the status message | ||
self.liveblog.unrestrictedTraverse('add-microupdate')() | ||
msg = IStatusMessage(self.request).show() | ||
self.assertEqual(len(msg), 1) | ||
expected = u'There were some errors. Required input is missing.' | ||
self.assertEqual(msg[0].message, expected) | ||
|
||
def test_add_microupdate(self): | ||
self.request.form['title'] = '' | ||
self.request.form['text'] = 'Extra! Extra! Read All About It!' | ||
# invoke the view and check the status message | ||
self.liveblog.unrestrictedTraverse('add-microupdate')() | ||
msg = IStatusMessage(self.request).show() | ||
self.assertEqual(len(msg), 1) | ||
expected = u'Item published.' | ||
self.assertEqual(msg[0].message, expected) |
Oops, something went wrong.