Skip to content

Commit

Permalink
Formatting admin.py
Browse files Browse the repository at this point in the history
  • Loading branch information
shacker committed Nov 1, 2020
1 parent 3066ef1 commit 33ac347
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions todo/admin.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
from django.contrib import admin

from todo.models import Attachment, Comment, Task, TaskList
import csv
import datetime

from django.contrib import admin
from django.http import HttpResponse
from django.urls import reverse
from django.utils.safestring import mark_safe

from todo.models import Attachment, Comment, Task, TaskList


def export_to_csv(modeladmin, request, queryset):
opts = modeladmin.model._meta
content_disposition = f'attachment; filename={opts.verbose_name}.csv'
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = content_disposition
content_disposition = f"attachment; filename={opts.verbose_name}.csv"
response = HttpResponse(content_type="text/csv")
response["Content-Disposition"] = content_disposition
writer = csv.writer(response)
fields = [field for field in opts.get_fields() if not field.many_to_many\
and not field.one_to_many]
fields = [
field for field in opts.get_fields() if not (field.many_to_many and not field.one_to_many)
]
# Write a first row with header information
writer.writerow([field.verbose_name for field in fields])
# Write data rows
Expand All @@ -24,18 +24,21 @@ def export_to_csv(modeladmin, request, queryset):
for field in fields:
value = getattr(obj, field.name)
if isinstance(value, datetime.datetime):
value = value.strftime('%d/%m/%Y')
value = value.strftime("%d/%m/%Y")
data_row.append(value)
writer.writerow(data_row)
return response
export_to_csv.short_description = 'Export to CSV'


export_to_csv.short_description = "Export to CSV"


class TaskAdmin(admin.ModelAdmin):
list_display = ("title", "task_list", "completed", "priority", "due_date")
list_filter = ("task_list",)
ordering = ("priority",)
search_fields = ("title",)
actions=[export_to_csv]
actions = [export_to_csv]


class CommentAdmin(admin.ModelAdmin):
Expand Down

0 comments on commit 33ac347

Please sign in to comment.