def bulk_corporate_approve(modeladmin, request, queryset): """ Allow ePFP Application Administrator to bulk apply corporate approval to selected burns. """ opts = modeladmin.model._meta app_label = opts.app_label if not request.user.has_perm('prescription.can_corporate_approve'): raise PermissionDenied # If any ePFPs are not seeking corporate approval, redirect back to the # regional overview with a message. prescriptions = list(queryset) if any([ prescription.planning_status != prescription.PLANNING_SUBMITTED for prescription in prescriptions ]): modeladmin.message_user( request, _("All ePFPs must be seeking corporate approval. Please review " "your selection and run this action again."), messages.ERROR) url = reverse('admin:prescription_prescription_changelist') return HttpResponseRedirect(url) # The user confirmed they want to carry over the selected burns. if request.POST.get('post'): for prescription in prescriptions: prescription.planning_status = prescription.PLANNING_APPROVED prescription.planning_status_modified = timezone.now() prescription.save() update_permissions(prescription, modeladmin.admin_site, "endorsement", assign_perm) modeladmin.message_user( request, _("Successfully applied approval for %s burns. They are now " "available to continue editing." % len(prescriptions)), messages.SUCCESS) url = reverse('admin:prescription_prescription_changelist') return HttpResponseRedirect(url) title = _("Are you sure you wish to apply corporate approval?") context = { "title": title, "action": 'bulk_corporate_approve', "objects_name": force_text(opts.verbose_name), 'queryset': queryset, "opts": opts, "app_label": app_label, 'action_checkbox_name': helpers.ACTION_CHECKBOX_NAME, } return TemplateResponse( request, "admin/prescription/prescription/bulk_corporate_approve.html", context, current_app=modeladmin.admin_site.name)
def carry_over_burns(modeladmin, request, queryset): """ Carry over the selected burns into another year or season. This strips the burn of any corporate approval, endorsements, and approvals and allows the burn program developer to reschedule and edit the burn again. """ opts = modeladmin.model._meta app_label = opts.app_label if not request.user.has_perm('prescription.can_carry_over'): raise PermissionDenied # Check if the user has selected any closed burns, and redirect them back # to the regional overview with a message if they have. if queryset.filter(status=Prescription.STATUS_CLOSED).count() > 0: modeladmin.message_user(request, _("You cannot carry over closed burns. Please review your " "selection and run this action again."), messages.ERROR) url = reverse('admin:prescription_prescription_changelist') return HttpResponseRedirect(url) # The user confirmed they want to carry over the selected burns. if request.POST.get('post'): for prescription in queryset: prescription.clear_approvals() prescription.carried_over = True prescription.planning_status = prescription.PLANNING_DRAFT prescription.planning_status_modified = timezone.now() prescription.prescribing_officer = None #prescription.planned_year = None prescription.financial_year = '' prescription.save() update_permissions(prescription, modeladmin.admin_site, "endorsement", assign_perm) _create_approvals_pdf(prescription, request) modeladmin.message_user(request, _("Successfully carried over %s burns. They are now available for " "editing again." % queryset.count()), messages.SUCCESS) url = reverse('admin:prescription_prescription_changelist') return HttpResponseRedirect(url) title = _("Are you sure you want to reschedule these burns?") context = { "title": title, "action": 'carry_over_burns', "objects_name": force_text(opts.verbose_name), 'queryset': queryset, "opts": opts, "app_label": app_label, 'action_checkbox_name': helpers.ACTION_CHECKBOX_NAME, } return TemplateResponse( request, "admin/prescription/prescription/carry_over_burns.html", context, current_app=modeladmin.admin_site.name)
def delete_approval_endorsement(modeladmin, request, queryset): opts = modeladmin.model._meta app_label = opts.app_label if not request.user.has_perm('prescription.can_delete_approval'): raise PermissionDenied if len(queryset) > 1: messages.error( request, _("You can only remove approvals and endorsements " "from one ePFP at a time.")) return None # The user has already confirmed the deletion. # Do the deletion and return a None to display the change list view again. # We have already evaluated the queryset at this point and ensured that # there is only one member, just index it directly. if request.POST.get('post'): obj = queryset[0] obj.clear_approvals() msg = 'Delete: Clearing Approvals/Endorsements', 'Burn ID: {}, Deleted by: {}'.format( obj.burn_id, request.user.get_full_name()) logger.warning(msg) support_email('Delete: Clearing Approvals/Endorsements', msg) update_permissions(obj, modeladmin.admin_site, "endorsement", assign_perm) modeladmin.message_user( request, _("Successfully removed endorsements and approval from " "%(id)s - %(name)s (%(season)s).") % { "id": obj.burn_id, "name": obj.name, "season": obj.season }, messages.SUCCESS) # Return None to display the change list page again. return None title = _("Are you sure?") context = { "title": title, "remove": 'all endorsements and approval', "action": 'delete_approval_endorsement', "objects_name": force_text(opts.verbose_name), 'queryset': queryset, "opts": opts, "app_label": app_label, 'action_checkbox_name': helpers.ACTION_CHECKBOX_NAME, } # Display the confirmation page return TemplateResponse(request, modeladmin.remove_selected_confirmation_template, context, current_app=modeladmin.admin_site.name)
def bulk_corporate_approve(modeladmin, request, queryset): """ Allow ePFP Application Administrator to bulk apply corporate approval to selected burns. """ opts = modeladmin.model._meta app_label = opts.app_label if not request.user.has_perm('prescription.can_corporate_approve'): raise PermissionDenied # If any ePFPs are not seeking corporate approval, redirect back to the # regional overview with a message. prescriptions = list(queryset) if any([prescription.planning_status != prescription.PLANNING_SUBMITTED for prescription in prescriptions]): modeladmin.message_user(request, _("All ePFPs must be seeking corporate approval. Please review " "your selection and run this action again."), messages.ERROR) url = reverse('admin:prescription_prescription_changelist') return HttpResponseRedirect(url) # The user confirmed they want to carry over the selected burns. if request.POST.get('post'): for prescription in prescriptions: prescription.planning_status = prescription.PLANNING_APPROVED prescription.planning_status_modified = timezone.now() prescription.save() update_permissions(prescription, modeladmin.admin_site, "endorsement", assign_perm) modeladmin.message_user(request, _("Successfully applied approval for %s burns. They are now " "available to continue editing." % len(prescriptions)), messages.SUCCESS) url = reverse('admin:prescription_prescription_changelist') return HttpResponseRedirect(url) title = _("Are you sure you wish to apply corporate approval?") context = { "title": title, "action": 'bulk_corporate_approve', "objects_name": force_text(opts.verbose_name), 'queryset': queryset, "opts": opts, "app_label": app_label, 'action_checkbox_name': helpers.ACTION_CHECKBOX_NAME, } return TemplateResponse( request, "admin/prescription/prescription/bulk_corporate_approve.html", context, current_app=modeladmin.admin_site.name)
def delete_approval_endorsement(modeladmin, request, queryset): opts = modeladmin.model._meta app_label = opts.app_label if not request.user.has_perm('prescription.can_delete_approval'): raise PermissionDenied if len(queryset) > 1: messages.error(request, _( "You can only remove approvals and endorsements " "from one ePFP at a time.")) return None # The user has already confirmed the deletion. # Do the deletion and return a None to display the change list view again. # We have already evaluated the queryset at this point and ensured that # there is only one member, just index it directly. if request.POST.get('post'): obj = queryset[0] obj.clear_approvals() update_permissions(obj, modeladmin.admin_site, "endorsement", assign_perm) modeladmin.message_user( request, _("Successfully removed endorsements and approval from " "%(id)s - %(name)s (%(season)s).") % { "id": obj.burn_id, "name": obj.name, "season": obj.season }, messages.SUCCESS ) # Return None to display the change list page again. return None title = _("Are you sure?") context = { "title": title, "remove": 'all endorsements and approval', "action": 'delete_approval_endorsement', "objects_name": force_text(opts.verbose_name), 'queryset': queryset, "opts": opts, "app_label": app_label, 'action_checkbox_name': helpers.ACTION_CHECKBOX_NAME, } # Display the confirmation page return TemplateResponse( request, modeladmin.remove_selected_confirmation_template, context, current_app=modeladmin.admin_site.name)
def carry_over_burns(modeladmin, request, queryset): """ Carry over the selected burns into another year or season. This strips the burn of any corporate approval, endorsements, and approvals and allows the burn program developer to reschedule and edit the burn again. """ opts = modeladmin.model._meta app_label = opts.app_label if not request.user.has_perm('prescription.can_carry_over'): raise PermissionDenied # Check if the user has selected any closed burns, and redirect them back # to the regional overview with a message if they have. if queryset.filter(status=Prescription.STATUS_CLOSED).count() > 0: modeladmin.message_user( request, _("You cannot carry over closed burns. Please review your " "selection and run this action again."), messages.ERROR) url = reverse('admin:prescription_prescription_changelist') return HttpResponseRedirect(url) # The user confirmed they want to carry over the selected burns. if request.POST.get('post'): for prescription in queryset: #archive prescription before carry over now = timezone.localtime(timezone.now()) timestamp = now.strftime("%Y-%m-%dT%H%M%S") archivename = "{0}_pre_carry_over_{1}".format( prescription.burn_id, timestamp) with pdflatex(prescription, template="pfp", downloadname=archivename, embed=True, headers=True, title="Prescribed Fire Plan") as pdfresult: if pdfresult.succeed: directory = os.path.join( settings.MEDIA_ROOT, 'snapshots', prescription.financial_year.replace("/", "-"), prescription.burn_id) if not os.path.exists(directory): os.makedirs(directory) shutil.move( pdfresult.pdf_file, os.path.join(directory, "{}.pdf".format(archivename))) else: raise Exception(pdfresult.errormessage) prescription.clear_approvals() prescription.carried_over = True prescription.planning_status = prescription.PLANNING_DRAFT prescription.planning_status_modified = timezone.now() prescription.prescribing_officer = None #prescription.planned_year = None prescription.financial_year = '' prescription.save() update_permissions(prescription, modeladmin.admin_site, "endorsement", assign_perm) _create_approvals_pdf(prescription, request) modeladmin.message_user( request, _("Successfully carried over %s burns. They are now available for " "editing again." % queryset.count()), messages.SUCCESS) url = reverse('admin:prescription_prescription_changelist') return HttpResponseRedirect(url) title = _("Are you sure you want to reschedule these burns?") context = { "title": title, "action": 'carry_over_burns', "objects_name": force_text(opts.verbose_name), 'queryset': queryset, "opts": opts, "app_label": app_label, 'action_checkbox_name': helpers.ACTION_CHECKBOX_NAME, } return TemplateResponse( request, "admin/prescription/prescription/carry_over_burns.html", context, current_app=modeladmin.admin_site.name)