cehdeti / pyeti

A Python package of awesome stuff.
MIT License
0 stars 0 forks source link

Adding export_as_csv method to usage license admin #3

Closed ashleyelaine closed 4 years ago

theunraveler commented 4 years ago

I wonder if we want all of the model's fields in the export, or just the columns in the admin table view. Thoughts?

ashleyelaine commented 4 years ago

I was thinking about that, but then I thought maybe it's better to be able to select 'active' vs. 'inactive'? Or select specific ones? You're right though, we could just program it to download a CSV of all 'active' usage licenses. This would also make sense in case you weren't able to get all on one page in order to 'select all'. I did check on CC to see how pagination works, and it appears to be set up as a 'load more' so it would allow you to still select all.

theunraveler commented 4 years ago

Sorry, I think I wasn't clear. I do like the idea of selecting certain ones to export. What I meant was that it looks like the columns that get exported are all of the model's fields, where I think you might just want the columns from the admin table.

ashleyelaine commented 4 years ago

ohh I see :)

theunraveler commented 4 years ago

I'm sorry---actually, here's what I'd do, so that it can be overridden in projects that subclass this class:

class UsageLicenseAdmin(admin.ModelAdmin):

    csv_export_fields = ['token', 'num_seats', 'start_date', 'end_date', 'spree_order_number']

    ...

    def export_as_csv(self, request, queryset):
        response = HttpResponse(content_type='text/csv')
        response['Content-Disposition'] = 'attachment; filename=%s.csv' % self.model._meta

        writer = csv.writer(response)
        writer.writerow(self.csv_export_fields)
        for obj in queryset:
            writer.writerow([getattr(obj, field) for field in self.csv_export_fields])

        return response

    export_as_csv.short_description = 'Export Selected'

That way, subclasses can just redefine csv_export_fields to change that settings, rather than overriding all of export_as_csv. Make sense?

ashleyelaine commented 4 years ago

That does make sense. I like it. Updated!