Exemplo n.º 1
0
    def test_merge_accounts_o2m_unique(self):
        """Test merging of o2m unique elements of two accounts"""
        # Setup
        press = helpers.create_press()
        journal, _ = helpers.create_journals()
        from_account = models.Account.objects.create(email="*****@*****.**")
        to_account = models.Account.objects.create(email="*****@*****.**")
        role_obj = models.Role.objects.create(name="t", slug="t")
        role = models.AccountRole.objects.create(
            user=from_account,
            journal=journal,
            role=role_obj,
        )
        unique_violation = models.AccountRole.objects.create(user=to_account,
                                                             journal=journal,
                                                             role=role_obj)

        # Test
        merge_models(from_account, to_account)

        # Assert
        self.assertTrue(
            unique_violation in to_account.accountrole_set.all(),
            msg="Failed to merge user models",
        )
Exemplo n.º 2
0
def merge_users(request):
    users = core_models.Account.objects.all()
    if request.POST:
        from_id = request.POST["from"]
        to_id = request.POST["to"]
        if from_id == to_id:
            messages.add_message(
                request,
                messages.ERROR,
                "Can't merge a user with itself",
            )
        return redirect(reverse('merge_users'))

        try:
            from_acc = core_models.Account.objects.get(id=from_id)
            to_acc = core_models.Account.objects.get(id=to_id)
        except core_models.Account.DoesNotExist:
            messages.add_message(
                request,
                messages.ERROR,
                "Can't find users with ids %d, %d" % (from_id, to_id),
            )
        merge_models(from_acc, to_acc)
        messages.add_message(
            request,
            messages.INFO,
            "Merged %s into %s" % (from_acc.username, to_acc.username),
        )
        return redirect(reverse('merge_users'))

    template = "press/merge_users.html"
    context = {
        'users': users,
    }
    return render(request, template, context)
Exemplo n.º 3
0
    def test_merge_accounts_m2m(self):
        """Test merging of m2m elements when mergint two accounts"""
        # Setup
        from_account = models.Account.objects.create(email="*****@*****.**")
        to_account = models.Account.objects.create(email="*****@*****.**")
        interest = models.Interest.objects.create(name="test")
        from_account.interest.add(interest)

        # Test
        merge_models(from_account, to_account)

        # Assert
        self.assertTrue(
            interest in to_account.interest.all(),
            msg="Failed to merge user models",
        )
Exemplo n.º 4
0
def handle_unique_username_violation(same_accounts, apps):
    AccountRole = apps.get_model('core', 'Accountrole')
    Account = apps.get_model('core', 'Account')
    real_account = None

    # Try checking if one has logged in
    has_logged_in = same_accounts.filter(last_login__isnull=False)
    if has_logged_in.count == 1:
        real_account = has_logged_in[0]

    if real_account is None:
        # Try checking if one account has a non-author role:
        account_roles_ids = AccountRole.objects.filter(
            user__in=same_accounts,
        ).exclude(
            role__slug="author"
        ).values_list("user", flat=True)
        if len(set(account_roles_ids)) == 1:
            real_account = Account.objects.get(id=account_roles_ids[0])

    if real_account is None:
        # Try checking if one account is author
        authors = []
        for acc in same_accounts:
            if acc.article_set.exists():
                authors.append(acc)
        if len(authors) >2:
            raise Exception(
                "Can't workout the real user for username %s" % acc.username)
        elif len(authors) == 1:
            real_account = authors[0]

    if real_account is None:
        # At this point no account is an author, has a role or logged in
        # Declare any of them the real author
        real_account = same_accounts[0]

    merged = set()
    for acc in same_accounts:
        if acc != real_account:
            merge_models(acc, real_account)
            merged.add(acc.username.lower())
    real_account.username = real_account.username.lower()
    real_account.save()
    return merged
Exemplo n.º 5
0
    def test_merge_accounts_m2m_through(self):
        """Test merging of m2m declaring 'through' when merging two accounts"""
        # Setup
        from_account = models.Account.objects.create(email="*****@*****.**")
        to_account = models.Account.objects.create(email="*****@*****.**")
        press = helpers.create_press()
        journal, _ = helpers.create_journals()
        issue = journal_models.Issue.objects.create(journal=journal)

        # Issue editors have a custom through model
        issue_editor = journal_models.IssueEditor.objects.create(
            issue=issue,
            account=from_account,
        )

        # Test
        merge_models(from_account, to_account)

        # Assert
        self.assertTrue(
            to_account.issueeditor_set.filter(issue=issue).exists(),
            msg="Failed to merge user models",
        )