Files @ b50974263c36
Branch filter:

Location: symposion_app/symposion/proposals/actions.py

Hiroshi Miura
i18n sponsor fields

Feedback from PyConJP development

```
commit 5973e32ebdc231b209b5c058664e8b2b4a1dbc54
Author: MURAOKA Yusuke <yusuke@jbking.org>
Date: Mon Mar 31 15:35:40 2014 +0900

introduce Benefit.content_type which is used to display localized
text
```

Signed-off-by: Hiroshi Miura <miurahr@linux.com>
import csv

from django.http import HttpResponse


def export_as_csv_action(description="Export selected objects as CSV file",
                         fields=None, exclude=None, header=True):
    """
    This function returns an export csv action
    'fields' and 'exclude' work like in Django ModelForm
    'header' is whether or not to output the column names as the first row
    """
    def export_as_csv(modeladmin, request, queryset):
        """
        Generic csv export admin action.
        based on http://djangosnippets.org/snippets/1697/
        """
        opts = modeladmin.model._meta
        if fields:
            fieldset = set(fields)
            field_names = fieldset
        elif exclude:
            excludeset = set(exclude)
            field_names = field_names - excludeset
        response = HttpResponse(content_type="text/csv")
        response["Content-Disposition"] = "attachment; filename=%s.csv" % unicode(opts).replace(".", "_")
        writer = csv.writer(response)
        if header:
            writer.writerow(list(field_names))
        for obj in queryset:
            writer.writerow(
                [unicode(getattr(obj, field)).encode("utf-8", "replace") for field in field_names])
        return response
    export_as_csv.short_description = description
    return export_as_csv