|
| 1 | +import pandas as pd |
1 | 2 | import tablib |
2 | 3 | import re |
3 | 4 | from django.contrib import admin |
4 | 5 | from django.http import HttpResponse |
5 | 6 | from django.urls import path |
6 | 7 | from django.utils import timezone |
| 8 | + |
7 | 9 | from mailing.views import MailingTemplateRender |
8 | 10 | from partner_programs.models import PartnerProgram, PartnerProgramUserProfile |
| 11 | +from project_rates.models import Criteria, ProjectScore |
| 12 | +from projects.models import Project |
| 13 | +from users.models import Expert |
9 | 14 |
|
10 | 15 |
|
11 | 16 | @admin.register(PartnerProgram) |
12 | 17 | class PartnerProgramAdmin(admin.ModelAdmin): |
13 | | - list_display = ( |
14 | | - "id", |
15 | | - "name", |
16 | | - "tag", |
17 | | - "city", |
18 | | - "datetime_created", |
19 | | - ) |
| 18 | + list_display = ("id", "name", "tag", "city", "datetime_created") |
20 | 19 | list_display_links = ( |
21 | 20 | "id", |
22 | 21 | "name", |
@@ -48,6 +47,11 @@ def get_urls(self): |
48 | 47 | self.admin_site.admin_view(self.mailing), |
49 | 48 | name="partner_programs_mailing", |
50 | 49 | ), |
| 50 | + path( |
| 51 | + "export-rates/<int:object_id>/", |
| 52 | + self.admin_site.admin_view(self.get_export_rates_view), |
| 53 | + name="partner_programs_export_rates", |
| 54 | + ), |
51 | 55 | ] |
52 | 56 | return custom_urls + default_urls |
53 | 57 |
|
@@ -126,6 +130,87 @@ def get_export_file(self, partner_program: PartnerProgram): |
126 | 130 | response.write(binary_data) |
127 | 131 | return response |
128 | 132 |
|
| 133 | + def get_export_rates_view(self, request, object_id): |
| 134 | + criterias = Criteria.objects.filter(partner_program__id=object_id).select_related( |
| 135 | + "partner_program" |
| 136 | + ) |
| 137 | + experts = Expert.objects.filter(programs=object_id).select_related("user") |
| 138 | + scores = ProjectScore.objects.filter(criteria__in=criterias) |
| 139 | + projects = Project.objects.filter(scores__in=scores) |
| 140 | + return self.get_export_rates(criterias, experts, scores, projects) |
| 141 | + |
| 142 | + def get_export_rates(self, criterias, experts, scores, projects): |
| 143 | + col_names = list( |
| 144 | + criterias.exclude(name="Комментарий").values_list("name", flat=True) |
| 145 | + ) |
| 146 | + |
| 147 | + expert_names = [ |
| 148 | + expert.user.first_name + " " + expert.user.last_name for expert in experts |
| 149 | + ] |
| 150 | + |
| 151 | + all_projects_data = [] |
| 152 | + for project in projects: |
| 153 | + project_data = [[project.name, *col_names, "Комментарий"]] |
| 154 | + |
| 155 | + for expert, expert_name in zip(experts, expert_names): |
| 156 | + single_rate_data = [expert_name] |
| 157 | + |
| 158 | + scores_of_expert = [] |
| 159 | + criterias_to_check = criterias.exclude(name="Комментарий") |
| 160 | + for criteria in criterias_to_check: |
| 161 | + checking_score = ( |
| 162 | + scores.filter( |
| 163 | + criteria=criteria, |
| 164 | + user__first_name=expert.user.first_name, |
| 165 | + user__last_name=expert.user.last_name, |
| 166 | + project__name=project.name, |
| 167 | + ) |
| 168 | + .exclude(criteria__name="Комментарий") |
| 169 | + .first() |
| 170 | + ) |
| 171 | + if not checking_score: |
| 172 | + scores_of_expert.append("") |
| 173 | + else: |
| 174 | + scores_of_expert.append(checking_score.value) |
| 175 | + |
| 176 | + commentary = scores.filter( |
| 177 | + user__first_name=expert.user.first_name, |
| 178 | + user__last_name=expert.user.last_name, |
| 179 | + criteria__name="Комментарий", |
| 180 | + project__name=project.name, |
| 181 | + ).first() |
| 182 | + commentary = [commentary.value] if commentary else [""] |
| 183 | + |
| 184 | + scores_of_expert += commentary |
| 185 | + |
| 186 | + single_rate_data += scores_of_expert |
| 187 | + |
| 188 | + project_data.append(single_rate_data) |
| 189 | + all_projects_data.append(project_data) |
| 190 | + |
| 191 | + dataframed_projects_data = [ |
| 192 | + pd.DataFrame(project_data) for project_data in all_projects_data |
| 193 | + ] |
| 194 | + with pd.ExcelWriter("output.xlsx") as writer: |
| 195 | + for df, pr_data in zip(dataframed_projects_data, all_projects_data): |
| 196 | + df.to_excel(writer, sheet_name=pr_data[0][0], index=False) |
| 197 | + |
| 198 | + with open("output.xlsx", "rb") as f: |
| 199 | + binary_data = f.read() |
| 200 | + |
| 201 | + # Формирование HTTP-ответа |
| 202 | + file_name = ( |
| 203 | + f'{criterias.first().partner_program.name}_оценки {timezone.now().strftime("%d-%m-%Y %H:%M:%S")}' |
| 204 | + f".xlsx" |
| 205 | + ) |
| 206 | + response = HttpResponse( |
| 207 | + binary_data, |
| 208 | + content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", |
| 209 | + ) |
| 210 | + response["Content-Disposition"] = f'attachment; filename="{file_name}"' |
| 211 | + |
| 212 | + return response |
| 213 | + |
129 | 214 |
|
130 | 215 | @admin.register(PartnerProgramUserProfile) |
131 | 216 | class PartnerProgramUserProfileAdmin(admin.ModelAdmin): |
|
0 commit comments