Skip to content

Commit

Permalink
Merge pull request #888 from LibraryOfCongress/admin-reopen-feature
Browse files Browse the repository at this point in the history
Admin reopen asset feature
  • Loading branch information
rstorey committed Apr 10, 2019
2 parents aa9d0f8 + e088669 commit afa75a2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
3 changes: 2 additions & 1 deletion concordia/admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from .actions import (
publish_action,
publish_item_action,
reopen_asset_action,
unpublish_action,
unpublish_item_action,
)
Expand Down Expand Up @@ -291,7 +292,7 @@ class AssetAdmin(admin.ModelAdmin, CustomListDisplayFieldsMixin):
"media_type",
"transcription_status",
)
actions = (publish_action, unpublish_action)
actions = (publish_action, reopen_asset_action, unpublish_action)
autocomplete_fields = ("item",)
ordering = ("item__item_id", "sequence")

Expand Down
39 changes: 38 additions & 1 deletion concordia/admin/actions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.contrib import messages
from django.utils.timezone import now

from ..models import Asset
from ..models import Asset, Transcription, TranscriptionStatus


def publish_item_action(modeladmin, request, queryset):
Expand Down Expand Up @@ -57,3 +58,39 @@ def unpublish_action(modeladmin, request, queryset):


unpublish_action.short_description = "Unpublish selected"


def reopen_asset_action(modeladmin, request, queryset):

# Can only reopen completed assets
assets = queryset.filter(transcription_status=TranscriptionStatus.COMPLETED)

# Count the number of assets that will become reopened
count = assets.count()

"""
For each asset, create a new transcription that:
- supersedes the currently-latest transcription
- has rejected set to now
- has reviewed_by set to the current user
- has the same transcription text as the latest transcription
Don't use bulk_create because then the post-save signal will not be sent.
"""
for asset in assets:
latest_transcription = asset.transcription_set.order_by("-pk").first()
new_transcription = Transcription(
supersedes=latest_transcription,
rejected=now(),
reviewed_by=request.user,
text=latest_transcription.text,
asset=asset,
user=request.user,
)
new_transcription.full_clean()
new_transcription.save()

messages.info(request, f"Reopened {count} assets")


reopen_asset_action.short_description = "Reopen selected assets"
2 changes: 2 additions & 0 deletions concordia/templates/admin/concordia/asset/change_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
{% block content %}
{{ block.super }}

<h4>Current status: {{ original.transcription_status }}</h4>

{% if transcriptions %}
<table>
<caption>Transcription History</caption>
Expand Down

0 comments on commit afa75a2

Please sign in to comment.