From e9b60d7205aee011558f3f86023a5ef6ccdf135a Mon Sep 17 00:00:00 2001 From: Oliver Zander Date: Fri, 17 Oct 2025 14:15:56 +0200 Subject: [PATCH] send decision mails on grant --- input/admin.py | 34 ++++++++++++++++++++++++++++------ input/utils/mail/__init__.py | 6 +++--- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/input/admin.py b/input/admin.py index aa9e9ea..534097c 100755 --- a/input/admin.py +++ b/input/admin.py @@ -1,9 +1,11 @@ import csv from django.contrib import admin -from django.db import models +from django.db import models, transaction from django.http import HttpResponse +from input.utils.mail import send_decision_mail, send_staff_decision_mail + from .forms import BaseProjectForm from .models import ( Account, @@ -22,6 +24,7 @@ from .models import ( BusinessCard, List, Literature, + TYPE_PROJ, ) @@ -100,8 +103,7 @@ class ProjectAdminForm(BaseProjectForm): return cleaned_data -@admin.register(Project) -class ProjectAdmin(admin.ModelAdmin): +class BaseProjectAdmin(admin.ModelAdmin): save_as = True form = ProjectAdminForm search_fields = ('name', 'pid','finance_id', 'realname', 'start', 'end', 'participants_estimated', 'participants_real', 'cost', 'status', 'end_quartal') @@ -158,19 +160,39 @@ class ProjectAdmin(admin.ModelAdmin): class Media: js = ('dropdown/js/otrs_link.js',) - granted = True + granted: bool def get_queryset(self, request): return super().get_queryset(request).filter(granted=self.granted) +@admin.register(Project) +class ProjectAdmin(BaseProjectAdmin): + granted = True + + @admin.register(ProjectRequest) -class ProjectRequestAdmin(ProjectAdmin): +class ProjectRequestAdmin(BaseProjectAdmin): granted = None + def save_model(self, request, obj: ProjectRequest, form: ProjectAdminForm, change: bool): + super().save_model(request, obj, form, change) + + if obj.granted is None: + return None + + transaction.on_commit(lambda: self.send_decision_mails(obj)) + + return obj.granted + + @staticmethod + def send_decision_mails(obj): + send_decision_mail(obj, TYPE_PROJ, obj.granted) + send_staff_decision_mail(obj, TYPE_PROJ, obj.granted) + @admin.register(ProjectDeclined) -class ProjectDeclinedAdmin(ProjectAdmin): +class ProjectDeclinedAdmin(BaseProjectAdmin): granted = False diff --git a/input/utils/mail/__init__.py b/input/utils/mail/__init__.py index db3a53c..1a12737 100644 --- a/input/utils/mail/__init__.py +++ b/input/utils/mail/__init__.py @@ -21,7 +21,7 @@ def _type_labels(choice: str): Returns (HTML label, plain text label). """ html = TYPE_CHOICES.get(choice, choice) - plain = strip_tags(str(html)) + plain = strip_tags(html) return html, plain @@ -35,8 +35,8 @@ def _decision_context(obj, choice_code: str) -> dict: return { 'data': { 'realname': realname, - 'typestring': type_html, - 'typestring_plain': type_plain, + 'type_label': type_html, + 'type_label_plain': type_plain, 'name': getattr(obj, 'name', None), }, 'project': obj,