This repository has been archived by the owner on Dec 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
admin.py
140 lines (109 loc) · 4.98 KB
/
admin.py
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
from django.contrib import admin
from import_export import resources
from import_export.admin import ImportExportModelAdmin
from .models import (
AdditionalLanguage,
TempLanguage,
JSONData,
Region,
WARegion,
Language,
Country,
Network,
LanguageAltName,
CountryEAV,
LanguageEAV,
)
from .resources.admin import EntryTrackingAdmin
class AdditionalLanguageAdmin(EntryTrackingAdmin):
list_display = ["ietf_tag", "common_name", "two_letter", "three_letter", "native_name", "direction", "comment",
"created_at", "updated_at"]
list_filter = ["created_at", "updated_at", "direction"]
search_fields = ["ietf_tag", "common_name", "two_letter", "three_letter", "native_name", "comment"]
class TempLanguageAdmin(EntryTrackingAdmin):
list_display = ["code", "name", "lang_assigned", "status", "created_at", "modified_at"]
list_filter = ["status", "app", "requester", "created_at", "modified_at"]
search_fields = ["code", ]
def save_model(self, request, obj, form, change):
if not change:
obj.created_by = request.user
obj.modified_by = request.user
obj.save()
class NetworkAdmin(EntryTrackingAdmin):
list_display = ["name"]
class CountryAdmin(EntryTrackingAdmin):
list_display = ["code", "name", "region", "wa_region", "population"]
search_fields = ["code", "name", "region__name", "wa_region__name"]
def get_readonly_fields(self, request, obj=None):
if obj:
return self.readonly_fields + ("code",)
return self.readonly_fields
class LanguageAdmin(EntryTrackingAdmin, ImportExportModelAdmin):
list_display = ["code", "name", "gateway_language", "direction", "native_speakers"]
list_filter = ["gateway_flag", "country__region", "wa_region", ]
search_fields = ["code", "name", "anglicized_name", ]
def get_readonly_fields(self, request, obj=None):
if obj:
return self.readonly_fields + ("code",)
return self.readonly_fields
# NOTE: This is superseded by management command language_alt_names
class LanguageAltNameResource(resources.ModelResource):
# source is needed to create LanguageEAV
source = None
def before_import(self, dataset, dry_run, **kwargs):
self.source = kwargs.pop("user")
return super(LanguageAltNameResource, self).before_import(dataset, dry_run, **kwargs)
def skip_row(self, instance, original):
# NOTE: This check is written because unique_together constrain on
# LanguageAltName doesn't seem to work to avoid duplicates. Even if
# it did, importing duplicates will throw an IntegrityError and
# aborts the import process.
result = super(LanguageAltNameResource, self).skip_row(instance, original)
try:
LanguageAltName.objects.get(code=instance.code, name=instance.name)
print "".join(["*LanguageAltName with code ", instance.code,
" and ", instance.name, " already exist. Skipped."])
return True
except LanguageAltName.DoesNotExist:
return result
def after_save_instance(self, instance, dry_run):
# Avoiding double LanguageEAV creations
if not dry_run:
try:
# Filter instead of get because diff langs may have the same
# iso-639-3 code. Specific example: 'pt' and 'pt-br'.
for language in Language.objects.filter(iso_639_3=instance.code):
language.alt_name = instance
language.source = self.source
language.save()
except Language.DoesNotExist:
print "Language code: " + instance.code + " doesn't exist."
class Meta:
model = LanguageAltName
skip_unchanged = True
report_skipped = True
fields = ["id", "code", "name", ]
class LanguageAltNameAdmin(EntryTrackingAdmin, ImportExportModelAdmin):
resource_class = LanguageAltNameResource
list_display = ["code", "name", ]
search_fields = ["code", "name", ]
class RegionAdmin(EntryTrackingAdmin):
list_display = ["name"]
prepopulated_fields = {"slug": ["name"]}
class LanguageEAVAdmin(EntryTrackingAdmin):
list_display = ["entity", "attribute", "value", "source_ct", "source_id"]
list_filter = ["attribute", "source_ct", ]
class CountryEAVAdmin(EntryTrackingAdmin):
list_display = ["entity", "attribute", "value", "source_ct", "source_id"]
list_filter = ["attribute", "source_ct", ]
admin.site.register(AdditionalLanguage, AdditionalLanguageAdmin)
admin.site.register(Network, NetworkAdmin)
admin.site.register(Country, CountryAdmin)
admin.site.register(Language, LanguageAdmin)
admin.site.register(LanguageAltName, LanguageAltNameAdmin)
admin.site.register(Region, RegionAdmin)
admin.site.register(WARegion)
admin.site.register(JSONData)
admin.site.register(CountryEAV, CountryEAVAdmin)
admin.site.register(LanguageEAV, LanguageEAVAdmin)
admin.site.register(TempLanguage, TempLanguageAdmin)