Skip to content

Commit

Permalink
Converts rest of actions
Browse files Browse the repository at this point in the history
  • Loading branch information
mrharpo committed Nov 17, 2023
1 parent 5c91afd commit 6f07215
Showing 1 changed file with 133 additions and 9 deletions.
142 changes: 133 additions & 9 deletions chowda/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,13 @@ class BatchView(ClammerModelView):
'combine_batches',
'download_mmif',
]
row_actions: ClassVar[list[Any]] = [
'view',
'edit',
'duplicate_batch',
'start_batch',
'download_mmif',
]

fields: ClassVar[list[Any]] = [
'id',
Expand Down Expand Up @@ -250,6 +257,22 @@ async def is_action_allowed(self, request: Request, name: str) -> bool:
return user.is_clammer or user.is_admin
return await super().is_action_allowed(request, name)

@row_action(
name='start_batch',
text='Start',
confirmation='This might cost money. Are you sure?',
icon_class='fa fa-play',
action_btn_class='btn-outline-success',
submit_btn_text=yes(),
submit_btn_class='btn-success',
form="""
<form>
<input type="checkbox" id="new_mmif" name="new_mmif">
<label for="new_mmif">Start from blank MMIF?</label>
<input type="hidden" name="_" value="">
</form>
""",
)
@action(
name='start_batches',
text='Start',
Expand All @@ -266,8 +289,12 @@ async def is_action_allowed(self, request: Request, name: str) -> bool:
</form>
""",
)
async def start_batches(self, request: Request, pks: List[Any]) -> str:
async def start_batches(
self, request: Request, pks: list[int | str] | int | str
) -> str:
"""Starts a Batch by sending a message to the Argo Event Bus"""
if not isinstance(pks, list):
pks = [pks]
try:
data: FormData = await request.form()
except MultipartParseError as parse_error:
Expand Down Expand Up @@ -312,6 +339,15 @@ async def start_batches(self, request: Request, pks: List[Any]) -> str:
# Display Success message
return f'Started {len(pks)} Batch(es)'

@row_action(
name='duplicate_batch',
text='Duplicate',
confirmation='Duplicate this Batch?',
icon_class='fa fa-copy',
submit_btn_text=yes(),
submit_btn_class='btn-outline-primary',
exclude_from_list=True,
)
@action(
name='duplicate_batches',
text='Duplicate',
Expand All @@ -320,8 +356,12 @@ async def start_batches(self, request: Request, pks: List[Any]) -> str:
submit_btn_text=yes(),
submit_btn_class='btn-outline-primary',
)
async def duplicate_batches(self, request: Request, pks: List[Any]) -> str:
async def duplicate_batches(
self, request: Request, pks: list[int | str] | int | str
) -> str:
"""Create a new batch from the selected batch"""
if not isinstance(pks, list):
pks = [pks]
try:
with Session(engine) as db:
for batch_id in pks:
Expand Down Expand Up @@ -372,18 +412,33 @@ async def combine_batches(self, request: Request, pks: List[Any]) -> str:
# Display Success message
return f'Combined {len(pks)} Batch(es)'

@row_action(
name='download_mmif',
text='Download MMIF',
confirmation='Download all MMIF JSON for this Batch?',
icon_class='fa fa-download',
submit_btn_text=yes() + ' Gimme the MMIF!',
submit_btn_class='btn-outline-primary',
custom_response=True,
action_btn_class='btn-ghost',
)
@action(
name='download_mmif',
text='Download MMIF',
confirmation='Download all MMIF JSON for these Batches?',
icon_class='fa fa-download',
submit_btn_text=yes() + ' Gimme the MMIF!',
submit_btn_text=yes() + ' Gimme ALL the MMIF!',
submit_btn_class='btn-outline-primary',
custom_response=True,
)
async def download_mmif(self, request: Request, pks: List[Any]) -> str:
async def download_mmif(
self, request: Request, pks: list[int | str] | int | str
) -> str:
"""Create a new batch from the selected batch"""
import zipfile

if not isinstance(pks, list):
pks = [pks]
from tempfile import TemporaryDirectory

import boto3
Expand Down Expand Up @@ -472,6 +527,23 @@ class MediaFileView(ClammerModelView):
def can_create(self, request: Request) -> bool:
return get_oauth_user(request).is_admin

@row_action(
name='create_new_batch',
text='Create Batch',
confirmation='Create a Batch from this Media File?',
action_btn_class='btn-ghost-primary',
submit_btn_text=yes(),
form="""
<form>
<div class="mt-3">
<input type="text" class="form-control" name="batch_name"
placeholder="Batch Name">
<textarea class="form-control" name="batch_description"
placeholder="Batch Description"></textarea>
</div>
</form>
""",
)
@action(
name='create_new_batch',
text='Create Batch',
Expand All @@ -489,7 +561,12 @@ def can_create(self, request: Request) -> bool:
</form>
""",
)
async def create_new_batch(self, request: Request, pks: List[Any]) -> str:
async def create_new_batch(
self, request: Request, pks: list[int | str] | int | str
) -> str:
"""Create a new batch from the selected media files"""
if not isinstance(pks, list):
pks = [pks]
with Session(engine) as db:
media_files = db.exec(
select(MediaFile).where(MediaFile.guid.in_(pks))
Expand Down Expand Up @@ -598,11 +675,31 @@ class MMIFView(ChowdaModelView):
]
actions: ClassVar[List[str]] = ['add_to_new_batch', 'add_to_existing_batch']

@row_action(
name='add_to_new_batch',
text='Add to New Batch',
confirmation='Create a Batch from this MMIF?',
action_btn_class='btn-ghost-primary',
icon_class='fa-regular fa-square-plus',
submit_btn_text=yes(),
exclude_from_list=True,
form="""
<form>
<div class="mt-3">
<input type="text" class="form-control" name="batch_name"
placeholder="Batch Name">
<textarea class="form-control" name="batch_description"
placeholder="Batch Description"></textarea>
</div>
</form>
""",
)
@action(
name='add_to_new_batch',
text='Add to New Batch',
confirmation='Create a Batch from these MMIFs?',
action_btn_class='btn-ghost-primary',
icon_class='fa-regular fa-square-plus',
submit_btn_text=yes(),
form="""
<form>
Expand All @@ -615,7 +712,12 @@ class MMIFView(ChowdaModelView):
</form>
""",
)
async def add_to_new_batch(self, request: Request, pks: List[Any]) -> str:
async def add_to_new_batch(
self, request: Request, pks: list[int | str] | int | str
) -> str:
"""Create a new batch from the selected media files"""
if not isinstance(pks, list):
pks = [pks]
try:
data: FormData = await request.form()
with Session(engine) as db:
Expand All @@ -642,11 +744,29 @@ async def add_to_new_batch(self, request: Request, pks: List[Any]) -> str:
except Exception as error:
raise ActionFailed(f'{error!s}') from error

@row_action(
name='add_to_existing_batch',
text='Add to Existing Batch',
confirmation='Which batch should this be added to?',
action_btn_class='btn-ghost-primary',
icon_class='fa-regular fa-square-plus',
submit_btn_text=yes(),
exclude_from_list=True,
form="""
<form>
<div class="mt-3">
<input type="text" class="form-control" name="batch_id"
placeholder="Batch ID">
</div>
</form>
""",
)
@action(
name='add_to_existing_batch',
text='Add to Existing Batch',
confirmation='Which batch should these be added to?',
action_btn_class='btn-ghost-primary',
icon_class='fa-regular fa-square-plus',
submit_btn_text=yes(),
form="""
<form>
Expand All @@ -657,13 +777,17 @@ async def add_to_new_batch(self, request: Request, pks: List[Any]) -> str:
</form>
""",
)
async def add_to_existing_batch(self, request: Request, pks: List[Any]) -> str:
async def add_to_existing_batch(
self, request: Request, pks: list[int | str] | int | str
) -> str:
"""Add MMIFs to an existing batch"""
if not isinstance(pks, list):
pks = [pks]
try:
data: FormData = await request.form()
with Session(engine) as db:
mmifs: List[MMIF] = db.exec(select(MMIF).where(MMIF.id.in_(pks))).all()
batch: Batch = db.get(Batch, data.get('batch_id'))
batch.input_mmifs += mmifs
media_files: List[MediaFile] = [mmif.media_file for mmif in mmifs]

# Check for duplicate Media Files in selection
Expand All @@ -685,7 +809,7 @@ async def add_to_existing_batch(self, request: Request, pks: List[Any]) -> str:
f'{len(existing_media_files)} Media File{s} already exist{"" if s else "s"} in Batch {batch.id}:<br>' # noqa: E501
+ '<br>'.join(existing_media_files)
)

batch.input_mmifs += mmifs
batch.media_files += media_files

db.commit()
Expand Down

0 comments on commit 6f07215

Please sign in to comment.