Skip to content

Commit 1887ead

Browse files
committed
csv export from admin
1 parent e804fb7 commit 1887ead

File tree

2 files changed

+101
-4
lines changed

2 files changed

+101
-4
lines changed

django/gsmap/admin.py

Lines changed: 84 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
from django.contrib.gis import admin
22
from django.contrib.admin import SimpleListFilter
3+
from django.contrib import admin as admin_
4+
from django.contrib.auth import get_permission_codename
35
from django.contrib.postgres import fields
46
from django.utils.translation import gettext as _
7+
from django.utils.translation import ngettext as __
58
from django_json_widget.widgets import JSONEditorWidget
69
from django.utils.html import mark_safe
710
from django.contrib import messages
@@ -11,6 +14,10 @@
1114
from parler.forms import TranslatableModelForm
1215
from sortedm2m_filter_horizontal_widget.forms import SortedFilteredSelectMultiple
1316
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
1421

1522

1623
class MunicipalityAdmin(admin.OSMGeoAdmin):
@@ -207,25 +214,98 @@ class AnnotationAdmin(admin.ModelAdmin):
207214
)
208215
list_display = (
209216
'id',
217+
'created',
218+
'rating',
210219
'public',
211220
'title',
212221
'description',
213222
'usergroup',
214-
'category',
215-
'created',
216-
'rating',
223+
'category',
224+
'email_domain',
225+
'email_hash_short',
217226
'kind',
218227
'workspace',
219228
)
220229
inlines = [ AttachementInline, ]
221-
list_filter = (AnnotationWorkspaceFilter, CategoryGroupFilter, 'kind', UsergroupGroupFilter,)
230+
list_filter = (AnnotationWorkspaceFilter, CategoryGroupFilter, 'kind', UsergroupGroupFilter)
222231
search_fields = ('id', 'data')
232+
actions = ['make_published','make_unpublished','export_as_csv']
223233

224234
def get_queryset(self, request):
225235
queryset = super().get_queryset(request)
226236
if not request.user.is_superuser:
227237
return queryset.select_related('workspace__group').filter(workspace__group__in=request.user.groups.all())
228238
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))
229309

230310
class CategoryAdminForm(TranslatableModelForm):
231311
class Meta:

django/gsmap/models.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import requests
77
from enum import IntFlag
88
import bleach
9+
import hashlib
910

1011
from sortedm2m.fields import SortedManyToManyField
1112
from sorl.thumbnail import ImageField, get_thumbnail
@@ -35,6 +36,7 @@
3536
from gsuser.models import User
3637
from main.utils import get_website
3738

39+
3840
SECRET_KEY = os.getenv('DJANGO_SECRET_KEY') or os.getenv('DJANGO_SECRET_KEY_DEV')
3941

4042
class OverwriteStorage(FileSystemStorage):
@@ -499,6 +501,21 @@ def title(self):
499501
return f'{self.data["properties"]["title"]}'
500502
else:
501503
return None
504+
505+
@property
506+
def email_domain(self):
507+
return self.author_email.split("@")[1]
508+
509+
@property
510+
def email_hash(self):
511+
m = hashlib.sha512()
512+
m.update(SECRET_KEY.encode('ascii'))
513+
m.update(self.author_email.encode('ascii'))
514+
return m.hexdigest()
515+
516+
@property
517+
def email_hash_short(self):
518+
return self.email_hash[:10]
502519

503520
@property
504521
def description(self):

0 commit comments

Comments
 (0)