Skip to content

Commit

Permalink
Merge pull request #34 from mrunderline/feat/ExportCSV
Browse files Browse the repository at this point in the history
feat: add ExportCsvMixin class to export CSV
  • Loading branch information
ZeroCoolHacker committed Oct 12, 2020
2 parents 8fdbe88 + 51f85d5 commit 5d8df8a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 9 deletions.
7 changes: 6 additions & 1 deletion course/admin.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from django.contrib import admin

from .models import Course
from easyschool.utils import ExportCsvMixin


# Register your models here.
@admin.register(Course)
class CourseAdmin(admin.ModelAdmin):
class CourseAdmin(admin.ModelAdmin, ExportCsvMixin):
"""Admin Display Options to display the Course data in admin panel"""

list_display = (
Expand All @@ -16,3 +17,7 @@ class CourseAdmin(admin.ModelAdmin):
search_fields = (
'course_name',
)

actions = [
'export_as_csv',
]
22 changes: 21 additions & 1 deletion easyschool/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from datetime import date
import csv
from django.http import HttpResponse

GENDER_CHOICES = (
('M', 'Male'),
Expand All @@ -25,4 +27,22 @@ def next_month():
month = date.today().month + 1
year = date.today().year
day = 1
return date(year, month, day)
return date(year, month, day)


class ExportCsvMixin:
def export_as_csv(self, request, queryset):
meta = self.model._meta
field_names = [field.name for field in meta.fields]

response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename={}.csv'.format(meta)
writer = csv.writer(response)

writer.writerow(field_names)
for obj in queryset:
writer.writerow([getattr(obj, field) for field in field_names])

return response

export_as_csv.short_description = 'Export Selected'
18 changes: 14 additions & 4 deletions students/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.utils.html import format_html, mark_safe

from .models import Guardian, Student, StudentFee
from easyschool.utils import ExportCsvMixin

# Register your models here.

Expand All @@ -20,7 +21,7 @@ class GuardianInline(admin.TabularInline):


@admin.register(Student)
class StudentAdmin(admin.ModelAdmin):
class StudentAdmin(admin.ModelAdmin, ExportCsvMixin):
""" Display Class of Student Model in Admin Panel"""

# remove delete actions
Expand Down Expand Up @@ -130,6 +131,7 @@ def profile_image_display(self, obj):
]
actions = [
'expel_from_school',
'export_as_csv',
]

def get_readonly_fields(self, request, obj=None):
Expand All @@ -147,7 +149,7 @@ def get_readonly_fields(self, request, obj=None):


@admin.register(StudentFee)
class StudentFeeAdmin(admin.ModelAdmin):
class StudentFeeAdmin(admin.ModelAdmin, ExportCsvMixin):
""" Admin display class for the model StudentFee """

# Delete Delete Action from admin
Expand Down Expand Up @@ -175,14 +177,22 @@ def get_actions(self, request):
'date_submitted',
)

actions = [
'export_as_csv',
]


@admin.register(Guardian)
class GuardianAdmin(admin.ModelAdmin):
class GuardianAdmin(admin.ModelAdmin, ExportCsvMixin):
list_display = (
'name',
'gender',
'relation_to_student',
'social_security_number',
'phone_number',
'profession',
)
)

actions = [
'export_as_csv',
]
15 changes: 12 additions & 3 deletions teachers/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.utils.translation import ugettext_lazy as _

from .models import Teacher, TeacherSalary
from easyschool.utils import ExportCsvMixin


# Change User Creation Form
Expand Down Expand Up @@ -35,7 +36,7 @@ def __init__(self, *args, **kwargs):
# Register your models here.

@admin.register(Teacher)
class TeacherAdmin(admin.ModelAdmin):
class TeacherAdmin(admin.ModelAdmin, ExportCsvMixin):
'''Admin View for Teacher'''

list_display = (
Expand All @@ -59,9 +60,13 @@ class TeacherAdmin(admin.ModelAdmin):

ordering = ('-date_of_joining',)

actions = [
'export_as_csv',
]


@admin.register(TeacherSalary)
class TeacherSalaryAdmin(admin.ModelAdmin):
class TeacherSalaryAdmin(admin.ModelAdmin, ExportCsvMixin):
""" Admin display class for the model StudentFee """

# Delete Delete Action from admin
Expand All @@ -85,4 +90,8 @@ def get_actions(self, request):
'valid_until',
'total_amount',
'paid_on',
)
)

actions = [
'export_as_csv',
]

0 comments on commit 5d8df8a

Please sign in to comment.