Skip to content
This repository has been archived by the owner on Sep 5, 2019. It is now read-only.

Commit

Permalink
Properly name the webhook notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
msom committed Sep 22, 2016
1 parent 3202124 commit 6eafa08
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 53 deletions.
2 changes: 1 addition & 1 deletion HISTORY.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Changelog
---------

- Adds webhooks.
- Adds support for webhooks.
[msom]

0.9.5 (2016-09-21)
Expand Down
44 changes: 25 additions & 19 deletions onegov/election_day/collection.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,44 @@
from onegov.election_day.models import Webhook
from onegov.ballot.models import Election, Vote
from onegov.election_day.models import Notification


class WebhookCollection(object):
class NotificationCollection(object):

def __init__(self, session):
self.session = session

def add(self, url, last_change, election=None, vote=None):
webhook = Webhook()
webhook.url = url
webhook.last_change = last_change
if election:
webhook.election_id = election.id
if vote:
webhook.vote_id = vote.id
def add(self, url, last_change, election_or_vote):
""" Adds a new notification. """

self.session.add(webhook)
notification = Notification()
notification.url = url
notification.last_change = last_change
if isinstance(election_or_vote, Election):
notification.election_id = election_or_vote.id
if isinstance(election_or_vote, Vote):
notification.vote_id = election_or_vote.id

self.session.add(notification)
self.session.flush()

return webhook
return notification

def query(self):
return self.session.query(Webhook)
return self.session.query(Notification)

def by_election(self, election, url, last_change):
""" Returns the notification specified by given parameters. """
return self.query().filter(
Webhook.election_id == election.id,
Webhook.url == url,
Webhook.last_change == last_change
Notification.election_id == election.id,
Notification.url == url,
Notification.last_change == last_change
).first()

def by_vote(self, vote, url, last_change):
""" Returns the notification specified by given parameters. """

return self.query().filter(
Webhook.vote_id == vote.id,
Webhook.url == url,
Webhook.last_change == last_change
Notification.vote_id == vote.id,
Notification.url == url,
Notification.last_change == last_change
).first()
3 changes: 2 additions & 1 deletion onegov/election_day/forms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ class DeleteForm(Form):
pass


class TriggerWebhookForm(Form):
class TriggerNotificationForm(Form):
pass


__all__ = [
'DeleteForm',
'ElectionForm',
'TriggerNotificationForm',
'UploadElectionForm',
'UploadVoteForm',
'VoteForm'
Expand Down
4 changes: 4 additions & 0 deletions onegov/election_day/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ def get_link(locale):
for locale in sorted(self.app.locales)
]

@cached_property
def notifications(self):
return len(self.principal.webhooks) > 0


class DefaultLayout(Layout):
pass
Expand Down
4 changes: 2 additions & 2 deletions onegov/election_day/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from onegov.election_day.models.archive import Archive
from onegov.election_day.models.principal import Principal
from onegov.election_day.models.webhook import Webhook
from onegov.election_day.models.notification import Notification


__all__ = ['Archive', 'Principal', 'Webhook']
__all__ = ['Archive', 'Principal', 'Notification']
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,22 @@
from uuid import uuid4


class Webhook(Base, TimestampMixin):
""" tbd """
class Notification(Base, TimestampMixin):
""" Stores triggered notifications. """

__tablename__ = 'webhooks'
__tablename__ = 'notifications'

#: Identifies the notification
id = Column(UUID, primary_key=True, default=uuid4)

#: The URL to call
url = Column(Text, nullable=False)

#: The corresponding election
election_id = Column(Text, ForeignKey(Election.id), nullable=True)

#: The corresponding vote
vote_id = Column(Text, ForeignKey(Vote.id), nullable=True)

#: The last update of the corresponding election/vote
last_change = Column(UTCDateTime)
4 changes: 2 additions & 2 deletions onegov/election_day/templates/manage_elections.pt
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@
<li>
<a i18n:translate="" class="action-upload" href="${request.link(election, 'upload')}">Upload Results</a>
</li>
<li tal:condition="layout.principal.webhooks">
<a i18n:translate="" class="action-trigger" href="${request.link(election, 'trigger-webhooks')}">Trigger Notifications</a>
<li tal:condition="layout.notifications">
<a i18n:translate="" class="action-trigger" href="${request.link(election, 'trigger')}">Trigger Notifications</a>
</li>
<li>
<a i18n:translate="" class="action-delete" href="${request.link(election, 'delete')}">Delete</a>
Expand Down
4 changes: 2 additions & 2 deletions onegov/election_day/templates/manage_votes.pt
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@
<li>
<a i18n:translate="" class="action-upload" href="${request.link(vote, 'upload')}">Upload Results</a>
</li>
<li tal:condition="layout.principal.webhooks">
<a i18n:translate="" class="action-trigger" href="${request.link(vote, 'trigger-webhooks')}">Trigger Notifications</a>
<li tal:condition="layout.notifications">
<a i18n:translate="" class="action-trigger" href="${request.link(vote, 'trigger')}">Trigger Notifications</a>
</li>
<li>
<a i18n:translate="" class="action-delete" href="${request.link(vote, 'delete')}">Delete</a>
Expand Down
4 changes: 2 additions & 2 deletions onegov/election_day/templates/upload_election.pt
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
Your results were uploaded successfully.
</div>

<div tal:condition="layout.principal.webhooks" class="panel callout radius">
<div tal:condition="layout.notifications" class="panel callout radius">
<p>
<strong><a href="${request.link(election, 'trigger-webhooks')}" i18n:translate="">Click here to trigger the notifications</a></strong>
<strong><a href="${request.link(election, 'trigger')}" i18n:translate="">Click here to trigger the notifications</a></strong>
</p>
</div>
</div>
Expand Down
4 changes: 2 additions & 2 deletions onegov/election_day/templates/upload_vote.pt
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
Your results were uploaded successfully.
</div>

<div tal:condition="layout.principal.webhooks" class="panel callout radius">
<div tal:condition="layout.notifications" class="panel callout radius">
<p>
<strong><a href="${request.link(vote, 'trigger-webhooks')}" i18n:translate="">Click here to trigger the notifications</a></strong>
<strong><a href="${request.link(vote, 'trigger')}" i18n:translate="">Click here to trigger the notifications</a></strong>
</p>
</div>

Expand Down
22 changes: 12 additions & 10 deletions onegov/election_day/views/manage_elections.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
from onegov.core.security import Private
from onegov.election_day import _
from onegov.election_day import ElectionDayApp
from onegov.election_day.collection import WebhookCollection
from onegov.election_day.collection import NotificationCollection
from onegov.election_day.forms import DeleteForm
from onegov.election_day.forms import ElectionForm
from onegov.election_day.forms import TriggerWebhookForm
from onegov.election_day.forms import TriggerNotificationForm
from onegov.election_day.layout import ManageElectionsLayout


Expand Down Expand Up @@ -95,19 +95,19 @@ def delete_election(self, request, form):
}


@ElectionDayApp.form(model=Election, name='trigger-webhooks',
@ElectionDayApp.form(model=Election, name='trigger',
template='form.pt', permission=Private,
form=TriggerWebhookForm)
def trigger_webhooks(self, request, form):
form=TriggerNotificationForm)
def trigger_notifications(self, request, form):

session = request.app.session()
webhooks = WebhookCollection(session)
notifications = NotificationCollection(session)
layout = ManageElectionsLayout(self, request)

if form.submitted(request):
for url in request.app.principal.webhooks:
webhooks.add(url, self.last_result_change, election=self)
# todo: trigger notification, add webhook entry
notifications.add(url, self.last_result_change, election=self)
# todo: trigger notification, add notification entry
return morepath.redirect(layout.manage_model_link)

callout = None
Expand All @@ -116,8 +116,10 @@ def trigger_webhooks(self, request, form):
button_class = 'primary'

for url in request.app.principal.webhooks:
webhook = webhooks.by_election(self, url, self.last_result_change)
if webhook is not None:
existing = notifications.by_election(
self, url, self.last_result_change
)
if existing is not None:
callout = _(
"There are no changes since the last time the notifications "
"have been triggered!"
Expand Down
20 changes: 11 additions & 9 deletions onegov/election_day/views/manage_votes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
from onegov.core.security import Private
from onegov.election_day import _
from onegov.election_day import ElectionDayApp
from onegov.election_day.collection import WebhookCollection
from onegov.election_day.collection import NotificationCollection
from onegov.election_day.forms import DeleteForm
from onegov.election_day.forms import TriggerWebhookForm
from onegov.election_day.forms import TriggerNotificationForm
from onegov.election_day.forms import VoteForm
from onegov.election_day.layout import ManageVotesLayout
from onegov.election_day.utils import get_vote_summary, post_to
Expand Down Expand Up @@ -96,18 +96,18 @@ def delete_vote(self, request, form):
}


@ElectionDayApp.form(model=Vote, name='trigger-webhooks', template='form.pt',
permission=Private, form=TriggerWebhookForm)
def trigger_webhooks(self, request, form):
@ElectionDayApp.form(model=Vote, name='trigger', template='form.pt',
permission=Private, form=TriggerNotificationForm)
def trigger_notifications(self, request, form):

session = request.app.session()
webhooks = WebhookCollection(session)
notifications = NotificationCollection(session)
layout = ManageVotesLayout(self, request)

if form.submitted(request):
for url in request.app.principal.webhooks:
if post_to(url, get_vote_summary(self, request)):
webhooks.add(url, self.last_result_change, vote=self)
notifications.add(url, self.last_result_change, vote=self)
return morepath.redirect(layout.manage_model_link)

callout = None
Expand All @@ -116,8 +116,10 @@ def trigger_webhooks(self, request, form):
button_class = 'primary'

for url in request.app.principal.webhooks:
webhook = webhooks.by_vote(self, url, self.last_result_change)
if webhook is not None:
existing = notifications.by_vote(
self, url, self.last_result_change
)
if existing is not None:
callout = _(
"There are no changes since the last time the notifications "
"have been triggered!"
Expand Down

0 comments on commit 6eafa08

Please sign in to comment.