Skip to content
Snippets Groups Projects
Unverified Commit be3c92d4 authored by Seon82's avatar Seon82 Committed by GitHub
Browse files

Amélioration de l'interface admin (#24)


* Password reset feature (#8)

* Add Django Rest auth module

* Try to make the send reset password email work

* Modified template mail for reset

* Add Django Rest auth module

* Try to make the send reset password email work

* Modified template mail for reset

* test

* Added utf-8 support to exported csv and switched delimiter from , to ; in admin interface

* Disabled emails while in dev

* Added multi selection filter in admin

* Fixed mail settings

Co-authored-by: default avatarchiahetcho <44137047+chiahetcho@users.noreply.github.com>
Co-authored-by: default avatarflorimondmanca <florimond.manca@gmail.com>
parent 282e84e4
No related branches found
No related tags found
No related merge requests found
from django.contrib import admin
from django.contrib.admin.utils import reverse_field_path
from django.utils.translation import gettext_lazy as _
class MultiSelectFieldListFilter(admin.FieldListFilter):
def __init__(self, field, request, params, model, model_admin, field_path):
self.lookup_kwarg = field_path + '__in'
self.lookup_kwarg_isnull = field_path + '__isnull'
super().__init__(field, request, params, model, model_admin, field_path)
self.lookup_val = self.used_parameters.get(self.lookup_kwarg, [])
if len(self.lookup_val) == 1 and self.lookup_val[0] == '':
self.lookup_val = []
self.lookup_val_isnull = self.used_parameters.get(self.lookup_kwarg_isnull)
self.empty_value_display = model_admin.get_empty_value_display()
parent_model, reverse_path = reverse_field_path(model, field_path)
# Obey parent ModelAdmin queryset when deciding which options to show
if model == parent_model:
queryset = model_admin.get_queryset(request)
else:
queryset = parent_model._default_manager.all()
self.lookup_choices = queryset.distinct().order_by(field.name).values_list(field.name, flat=True)
def expected_parameters(self):
return [self.lookup_kwarg, self.lookup_kwarg_isnull]
def choices(self, changelist):
yield {
'selected': not self.lookup_val and self.lookup_val_isnull is None,
'query_string': changelist.get_query_string(remove=[self.lookup_kwarg, self.lookup_kwarg_isnull]),
'display': _('All'),
}
include_none = False
for val in self.lookup_choices:
if val is None:
include_none = True
continue
val = str(val)
if val in self.lookup_val:
values = [v for v in self.lookup_val if v != val]
else:
values = self.lookup_val + [ val ]
if values:
yield {
'selected': val in self.lookup_val,
'query_string': changelist.get_query_string({self.lookup_kwarg: ','.join(values)}, [self.lookup_kwarg_isnull]),
'display': val,
}
else:
yield {
'selected': val in self.lookup_val,
'query_string': changelist.get_query_string(remove=[self.lookup_kwarg]),
'display': val,
}
if include_none:
yield {
'selected': bool(self.lookup_val_isnull),
'query_string': changelist.get_query_string({self.lookup_kwarg_isnull: 'True'}, [self.lookup_kwarg]),
'display': self.empty_value_display,
}
\ No newline at end of file
......@@ -2,6 +2,8 @@
from django.contrib import admin
from .models import Student, Tutor
from .MultiSelectFieldListFilter import MultiSelectFieldListFilter
import codecs
import csv
from django.http import HttpResponse
......@@ -13,7 +15,8 @@ class ExportCsvMixin:
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename={}.csv'.format(meta)
writer = csv.writer(response)
response.write(codecs.BOM_UTF8) #force response to be UTF-8
writer = csv.writer(response, delimiter=';')
writer.writerow(field_names)
for obj in queryset:
......@@ -21,7 +24,7 @@ class ExportCsvMixin:
return response
export_as_csv.short_description = "Export Selected"
export_as_csv.short_description = "Exporter sélection (en .csv)"
class ProfileAdminMixin:
......@@ -40,10 +43,11 @@ class TutorAdmin(ProfileAdminMixin, admin.ModelAdmin,ExportCsvMixin):
model = Tutor
actions = ["export_as_csv"]
@admin.register(Student)
class StudentAdmin(ProfileAdminMixin, admin.ModelAdmin,ExportCsvMixin):
"""Student admin panel."""
list_filter = ('school', 'year')
list_filter = (('school',MultiSelectFieldListFilter), 'year')
class Meta: # noqa
model = Student
actions = ["export_as_csv"]
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment