|
1 | | -from django.contrib import admin |
2 | | -from .models import Criteria, ProjectScore |
| 1 | +from django import forms |
| 2 | +from django.contrib import admin, messages |
| 3 | +from django.db.models import Count |
| 4 | +from django.contrib.admin.widgets import FilteredSelectMultiple |
| 5 | +from django.core.exceptions import ValidationError as DjangoValidationError |
| 6 | +from django.shortcuts import redirect |
| 7 | +from django.urls import reverse |
| 8 | + |
| 9 | +from partner_programs.models import PartnerProgramProject |
| 10 | +from projects.models import Project |
| 11 | +from users.models import Expert |
| 12 | + |
| 13 | +from .models import Criteria, ProjectExpertAssignment, ProjectScore |
3 | 14 |
|
4 | 15 |
|
5 | 16 | # Register your models here. |
@@ -28,3 +39,237 @@ def get_criteria_name(self, obj): |
28 | 39 |
|
29 | 40 | def get_project_name(self, obj): |
30 | 41 | return obj.project.name |
| 42 | + |
| 43 | + |
| 44 | +class ProjectExpertAssignmentBulkAddForm(forms.ModelForm): |
| 45 | + partner_program = forms.ModelChoiceField( |
| 46 | + queryset=ProjectExpertAssignment._meta.get_field("partner_program") |
| 47 | + .remote_field.model.objects.all() |
| 48 | + .order_by("name"), |
| 49 | + label="Программа", |
| 50 | + ) |
| 51 | + expert = forms.ModelChoiceField(queryset=Expert.objects.none(), label="Эксперт") |
| 52 | + projects = forms.ModelMultipleChoiceField( |
| 53 | + queryset=Project.objects.none(), |
| 54 | + label="Проекты", |
| 55 | + widget=FilteredSelectMultiple("Проекты", is_stacked=False), |
| 56 | + ) |
| 57 | + |
| 58 | + class Meta: |
| 59 | + model = ProjectExpertAssignment |
| 60 | + fields = ("partner_program", "expert") |
| 61 | + |
| 62 | + def __init__(self, *args, **kwargs): |
| 63 | + super().__init__(*args, **kwargs) |
| 64 | + self.fields["expert"].queryset = Expert.objects.select_related("user").order_by( |
| 65 | + "user__last_name", "user__first_name" |
| 66 | + ) |
| 67 | + self.fields["projects"].queryset = ( |
| 68 | + Project.objects.filter(program_links__isnull=False, draft=False) |
| 69 | + .distinct() |
| 70 | + .order_by("name") |
| 71 | + ) |
| 72 | + |
| 73 | + program_id = None |
| 74 | + if self.is_bound: |
| 75 | + program_id = self.data.get("partner_program") |
| 76 | + else: |
| 77 | + program_id = self.initial.get("partner_program") |
| 78 | + |
| 79 | + if program_id: |
| 80 | + self.fields["expert"].queryset = ( |
| 81 | + Expert.objects.filter(programs__id=program_id) |
| 82 | + .select_related("user") |
| 83 | + .order_by("user__last_name", "user__first_name") |
| 84 | + ) |
| 85 | + self.fields["projects"].queryset = ( |
| 86 | + Project.objects.filter( |
| 87 | + program_links__partner_program_id=program_id, |
| 88 | + draft=False, |
| 89 | + ) |
| 90 | + .distinct() |
| 91 | + .order_by("name") |
| 92 | + ) |
| 93 | + |
| 94 | + def clean(self): |
| 95 | + cleaned_data = super().clean() |
| 96 | + program = cleaned_data.get("partner_program") |
| 97 | + expert = cleaned_data.get("expert") |
| 98 | + projects = cleaned_data.get("projects") |
| 99 | + |
| 100 | + if not program or not expert or not projects: |
| 101 | + return cleaned_data |
| 102 | + |
| 103 | + if not expert.programs.filter(id=program.id).exists(): |
| 104 | + self.add_error("expert", "Эксперт не состоит в выбранной программе.") |
| 105 | + |
| 106 | + linked_project_ids = set( |
| 107 | + PartnerProgramProject.objects.filter( |
| 108 | + partner_program=program, |
| 109 | + project_id__in=projects.values_list("id", flat=True), |
| 110 | + ).values_list("project_id", flat=True) |
| 111 | + ) |
| 112 | + if len(linked_project_ids) != len(projects): |
| 113 | + self.add_error("projects", "Выбраны проекты, не привязанные к программе.") |
| 114 | + |
| 115 | + selected_project_ids = list(projects.values_list("id", flat=True)) |
| 116 | + existing_ids = set( |
| 117 | + ProjectExpertAssignment.objects.filter( |
| 118 | + partner_program=program, |
| 119 | + expert=expert, |
| 120 | + project_id__in=selected_project_ids, |
| 121 | + ).values_list("project_id", flat=True) |
| 122 | + ) |
| 123 | + |
| 124 | + blocked_by_limit_ids = set() |
| 125 | + if program.max_project_rates: |
| 126 | + blocked_by_limit_ids = set( |
| 127 | + ProjectExpertAssignment.objects.filter( |
| 128 | + partner_program=program, |
| 129 | + project_id__in=selected_project_ids, |
| 130 | + ) |
| 131 | + .values("project_id") |
| 132 | + .annotate(total=Count("id")) |
| 133 | + .filter(total__gte=program.max_project_rates) |
| 134 | + .values_list("project_id", flat=True) |
| 135 | + ) |
| 136 | + |
| 137 | + actionable = [ |
| 138 | + project_id |
| 139 | + for project_id in selected_project_ids |
| 140 | + if project_id not in existing_ids and project_id not in blocked_by_limit_ids |
| 141 | + ] |
| 142 | + if not actionable: |
| 143 | + raise forms.ValidationError( |
| 144 | + "Нет проектов для нового назначения: все уже назначены или достигли лимита." |
| 145 | + ) |
| 146 | + |
| 147 | + return cleaned_data |
| 148 | + |
| 149 | + |
| 150 | +@admin.register(ProjectExpertAssignment) |
| 151 | +class ProjectExpertAssignmentAdmin(admin.ModelAdmin): |
| 152 | + list_display = ( |
| 153 | + "id", |
| 154 | + "partner_program", |
| 155 | + "project", |
| 156 | + "expert", |
| 157 | + "datetime_created", |
| 158 | + ) |
| 159 | + list_filter = ("partner_program",) |
| 160 | + search_fields = ( |
| 161 | + "project__name", |
| 162 | + "partner_program__name", |
| 163 | + "expert__user__first_name", |
| 164 | + "expert__user__last_name", |
| 165 | + "expert__user__email", |
| 166 | + ) |
| 167 | + |
| 168 | + def get_form(self, request, obj=None, **kwargs): |
| 169 | + if obj is None: |
| 170 | + kwargs["form"] = ProjectExpertAssignmentBulkAddForm |
| 171 | + return super().get_form(request, obj, **kwargs) |
| 172 | + |
| 173 | + def get_fields(self, request, obj=None): |
| 174 | + if obj is None: |
| 175 | + return ("partner_program", "expert", "projects") |
| 176 | + return ("partner_program", "project", "expert") |
| 177 | + |
| 178 | + def save_model(self, request, obj, form, change): |
| 179 | + if change: |
| 180 | + return super().save_model(request, obj, form, change) |
| 181 | + |
| 182 | + program = form.cleaned_data["partner_program"] |
| 183 | + expert = form.cleaned_data["expert"] |
| 184 | + projects = list(form.cleaned_data["projects"]) |
| 185 | + selected_project_ids = [project.id for project in projects] |
| 186 | + |
| 187 | + existing_ids = set( |
| 188 | + ProjectExpertAssignment.objects.filter( |
| 189 | + partner_program=program, |
| 190 | + expert=expert, |
| 191 | + project_id__in=selected_project_ids, |
| 192 | + ).values_list("project_id", flat=True) |
| 193 | + ) |
| 194 | + |
| 195 | + blocked_by_limit_ids = set() |
| 196 | + if program.max_project_rates: |
| 197 | + blocked_by_limit_ids = set( |
| 198 | + ProjectExpertAssignment.objects.filter( |
| 199 | + partner_program=program, |
| 200 | + project_id__in=selected_project_ids, |
| 201 | + ) |
| 202 | + .values("project_id") |
| 203 | + .annotate(total=Count("id")) |
| 204 | + .filter(total__gte=program.max_project_rates) |
| 205 | + .values_list("project_id", flat=True) |
| 206 | + ) |
| 207 | + |
| 208 | + actionable_projects = [ |
| 209 | + project |
| 210 | + for project in projects |
| 211 | + if project.id not in existing_ids and project.id not in blocked_by_limit_ids |
| 212 | + ] |
| 213 | + |
| 214 | + primary_project = actionable_projects[0] |
| 215 | + obj.partner_program = program |
| 216 | + obj.expert = expert |
| 217 | + obj.project = primary_project |
| 218 | + super().save_model(request, obj, form, change) |
| 219 | + |
| 220 | + created = 1 |
| 221 | + skipped = len(existing_ids) |
| 222 | + failed = len(blocked_by_limit_ids) |
| 223 | + |
| 224 | + for project in actionable_projects[1:]: |
| 225 | + try: |
| 226 | + ProjectExpertAssignment.objects.create( |
| 227 | + partner_program=program, |
| 228 | + project=project, |
| 229 | + expert=expert, |
| 230 | + ) |
| 231 | + created += 1 |
| 232 | + except DjangoValidationError: |
| 233 | + failed += 1 |
| 234 | + |
| 235 | + self.message_user( |
| 236 | + request, |
| 237 | + ( |
| 238 | + f"Назначения обработаны. Создано: {created}, " |
| 239 | + f"уже существовало: {skipped}, с ошибкой: {failed}." |
| 240 | + ), |
| 241 | + level=messages.SUCCESS if failed == 0 else messages.WARNING, |
| 242 | + ) |
| 243 | + |
| 244 | + def delete_view(self, request, object_id, extra_context=None): |
| 245 | + obj = self.get_object(request, object_id) |
| 246 | + if obj and obj.has_scores(): |
| 247 | + self.message_user( |
| 248 | + request, |
| 249 | + "Нельзя удалить назначение: эксперт уже оценил этот проект.", |
| 250 | + level=messages.ERROR, |
| 251 | + ) |
| 252 | + return redirect( |
| 253 | + reverse( |
| 254 | + "admin:project_rates_projectexpertassignment_change", |
| 255 | + args=[object_id], |
| 256 | + ) |
| 257 | + ) |
| 258 | + return super().delete_view(request, object_id, extra_context=extra_context) |
| 259 | + |
| 260 | + def delete_queryset(self, request, queryset): |
| 261 | + blocked = 0 |
| 262 | + for obj in queryset: |
| 263 | + if obj.has_scores(): |
| 264 | + blocked += 1 |
| 265 | + continue |
| 266 | + obj.delete() |
| 267 | + if blocked: |
| 268 | + self.message_user( |
| 269 | + request, |
| 270 | + ( |
| 271 | + "Часть назначений не удалена, потому что по ним уже выставлены оценки: " |
| 272 | + f"{blocked}" |
| 273 | + ), |
| 274 | + level=messages.WARNING, |
| 275 | + ) |
0 commit comments