def test_multiple_identifiers_found(self, faked_get):
     popolo_source = PopoloSource.objects.create(
         url='http://example.com/single-person.json')
     alice = Person.objects.create(name='Alice')
     alice.identifiers.create(scheme='popit-person', identifier='a1b2')
     bob_with_alice_id = Person.objects.create(name='Bob')
     bob_with_alice_id.identifiers.create(scheme='popit-person',
                                          identifier='a1b2')
     LinkToPopoloSource.objects.create(
         object_id=alice.id,
         content_type=ContentType.objects.get_for_model(alice),
         popolo_source=popolo_source)
     LinkToPopoloSource.objects.create(
         object_id=bob_with_alice_id.id,
         content_type=ContentType.objects.get_for_model(bob_with_alice_id),
         popolo_source=popolo_source)
     # Now there should be multiple identifiers found when
     # importing Alice from source:
     importer = PopoloSourceImporter(popolo_source)
     with capture_output() as (out, err):
         with self.assertRaisesRegexp(
                 Person.MultipleObjectsReturned,
                 r"^Unexpectedly found more than 1 objects matching PopoloSource object, collection 'person' and ID 'a1b2' - found 2 instead.$"
         ):
             importer.update_from_source()
 def test_unknown_collection(self, faked_get):
     popolo_source = PopoloSource.objects.create(
         url='http://example.com/single-person.json')
     importer = PopoloSourceImporter(popolo_source)
     with self.assertRaisesRegexp(Exception,
                                  r"Unknown collection 'not-a-collection'"):
         importer.get_existing_django_object('not-a-collection', 'y1z2')
Esempio n. 3
0
 def relate_with_persons_from_popolo_json(self, popolo_source):
     try:
         importer = PopoloSourceImporter(popolo_source,
                                         id_prefix='popolo:',
                                         truncate='yes',
                                         id_schemes_to_preserve={
                                             'person': {
                                                 'popit_id', 'popit_url',
                                                 'popit_django_person_id',
                                                 'popolo_uri'
                                             }
                                         })
         importer.add_observer(ExtraIdentifierCreator(popolo_source))
         importer.add_observer(InstanceMembershipUpdater(self))
         person_tracker = PersonTracker()
         importer.add_observer(person_tracker)
         importer.update_from_source()
         # Now update the contacts - note that we can't do this in
         # an observer, because when you're notified of updates to
         # a person their memberships won't be up to date yet - we
         # need to wait until the end.
         for person in person_tracker.persons_present:
             create_contactos(person, self)
         for person in person_tracker.persons_deleted:
             # Make sure any Contact objects for people who have been
             # deleted are disabled:
             Contact.objects.filter(writeitinstance=self,
                                    person=person).update(enabled=False)
     except ConnectionError, e:
         self.do_something_with_a_vanished_popit_api_instance(popolo_source)
         e.message = _('We could not connect with the URL')
         return (False, e)
Esempio n. 4
0
 def relate_with_persons_from_popolo_json(self, popolo_source):
     try:
         importer = PopoloSourceImporter(
             popolo_source,
             id_prefix='popolo:',
             truncate='yes',
             id_schemes_to_preserve={
                 'person': {'popit_id', 'popit_url',
                            'popit_django_person_id', 'popolo_uri'}
             })
         importer.add_observer(ExtraIdentifierCreator(popolo_source))
         importer.add_observer(InstanceMembershipUpdater(self))
         person_tracker = PersonTracker()
         importer.add_observer(person_tracker)
         importer.update_from_source()
         # Now update the contacts - note that we can't do this in
         # an observer, because when you're notified of updates to
         # a person their memberships won't be up to date yet - we
         # need to wait until the end.
         for person in person_tracker.persons_present:
             create_contactos(person, self)
         for person in person_tracker.persons_deleted:
             # Make sure any Contact objects for people who have been
             # deleted are disabled:
             Contact.objects.filter(
                 writeitinstance=self.writeitinstance,
                 person=person).update(enabled=False)
     except ConnectionError, e:
         self.do_something_with_a_vanished_popit_api_instance(popolo_source)
         e.message = _('We could not connect with the URL')
         return (False, e)
 def test_more_popolo_collections(self, faked_get):
     popolo_source = PopoloSource.objects.create(
         url='http://example.com/more-collections.json')
     importer = PopoloSourceImporter(popolo_source)
     importer.update_from_source()
     # Check all the expected links have been created. Note that
     # this doesn't include ContactDetail, Identifier, Source or
     # other objects related to the top-level collections - it's
     # just the top-level objects.
     links_cts = LinkToPopoloSource.objects.values_list(
         'content_type__model', flat=True)
     self.assertEqual(
         sorted(links_cts),
         ['area', 'membership', 'organization', 'person', 'post'])
    def test_deleted_notification(self, fake_get):
        observer = Mock()
        popolo_source = PopoloSource.objects.create(
            url='http://example.com/two-people.json')
        importer = PopoloSourceImporter(popolo_source)
        importer.add_observer(observer)
        importer.update_from_source()
        # notify should now have been called twice, once for each
        # person.
        self.assertEqual(observer.notify.call_count, 2)
        self.assertEqual(observer.notify_deleted.call_count, 0)

        # Now change the URL of the source to one that only has one
        # person:
        popolo_source.url = 'http://example.com/single-person.json'
        popolo_source.save()

        importer.update_from_source()

        # Now notify_deleted should have been triggered once, and
        # notify one more time than before:
        self.assertEqual(observer.notify.call_count, 3)
        self.assertEqual(observer.notify_deleted.call_count, 1)
        observer.notify_deleted.assert_called_once_with(
            'person', Person.objects.get(name='Bob'))
 def test_same_id_mulitiple_sources(self, faked_get):
     popolo_source_a = PopoloSource.objects.create(
         url='http://example.com/single-person.json')
     importer_a = PopoloSourceImporter(popolo_source_a)
     importer_a.update_from_source()
     popolo_source_b = PopoloSource.objects.create(
         url='http://example.com/same-person-different-source.json')
     importer_b = PopoloSourceImporter(popolo_source_b)
     importer_b.update_from_source()
     self.assertEqual(Person.objects.count(), 2)
 def test_deleted_person_reappears(self, faked_get):
     popolo_source = PopoloSource.objects.create(
         url='http://example.com/two-people.json')
     # Create a deleted Alice by hand:
     alice = Person.objects.create(name='Alice')
     alice.identifiers.create(scheme='popit-person', identifier='a1b2')
     LinkToPopoloSource.objects.create(popolo_object=alice,
                                       popolo_source=popolo_source,
                                       deleted_from_source=True)
     # Now try updating from the source:
     importer = PopoloSourceImporter(popolo_source)
     importer.update_from_source()
     # Now try getting the link and checking that Alice is no
     # longer deleted:
     self.assertEqual(2, Person.objects.count())
     self.assertEqual(2, LinkToPopoloSource.objects.count())
     link = LinkToPopoloSource.objects.get(
         object_id=alice.id,
         content_type=ContentType.objects.get_for_model(alice),
         popolo_source=popolo_source)
     self.assertFalse(link.deleted_from_source)
 def handle(self, *args, **options):
     source_arg = options[REQUIRED_ARG]
     create = options['create']
     try:
         ps = get_source(source_arg)
         if create:
             msg = "You specified --create, but that source already exists"
             raise CommandError(msg)
     except PopoloSource.DoesNotExist:
         if create:
             ps = create_source(source_arg)
             print("Created a source for that URL: {0}".format(ps.id))
         else:
             print("That source could not be found.")
             if PopoloSource.objects.exists():
                 print("Did you mean one of the following?")
                 for existing_source in PopoloSource.objects.order_by('pk'):
                     print('{0.id}: {0.url}'.format(existing_source))
             raise CommandError('Source not found')
     print("Attempting to import from {0}".format(repr(ps)))
     importer = PopoloSourceImporter(ps)
     importer.update_from_source()
 def test_second_import_from_same_source_the_same(self, faked_get):
     popolo_source = PopoloSource.objects.create(
         url='http://example.com/single-person.json')
     importer = PopoloSourceImporter(popolo_source)
     importer.update_from_source()
     self.assertEqual(Person.objects.count(), 1)
     importer.update_from_source()
     self.assertEqual(Person.objects.count(), 1)
 def test_import_two_people_twice(self, faked_get):
     popolo_source = PopoloSource.objects.create(
         url='http://example.com/two-people.json')
     importer = PopoloSourceImporter(popolo_source)
     importer.update_from_source()
     self.assertEqual(Person.objects.count(), 2)
     importer.update_from_source()
     self.assertEqual(Person.objects.count(), 2)
 def test_two_people_one_later_removed(self, faked_get):
     popolo_source = PopoloSource.objects.create(
         url='http://example.com/two-people.json')
     importer = PopoloSourceImporter(popolo_source)
     importer.update_from_source()
     # Now change the URL of the source to one that only has one
     # person:
     popolo_source.url = 'http://example.com/single-person.json'
     popolo_source.save()
     importer.update_from_source()
     self.assertEqual(2, LinkToPopoloSource.objects.count())
     deleted_person = LinkToPopoloSource.objects.get(
         deleted_from_source=True)
     self.assertEqual(deleted_person.popolo_object.name, 'Bob')
 def test_missing_source(self, faked_get):
     with self.assertRaises(Exception):
         popolo_source = PopoloSource.objects.create(
             url='http://example.com/does-not-exist.json')
         importer = PopoloSourceImporter(popolo_source)
         importer.update_from_source()