Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds ability to self publish workbaskets via the django admin workbasket view #1292

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions workbaskets/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django import forms
from django.conf import settings
from django.contrib import admin
from django.contrib import admin, messages
from django.contrib.admin.views.decorators import staff_member_required
from django.contrib.admin.widgets import AdminTextInputWidget
from django.http import HttpResponseRedirect
Expand Down Expand Up @@ -118,7 +118,27 @@ def response_change(self, request, obj):
f"Deleted {tracked_model_count} TrackedModel(s) in "
f"{transaction_count} from WorkBasket.",
)
return HttpResponseRedirect(".")
if "_self-publish" in request.POST:
# check if user is super user
if not request.user.is_superuser:
self.message_user(request, "You do not have permission to perform this action.", level=messages.ERROR)
return HttpResponseRedirect(request.get_full_path())

# check if workbasket status is 'editing'
if obj.status != 'EDITING':
messages.error(request, "The workbasket is not in EDITING status.")
return HttpResponseRedirect(request.get_full_path())

# check if rule checks have passed
if obj.unchecked_or_errored_transactions.exists():
messages.error(request, "Rule checks have not passed on the current contents of this workbasket.")
return HttpResponseRedirect(request.get_full_path())

obj.full_clean()
obj.approve(user=request.user.id, scheme_name=settings.TRANSACTION_SCHEMA)
obj.status = "PUBLISHED"
obj.save()
return HttpResponseRedirect(request.get_full_path())
Comment on lines +121 to +141
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has the potential to make important and far reaching changes to published data. I think it probably needs accompanying unit tests.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This workbasket transition to PUBLISHED is probably better placed on the WorkBasket model to make it less of a back channel approach to workbasket state management and publishing. Probably a new transition (using the django_fsm.transition decorator), that more or less combines queue and cds_confirmed.

Copy link
Collaborator

@paulpepper-trade paulpepper-trade Sep 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It also sounds as though these changes are to support a very specific quotas workflow. To reduce the chances of incorrectly publishing data, validation should ensure only specific data types are present in the workbasket. Were validation to fail, then that'd involve disabling the capability in the UI and preventing the publish transition when implemented on the workbasket.


return super().response_change(request, obj)

Expand Down
51 changes: 42 additions & 9 deletions workbaskets/templates/admin/workbaskets/change_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,55 @@
{% block submit_buttons_bottom %}
{{ block.super }}

{% if request.user.is_superuser %}
<div class="submit-row">
<input
type="submit"
value="Self Publish"
name="_self-publish"
class="default"
title="""
{% if original.status != 'EDITING' %} This button is disabled because the workbasket is not in EDITING
status.
{# {% elif workbasket.unchecked_or_errored_transactions.exists() %}This button is disabled because the workbasket rule check task status is not SUCCESS.#}
{% elif original.rule_check_task_status != 'SUCCESS' %}This button is disabled because the workbasket rule
check task status is not SUCCESS.
{% else %}Click to self publish this workbasket.
{% endif %}
"""
{% if original.status != 'EDITING' or original.rule_check_task_status != 'SUCCESS' %}
disabled{% endif %}
>
<span class="button-description">
{% if original.status != 'EDITING' %}
<i>This button is disabled because the workbasket is not in EDITING status.</i>
{% elif original.rule_check_task_status != 'SUCCESS' %}
<i>This button is disabled because it contains transactions that have not passed rule checks.</i>
{% else %}
<i>Click to Self Publish workbasket.</i>
{% endif %}
<br>
Warning: This will publish the workbasket <Strong>only</Strong> for TAP. It will <Strong>not</Strong> be sent to other parties e.g. Channel Islands, HMRC etc.
</span>
</div>
{% endif %}

{% if original.status == 'EDITING' or original.status == 'ARCHIVED' %}
<div class="submit-row">
<div class="submit-row">
<span>
{{ original.tracked_models.count }}
tracked model{{ original.tracked_models.count|pluralize }}
/
{{ original.transactions.count }}
transaction{{ original.transactions.count|pluralize }}
</span>
<input
type="submit"
style="background-color:#a41515"
value="Delete WorkBasket Contents"
name="_clear-workbasket"
{% if not original.tracked_models.count and not original.transactions.count %}disabled{% endif %}
>
</div>
<input
type="submit"
style="background-color:#a41515"
value="Delete WorkBasket Contents"
name="_clear-workbasket"
{% if not original.tracked_models.count and not original.transactions.count %}disabled{% endif %}
>
</div>
{% endif %}
{% endblock %}
Loading