Exemplo n.º 1
0
def delete(request, report_id):
    report = get_object_or_404(Report, pk=report_id)
    if request.method == "POST":
        report.delete()
        messages.success(request, "Report deleted!")
        return redirect("reports-list")

    related_objects = list(will_be_deleted_with(report))

    return render(request, "delete.html", {
        "object": report,
        "will_be_deleted_with": related_objects,
    })
Exemplo n.º 2
0
def delete(request, subscription_id):
    subscription = get_object_or_404(UserNotificationQuery, pk=subscription_id)
    if request.method == 'POST':
        subscription.delete()
        messages.success(request, 'Subscription deleted!')
        return redirect('notifications-admin-list')

    related_objects = list(will_be_deleted_with(subscription))

    return render(request, 'delete.html', {
        'object': subscription,
        'will_be_deleted_with': related_objects,
    })
Exemplo n.º 3
0
    def test_will_be_deleted_with(self):
        user = self.create_user(username='******', groups=('group-a', 'group-b'))
        self.create_user(username='******', groups=('group-a'))
        self.create_user(username='******', groups=('group-a'))

        results = will_be_deleted_with(user)
        record_type, records = next(results)

        # The user shouldn't be in the list of additional objects to be
        # deleted.
        self.assertNotIn(user, records)

        # When the user is deleted, the associated records in
        # auth_user_groups should be deleted too.
        self.assertEqual(record_type._meta.db_table, 'auth_user_groups')
        self.assertEqual(len(records), 2)
        for r in records:
            self.assertEqual(r.user, user)
Exemplo n.º 4
0
    def get_context_data(self, **kwargs):
        obj = super(DeleteView, self).get_object()
        context = super(SpeciesDeleteView, self).get_context_data(**kwargs)
        context['will_be_deleted_with'] = will_be_deleted_with(obj)

        return context
Exemplo n.º 5
0
def remove_duplicate_users(ctx):
    setup()
    from django.apps import apps
    from django.contrib.auth import get_user_model
    from arcutils.db import will_be_deleted_with

    Comment = apps.get_model('comments', 'Comment')
    Image = apps.get_model('images', 'Image')
    Notification = apps.get_model('notifications', 'Notification')
    UserNotificationQuery = apps.get_model('notifications', 'UserNotificationQuery')
    Invite = apps.get_model('reports', 'Invite')
    Report = apps.get_model('reports', 'Report')

    user_model = get_user_model()
    dupes = user_model.objects.raw(
        'SELECT * from "user" u1 '
        'WHERE ('
        '    SELECT count(*) FROM "user" u2 WHERE lower(u2.email) = lower(u1.email )'
        ') > 1 '
        'ORDER BY lower(email)'
    )
    dupes = [d for d in dupes]

    print_info('Found {n} duplicates'.format(n=len(dupes)))

    # Delete any dupes we can.
    # Active and staff users are never deleted.
    # Public users with no associated records will be deleted.
    for user in dupes:
        email = user.email
        objects = list(will_be_deleted_with(user))
        num_objects = len(objects)
        f = locals()
        if user.is_active:
            print('Skipping active user: {email}.'.format_map(f))
        elif user.is_staff:
            print('Skipping inactive staff user: {email}.'.format_map(f))
        elif num_objects == 0:
            print_warning('Deleting {email} will *not* cascade.'.format_map(f))
            if confirm(ctx, 'Delete {email}?'.format_map(f), yes_values=('yes',)):
                print('Okay, deleting {email}...'.format_map(f), end='')
                user.delete()
                dupes.remove(user)
                print('Deleted')
        else:
            print(
                'Deleting {email} would cascade to {num_objects} objects. Skipping.'.format_map(f))

    # Group the remaining duplicates by email address
    grouped_dupes = defaultdict(list)
    for user in dupes:
        email = user.email.lower()
        grouped_dupes[email].append(user)
    grouped_dupes = {email: users for (email, users) in grouped_dupes.items() if len(users) > 1}

    # For each group, find the "best" user (staff > active > inactive).
    # The other users' associated records will be associated with this
    # "winner".
    for email, users in grouped_dupes.items():
        winner = None
        for user in users:
            if user.is_staff:
                winner = user
                break
        if winner is None:
            for user in users:
                if user.is_active:
                    winner = user
                    break
        if winner is None:
            for user in users:
                if user.full_name:
                    winner = user
        if winner is None:
            winner = users[0]
        losers = [user for user in users if user != winner]
        print('Winner:', winner.full_name, '<{0.email}>'.format(winner))
        for loser in losers:
            print('Loser:', loser.full_name, '<{0.email}>'.format(loser))
            print('Re-associating loser objects...', end='')
            Comment.objects.filter(created_by=loser).update(created_by=winner)
            Image.objects.filter(created_by=loser).update(created_by=winner)
            Invite.objects.filter(user=loser).update(user=winner)
            Invite.objects.filter(created_by=loser).update(created_by=winner)
            Notification.objects.filter(user=loser).update(user=winner)
            Report.objects.filter(claimed_by=loser).update(claimed_by=winner)
            Report.objects.filter(created_by=loser).update(created_by=winner)
            UserNotificationQuery.objects.filter(user=loser).update(user=winner)
            print('Done')