Repository: svthalia/concrexit

Issue:
Fix "identical-code" issue in website/education/admin.py
Identical blocks of code found in 2 locations. Consider refactoring.

https://codeclimate.com/github/svthalia/concrexit/website/education/admin.py#issue_5ecfbb80e8d0fa00010003f6


Discussion:

Resolving patch:
diff --git a/website/education/admin.py b/website/education/admin.py
--- a/website/education/admin.py
+++ b/website/education/admin.py
@@ -29,8 +29,27 @@ class CourseAdmin(TranslatedModelAdmin):
     search_fields = ("name", "course_code")
 
 
+class WithDownloadCsv:
+    def download_csv(self, request, queryset):
+        opts = queryset.model._meta
+        response = HttpResponse(content_type="text/csv")
+        # force download.
+        response["Content-Disposition"] = "attachment;filename=export.csv"
+        # the csv writer
+        writer = csv.writer(response)
+        field_names = [field.name for field in opts.fields]
+        # Write a first row with header information
+        writer.writerow(field_names)
+        # Write data rows
+        for obj in queryset:
+            writer.writerow([getattr(obj, field) for field in field_names])
+        return response
+
+    download_csv.short_description = _("Download marked as csv")
+
+
 @admin.register(models.Exam)
-class ExamAdmin(TranslatedModelAdmin):
+class ExamAdmin(TranslatedModelAdmin, WithDownloadCsv):
     list_display = (
         "type",
         "course",
@@ -66,26 +85,9 @@ def reset_download_count(self, request, queryset):
 
     reset_download_count.short_description = _("Reset the marked exams download count")
 
-    def download_csv(self, request, queryset):
-        opts = queryset.model._meta
-        response = HttpResponse(content_type="text/csv")
-        # force download.
-        response["Content-Disposition"] = "attachment;filename=export.csv"
-        # the csv writer
-        writer = csv.writer(response)
-        field_names = [field.name for field in opts.fields]
-        # Write a first row with header information
-        writer.writerow(field_names)
-        # Write data rows
-        for obj in queryset:
-            writer.writerow([getattr(obj, field) for field in field_names])
-        return response
-
-    download_csv.short_description = _("Download marked as csv")
-
 
 @admin.register(models.Summary)
-class SummaryAdmin(TranslatedModelAdmin):
+class SummaryAdmin(TranslatedModelAdmin, WithDownloadCsv):
     list_display = (
         "name",
         "course",
@@ -122,20 +124,3 @@ def reset_download_count(self, request, queryset):
     reset_download_count.short_description = _(
         "Reset the marked summaries download count"
     )
-
-    def download_csv(self, request, queryset):
-        opts = queryset.model._meta
-        response = HttpResponse(content_type="text/csv")
-        # force download.
-        response["Content-Disposition"] = "attachment;filename=export.csv"
-        # the csv writer
-        writer = csv.writer(response)
-        field_names = [field.name for field in opts.fields]
-        # Write a first row with header information
-        writer.writerow(field_names)
-        # Write data rows
-        for obj in queryset:
-            writer.writerow([getattr(obj, field) for field in field_names])
-        return response
-
-    download_csv.short_description = _("Download marked as csv")


Test patch: