1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2010-2015 Étienne Loks <etienne.loks_AT_peacefrogsDOTnet>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# See the file COPYING for details.
"""
Admin description
"""
import csv
from django.conf import settings
from django.contrib import admin
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.template.defaultfilters import slugify
from django import forms
import models
class ImportGenericForm(forms.Form):
_selected_action = forms.CharField(widget=forms.MultipleHiddenInput)
csv_file = forms.FileField(
"CSV file", help_text="Only unicode encoding is managed - convert your"
" file first")
def gen_import_generic(self, request, queryset):
form = None
if 'apply' in request.POST:
form = ImportGenericForm(request.POST, request.FILES)
if form.is_valid():
csv_file = request.FILES['csv_file']
reader = csv.reader(csv_file)
idx = 0
for row in reader:
slug = slugify(row[0])
if self.model.objects.filter(txt_idx=slug).count():
continue
obj, c = self.model.objects.get_or_create(
label=row[0], txt_idx=slug)
if c:
idx += 1
self.message_user(request, "Successfully added %d new items." % (
idx))
return HttpResponseRedirect(request.get_full_path())
if not form:
form = ImportGenericForm(
initial={'_selected_action':
request.POST.getlist(admin.ACTION_CHECKBOX_NAME)})
return render_to_response(
'admin/import_from_csv.html', {'csv_form': form},
context_instance=RequestContext(request))
gen_import_generic.short_description = "Import from a CSV file"
class HistorizedObjectAdmin(admin.ModelAdmin):
readonly_fields = ('history_modifier',)
def save_model(self, request, obj, form, change):
obj.history_modifier = request.user
obj.save()
class DepartmentAdmin(admin.ModelAdmin):
list_display = ('number', 'label',)
model = models.Department
admin.site.register(models.Department, DepartmentAdmin)
class OrganizationAdmin(HistorizedObjectAdmin):
list_display = ('name', 'organization_type')
list_filter = ("organization_type",)
search_fields = ('name',)
model = models.Organization
admin.site.register(models.Organization, OrganizationAdmin)
class PersonAdmin(HistorizedObjectAdmin):
list_display = ('name', 'surname', 'raw_name', 'email')
list_filter = ("person_types",)
search_fields = ('name', 'surname', 'email', 'raw_name')
model = models.Person
admin.site.register(models.Person, PersonAdmin)
class TownAdmin(admin.ModelAdmin):
list_display = ['name', ]
search_fields = ['name']
if settings.COUNTRY == 'fr':
list_display += ['numero_insee', 'departement', ]
search_fields += ['numero_insee', 'departement__label', ]
list_filter = ("departement",)
model = models.Town
admin.site.register(models.Town, TownAdmin)
class AuthorAdmin(admin.ModelAdmin):
list_display = ['person', 'author_type']
list_filter = ("author_type",)
model = models.Author
admin.site.register(models.Author, AuthorAdmin)
class PersonTypeAdmin(admin.ModelAdmin):
model = models.PersonType
filter_vertical = ('groups',)
admin.site.register(models.PersonType, PersonTypeAdmin)
class GlobalVarAdmin(admin.ModelAdmin):
list_display = ['slug', 'description', 'value']
admin.site.register(models.GlobalVar, GlobalVarAdmin)
class GeneralTypeAdmin(admin.ModelAdmin):
list_display = ['label', 'txt_idx', 'available']
actions = ['import_generic']
import_generic = gen_import_generic
general_models = [models.OrganizationType, models.SourceType,
models.AuthorType]
for model in general_models:
admin.site.register(model, GeneralTypeAdmin)
class ImporterDefaultValuesInline(admin.TabularInline):
model = models.ImporterDefaultValues
class ImporterDefaultAdmin(admin.ModelAdmin):
list_display = ('importer_type', 'target')
model = models.ImporterDefault
inlines = (ImporterDefaultValuesInline,)
admin.site.register(models.ImporterDefault, ImporterDefaultAdmin)
class ImporterTypeAdmin(admin.ModelAdmin):
list_display = ('name', 'associated_models', 'is_template')
admin.site.register(models.ImporterType, ImporterTypeAdmin)
class RegexpAdmin(admin.ModelAdmin):
list_display = ('name', 'description', "regexp")
admin.site.register(models.Regexp, RegexpAdmin)
class ImporterDuplicateFieldInline(admin.TabularInline):
model = models.ImporterDuplicateField
class ImportTargetInline(admin.TabularInline):
model = models.ImportTarget
class ImporterColumnAdmin(admin.ModelAdmin):
list_display = ('importer_type', 'col_number', 'description', 'required')
list_filter = ('importer_type',)
inlines = (ImporterDuplicateFieldInline, ImportTargetInline)
admin.site.register(models.ImporterColumn, ImporterColumnAdmin)
class FormaterTypeAdmin(admin.ModelAdmin):
list_display = ('formater_type', 'options')
admin.site.register(models.FormaterType, FormaterTypeAdmin)
class ImportAdmin(admin.ModelAdmin):
list_display = ('importer_type', 'imported_file', 'user', 'state',
'creation_date')
admin.site.register(models.Import, ImportAdmin)
class TargetKeyAdmin(admin.ModelAdmin):
list_display = ('target', 'key', 'value', 'is_set')
list_filter = ("is_set",)
search_fields = ('target', 'key')
admin.site.register(models.TargetKey, TargetKeyAdmin)
basic_models = [models.IshtarUser, models.DocumentTemplate]
if settings.COUNTRY == 'fr':
basic_models += [models.Arrondissement, models.Canton]
for model in basic_models:
admin.site.register(model)
|