Example #1
0
    def test_can_create_mandragore_user(self):

        self.assertTrue(
            can_create_mandragore_user(
                "new username !!",
                "dummy mandragore id!",
            ),
            "We should be able to create a mandragore user for a nonexistent username and nonexistent mandragore id"  # noqa
        )

        self.assertFalse(
            can_create_mandragore_user(
                self.user.username,
                "dummy mandragore id!",
            ),
            "We should not be able to create a mandragore user for an existing username"
        )

        create_mandragore_profile_for_user("12345", self.user)

        self.assertFalse(
            can_create_mandragore_user(
                "dummy user",
                "12345"
            ),
            "We should not be able to create a mandragore user for an existing mandragore id"
        )
Example #2
0
    def handle(self, *args, **options):

        self.created_or_updated_publishers = []
        self.created_or_updated_journals = []

        persons_to_add = {}

        # The data required to create the user accounts is in Edinum
        # and Mandragore.
        #
        # We need to manually join between the databases, so we keep
        # local association tables for this.
        person_ids_to_fetch = set()
        persons_collections = {}
        collections_journals = {}

        # Retrieve the accounts from Mandragore
        for (username, email, person_id,
             collectionid) in fetch_accounts_from_mandragore():

            user = User.objects.filter(username=username)
            if user.exists():
                user = user.first()
                user_coherent, message = user_coherent_with_mandragore(
                    user, {
                        'username': username,
                        'person_id': person_id
                    }
                )
                if not user_coherent:
                    raise MandragoreError(
                        "Error importing user {}: {}".format(username, message)  # noqa
                    )

            if can_create_mandragore_user(username, person_id):
                person_ids_to_fetch.add(person_id)
                persons_collections[person_id] = collectionid

                user = create_or_update_mandragore_user(
                    person_id, email=email, username=username
                )

                persons_to_add[person_id] = user

        # Update the accounts with the values from Edinum
        for (person_id, firstname,
             middlename, familyname) in fetch_users_from_edinum(
                 person_ids_to_fetch):
            user = persons_to_add[person_id]

            create_or_update_mandragore_user(
                person_id, email=email, username=user.username,
                first_name=firstname, last_name=familyname
            )

        # Add the users to the journal
        for (id, journal_id) in fetch_series_from_edinum(persons_collections.values()):
            collections_journals[id] = journal_id

        for person_id, collection in persons_collections.items():
            try:
                journal_id = collections_journals[collection]
                journal = Journal.objects.get(edinum_id=journal_id)
                to_add = persons_to_add[person_id]
                to_add.save()

                journal.members.add(to_add)
                journal.save()
            except Exception as e:
                log.error(
                    e
                )