Skip to content

Commit

Permalink
Merge pull request #56 from arkavidia-hmif/bugfix-and-exportcsv
Browse files Browse the repository at this point in the history
Bug fixes, add export models as csv from admin panel
  • Loading branch information
hashshura authored Jan 24, 2021
2 parents 1ad0e2a + 11dc6ca commit 79800e8
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 11 deletions.
32 changes: 28 additions & 4 deletions arkav/mainevent/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from django.urls import path
from django.urls import reverse
from django.utils.html import format_html
from django.utils.translation import gettext as _
from arkav.eventcheckin.models import CheckInEvent
from arkav.mainevent.admin_forms import AcceptTaskResponseActionForm
from arkav.mainevent.admin_forms import RejectTaskResponseActionForm
Expand All @@ -22,6 +23,7 @@
from arkav.mainevent.models import TaskWidget
from arkav.mainevent.models import Registrant
from arkav.mainevent.services import RegistrantService
from arkav.utils.mixins import ExportCsvMixin
import django_rq


Expand Down Expand Up @@ -159,19 +161,41 @@ def process_task_response(self, request, task_response_id, action_form, action_t
return TemplateResponse(request, 'admin_task_response.html', context)


class HasCompletedActiveStageFilter(admin.SimpleListFilter):
title = _('active stage completion')

parameter_name = 'has_completed_active_stage'

def lookups(self, request, model_admin):
return (
('complete', _('Complete')),
('incomplete', _('Incomplete')),
)

def queryset(self, request, queryset):
includes = []
for registrant in queryset:
if registrant.has_completed_active_stage:
includes.append(registrant.pk)
if self.value() == 'complete':
return queryset.filter(pk__in=includes)
if self.value() == 'incomplete':
return queryset.exclude(pk__in=includes)


@admin.register(Registrant)
class RegistrantAdmin(admin.ModelAdmin):
class RegistrantAdmin(admin.ModelAdmin, ExportCsvMixin):
fieldsets = (
(
None, {"fields": ['user', 'mainevent', 'active_stage']}
),
)
actions = [send_reminder, 'send_custom_email', 'migrate_checkinevent', 'set_participating']
actions = [send_reminder, 'send_custom_email', 'migrate_checkinevent', 'set_participating', 'export_as_csv']
list_display = ['id', 'mainevent', 'user', 'active_stage',
'has_completed_active_stage', 'is_participating', 'created_at']
list_display_links = ['id', 'user']
list_filter = ['is_participating', 'mainevent', 'active_stage']
search_fields = ['user']
list_filter = ['is_participating', HasCompletedActiveStageFilter, 'mainevent', 'active_stage']
search_fields = ['user__full_name', 'user__email']
readonly_fields = ['full_name', 'current_education', 'institution', 'phone_number', 'address', 'birth_date']
inlines = [TaskResponseInline]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<p>Email akan dikirim ke:</p>
<ul>
{% for registrant in registrants %}
<li>{{ registrant.user.name }} ({{ registrant.user.email }})</li>
<li>{{ registrant.user.full_name }} ({{ registrant.user.email }})</li>
<input type="hidden" name="_selected_action" value="{{ registrant.pk }}" />
{% endfor %}
</ul>
Expand Down
32 changes: 28 additions & 4 deletions arkav/preevent/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.urls import path
from django.urls import reverse
from django.utils.html import format_html
from django.utils.translation import gettext as _
from arkav.eventcheckin.models import CheckInEvent
from arkav.preevent.admin_forms import AcceptTaskResponseActionForm
from arkav.preevent.admin_forms import RejectTaskResponseActionForm
Expand All @@ -20,6 +21,7 @@
from arkav.preevent.models import TaskWidget
from arkav.preevent.models import Registrant
from arkav.preevent.services import RegistrantService
from arkav.utils.mixins import ExportCsvMixin
import django_rq


Expand Down Expand Up @@ -151,19 +153,41 @@ def process_task_response(self, request, task_response_id, action_form, action_t
return TemplateResponse(request, 'admin_task_response.html', context)


class HasCompletedActiveStageFilter(admin.SimpleListFilter):
title = _('active stage completion')

parameter_name = 'has_completed_active_stage'

def lookups(self, request, model_admin):
return (
('complete', _('Complete')),
('incomplete', _('Incomplete')),
)

def queryset(self, request, queryset):
includes = []
for registrant in queryset:
if registrant.has_completed_active_stage:
includes.append(registrant.pk)
if self.value() == 'complete':
return queryset.filter(pk__in=includes)
if self.value() == 'incomplete':
return queryset.exclude(pk__in=includes)


@admin.register(Registrant)
class RegistrantAdmin(admin.ModelAdmin):
class RegistrantAdmin(admin.ModelAdmin, ExportCsvMixin):
fieldsets = (
(
None, {"fields": ['user', 'preevent', 'active_stage']}
),
)
actions = [send_reminder, 'send_custom_email', 'migrate_checkinevent']
actions = [send_reminder, 'send_custom_email', 'migrate_checkinevent', 'export_as_csv']
list_display = ['id', 'preevent', 'user', 'active_stage', 'has_completed_active_stage',
'is_participating', 'created_at']
list_display_links = ['id', 'user']
list_filter = ['is_participating', 'preevent', 'active_stage']
search_fields = ['user']
list_filter = ['is_participating', HasCompletedActiveStageFilter, 'preevent', 'active_stage']
search_fields = ['user__full_name', 'user__email']
readonly_fields = ['full_name', 'current_education', 'institution', 'phone_number', 'address', 'birth_date']
inlines = [TaskResponseInline]

Expand Down
2 changes: 1 addition & 1 deletion arkav/preevent/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def send_reminder_email(self, registrant):
need_to_notify.append(task)

context = {
'name': registrant.user.name,
'name': registrant.user.full_name,
'active_stage': registrant.active_stage,
'tasks': need_to_notify,
}
Expand Down
2 changes: 1 addition & 1 deletion arkav/preevent/templates/preevent_admin_custom_email.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<p>Email akan dikirim ke:</p>
<ul>
{% for registrant in registrants %}
<li>{{ registrant.user.name }} ({{ registrant.user.email }})</li>
<li>{{ registrant.user.full_name }} ({{ registrant.user.email }})</li>
<input type="hidden" name="_selected_action" value="{{ registrant.pk }}" />
{% endfor %}
</ul>
Expand Down
27 changes: 27 additions & 0 deletions arkav/utils/mixins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import csv
from operator import attrgetter
from django.http import HttpResponse

DEEP_SEPARATOR = '__'


class ExportCsvMixin:
def export_as_csv(self, request, queryset):

meta = self.model._meta
if hasattr(self, 'csv_fields'):
field_names = [x.replace(DEEP_SEPARATOR, '.') for x in self.csv_fields]
else:
field_names = self.list_display

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([attrgetter(field)(obj) for field in field_names])

return response

export_as_csv.short_description = 'Export selected as csv'

0 comments on commit 79800e8

Please sign in to comment.