puchar/app/admin.py

68 lines
2.2 KiB
Python
Raw Normal View History

2021-08-30 02:29:20 +02:00
from django.apps import apps
from django.contrib import admin
from django.contrib.admin.sites import AlreadyRegistered
from django_summernote.admin import SummernoteModelAdmin
from .models import Announcement, Edition, School, Student
from django.db import transaction
class AnnouncementModelAdmin(SummernoteModelAdmin):
summernote_fields = ['content']
class EditionModelAdmin(admin.ModelAdmin):
list_display = ('__str__', 'active')
ordering = ('-active', '-year')
change_form_template = 'admin/edition_buttons.html'
def response_change(self, request, obj):
if "_update-student-scores" in request.POST:
file = obj.scores.open(mode='r')
content = filter(lambda x: x != '', file.read().split('\n'))
file.close()
rows = [x.split(',') for x in content]
with transaction.atomic():
for index, row in enumerate(rows):
if index == 0:
continue
Student.objects.filter(identifier=row[0]).update(score_first=row[1], score_second=row[2])
if "_update-disable-submissions" in request.POST and obj.active and obj.submissions:
if obj.active is True:
schools = School.objects.all()
with transaction.atomic():
for school in schools:
school.updateTeam()
obj.submissions = False
obj.save()
return super().response_change(request, obj)
class StudentModelAdmin(admin.ModelAdmin):
list_display = ('identifier', '__str__', 'grade')
search_fields = ('identifier', 'name', 'surname', 'grade')
ordering = ('identifier',)
admin.site.register(Student, StudentModelAdmin)
admin.site.register(Announcement, AnnouncementModelAdmin)
admin.site.register(Edition, EditionModelAdmin)
app_models = apps.get_app_config('app').get_models()
for model in app_models:
try:
admin.site.register(model)
except AlreadyRegistered:
pass
admin.site.site_header = 'Puchar Dyrektora LO V'
admin.site.site_title = 'Puchar LO V'
admin.site.index_title = 'Panel administracyjny'
admin.site.site_url = '/'
admin.site.enable_nav_sidebar = False