1+ import tablib
12from django .contrib import admin
3+ from django .http import HttpResponse
4+ from django .urls import path
25
36from vacancy .models import Vacancy , VacancyResponse
47
710class VacancyAdmin (admin .ModelAdmin ):
811 list_display = [
912 "role" ,
10- "required_skills" ,
1113 "description" ,
1214 "project" ,
1315 "is_active" ,
@@ -16,6 +18,50 @@ class VacancyAdmin(admin.ModelAdmin):
1618 ]
1719 list_display_links = ["role" ]
1820
21+ change_list_template = "vacancies/vacancies_change_list.html"
22+
23+ def get_urls (self ):
24+ default_urls = super (VacancyAdmin , self ).get_urls ()
25+ custom_urls = [
26+ path (
27+ "email-vacancies/" ,
28+ self .admin_site .admin_view (self .email_leaders_vacancies ),
29+ name = "vacancy_leaders_email" ,
30+ )
31+ ]
32+ return custom_urls + default_urls
33+
34+ def email_leaders_vacancies (self , request ):
35+ data = list (
36+ Vacancy .objects .select_related ("project" , "project__leader" ).values_list (
37+ "project__leader__email" , "datetime_created" , "project__id" , "role"
38+ )
39+ )
40+ return self .excel_email_leaders_vacancies (data )
41+
42+ def excel_email_leaders_vacancies (self , data : list ):
43+ response_data = tablib .Dataset (
44+ headers = ["email" , "дата" , "ссылка на проект" , "название вакансии" ]
45+ )
46+
47+ for row in data :
48+ row_to_add = [
49+ row [0 ],
50+ row [1 ].strftime ("%d-%m-%Y %S:%M:%H %Z" ),
51+ f"https://app.procollab.ru/office/projects/{ row [2 ]} " ,
52+ row [3 ],
53+ ]
54+ response_data .append (row_to_add )
55+
56+ binary_data = response_data .export ("xlsx" )
57+ file_name = "email_of_leaders_with_users"
58+ response = HttpResponse (
59+ content_type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ,
60+ headers = {"Content-Disposition" : f'attachment; filename="{ file_name } .xlsx"' },
61+ )
62+ response .write (binary_data )
63+ return response
64+
1965
2066@admin .register (VacancyResponse )
2167class VacancyResponseAdmin (admin .ModelAdmin ):
0 commit comments