Première version en Python-Django.
[auf_paf.git] / paf / admin.py
... / ...
CommitLineData
1from django.contrib import admin
2from paf.models import Student, LinguisticLevel, CurrentAcademic, University, Faculty, Level, DiplomaObtained, City, Country, Employment, Entreprise
3from django.http import HttpResponseRedirect
4from django.core.urlresolvers import reverse
5from django.core.exceptions import PermissionDenied
6from django.contrib.auth.models import User
7from django import forms
8
9
10class LinguisticLevelAdmin(admin.ModelAdmin):
11 fieldsets = (
12 ('Delf/Dalf', {
13 'fields': ('delf_dalf', 'year_delf_dalf')
14 }),
15 ('Autre', {
16 'fields': ('other', 'year_other')
17 }),
18 )
19
20
21class LinguisticLevelInline(admin.TabularInline):
22 model = LinguisticLevel
23
24 def get_readonly_fields(self, request, obj=None):
25 if request.user.username == 'enseignant':
26 return ('delf_dalf', 'year_delf_dalf', 'other', 'year_other')
27 return ()
28
29class CurrentAcademicInline(admin.TabularInline):
30 model = CurrentAcademic
31
32 def get_readonly_fields(self, request, obj=None):
33 if request.user.username == 'enseignant':
34 return ('university', 'faculty', 'level', 'specialty', 'city', 'entry_year', 'end_year', 'certified')
35 return ()
36
37class DiplomaObtainedInline(admin.TabularInline):
38 model = DiplomaObtained
39
40 def get_readonly_fields(self, request, obj=None):
41 if request.user.username == 'enseignant':
42 return ('university', 'entitle', 'level', 'year')
43 return ()
44
45class EmploymentInline(admin.StackedInline):
46 model = Employment
47
48 def get_readonly_fields(self, request, obj=None):
49 if request.user.username == 'enseignant':
50 return ('entreprise', 'activity_sector', 'country', 'website', 'position', 'detail_position', 'observation')
51 return ()
52
53#class StudentForm(forms.ModelForm):
54# class Meta:
55# model = Student
56# widgets = {
57# 'identifier': forms.TextInput(attrs={'readonly':'readonly'}),
58# }
59
60
61class StudentAdmin(admin.ModelAdmin):
62# form = StudentForm
63 list_display = ('firstname', 'lastname', 'tel', 'email')
64 search_fields = ('firstname', 'lastname')
65 list_filter = ('sex', 'linguisticlevel__delf_dalf', 'currentacademic__university',)
66 date_hierarchy = 'dob'
67 ordering = ('-lastname',)
68 inlines = [CurrentAcademicInline, LinguisticLevelInline, DiplomaObtainedInline, EmploymentInline]
69 readonly_fields = ('identifier',)
70 fields = ('identifier', 'firstname', 'lastname', 'nationality', 'sex', 'dob', 'email', 'tel', 'parent_tel', 'parent_address')
71
72
73 def get_readonly_fields(self, request, obj=None):
74 if request.user.username == 'enseignant':
75 return ('identifier', 'firstname', 'lastname', 'nationality', 'sex', 'dob', 'email', 'tel', 'parent_tel', 'parent_address')
76 return ('identifier',)
77
78 def queryset(self, request):
79 qs = super(StudentAdmin, self).queryset(request)
80 if request.user.first_name == 'student':
81 return qs.filter(identifier=request.user.username)
82 else:
83 return qs
84
85 def change_view(self, request, object_id, form_url='', extra_context=None):
86 if request.user.first_name == 'student':
87 self.change_form_template = 'edit_info.html'
88 elif request.user.username == 'enseignant':
89 self.change_form_template = 'student_info.html'
90 else:
91 self.change_form_template = None
92 return super(StudentAdmin, self).change_view(request, object_id, form_url, extra_context)
93
94 def changelist_view(self, request, extra_context=None):
95 if request.user.first_name == 'student':
96 student = Student.objects.get(identifier=request.user.username)
97 return HttpResponseRedirect(reverse('edit_info'))
98 elif request.user.username == 'enseignant':
99 self.change_list_template = 'student_list.html'
100 else:
101 self.change_list_template = None
102 return super(StudentAdmin, self).changelist_view(request, extra_context)
103
104 def delete_model(self, request, obj):
105 User.objects.filter(username=obj.identifier).delete()
106 obj.delete()
107
108class CurrentAcademicAdmin(admin.ModelAdmin):
109 pass
110
111class FacultyAdmin(admin.ModelAdmin):
112 pass
113
114class UniversityAdmin(admin.ModelAdmin):
115 pass
116
117class LevelAdmin(admin.ModelAdmin):
118 pass
119
120class CityAdmin(admin.ModelAdmin):
121 pass
122
123class EntrepriseAdmin(admin.ModelAdmin):
124 pass
125
126class CountryAdmin(admin.ModelAdmin):
127 pass
128
129class DiplomaObtainedAdmin(admin.ModelAdmin):
130 pass
131
132
133admin.site.register(Entreprise, CityAdmin)
134admin.site.register(Country, CityAdmin)
135admin.site.register(City, CityAdmin)
136admin.site.register(Student, StudentAdmin)
137admin.site.register(University, UniversityAdmin)
138admin.site.register(Faculty, FacultyAdmin)
139admin.site.register(Level, LevelAdmin)
140admin.site.register(DiplomaObtained, DiplomaObtainedAdmin)
141admin.site.register(CurrentAcademic, CurrentAcademicAdmin)