Skip to content

Commit e99cf41

Browse files
committed
chore(logs/admin.py): Update year filter
1 parent 64918d6 commit e99cf41

1 file changed

Lines changed: 13 additions & 19 deletions

File tree

src/apps/logs/admin.py

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111
from .models import ReviewLog
1212

1313

14+
YEAR_APP_MAP = {}
15+
APP_YEAR_MAP = {}
16+
for year, app in zip(('106', '107', '108', '110'), ('surveys18', 'surveys19', 'surveys20', 'surveys22')):
17+
YEAR_APP_MAP[year] = app
18+
APP_YEAR_MAP[app] = year
19+
20+
1421
class ReviewLogResource(ModelResource):
1522
farmer_id = Field(column_name=_('Farmer Id'))
1623
year = Field(column_name='年份')
@@ -33,12 +40,7 @@ def dehydrate_farmer_id(self, obj):
3340
return '此調查表已經被刪除'
3441

3542
def dehydrate_year(self, obj):
36-
if obj.content_type.app_label == 'surveys18':
37-
return '106'
38-
if obj.content_type.app_label == 'surveys19':
39-
return '107'
40-
if obj.content_type.app_label == 'surveys20':
41-
return '108'
43+
return APP_YEAR_MAP[obj.content_type.app_label]
4244

4345

4446
class YearFilter(SimpleListFilter):
@@ -53,20 +55,16 @@ def lookups(self, request, model_admin):
5355
human-readable name for the option that will appear
5456
in the right sidebar.
5557
"""
56-
return [('106', '106'), ('107', '107'), ('108', '108')]
58+
return [(y, y) for y in YEAR_APP_MAP.keys()]
5759

5860
def queryset(self, request, queryset):
5961
"""
6062
Returns the filtered queryset based on the value
6163
provided in the query string and retrievable via
6264
`self.value()`.
6365
"""
64-
if self.value() == '106':
65-
return queryset.filter(content_type__app_label='surveys18')
66-
if self.value() == '107':
67-
return queryset.filter(content_type__app_label='surveys19')
68-
if self.value() == '108':
69-
return queryset.filter(content_type__app_label='surveys20')
66+
if self.value() in YEAR_APP_MAP:
67+
return queryset.filter(content_type__app_label=YEAR_APP_MAP[self.value()])
7068
else:
7169
return queryset
7270

@@ -100,12 +98,8 @@ def farmer_id(self, obj):
10098
farmer_id.short_description = '農戶編號'
10199

102100
def year(self, obj):
103-
if obj.content_type.app_label == 'surveys18':
104-
return '106'
105-
if obj.content_type.app_label == 'surveys19':
106-
return '107'
107-
if obj.content_type.app_label == 'surveys20':
108-
return '108'
101+
return APP_YEAR_MAP[obj.content_type.app_label]
102+
109103
year.short_description = '年份'
110104

111105
class Media:

0 commit comments

Comments
 (0)