From 53c37e275cec7ab1c85ba690726f6e743de17b5e Mon Sep 17 00:00:00 2001 From: jpic Date: Thu, 11 Feb 2016 22:18:49 +0100 Subject: [PATCH] Added representatives_votes.signals.sync If `representatives_votes.contrib.parltrack` is in INSTALLED_APPS, then it will register a receiver for the representatives_votes.signals.sync signal to sync a Dossier that has a URL on europarl. --- .../contrib/parltrack/models.py | 12 ++++++++ .../contrib/parltrack/tests/test_sync.py | 29 +++++++++++++++++++ representatives_votes/signals.py | 3 ++ representatives_votes/tests/settings.py | 1 + 4 files changed, 45 insertions(+) create mode 100644 representatives_votes/contrib/parltrack/models.py create mode 100644 representatives_votes/contrib/parltrack/tests/test_sync.py create mode 100644 representatives_votes/signals.py diff --git a/representatives_votes/contrib/parltrack/models.py b/representatives_votes/contrib/parltrack/models.py new file mode 100644 index 0000000..897f43a --- /dev/null +++ b/representatives_votes/contrib/parltrack/models.py @@ -0,0 +1,12 @@ +from representatives_votes.models import Dossier +from representatives_votes.signals import sync + +from import_dossiers import sync_dossier + + +def sync_dossier_receiver(sender, instance, **kwargs): + if not instance.link.startswith('http://www.europarl.europa.eu'): + return + + sync_dossier(instance.reference) +sync.connect(sync_dossier_receiver, sender=Dossier) diff --git a/representatives_votes/contrib/parltrack/tests/test_sync.py b/representatives_votes/contrib/parltrack/tests/test_sync.py new file mode 100644 index 0000000..27a3853 --- /dev/null +++ b/representatives_votes/contrib/parltrack/tests/test_sync.py @@ -0,0 +1,29 @@ +from django import test + +import mock + +from representatives_votes.models import Dossier +from representatives_votes.signals import sync + + +class SyncTest(test.TestCase): + MODELS = 'representatives_votes.contrib.parltrack.models' + + def test_sync_dossier_receiver(self): + parltrack = Dossier.objects.create( + reference='parltrack', + title='from parltrack', + link='http://www.europarl.europa.eu/foo' + ) + + other = Dossier.objects.create( + reference='other', + title='from other', + link='http://other/foo' + ) + + with mock.patch('%s.sync_dossier' % self.MODELS) as sync_dossiers: + sync.send(sender=Dossier, instance=parltrack) + sync.send(sender=Dossier, instance=other) + + assert sync_dossiers.call_args_list == [mock.call('parltrack')] diff --git a/representatives_votes/signals.py b/representatives_votes/signals.py new file mode 100644 index 0000000..cf5b575 --- /dev/null +++ b/representatives_votes/signals.py @@ -0,0 +1,3 @@ +from django import dispatch + +sync = dispatch.Signal(providing_args=['instance']) diff --git a/representatives_votes/tests/settings.py b/representatives_votes/tests/settings.py index 7bdbf35..cb9bf35 100644 --- a/representatives_votes/tests/settings.py +++ b/representatives_votes/tests/settings.py @@ -16,6 +16,7 @@ INSTALLED_APPS = ( 'rest_framework', 'representatives', 'representatives_votes', + 'representatives_votes.contrib.parltrack', ) DEBUG = True -- GitLab