Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
djangoldp
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
djangoldp-packages
djangoldp
Merge requests
!283
feature: csv export in admin
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
feature: csv export in admin
export_csv
into
master
Overview
5
Commits
4
Pipelines
4
Changes
2
1 unresolved thread
Hide all comments
Merged
Sylvain Le Bon
requested to merge
export_csv
into
master
10 months ago
Overview
5
Commits
4
Pipelines
4
Changes
2
1 unresolved thread
Hide all comments
Expand
0
0
Merge request reports
Compare
master
version 3
dc436ff4
10 months ago
version 2
bd7d2311
10 months ago
version 1
8ed4099a
10 months ago
master (base)
and
latest version
latest version
15b7b3ef
4 commits,
10 months ago
version 3
dc436ff4
3 commits,
10 months ago
version 2
bd7d2311
2 commits,
10 months ago
version 1
8ed4099a
1 commit,
10 months ago
2 files
+
40
−
5
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
2
Search (e.g. *.vue) (Ctrl+P)
djangoldp/admin.py
+
31
−
5
Options
from
csv
import
DictWriter
from
django.contrib
import
admin
from
guardian.admin
import
GuardedModelAdmin
from
django.contrib.auth.admin
import
UserAdmin
from
django.core.exceptions
import
FieldDoesNotExist
from
django.http
import
HttpResponse
from
guardian.admin
import
GuardedModelAdmin
from
djangoldp.models
import
Activity
,
ScheduledActivity
,
Follower
from
djangoldp.activities.services
import
ActivityQueueService
@@ -10,10 +13,33 @@ class DjangoLDPAdmin(GuardedModelAdmin):
An admin model representing a federated object. Inherits from GuardedModelAdmin to provide Django-Guardian
object-level permissions
'''
pass
class
DjangoLDPUserAdmin
(
UserAdmin
,
GuardedModelAdmin
):
actions
=
[
'
export_csv
'
]
export_fields
=
[]
def
resolve_verbose_name
(
self
,
field_path
):
field
=
self
for
field_name
in
field_path
.
split
(
'
__
'
):
try
:
field
=
field
.
model
.
_meta
.
get_field
(
field_name
)
except
FieldDoesNotExist
:
return
None
return
field
.
verbose_name
@admin.action
(
description
=
"
Export CSV
"
)
def
export_csv
(
self
,
request
,
queryset
):
response
=
HttpResponse
(
content_type
=
"
text/csv
"
)
response
[
'
Content-Disposition
'
]
=
f
'
attachment; filename=
"
{
self
.
model
.
__name__
}
.csv
"'
# only keep fields that can be resolved, keep only urlid if none
field_list
=
list
(
filter
(
self
.
resolve_verbose_name
,
self
.
export_fields
or
self
.
list_display
))
or
[
'
urlid
'
]
headers
=
{
field
:
self
.
resolve_verbose_name
(
field
)
for
field
in
field_list
}
writer
=
DictWriter
(
response
,
fieldnames
=
field_list
)
writer
.
writerow
(
headers
)
writer
.
writerows
(
queryset
.
values
(
*
field_list
))
return
response
class
DjangoLDPUserAdmin
(
UserAdmin
,
DjangoLDPAdmin
):
'''
An extension of UserAdmin providing the functionality of DjangoLDPAdmin
'''
list_display
=
(
'
urlid
'
,
'
email
'
,
'
first_name
'
,
'
last_name
'
,
'
date_joined
'
,
'
last_login
'
,
'
is_staff
'
)
Loading