5d4cab3ca22cb1f64f2b4015378e779735a748ed
[auf_paf.git] / paf / admin.py
1 from django.contrib import admin
2 from paf.models import Student, LinguisticLevel, CurrentAcademic, University, Faculty, Level, DiplomaObtained, City, Country, Employment, Entreprise
3 from django.http import HttpResponseRedirect
4 from django.core.urlresolvers import reverse
5 from django.core.exceptions import PermissionDenied
6 from django.contrib.auth.models import User
7 from django import forms
8
9
10 class 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
21 class 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
29 class 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
37 class 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
45 class 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
61 class 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__name',)
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 ()
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.readonly_fields = list(self.fields)
90 #for inline in self.get_inline_instances(request):
91 # inline.readonly_fields = list(inline.fields)
92 #self.readonly_fields = ('identifier', 'firstname', 'lastname', 'nationality', 'sex', 'dob', 'email', 'tel', 'parent_tel', 'parent_address')
93
94 self.change_form_template = 'student_info.html'
95 else:
96 self.change_form_template = None
97 return super(StudentAdmin, self).change_view(request, object_id, form_url, extra_context)
98
99 def changelist_view(self, request, extra_context=None):
100 if request.user.first_name == 'student':
101 student = Student.objects.get(identifier=request.user.username)
102 return HttpResponseRedirect(reverse('edit_info'))
103 elif request.user.username == 'enseignant':
104 self.change_list_template = 'student_list.html'
105 else:
106 self.change_list_template = None
107 return super(StudentAdmin, self).changelist_view(request, extra_context)
108
109 def delete_model(self, request, obj):
110 User.objects.filter(username=obj.identifier).delete()
111 obj.delete()
112
113 class CurrentAcademicAdmin(admin.ModelAdmin):
114 pass
115
116 class FacultyAdmin(admin.ModelAdmin):
117 pass
118
119 class UniversityAdmin(admin.ModelAdmin):
120 pass
121
122 class LevelAdmin(admin.ModelAdmin):
123 pass
124
125 class CityAdmin(admin.ModelAdmin):
126 pass
127
128 class EntrepriseAdmin(admin.ModelAdmin):
129 pass
130
131 class CountryAdmin(admin.ModelAdmin):
132 pass
133
134 class DiplomaObtainedAdmin(admin.ModelAdmin):
135 pass
136
137
138 admin.site.register(Entreprise, CityAdmin)
139 admin.site.register(Country, CityAdmin)
140 admin.site.register(City, CityAdmin)
141 admin.site.register(Student, StudentAdmin)
142 admin.site.register(University, UniversityAdmin)
143 admin.site.register(Faculty, FacultyAdmin)
144 admin.site.register(Level, LevelAdmin)
145 admin.site.register(DiplomaObtained, DiplomaObtainedAdmin)
146 admin.site.register(CurrentAcademic, CurrentAcademicAdmin)