|
7 | 7 | from django.utils.encoding import smart_str |
8 | 8 | from django.http.response import HttpResponse |
9 | 9 | from django.utils.html import mark_safe |
| 10 | +from django.utils.translation import ugettext_lazy as _ |
10 | 11 |
|
11 | 12 |
|
12 | 13 | def export_full_csv(modeladmin, request, queryset): |
@@ -116,43 +117,163 @@ class PhotoInline(admin.StackedInline): |
116 | 117 |
|
117 | 118 |
|
118 | 119 | class ReportAdmin(admin.ModelAdmin): |
119 | | - list_display = ('version_UUID', 'report_id', 'deleted', 'user', 'version_number', 'creation_time', 'version_time', 'type', 'mission', |
120 | | - 'package_version', 'os', 'n_photos', 'map_link', 'movelab_score', 'crowd_score') |
121 | | - inlines = [ReportResponseInline, PhotoInline] |
122 | | - ordering = ('creation_time', 'report_id', 'version_number') |
123 | | - readonly_fields = ('deleted', 'version_UUID', 'user', 'report_id', 'version_number', 'other_versions_of_this_report', 'creation_time', 'version_time', 'server_upload_time', 'updated_at', 'datetime_fix_offset', 'phone_upload_time', 'type', 'mission', 'location_choice', 'current_location_lon', 'current_location_lat', 'selected_location_lon', 'selected_location_lat', 'note', 'package_name', 'package_version', 'device_manufacturer', 'device_model', 'os', 'os_version', 'os_language', 'app_language', 'n_photos', 'lon', 'lat', 'tigaprob', 'tigaprob_text', 'site_type', 'site_type_trans', 'embornals', 'fonts', 'basins', 'buckets', 'wells', 'other', 'masked_lat', 'masked_lon', 'map_link', 'movelab_score', 'crowd_score') |
124 | | - fields = ('hide', 'deleted', 'map_link', 'version_UUID', 'user', 'report_id', 'version_number', 'other_versions_of_this_report', ('creation_time', 'version_time', 'datetime_fix_offset'), ('server_upload_time','phone_upload_time'), 'updated_at', 'type', 'mission', 'location_choice', 'current_location_lon', 'current_location_lat', 'selected_location_lon', 'selected_location_lat', 'note', 'package_name', 'package_version', 'device_manufacturer', 'device_model', 'os', 'os_version', 'os_language', 'app_language', 'n_photos', 'lon', 'lat', 'tigaprob', 'tigaprob_text', 'site_type', 'site_type_trans', 'embornals', 'fonts', 'basins', 'buckets', 'wells', 'other', 'masked_lat', 'masked_lon', 'movelab_score', 'crowd_score') |
125 | | - search_fields = ('version_UUID',) |
| 120 | + list_display = ( |
| 121 | + 'version_UUID', 'report_id', 'deleted', 'user', 'version_number', 'creation_time', 'version_time', 'type', 'mission', |
| 122 | + 'package_version', 'os', 'n_photos' |
| 123 | + ) |
126 | 124 | list_filter = ['os', 'type', 'mission', 'package_name', 'package_version'] |
| 125 | + search_fields = ('version_UUID',) |
| 126 | + |
| 127 | + inlines = [ReportResponseInline, PhotoInline] |
127 | 128 | actions = [export_full_csv, export_full_csv_sc] |
128 | 129 |
|
| 130 | + readonly_fields = [ |
| 131 | + "deleted", |
| 132 | + "report_id", |
| 133 | + "version_number", |
| 134 | + "type", |
| 135 | + "user", |
| 136 | + "mission", |
| 137 | + "session", |
| 138 | + "server_upload_time", |
| 139 | + "updated_at", |
| 140 | + "version_time", |
| 141 | + "phone_upload_time", |
| 142 | + "creation_time", |
| 143 | + "other_versions_of_this_report" |
| 144 | + ] |
| 145 | + |
| 146 | + fieldsets = [ |
| 147 | + ( |
| 148 | + _('General info'), |
| 149 | + { |
| 150 | + "fields": [ |
| 151 | + ("report_id", "version_number"), |
| 152 | + ("hide", "deleted"), |
| 153 | + "type", |
| 154 | + "user", |
| 155 | + ("mission","session"), |
| 156 | + "other_versions_of_this_report", |
| 157 | + ("server_upload_time", "updated_at"), |
| 158 | + ("version_time", "datetime_fix_offset"), |
| 159 | + ("creation_time", "phone_upload_time") |
| 160 | + ] |
| 161 | + } |
| 162 | + ), |
| 163 | + ( |
| 164 | + _("Location information"), |
| 165 | + { |
| 166 | + "fields": [ |
| 167 | + ("country", "nuts_2", "nuts_3"), |
| 168 | + "location_choice", |
| 169 | + "point" |
| 170 | + ] |
| 171 | + } |
| 172 | + ), |
| 173 | + ( |
| 174 | + _("Other"), |
| 175 | + { |
| 176 | + "fields": [ |
| 177 | + ("package_name", "package_version", "app_language"), |
| 178 | + ("device_manufacturer", "device_model"), |
| 179 | + ("os", "os_version", "os_language"), |
| 180 | + "note" |
| 181 | + ], |
| 182 | + "classes": ["collapse",] |
| 183 | + } |
| 184 | + ) |
| 185 | + ] |
| 186 | + |
| 187 | + def get_readonly_fields(self, request, obj=None): |
| 188 | + # Only allow to edit 'hide' field. |
| 189 | + result = super().get_readonly_fields(request, obj) |
| 190 | + |
| 191 | + readonly_fields = [field.name for field in self.model._meta.get_fields()] |
| 192 | + allow_edit_fields = ['hide',] |
| 193 | + |
| 194 | + for field_name in readonly_fields: |
| 195 | + if not field_name in allow_edit_fields: |
| 196 | + result.append(field_name) |
| 197 | + |
| 198 | + return result |
| 199 | + |
| 200 | + def get_fieldsets(self, request, obj = None): |
| 201 | + result = super().get_fieldsets(request, obj) |
| 202 | + |
| 203 | + if not obj: |
| 204 | + return result |
| 205 | + |
| 206 | + extra_fieldsets = [] |
| 207 | + if obj.type == Report.TYPE_ADULT: |
| 208 | + extra_fieldsets.append( |
| 209 | + ( |
| 210 | + _("Classification"), |
| 211 | + { |
| 212 | + "fields": [ |
| 213 | + "ia_filter_1", "ia_filter_2" |
| 214 | + ] |
| 215 | + } |
| 216 | + ) |
| 217 | + ) |
| 218 | + extra_fieldsets.append( |
| 219 | + ( |
| 220 | + _("Specific information"), |
| 221 | + { |
| 222 | + "fields": [ |
| 223 | + ("event_environment", "event_moment"), |
| 224 | + "user_perceived_mosquito_specie", |
| 225 | + ("user_perceived_mosquito_thorax", "user_perceived_mosquito_abdomen", "user_perceived_mosquito_legs") |
| 226 | + ] |
| 227 | + } |
| 228 | + ) |
| 229 | + ) |
| 230 | + elif obj.type == Report.TYPE_BITE: |
| 231 | + extra_fieldsets.append( |
| 232 | + ( |
| 233 | + _("Specific information"), |
| 234 | + { |
| 235 | + "fields": [ |
| 236 | + ("event_environment", "event_moment"), |
| 237 | + "bite_count", |
| 238 | + ("head_bite_count", "left_arm_bite_count", "right_arm_bite_count", "chest_bite_count", "left_leg_bite_count", "right_leg_bite_count") |
| 239 | + ] |
| 240 | + } |
| 241 | + ) |
| 242 | + ) |
| 243 | + elif obj.type == Report.TYPE_SITE: |
| 244 | + extra_fieldsets.append( |
| 245 | + ( |
| 246 | + _("Specific information"), |
| 247 | + { |
| 248 | + "fields": [ |
| 249 | + "breeding_site_type", |
| 250 | + "breeding_site_has_water", |
| 251 | + "breeding_site_in_public_area", |
| 252 | + "breeding_site_has_near_mosquitoes", |
| 253 | + "breeding_site_has_larvae" |
| 254 | + ] |
| 255 | + } |
| 256 | + ) |
| 257 | + ) |
| 258 | + |
| 259 | + return result + extra_fieldsets |
| 260 | + |
129 | 261 | def has_add_permission(self, request): |
130 | 262 | return False |
131 | 263 |
|
132 | 264 | def has_delete_permission(self, request, obj=None): |
133 | 265 | return False |
134 | 266 |
|
135 | 267 | def other_versions_of_this_report(self, obj): |
136 | | - result = [] |
| 268 | + result = "" |
137 | 269 | for this_version in obj.other_versions: |
138 | 270 | result += '<a href="/admin/tigaserver_app/report/%s">Version %s</a> ' % ( |
139 | 271 | this_version.version_UUID, |
140 | 272 | this_version.version_number, |
141 | 273 | ) |
142 | | - return result |
| 274 | + return mark_safe(result) |
143 | 275 | other_versions_of_this_report.allow_tags = True |
144 | 276 |
|
145 | | - def movelab_score(self, obj): |
146 | | - return obj.movelab_score |
147 | | - |
148 | | - def crowd_score(self, obj): |
149 | | - return obj.crowd_score |
150 | | - |
151 | | - def map_link(self, obj): |
152 | | - return '<a href="/single_report_map/%s/">Show map</a>' % obj.version_UUID |
153 | | - map_link.allow_tags = True |
154 | | - |
155 | | - |
156 | 277 | def export_csv_photo(modeladmin, request, queryset): |
157 | 278 | response = HttpResponse(mimetype='text/csv') |
158 | 279 | response['Content-Disposition'] = 'attachment; filename=tigatrapp_photos.csv' |
@@ -247,7 +368,7 @@ def other_report_versions(self, obj): |
247 | 368 | this_version.version_UUID, |
248 | 369 | this_version.version_number, |
249 | 370 | ) |
250 | | - return result |
| 371 | + return mark_safe(result) |
251 | 372 | other_report_versions.allow_tags = True |
252 | 373 |
|
253 | 374 | def map_link(self, obj): |
|
0 commit comments