Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#521 Add Export to CSV for Queue Details and Enhance Admin Configuration #588

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
44 changes: 42 additions & 2 deletions src/officehours_api/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,57 @@
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.models import User
from safedelete.admin import SafeDeleteAdmin, highlight_deleted

from django.http import HttpResponse
import csv
from officehours_api.models import Queue
from officehours_api.models import Queue, Meeting, Attendee, Profile

@admin.action(description="Export selected Queues with details to CSV")
def export_selected_queues_with_details(modeladmin, request, queryset):

response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="selected_queues_with_details.csv"'

writer = csv.writer(response)
writer.writerow([
'Queue ID',
'Queue Name',
'Status',
'Created At',
'Hosts',
'Allowed Backends',
'Description',
'Meeting Count',
'Attendee Count'
])

for queue in queryset.prefetch_related('hosts', 'meeting_set__attendees'):
hosts = ', '.join([host.username for host in queue.hosts.all()])
allowed_backends = ', '.join(queue.allowed_backends)
meetings = queue.meeting_set.all()
meeting_count = meetings.count()
attendee_count = sum(meeting.attendees.count() for meeting in meetings)
writer.writerow([
queue.id,
queue.name,
queue.status,
queue.created_at,
hosts,
allowed_backends,
queue.description,
meeting_count,
attendee_count
])
return response

@admin.register(Queue)
class QueueAdmin(SafeDeleteAdmin):
list_display = ('id', highlight_deleted, 'created_at', 'status') + SafeDeleteAdmin.list_display
list_filter = ('hosts', 'status',) + SafeDeleteAdmin.list_filter
search_fields = ['id', 'name']
actions = SafeDeleteAdmin.actions + (export_selected_queues_with_details,)


class AttendeeInline(admin.TabularInline):
model = Attendee
extra = 1
Expand Down