diff --git a/workbaskets/admin.py b/workbaskets/admin.py index b6b7ec357..8cff2a035 100644 --- a/workbaskets/admin.py +++ b/workbaskets/admin.py @@ -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 @@ -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()) return super().response_change(request, obj) diff --git a/workbaskets/templates/admin/workbaskets/change_form.html b/workbaskets/templates/admin/workbaskets/change_form.html index 8e48a0269..74d421d00 100644 --- a/workbaskets/templates/admin/workbaskets/change_form.html +++ b/workbaskets/templates/admin/workbaskets/change_form.html @@ -6,8 +6,41 @@ {% block submit_buttons_bottom %} {{ block.super }} + {% if request.user.is_superuser %} +