2019-06-01 20:23:19 +02:00
|
|
|
from django.contrib import admin, messages
|
2020-06-01 13:44:54 +02:00
|
|
|
from django.db.models import Prefetch
|
2019-06-28 15:40:21 +02:00
|
|
|
from import_export import resources, fields
|
2018-05-22 11:10:09 +02:00
|
|
|
from import_export.admin import ExportMixin
|
2019-06-28 15:50:47 +02:00
|
|
|
from import_export.widgets import IntegerWidget, DecimalWidget
|
2018-05-21 19:28:36 +02:00
|
|
|
|
2019-06-28 16:33:59 +02:00
|
|
|
from manuels.models import Teacher, Book, Level, Editor, SuppliesRequirement, CommonSupply, ISBNError
|
2018-05-21 23:12:35 +02:00
|
|
|
|
|
|
|
|
2018-05-22 11:10:09 +02:00
|
|
|
class TeacherResource(resources.ModelResource):
|
|
|
|
class Meta:
|
|
|
|
model = Teacher
|
|
|
|
fields = ('first_name', 'last_name', 'email', 'phone_number')
|
|
|
|
|
|
|
|
|
2018-05-21 23:12:35 +02:00
|
|
|
@admin.register(Teacher)
|
2018-05-22 11:10:09 +02:00
|
|
|
class TeacherAdmin(ExportMixin, admin.ModelAdmin):
|
|
|
|
resource_class = TeacherResource
|
2018-06-23 11:25:26 +02:00
|
|
|
list_display = ['full_name', 'email', 'phone_number', 'has_confirmed_list']
|
2018-05-21 23:12:35 +02:00
|
|
|
|
2018-05-22 11:10:27 +02:00
|
|
|
def send_link(self, request, queryset):
|
2018-05-22 10:24:21 +02:00
|
|
|
for teacher in queryset:
|
|
|
|
teacher.send_link(request)
|
2019-06-01 20:23:19 +02:00
|
|
|
messages.success(request, f'Le lien a bien été envoyé aux {queryset.count()} coordonateur(s) sélectionné(s).')
|
2018-05-22 10:24:21 +02:00
|
|
|
|
|
|
|
send_link.short_description = 'Envoyer le lien'
|
|
|
|
|
|
|
|
actions = [send_link]
|
|
|
|
|
2018-05-21 23:12:35 +02:00
|
|
|
|
2019-06-28 15:40:21 +02:00
|
|
|
class LevelResource(resources.ModelResource):
|
2019-06-28 15:50:47 +02:00
|
|
|
non_acquired_book_count = fields.Field(attribute='non_acquired_book_count', widget=IntegerWidget())
|
|
|
|
non_acquired_book_price = fields.Field(attribute='non_acquired_book_price', widget=DecimalWidget())
|
|
|
|
non_acquired_consumable_count = fields.Field(attribute='non_acquired_consumable_count', widget=IntegerWidget())
|
|
|
|
non_acquired_consumable_price = fields.Field(attribute='non_acquired_consumable_price', widget=DecimalWidget())
|
|
|
|
non_acquired_total_price = fields.Field(attribute='non_acquired_total_price', widget=DecimalWidget())
|
2019-06-28 15:40:21 +02:00
|
|
|
|
2019-06-28 15:08:40 +02:00
|
|
|
class Meta:
|
2019-06-28 15:40:21 +02:00
|
|
|
model = Level
|
|
|
|
fields = (
|
|
|
|
'name',
|
|
|
|
'non_acquired_book_count',
|
|
|
|
'non_acquired_book_price',
|
|
|
|
'non_acquired_consumable_count',
|
|
|
|
'non_acquired_consumable_price',
|
|
|
|
'non_acquired_total_price',
|
|
|
|
)
|
|
|
|
export_order = (
|
|
|
|
'name',
|
|
|
|
'non_acquired_book_count',
|
|
|
|
'non_acquired_book_price',
|
|
|
|
'non_acquired_consumable_count',
|
|
|
|
'non_acquired_consumable_price',
|
|
|
|
'non_acquired_total_price',
|
|
|
|
)
|
2019-06-28 15:08:40 +02:00
|
|
|
|
|
|
|
|
2018-05-21 23:12:35 +02:00
|
|
|
@admin.register(Level)
|
2019-06-28 15:40:21 +02:00
|
|
|
class LevelAdmin(ExportMixin, admin.ModelAdmin):
|
|
|
|
resource_class = LevelResource
|
2019-06-28 15:15:12 +02:00
|
|
|
list_display = [
|
|
|
|
'name',
|
|
|
|
'order',
|
2019-06-28 15:24:03 +02:00
|
|
|
'non_acquired_book_count',
|
|
|
|
'non_acquired_book_price',
|
2019-06-28 15:15:12 +02:00
|
|
|
'non_acquired_consumable_count',
|
2019-06-28 15:24:03 +02:00
|
|
|
'non_acquired_consumable_price',
|
|
|
|
'non_acquired_total_price',
|
2019-06-28 15:15:12 +02:00
|
|
|
]
|
2019-04-06 19:32:47 +02:00
|
|
|
list_editable = ['order']
|
|
|
|
list_display_links = ['name']
|
2018-05-21 23:12:35 +02:00
|
|
|
|
2020-06-01 13:44:54 +02:00
|
|
|
def get_queryset(self, request):
|
|
|
|
return super(LevelAdmin, self).get_queryset(request).prefetch_related(
|
|
|
|
Prefetch("book_set", to_attr="prefetched_books"))
|
|
|
|
|
2019-06-28 15:24:03 +02:00
|
|
|
def non_acquired_book_count(self, obj: Level):
|
2019-06-28 15:40:21 +02:00
|
|
|
return obj.non_acquired_book_count
|
2020-06-01 13:44:54 +02:00
|
|
|
|
2019-06-28 15:24:03 +02:00
|
|
|
non_acquired_book_count.short_description = 'Nombre de livres à acheter (hors consommables)'
|
2019-06-28 14:47:35 +02:00
|
|
|
|
2019-06-28 15:24:03 +02:00
|
|
|
def non_acquired_book_price(self, obj: Level):
|
|
|
|
return f'{obj.non_acquired_book_price:.2f}€'
|
2020-06-01 13:44:54 +02:00
|
|
|
|
2019-06-28 15:24:03 +02:00
|
|
|
non_acquired_book_price.short_description = 'Coût des livres à acheter (hors consommables)'
|
2019-06-28 14:47:35 +02:00
|
|
|
|
2019-06-28 15:15:12 +02:00
|
|
|
def non_acquired_consumable_count(self, obj: Level):
|
2019-06-28 15:40:21 +02:00
|
|
|
return obj.non_acquired_consumable_count
|
2020-06-01 13:44:54 +02:00
|
|
|
|
2019-06-28 15:24:03 +02:00
|
|
|
non_acquired_consumable_count.short_description = 'Nombre de consommables à acheter'
|
2019-06-28 15:15:12 +02:00
|
|
|
|
|
|
|
def non_acquired_consumable_price(self, obj: Level):
|
|
|
|
return f'{obj.non_acquired_consumable_price:.2f}€'
|
2020-06-01 13:44:54 +02:00
|
|
|
|
2019-06-28 15:24:03 +02:00
|
|
|
non_acquired_consumable_price.short_description = 'Coût des consommables à acheter'
|
|
|
|
|
|
|
|
def non_acquired_total_price(self, obj: Level):
|
|
|
|
return f'{obj.non_acquired_total_price:.2f}€'
|
2020-06-01 13:44:54 +02:00
|
|
|
|
2019-06-28 15:24:03 +02:00
|
|
|
non_acquired_total_price.short_description = 'Coût total à acheter'
|
2019-06-28 15:15:12 +02:00
|
|
|
|
2018-05-21 23:12:35 +02:00
|
|
|
|
2018-05-22 11:10:09 +02:00
|
|
|
class BookResource(resources.ModelResource):
|
2020-06-01 13:53:46 +02:00
|
|
|
decitre_url = fields.Field(attribute='decitre_url')
|
|
|
|
|
2018-05-22 11:10:09 +02:00
|
|
|
class Meta:
|
|
|
|
model = Book
|
2018-06-24 17:33:32 +02:00
|
|
|
fields = ('title', 'authors', 'editor__name', 'publication_year', 'isbn', 'comments', 'other_editor',
|
2019-04-06 19:25:04 +02:00
|
|
|
'price', 'previously_acquired', 'teacher__first_name', 'teacher__last_name', 'level__name', 'field',
|
2020-06-01 13:53:46 +02:00
|
|
|
'consumable', "decitre_url")
|
2018-06-02 16:40:36 +02:00
|
|
|
export_order = ('level__name', 'field', 'title', 'authors', 'editor__name', 'publication_year', 'isbn', 'price',
|
2019-04-06 19:25:04 +02:00
|
|
|
'other_editor', 'previously_acquired', 'teacher__first_name', 'teacher__last_name', 'comments',
|
2020-06-01 13:53:46 +02:00
|
|
|
'consumable', "decitre_url")
|
2018-05-22 11:10:09 +02:00
|
|
|
|
|
|
|
|
2018-05-21 23:12:35 +02:00
|
|
|
@admin.register(Book)
|
2018-05-22 11:10:09 +02:00
|
|
|
class BookAdmin(ExportMixin, admin.ModelAdmin):
|
|
|
|
resource_class = BookResource
|
2018-06-10 22:15:58 +02:00
|
|
|
list_display = ['level', 'field', 'title', 'authors', 'editor', 'other_editor', 'publication_year', 'isbn',
|
2019-04-06 19:28:27 +02:00
|
|
|
'price', 'previously_acquired', 'teacher', 'done', 'consumable']
|
2018-06-02 16:35:52 +02:00
|
|
|
list_editable = ['done']
|
2019-04-06 19:28:27 +02:00
|
|
|
list_filter = ['done', 'previously_acquired', 'consumable', 'level', 'editor', 'teacher']
|
2018-05-24 00:31:21 +02:00
|
|
|
list_display_links = ['title']
|
2018-05-22 01:45:55 +02:00
|
|
|
fieldsets = [
|
|
|
|
('Infos livre', {
|
2019-04-06 19:28:27 +02:00
|
|
|
'fields': ('title', 'consumable', 'authors', ('editor', 'other_editor'), 'publication_year',
|
2018-06-10 22:15:58 +02:00
|
|
|
'isbn', 'created_at', 'updated_at', 'comments')
|
2018-05-22 01:45:55 +02:00
|
|
|
}),
|
|
|
|
('Élève', {
|
|
|
|
'fields': ('price', 'previously_acquired',)
|
|
|
|
}),
|
2018-06-02 16:31:29 +02:00
|
|
|
('Coordonnateur', {
|
2018-05-22 01:45:55 +02:00
|
|
|
'fields': ('teacher', 'level', 'field')
|
|
|
|
}),
|
2018-05-30 18:06:27 +02:00
|
|
|
('Gestion', {
|
|
|
|
'fields': ('done',)
|
|
|
|
}),
|
2018-05-22 01:45:55 +02:00
|
|
|
]
|
|
|
|
readonly_fields = ['created_at', 'updated_at']
|
2018-05-22 01:35:05 +02:00
|
|
|
|
2020-06-01 13:44:54 +02:00
|
|
|
def get_queryset(self, request):
|
|
|
|
return super(BookAdmin, self).get_queryset(request).select_related("editor", "level", "teacher")
|
|
|
|
|
2019-06-28 16:33:59 +02:00
|
|
|
def update_with_decitre(self, request, queryset):
|
|
|
|
for book in queryset:
|
|
|
|
try:
|
|
|
|
book.update_from_decitre()
|
|
|
|
messages.success(
|
|
|
|
request,
|
|
|
|
f'Mise à jour réussie du livre "{book.title}" ({book.level.name} - {book.field})'
|
|
|
|
)
|
|
|
|
except ISBNError as e:
|
|
|
|
messages.warning(
|
|
|
|
request,
|
|
|
|
f'Erreur lors de la mise à jour du livre "{book.title}" ({book.level.name} - {book.field}) : {e.data.get("error")}'
|
|
|
|
)
|
|
|
|
|
|
|
|
update_with_decitre.short_description = 'Mettre à jour avec Decitre'
|
|
|
|
|
2020-06-30 13:00:36 +02:00
|
|
|
def mark_as_done(self, request, queryset):
|
|
|
|
queryset.update(done=True)
|
|
|
|
|
|
|
|
mark_as_done.short_description = "Marquer comme traité"
|
|
|
|
actions = [update_with_decitre, mark_as_done]
|
2019-06-28 16:33:59 +02:00
|
|
|
|
2018-05-22 01:35:05 +02:00
|
|
|
|
|
|
|
@admin.register(Editor)
|
|
|
|
class EditorAdmin(admin.ModelAdmin):
|
|
|
|
pass
|
2018-05-22 10:13:51 +02:00
|
|
|
|
|
|
|
|
2019-06-15 11:18:37 +02:00
|
|
|
@admin.register(CommonSupply)
|
|
|
|
class CommonSupplyAdmin(admin.ModelAdmin):
|
2019-06-15 11:22:21 +02:00
|
|
|
list_display = ['name', 'order']
|
|
|
|
list_display_links = ['name']
|
2019-06-15 11:24:47 +02:00
|
|
|
list_editable = ['order']
|
2019-06-15 11:18:37 +02:00
|
|
|
|
|
|
|
|
2018-05-22 11:10:09 +02:00
|
|
|
class SuppliesResource(resources.ModelResource):
|
|
|
|
class Meta:
|
|
|
|
model = SuppliesRequirement
|
2019-06-03 14:45:42 +02:00
|
|
|
fields = ('supplies', 'field', 'level__name', 'teacher__first_name', 'teacher__last_name')
|
2019-04-15 17:39:08 +02:00
|
|
|
export_order = ('level__name', 'field', 'supplies', 'teacher__first_name', 'teacher__last_name')
|
2018-05-22 11:10:09 +02:00
|
|
|
|
|
|
|
|
2018-05-22 10:13:51 +02:00
|
|
|
@admin.register(SuppliesRequirement)
|
2018-05-22 11:10:09 +02:00
|
|
|
class SuppliesRequirementAdmin(ExportMixin, admin.ModelAdmin):
|
|
|
|
resource_class = SuppliesResource
|
2019-04-15 17:39:08 +02:00
|
|
|
list_display = ['id', 'teacher', 'level', 'field', 'supplies', 'done']
|
2018-06-02 16:35:52 +02:00
|
|
|
list_editable = ['done']
|
2018-05-22 10:13:51 +02:00
|
|
|
readonly_fields = ['created_at', 'updated_at']
|
2018-05-30 18:06:27 +02:00
|
|
|
list_filter = ['done', 'teacher', 'level']
|
2020-06-01 13:44:54 +02:00
|
|
|
|
|
|
|
def get_queryset(self, request):
|
|
|
|
return super(SuppliesRequirementAdmin, self).get_queryset(request).select_related("level", "teacher")
|