|
1 | 1 | from django.contrib.gis import admin |
2 | 2 | from django.contrib.admin import SimpleListFilter |
| 3 | +from django.contrib import admin as admin_ |
| 4 | +from django.contrib.auth import get_permission_codename |
3 | 5 | from django.contrib.postgres import fields |
4 | 6 | from django.utils.translation import gettext as _ |
| 7 | +from django.utils.translation import ngettext as __ |
5 | 8 | from django_json_widget.widgets import JSONEditorWidget |
6 | 9 | from django.utils.html import mark_safe |
7 | 10 | from django.contrib import messages |
|
11 | 14 | from parler.forms import TranslatableModelForm |
12 | 15 | from sortedm2m_filter_horizontal_widget.forms import SortedFilteredSelectMultiple |
13 | 16 | from gsmap.models import Municipality, Snapshot, Workspace, Category, Usergroup, Attachement, Annotation |
| 17 | +from django.http import HttpResponse |
| 18 | +import csv |
| 19 | +from django.utils.timezone import localtime |
| 20 | +import zoneinfo |
14 | 21 |
|
15 | 22 |
|
16 | 23 | class MunicipalityAdmin(admin.OSMGeoAdmin): |
@@ -207,25 +214,98 @@ class AnnotationAdmin(admin.ModelAdmin): |
207 | 214 | ) |
208 | 215 | list_display = ( |
209 | 216 | 'id', |
| 217 | + 'created', |
| 218 | + 'rating', |
210 | 219 | 'public', |
211 | 220 | 'title', |
212 | 221 | 'description', |
213 | 222 | 'usergroup', |
214 | | - 'category', |
215 | | - 'created', |
216 | | - 'rating', |
| 223 | + 'category', |
| 224 | + 'email_domain', |
| 225 | + 'email_hash_short', |
217 | 226 | 'kind', |
218 | 227 | 'workspace', |
219 | 228 | ) |
220 | 229 | inlines = [ AttachementInline, ] |
221 | | - list_filter = (AnnotationWorkspaceFilter, CategoryGroupFilter, 'kind', UsergroupGroupFilter,) |
| 230 | + list_filter = (AnnotationWorkspaceFilter, CategoryGroupFilter, 'kind', UsergroupGroupFilter) |
222 | 231 | search_fields = ('id', 'data') |
| 232 | + actions = ['make_published','make_unpublished','export_as_csv'] |
223 | 233 |
|
224 | 234 | def get_queryset(self, request): |
225 | 235 | queryset = super().get_queryset(request) |
226 | 236 | if not request.user.is_superuser: |
227 | 237 | return queryset.select_related('workspace__group').filter(workspace__group__in=request.user.groups.all()) |
228 | 238 | return queryset |
| 239 | + |
| 240 | + # @admin_.action(description='Publish selected annotations') |
| 241 | + def make_published(self, request, queryset): |
| 242 | + updated = queryset.update(public=True) |
| 243 | + self.message_user(request, __( |
| 244 | + '%d annotation was successfully published.', |
| 245 | + '%d annotations were successfully published.', |
| 246 | + updated, |
| 247 | + ) % updated, messages.SUCCESS) |
| 248 | + # todo refactor when upgrading to django 3.2 as action decorator |
| 249 | + make_published.short_description = "Publish selected annotations" |
| 250 | + make_published.allowed_permissions = ('annotation_publish',) |
| 251 | + |
| 252 | + # @admin_.action(description='Publish selected annotations') |
| 253 | + def make_unpublished(self, request, queryset): |
| 254 | + updated = queryset.update(public=False) |
| 255 | + self.message_user(request, __( |
| 256 | + '%d annotation was successfully unpublished.', |
| 257 | + '%d annotations were successfully unpublished.', |
| 258 | + updated, |
| 259 | + ) % updated, messages.SUCCESS) |
| 260 | + # todo refactor when upgrading to django 3.2 as action decorator |
| 261 | + make_unpublished.short_description = "Unpublish selected annotations" |
| 262 | + make_unpublished.allowed_permissions = ('annotation_publish',) |
| 263 | + |
| 264 | + def has_annotation_publish_permission(self, request): |
| 265 | + """Does the user have the publish permission?""" |
| 266 | + opts = self.opts |
| 267 | + codename = get_permission_codename('annotation_publish', opts) |
| 268 | + return request.user.has_perm('%s.%s' % (opts.app_label, codename)) |
| 269 | + |
| 270 | + def export_as_csv(self, request, queryset): |
| 271 | + response = HttpResponse(content_type="text/csv") |
| 272 | + # response['Content-Disposition'] = 'attachment; filename="export.csv"' |
| 273 | + writer = csv.writer(response) |
| 274 | + writer.writerow(['no', 'created (UTC)', 'title', 'description', 'usergroup_type', 'usergroup', 'category_type', 'category', 'rating', 'workspace', 'public', 'email_hash', 'email_domain', 'geojson']) |
| 275 | + |
| 276 | + for i, r in enumerate(queryset.all()): |
| 277 | + w = r.workspace |
| 278 | + u = r.usergroup |
| 279 | + ug = u.group if u else None |
| 280 | + c = r.category |
| 281 | + cg = c.group if c else None |
| 282 | + u_name = u.name if u else None |
| 283 | + u_group = ug.name if ug else None |
| 284 | + c_name = c.name if c else None |
| 285 | + c_group = ug.name if ug else None |
| 286 | + writer.writerow([ |
| 287 | + i + 1, |
| 288 | + r.created.strftime("%Y-%m-%d %H:%M:%S"), |
| 289 | + r.title, |
| 290 | + r.description, |
| 291 | + u_group, |
| 292 | + u_name, |
| 293 | + c_group, |
| 294 | + c_name, |
| 295 | + r.rating, |
| 296 | + w.title, |
| 297 | + r.public, |
| 298 | + r.email_hash_short, |
| 299 | + r.email_domain, |
| 300 | + r.data |
| 301 | + ]) |
| 302 | + return response |
| 303 | + |
| 304 | + def has_annotation_export_permission(self, request): |
| 305 | + """Does the user have the publish permission?""" |
| 306 | + opts = self.opts |
| 307 | + codename = get_permission_codename('annotation_export', opts) |
| 308 | + return request.user.has_perm('%s.%s' % (opts.app_label, codename)) |
229 | 309 |
|
230 | 310 | class CategoryAdminForm(TranslatableModelForm): |
231 | 311 | class Meta: |
|
0 commit comments