support adding and removing multiple tags at once using autocomplete multiselect field

This commit is contained in:
Nick Sweeting 2021-04-10 04:11:32 -04:00
parent a92e96500a
commit 722f530c80
3 changed files with 42 additions and 10 deletions

3
.gitmodules vendored
View file

@ -26,3 +26,6 @@
[submodule "archivebox/vendor/python-atomicwrites"] [submodule "archivebox/vendor/python-atomicwrites"]
path = archivebox/vendor/python-atomicwrites path = archivebox/vendor/python-atomicwrites
url = https://github.com/untitaker/python-atomicwrites url = https://github.com/untitaker/python-atomicwrites
[submodule "archivebox/vendor/tzlocal"]
path = archivebox/vendor/tzlocal
url = https://github.com/regebro/tzlocal

View file

@ -45,10 +45,36 @@ class TagInline(admin.TabularInline):
model = Snapshot.tags.through model = Snapshot.tags.through
from django.contrib.admin.helpers import ActionForm from django.contrib.admin.helpers import ActionForm
from django.contrib.admin.widgets import AutocompleteSelectMultiple
class AutocompleteTags:
model = Tag
search_fields = ['name']
class AutocompleteTagsAdminStub:
name = 'admin'
class SnapshotActionForm(ActionForm): class SnapshotActionForm(ActionForm):
tag = forms.ModelChoiceField(queryset=Tag.objects.all(), required=False) tags = forms.ModelMultipleChoiceField(
queryset=Tag.objects.all(),
required=False,
widget=AutocompleteSelectMultiple(
AutocompleteTags(),
AutocompleteTagsAdminStub(),
),
)
# TODO: allow selecting actions for specific extractors? is this useful?
# EXTRACTOR_CHOICES = [
# (name, name.title())
# for name, _, _ in get_default_archive_methods()
# ]
# extractor = forms.ChoiceField(
# choices=EXTRACTOR_CHOICES,
# required=False,
# widget=forms.MultileChoiceField(attrs={'class': "form-control"})
# )
class SnapshotAdmin(SearchResultsAdminMixin, admin.ModelAdmin): class SnapshotAdmin(SearchResultsAdminMixin, admin.ModelAdmin):
@ -59,7 +85,7 @@ class SnapshotAdmin(SearchResultsAdminMixin, admin.ModelAdmin):
fields = ('timestamp', 'url', 'title', 'tags', *readonly_fields) fields = ('timestamp', 'url', 'title', 'tags', *readonly_fields)
list_filter = ('added', 'updated', 'tags', 'archiveresult__status') list_filter = ('added', 'updated', 'tags', 'archiveresult__status')
ordering = ['-added'] ordering = ['-added']
actions = ['delete_snapshots', 'overwrite_snapshots', 'update_snapshots', 'update_titles', 'verify_snapshots', 'add_tag', 'remove_tag'] actions = ['add_tags', 'remove_tags', 'update_titles', 'update_snapshots', 'resnapshot_snapshot', 'overwrite_snapshots', 'delete_snapshots']
autocomplete_fields = ['tags'] autocomplete_fields = ['tags']
inlines = [ArchiveResultInline] inlines = [ArchiveResultInline]
list_per_page = SNAPSHOTS_PER_PAGE list_per_page = SNAPSHOTS_PER_PAGE
@ -212,19 +238,21 @@ class SnapshotAdmin(SearchResultsAdminMixin, admin.ModelAdmin):
delete_snapshots.short_description = "Delete" delete_snapshots.short_description = "Delete"
def add_tag(self, request, queryset): def add_tags(self, request, queryset):
tag = request.POST['tag'] tags = request.POST.getlist('tags')
print('[+] Adding tags', tags, 'to Snapshots', queryset)
for obj in queryset: for obj in queryset:
obj.tags.add(tag) obj.tags.add(*tags)
add_tag.short_description = "Add tag" add_tags.short_description = "+"
def remove_tag(self, request, queryset): def remove_tags(self, request, queryset):
tag = request.POST['tag'] tags = request.POST.getlist('tags')
print('[-] Removing tags', tags, 'to Snapshots', queryset)
for obj in queryset: for obj in queryset:
obj.tags.remove(tag) obj.tags.remove(*tags)
remove_tag.short_description = "Remove tag" remove_tags.short_description = ""

1
archivebox/vendor/tzlocal vendored Submodule

@ -0,0 +1 @@
Subproject commit c5282c6feded0d576937c0dcdf1f4fd00a95fbee