Skip to content

Commit

Permalink
Add ability to export members as CSV file
Browse files Browse the repository at this point in the history
  • Loading branch information
txels committed Apr 18, 2024
1 parent dda03f1 commit 4268caf
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
1 change: 1 addition & 0 deletions shipanaro/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

urlpatterns = [
url(r"^$", views.index, name="index"),
url(r"^afiliats.csv", views.csv_export, name="csv"),
url(r"^accounts/", include("shipanaro.auth.urls")),
url(r"^__capitania__/", admin.site.urls),
url(r"^sso/", include("shipanaro.sso.urls")),
Expand Down
46 changes: 39 additions & 7 deletions shipanaro/views.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
from django.contrib.auth.decorators import login_required
import csv

from django.contrib.auth.decorators import login_required, user_passes_test
from django.conf import settings
from django.http import HttpResponse
from django.utils.translation import gettext as _
from django.views.generic import TemplateView

from shipanaro.models import Membership


class IndexView(TemplateView):
template_name = 'shipanaro/index.html'
template_name = "shipanaro/index.html"
extra_context = None

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
'title': _('Home'),
})
context.update(
{
"title": _("Home"),
}
)
context.update(self.extra_context or {})
return context

Expand All @@ -24,9 +31,34 @@ def index(request, extra_context=None):

def view_defaults(extra_context=None, **kwargs):
extra_context = extra_context or {}
extra_context['site_title'] = _(settings.SHIPANARO_SITE_NAME)
extra_context["site_title"] = _(settings.SHIPANARO_SITE_NAME)
context = {
'extra_context': extra_context,
"extra_context": extra_context,
}
context.update(kwargs)
return context


@user_passes_test(lambda user: user.is_staff)
def csv_export(request):
# Create the HttpResponse object with the appropriate CSV header.
response = HttpResponse(
content_type="text/csv",
headers={"Content-Disposition": 'attachment; filename="afiliats.csv"'},
)

writer = csv.writer(response)
writer.writerow(["First Name", "Last Name", "Email Address", "ID", "Active"])

for member in Membership.objects.filter(drop_out=False):
writer.writerow(
[
member.user.first_name,
member.user.last_name,
member.user.email,
member.nid,
str(not member.drop_out).lower(),
]
)

return response

0 comments on commit 4268caf

Please sign in to comment.