prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Eksport w CSV EmailLog 
  Wprowadziliśmy w ```feder.letters.logs``` statystyki dostarczania wiadomości. Należy wprowadzić zestawienie wszystkich danych z EmailLog dla danego monitoringu, aby można było zrobić statystykę czy coś.
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `feder/letters/logs/views.py`
  Content:
  ```
  1 # -*- coding: utf-8 -*-
  2 from __future__ import unicode_literals
  3 
  4 from braces.views import SelectRelatedMixin, PrefetchRelatedMixin
  5 from cached_property import cached_property
  6 from django.shortcuts import get_object_or_404
  7 from django.views.generic import DetailView, ListView
  8 
  9 from feder.cases.models import Case
  10 from feder.letters.logs.models import EmailLog
  11 from feder.main.mixins import AttrPermissionRequiredMixin
  12 from feder.monitorings.models import Monitoring
  13 
  14 
  15 class ListMonitoringMixin(AttrPermissionRequiredMixin, SelectRelatedMixin):
  16     select_related = ['case']
  17     paginate_by = 100
  18     model = EmailLog
  19     permission_attribute = 'case__monitoring'
  20     permission_required = 'monitorings.view_log'
  21 
  22     def get_permission_object(self):
  23         return self.monitoring
  24 
  25     def get_queryset(self):
  26         return super(ListMonitoringMixin, self).get_queryset().filter(case__monitoring=self.monitoring).with_logrecord_count()
  27 
  28     def get_context_data(self, **kwargs):
  29         kwargs['monitoring'] = self.monitoring
  30         return super(ListMonitoringMixin, self).get_context_data(**kwargs)
  31 
  32 
  33 class EmailLogMonitoringListView(ListMonitoringMixin, ListView):
  34     template_name_suffix = '_list_for_monitoring'
  35     permission_required = 'monitorings.view_log'
  36 
  37     @cached_property
  38     def monitoring(self):
  39         return get_object_or_404(Monitoring, pk=self.kwargs['monitoring_pk'])
  40 
  41 
  42 class EmailLogCaseListView(ListMonitoringMixin, ListView):
  43     template_name_suffix = '_list_for_case'
  44 
  45     @cached_property
  46     def case(self):
  47         return get_object_or_404(Case.objects.select_related('monitoring'),
  48                                  pk=self.kwargs['case_pk'])
  49 
  50     @cached_property
  51     def monitoring(self):
  52         return self.case.monitoring
  53 
  54     def get_context_data(self, **kwargs):
  55         kwargs['case'] = self.case
  56         return super(EmailLogCaseListView, self).get_context_data(**kwargs)
  57 
  58     def get_queryset(self):
  59         return super(ListMonitoringMixin, self).get_queryset().filter(case=self.case)
  60 
  61 
  62 class EmailLogDetailView(AttrPermissionRequiredMixin, PrefetchRelatedMixin,
  63                          SelectRelatedMixin, DetailView):
  64     model = EmailLog
  65     select_related = ['case__monitoring']
  66     prefetch_related = ['logrecord_set']
  67     permission_attribute = 'case__monitoring'
  68     permission_required = 'monitorings.view_log'
  69
  ```
  
  Path: `feder/letters/logs/urls.py`
  Content:
  ```
  1 # -*- coding: utf-8 -*-
  2 from __future__ import unicode_literals
  3 
  4 from django.conf.urls import url
  5 from django.utils.translation import ugettext_lazy as _
  6 
  7 from . import views
  8 
  9 urlpatterns = [
  10     url(_(r'^case-(?P<case_pk>[\d-]+)$'), views.EmailLogCaseListView.as_view(),
  11         name="list"),
  12     url(_(r'^monitoring-(?P<monitoring_pk>[\d-]+)$'), views.EmailLogMonitoringListView.as_view(),
  13         name="list"),
  14     url(_(r'^log-(?P<pk>[\d-]+)$'), views.EmailLogDetailView.as_view(),
  15         name="detail"),
  16 ]
  17
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/feder/letters/logs/urls.py b/feder/letters/logs/urls.py
  --- a/feder/letters/logs/urls.py
  +++ b/feder/letters/logs/urls.py
  @@ -11,6 +11,8 @@
           name="list"),
       url(_(r'^monitoring-(?P<monitoring_pk>[\d-]+)$'), views.EmailLogMonitoringListView.as_view(),
           name="list"),
  +    url(_(r'^monitoring-(?P<monitoring_pk>[\d-]+)/export$'), views.EmailLogMonitoringCsvView.as_view(),
  +        name="export"),
       url(_(r'^log-(?P<pk>[\d-]+)$'), views.EmailLogDetailView.as_view(),
           name="detail"),
   ]
  diff --git a/feder/letters/logs/views.py b/feder/letters/logs/views.py
  --- a/feder/letters/logs/views.py
  +++ b/feder/letters/logs/views.py
  @@ -1,8 +1,12 @@
   # -*- coding: utf-8 -*-
   from __future__ import unicode_literals
   
  +from django.utils import timezone
  +import unicodecsv as csv
  +
   from braces.views import SelectRelatedMixin, PrefetchRelatedMixin
   from cached_property import cached_property
  +from django.http import HttpResponse
   from django.shortcuts import get_object_or_404
   from django.views.generic import DetailView, ListView
   
  @@ -10,7 +14,7 @@
   from feder.letters.logs.models import EmailLog
   from feder.main.mixins import AttrPermissionRequiredMixin
   from feder.monitorings.models import Monitoring
  -
  +from django.views.generic.list import ListView
   
   class ListMonitoringMixin(AttrPermissionRequiredMixin, SelectRelatedMixin):
       select_related = ['case']
  @@ -39,6 +43,61 @@
           return get_object_or_404(Monitoring, pk=self.kwargs['monitoring_pk'])
   
   
  +class EmailLogMonitoringCsvView(ListMonitoringMixin, ListView):
  +    permission_required = 'monitorings.view_log'
  +
  +    select_related = ['case', 'case__institution']
  +
  +    @cached_property
  +    def monitoring(self):
  +        return get_object_or_404(Monitoring, pk=self.kwargs['monitoring_pk'])
  +
  +    def get(self, *args, **kwargs):
  +        response = self._get_csv_response()
  +        self._write_rows(response, self.get_queryset())
  +        return response
  +
  +    @staticmethod
  +    def _get_base_model_field_names(queryset):
  +        opts = queryset.model._meta
  +        return [field.name for field in opts.fields if field.related_model is None]
  +
  +    def _get_csv_response(self):
  +        csv_response = HttpResponse(content_type='text/csv')
  +        current_time = timezone.now()
  +        filename = 'email_log_{0}-{1}-{2}.csv'.format(self.monitoring.id,
  +                                                      current_time.strftime('%Y_%m_%d-%H_%M_%S'),
  +                                                      current_time.tzname()
  +                                                      )
  +        csv_response['Content-Disposition'] = "attachment;filename={0}".format(filename)
  +        return csv_response
  +
  +    def _write_rows(self, response, queryset):
  +        writer = csv.writer(response)
  +
  +        # automatically add all fields from base table/model
  +        base_field_names = self._get_base_model_field_names(queryset)
  +
  +        # print header row
  +        writer.writerow(base_field_names +
  +                        [
  +                            'case id',
  +                            'case email',
  +                            'institution',
  +                            'institution id',
  +                            'monitoring id']
  +                        )
  +
  +        for obj in queryset:
  +            writer.writerow(
  +                [getattr(obj, field) for field in base_field_names] + [
  +                obj.case.id,
  +                obj.case.email,
  +                obj.case.institution.name,
  +                obj.case.institution_id,
  +                obj.case.monitoring_id,
  +            ])
  +
   class EmailLogCaseListView(ListMonitoringMixin, ListView):
       template_name_suffix = '_list_for_case'
  
verification_info: {"golden_diff": "diff --git a/feder/letters/logs/urls.py b/feder/letters/logs/urls.py\n--- a/feder/letters/logs/urls.py\n+++ b/feder/letters/logs/urls.py\n@@ -11,6 +11,8 @@\n         name=\"list\"),\n     url(_(r'^monitoring-(?P<monitoring_pk>[\\d-]+)$'), views.EmailLogMonitoringListView.as_view(),\n         name=\"list\"),\n+    url(_(r'^monitoring-(?P<monitoring_pk>[\\d-]+)/export$'), views.EmailLogMonitoringCsvView.as_view(),\n+        name=\"export\"),\n     url(_(r'^log-(?P<pk>[\\d-]+)$'), views.EmailLogDetailView.as_view(),\n         name=\"detail\"),\n ]\ndiff --git a/feder/letters/logs/views.py b/feder/letters/logs/views.py\n--- a/feder/letters/logs/views.py\n+++ b/feder/letters/logs/views.py\n@@ -1,8 +1,12 @@\n # -*- coding: utf-8 -*-\n from __future__ import unicode_literals\n \n+from django.utils import timezone\n+import unicodecsv as csv\n+\n from braces.views import SelectRelatedMixin, PrefetchRelatedMixin\n from cached_property import cached_property\n+from django.http import HttpResponse\n from django.shortcuts import get_object_or_404\n from django.views.generic import DetailView, ListView\n \n@@ -10,7 +14,7 @@\n from feder.letters.logs.models import EmailLog\n from feder.main.mixins import AttrPermissionRequiredMixin\n from feder.monitorings.models import Monitoring\n-\n+from django.views.generic.list import ListView\n \n class ListMonitoringMixin(AttrPermissionRequiredMixin, SelectRelatedMixin):\n     select_related = ['case']\n@@ -39,6 +43,61 @@\n         return get_object_or_404(Monitoring, pk=self.kwargs['monitoring_pk'])\n \n \n+class EmailLogMonitoringCsvView(ListMonitoringMixin, ListView):\n+    permission_required = 'monitorings.view_log'\n+\n+    select_related = ['case', 'case__institution']\n+\n+    @cached_property\n+    def monitoring(self):\n+        return get_object_or_404(Monitoring, pk=self.kwargs['monitoring_pk'])\n+\n+    def get(self, *args, **kwargs):\n+        response = self._get_csv_response()\n+        self._write_rows(response, self.get_queryset())\n+        return response\n+\n+    @staticmethod\n+    def _get_base_model_field_names(queryset):\n+        opts = queryset.model._meta\n+        return [field.name for field in opts.fields if field.related_model is None]\n+\n+    def _get_csv_response(self):\n+        csv_response = HttpResponse(content_type='text/csv')\n+        current_time = timezone.now()\n+        filename = 'email_log_{0}-{1}-{2}.csv'.format(self.monitoring.id,\n+                                                      current_time.strftime('%Y_%m_%d-%H_%M_%S'),\n+                                                      current_time.tzname()\n+                                                      )\n+        csv_response['Content-Disposition'] = \"attachment;filename={0}\".format(filename)\n+        return csv_response\n+\n+    def _write_rows(self, response, queryset):\n+        writer = csv.writer(response)\n+\n+        # automatically add all fields from base table/model\n+        base_field_names = self._get_base_model_field_names(queryset)\n+\n+        # print header row\n+        writer.writerow(base_field_names +\n+                        [\n+                            'case id',\n+                            'case email',\n+                            'institution',\n+                            'institution id',\n+                            'monitoring id']\n+                        )\n+\n+        for obj in queryset:\n+            writer.writerow(\n+                [getattr(obj, field) for field in base_field_names] + [\n+                obj.case.id,\n+                obj.case.email,\n+                obj.case.institution.name,\n+                obj.case.institution_id,\n+                obj.case.monitoring_id,\n+            ])\n+\n class EmailLogCaseListView(ListMonitoringMixin, ListView):\n     template_name_suffix = '_list_for_case'\n", "issue": "Eksport w CSV EmailLog \nWprowadzili\u015bmy w ```feder.letters.logs``` statystyki dostarczania wiadomo\u015bci. Nale\u017cy wprowadzi\u0107 zestawienie wszystkich danych z EmailLog dla danego monitoringu, aby mo\u017cna by\u0142o zrobi\u0107 statystyk\u0119 czy co\u015b.\n", "before_files": [{"content": "# -*- coding: utf-8 -*-\nfrom __future__ import unicode_literals\n\nfrom braces.views import SelectRelatedMixin, PrefetchRelatedMixin\nfrom cached_property import cached_property\nfrom django.shortcuts import get_object_or_404\nfrom django.views.generic import DetailView, ListView\n\nfrom feder.cases.models import Case\nfrom feder.letters.logs.models import EmailLog\nfrom feder.main.mixins import AttrPermissionRequiredMixin\nfrom feder.monitorings.models import Monitoring\n\n\nclass ListMonitoringMixin(AttrPermissionRequiredMixin, SelectRelatedMixin):\n    select_related = ['case']\n    paginate_by = 100\n    model = EmailLog\n    permission_attribute = 'case__monitoring'\n    permission_required = 'monitorings.view_log'\n\n    def get_permission_object(self):\n        return self.monitoring\n\n    def get_queryset(self):\n        return super(ListMonitoringMixin, self).get_queryset().filter(case__monitoring=self.monitoring).with_logrecord_count()\n\n    def get_context_data(self, **kwargs):\n        kwargs['monitoring'] = self.monitoring\n        return super(ListMonitoringMixin, self).get_context_data(**kwargs)\n\n\nclass EmailLogMonitoringListView(ListMonitoringMixin, ListView):\n    template_name_suffix = '_list_for_monitoring'\n    permission_required = 'monitorings.view_log'\n\n    @cached_property\n    def monitoring(self):\n        return get_object_or_404(Monitoring, pk=self.kwargs['monitoring_pk'])\n\n\nclass EmailLogCaseListView(ListMonitoringMixin, ListView):\n    template_name_suffix = '_list_for_case'\n\n    @cached_property\n    def case(self):\n        return get_object_or_404(Case.objects.select_related('monitoring'),\n                                 pk=self.kwargs['case_pk'])\n\n    @cached_property\n    def monitoring(self):\n        return self.case.monitoring\n\n    def get_context_data(self, **kwargs):\n        kwargs['case'] = self.case\n        return super(EmailLogCaseListView, self).get_context_data(**kwargs)\n\n    def get_queryset(self):\n        return super(ListMonitoringMixin, self).get_queryset().filter(case=self.case)\n\n\nclass EmailLogDetailView(AttrPermissionRequiredMixin, PrefetchRelatedMixin,\n                         SelectRelatedMixin, DetailView):\n    model = EmailLog\n    select_related = ['case__monitoring']\n    prefetch_related = ['logrecord_set']\n    permission_attribute = 'case__monitoring'\n    permission_required = 'monitorings.view_log'\n", "path": "feder/letters/logs/views.py"}, {"content": "# -*- coding: utf-8 -*-\nfrom __future__ import unicode_literals\n\nfrom django.conf.urls import url\nfrom django.utils.translation import ugettext_lazy as _\n\nfrom . import views\n\nurlpatterns = [\n    url(_(r'^case-(?P<case_pk>[\\d-]+)$'), views.EmailLogCaseListView.as_view(),\n        name=\"list\"),\n    url(_(r'^monitoring-(?P<monitoring_pk>[\\d-]+)$'), views.EmailLogMonitoringListView.as_view(),\n        name=\"list\"),\n    url(_(r'^log-(?P<pk>[\\d-]+)$'), views.EmailLogDetailView.as_view(),\n        name=\"detail\"),\n]\n", "path": "feder/letters/logs/urls.py"}], "after_files": [{"content": "# -*- coding: utf-8 -*-\nfrom __future__ import unicode_literals\n\nfrom django.utils import timezone\nimport unicodecsv as csv\n\nfrom braces.views import SelectRelatedMixin, PrefetchRelatedMixin\nfrom cached_property import cached_property\nfrom django.http import HttpResponse\nfrom django.shortcuts import get_object_or_404\nfrom django.views.generic import DetailView, ListView\n\nfrom feder.cases.models import Case\nfrom feder.letters.logs.models import EmailLog\nfrom feder.main.mixins import AttrPermissionRequiredMixin\nfrom feder.monitorings.models import Monitoring\nfrom django.views.generic.list import ListView\n\nclass ListMonitoringMixin(AttrPermissionRequiredMixin, SelectRelatedMixin):\n    select_related = ['case']\n    paginate_by = 100\n    model = EmailLog\n    permission_attribute = 'case__monitoring'\n    permission_required = 'monitorings.view_log'\n\n    def get_permission_object(self):\n        return self.monitoring\n\n    def get_queryset(self):\n        return super(ListMonitoringMixin, self).get_queryset().filter(case__monitoring=self.monitoring).with_logrecord_count()\n\n    def get_context_data(self, **kwargs):\n        kwargs['monitoring'] = self.monitoring\n        return super(ListMonitoringMixin, self).get_context_data(**kwargs)\n\n\nclass EmailLogMonitoringListView(ListMonitoringMixin, ListView):\n    template_name_suffix = '_list_for_monitoring'\n    permission_required = 'monitorings.view_log'\n\n    @cached_property\n    def monitoring(self):\n        return get_object_or_404(Monitoring, pk=self.kwargs['monitoring_pk'])\n\n\nclass EmailLogMonitoringCsvView(ListMonitoringMixin, ListView):\n    permission_required = 'monitorings.view_log'\n\n    select_related = ['case', 'case__institution']\n\n    @cached_property\n    def monitoring(self):\n        return get_object_or_404(Monitoring, pk=self.kwargs['monitoring_pk'])\n\n    def get(self, *args, **kwargs):\n        response = self._get_csv_response()\n        self._write_rows(response, self.get_queryset())\n        return response\n\n    @staticmethod\n    def _get_base_model_field_names(queryset):\n        opts = queryset.model._meta\n        return [field.name for field in opts.fields if field.related_model is None]\n\n    def _get_csv_response(self):\n        csv_response = HttpResponse(content_type='text/csv')\n        current_time = timezone.now()\n        filename = 'email_log_{0}-{1}-{2}.csv'.format(self.monitoring.id,\n                                                      current_time.strftime('%Y_%m_%d-%H_%M_%S'),\n                                                      current_time.tzname()\n                                                      )\n        csv_response['Content-Disposition'] = \"attachment;filename={0}\".format(filename)\n        return csv_response\n\n    def _write_rows(self, response, queryset):\n        writer = csv.writer(response)\n\n        # automatically add all fields from base table/model\n        base_field_names = self._get_base_model_field_names(queryset)\n\n        # print header row\n        writer.writerow(base_field_names +\n                        [\n                            'case id',\n                            'case email',\n                            'institution',\n                            'institution id',\n                            'monitoring id']\n                        )\n\n        for obj in queryset:\n            writer.writerow(\n                [getattr(obj, field) for field in base_field_names] + [\n                obj.case.id,\n                obj.case.email,\n                obj.case.institution.name,\n                obj.case.institution_id,\n                obj.case.monitoring_id,\n            ])\n\nclass EmailLogCaseListView(ListMonitoringMixin, ListView):\n    template_name_suffix = '_list_for_case'\n\n    @cached_property\n    def case(self):\n        return get_object_or_404(Case.objects.select_related('monitoring'),\n                                 pk=self.kwargs['case_pk'])\n\n    @cached_property\n    def monitoring(self):\n        return self.case.monitoring\n\n    def get_context_data(self, **kwargs):\n        kwargs['case'] = self.case\n        return super(EmailLogCaseListView, self).get_context_data(**kwargs)\n\n    def get_queryset(self):\n        return super(ListMonitoringMixin, self).get_queryset().filter(case=self.case)\n\n\nclass EmailLogDetailView(AttrPermissionRequiredMixin, PrefetchRelatedMixin,\n                         SelectRelatedMixin, DetailView):\n    model = EmailLog\n    select_related = ['case__monitoring']\n    prefetch_related = ['logrecord_set']\n    permission_attribute = 'case__monitoring'\n    permission_required = 'monitorings.view_log'\n", "path": "feder/letters/logs/views.py"}, {"content": "# -*- coding: utf-8 -*-\nfrom __future__ import unicode_literals\n\nfrom django.conf.urls import url\nfrom django.utils.translation import ugettext_lazy as _\n\nfrom . import views\n\nurlpatterns = [\n    url(_(r'^case-(?P<case_pk>[\\d-]+)$'), views.EmailLogCaseListView.as_view(),\n        name=\"list\"),\n    url(_(r'^monitoring-(?P<monitoring_pk>[\\d-]+)$'), views.EmailLogMonitoringListView.as_view(),\n        name=\"list\"),\n    url(_(r'^monitoring-(?P<monitoring_pk>[\\d-]+)/export$'), views.EmailLogMonitoringCsvView.as_view(),\n        name=\"export\"),\n    url(_(r'^log-(?P<pk>[\\d-]+)$'), views.EmailLogDetailView.as_view(),\n        name=\"detail\"),\n]\n", "path": "feder/letters/logs/urls.py"}]}