Skip to content

Commit

Permalink
Batch Export of tasks from TaskAdmin Panel (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamlovelesh committed Nov 1, 2020
1 parent 7efa164 commit 3066ef1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ assignment application for Django, designed to be dropped into an existing site
* Mobile-friendly (work in progress)
* Separate view for My Tasks (across lists)
* Batch-import tasks via CSV
* Batch-export tasks in CSV Format
* Multiple file attachments per task (see settings)
* Integrated mail tracking (unify a task list with an email box)

Expand Down Expand Up @@ -404,4 +405,4 @@ django-todo no longer references a jQuery datepicker, but defaults to native htm

### URLs

Some views and URLs were renamed for logical consistency. If this affects you, see source code and the demo GTD site for reference to the new URL names.
Some views and URLs were renamed for logical consistency. If this affects you, see source code and the demo GTD site for reference to the new URL names.
30 changes: 29 additions & 1 deletion todo/admin.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,41 @@
from django.contrib import admin

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

import csv
import datetime
from django.http import HttpResponse
from django.urls import reverse
from django.utils.safestring import mark_safe


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
writer = csv.writer(response)
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
for obj in queryset:
data_row = []
for field in fields:
value = getattr(obj, field.name)
if isinstance(value, datetime.datetime):
value = value.strftime('%d/%m/%Y')
data_row.append(value)
writer.writerow(data_row)
return response
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]


class CommentAdmin(admin.ModelAdmin):
Expand Down

0 comments on commit 3066ef1

Please sign in to comment.