From ab63623776a5ed26eab7781d0a2a4cfc43569a0d Mon Sep 17 00:00:00 2001 From: Stefan Wehrmeyer Date: Tue, 11 Jun 2024 10:56:49 +0200 Subject: [PATCH] Add merge author action to author admin --- fragdenstaat_de/fds_blog/admin.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/fragdenstaat_de/fds_blog/admin.py b/fragdenstaat_de/fds_blog/admin.py index d7ca9d3f1..e5d15b58e 100644 --- a/fragdenstaat_de/fds_blog/admin.py +++ b/fragdenstaat_de/fds_blog/admin.py @@ -23,7 +23,16 @@ from froide.helper.widgets import TagAutocompleteWidget from .documents import index_article -from .models import Article, ArticleTag, Author, Category, Publication, TaggedArticle +from .models import ( + Article, + ArticleAuthorship, + ArticleTag, + Author, + Category, + LatestArticlesPlugin, + Publication, + TaggedArticle, +) class RelatedPublishedFilter(admin.SimpleListFilter): @@ -97,6 +106,19 @@ class CategoryListFilter(RelatedPublishedFilter): class AuthorAdmin(admin.ModelAdmin): raw_id_fields = ("user",) + actions = ["merge_authors"] + + def merge_authors(self, request, queryset): + assert len(queryset) >= 1 + author = queryset[0] + other_authors = list(queryset)[1:] + for other in other_authors: + ArticleAuthorship.objects.filter(author=other).update(author=author) + plugins = LatestArticlesPlugin.objects.filter(authors=other) + for plugin in plugins: + plugin.authors.remove(other) + plugin.authors.add(author) + other.delete() class AuthorshipInlineAdmin(SortableInlineAdminMixin, admin.TabularInline):