Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion certification_system/certificate/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
from utils.email import send_bulk_email
import os
from utils.images import save_temporary_image

from django.http import HttpResponse
from django.shortcuts import get_object_or_404

# Create your views here.
from rest_framework import viewsets
Expand Down Expand Up @@ -303,3 +304,51 @@ def post(self, request, format=None):
},
status=status.HTTP_201_CREATED,
)

class CertificateCSVExportView(APIView):
def get(self, request, event_id = None, category_id = None):
certificates = Certificate.objects.all()
filename="all"
if event_id:
event = get_object_or_404(Event, id=event_id)
certificates = certificates.filter(event__id=event_id)
filename = event.name
if category_id:
category = get_object_or_404(Category, id=category_id, event__id=event_id)
certificates = certificates.filter(category__id=category_id)
filename = f'{category.name}_{event.name}'
response = HttpResponse(content_type="text/csv")
response["Content-Disposition"] = f"attachment; filename={filename}.csv"
writer = csv.writer(response)
writer.writerow(
[
"Certificate ID",
"Name",
"Email",
"Email Sent"
"Event",
"Event ID",
"Category",
"Category ID",
"Image URL",
"Created At",
"Updated At"
]
)
for certificate in certificates:
writer.writerow(
[
certificate.id,
certificate.name,
certificate.email,
certificate.email_sent,
certificate.event.name,
certificate.event.id,
certificate.category.name,
certificate.category.id,
certificate.image.url,
certificate.created_at,
certificate.updated_at,
]
)
return response
4 changes: 4 additions & 0 deletions certification_system/certification_system/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
TestTemplate,
DetailFromImageId,
RegenerateBulkCertficate,
CertificateCSVExportView,
)

from django.conf.urls.static import static
Expand Down Expand Up @@ -57,6 +58,9 @@
DetailFromImageId.as_view(),
name="detail-from-image",
),
path("export/", CertificateCSVExportView.as_view(), name="export"),
path("export/<int:event_id>/", CertificateCSVExportView.as_view(), name="export-event"),
path("export/<int:event_id>/<int:category_id>/", CertificateCSVExportView.as_view(), name="export-category")
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
# urlpatterns = [
# path('admin/', admin.site.urls),
Expand Down