[bug 654329] Provide batch-archiving and unarchiving of documents.

This commit is contained in:
Erik Rose 2011-05-03 14:55:18 -07:00
Родитель 0689f77a50
Коммит 9b15871d40
3 изменённых файлов: 71 добавлений и 5 удалений

Просмотреть файл

@ -5,12 +5,47 @@ from wiki.models import Document
class DocumentAdmin(admin.ModelAdmin):
exclude = ('tags',)
list_display = ('locale', 'title', 'category', 'is_localizable')
list_display = ('locale', 'title', 'category', 'is_localizable',
'is_archived')
list_display_links = ('title',)
list_filter = ('is_template', 'is_localizable', 'category', 'locale')
raw_id_fields = ('parent',)
readonly_fields = ('id', 'current_revision')
search_fields = ('title',)
@staticmethod
def _set_archival(queryset, should_be_archived):
"""Set archival bit of documents, percolating up to parents as well."""
for doc in queryset:
# If this is a child, change its parent instead, and saving the
# parent will change all the children to match.
doc_to_change = doc.parent or doc
doc_to_change.is_archived = should_be_archived
doc_to_change.save()
def _show_archival_message(self, request, queryset, verb):
count = len(queryset)
phrase = (
'document, along with its English version or translations, was '
'marked as '
if count == 1 else
'documents, along with their English versions or translations, '
'were marked as ')
self.message_user(request, '%s %s %s.' % (count, phrase, verb))
def archive(self, request, queryset):
"""Mark several documents as obsolete."""
self._set_archival(queryset, True)
self._show_archival_message(request, queryset, 'obsolete')
archive.short_description = 'Mark as obsolete'
def unarchive(self, request, queryset):
"""Mark several documents as not obsolete."""
self._set_archival(queryset, False)
self._show_archival_message(request, queryset, 'no longer obsolete')
unarchive.short_description = 'Mark as not obsolete'
actions = [archive, unarchive]
admin.site.register(Document, DocumentAdmin)

Просмотреть файл

@ -183,10 +183,11 @@ class Document(NotificationsMixin, ModelBase, BigVocabTaggableMixin):
# A document's is_archived flag must match that of its parent. If it has no
# parent, it can do what it wants. This invariant is enforced in save().
is_archived = models.BooleanField(
default=False, db_index=True, help_text=_lazy(
u'If checked, this wiki page will be hidden from basic searches and '
'dashboards. When viewed, the page will warn that it is no longer '
'maintained.'))
default=False, db_index=True, verbose_name='is obsolete',
help_text=_lazy(
u'If checked, this wiki page will be hidden from basic searches '
'and dashboards. When viewed, the page will warn that it is no ',
'longer maintained.'))
# firefox_versions,
@ -257,6 +258,9 @@ class Document(NotificationsMixin, ModelBase, BigVocabTaggableMixin):
"""
if self.parent:
# We always set the child according to the parent rather than vice
# versa, because we do not expose an Archived checkbox in the
# translation UI.
setattr(self, attr, getattr(self.parent, attr))
else: # An article cannot have both a parent and children.
# Make my children the same as me:

Просмотреть файл

@ -0,0 +1,27 @@
from nose.tools import eq_
from sumo.tests import TestCase
from wiki.admin import DocumentAdmin
from wiki.models import Document
from wiki.tests import document, translated_revision
class ArchiveTests(TestCase):
"""Tests for the archival bit flipping in the admin UI"""
def test_inheritance(self):
"""Make sure parent/child equality of is_archived is maintained."""
# Set up a child and a parent and an orphan (all false) and something
# true.
child = translated_revision(save=True).document
parent = translated_revision(save=True).document.parent
orphan = document(save=True)
true = document(is_archived=True, save=True)
# Batch-clear the archival bits:
DocumentAdmin._set_archival(Document.objects.all(), True)
# Assert the child of the parent and the parent of the child (along
# with everything else) became (or stayed) true:
eq_(Document.objects.filter(is_archived=True).count(), 6)
# We didn't lose any, and they're all true.