infrastructure for buttons, actions and a csv download

This commit is contained in:
Rachid Mrad 2023-10-26 16:17:25 -04:00
parent 95e557b7b3
commit ee2b001ab5
No known key found for this signature in database
GPG key ID: EF38E4CEC4A8F3CF
2 changed files with 55 additions and 0 deletions

View file

@ -1,5 +1,7 @@
import csv
import logging import logging
from django import forms from django import forms
from django.http import HttpResponse
from django_fsm import get_available_FIELD_transitions from django_fsm import get_available_FIELD_transitions
from django.contrib import admin, messages from django.contrib import admin, messages
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
@ -747,7 +749,35 @@ class DomainAdmin(ListHeaderAdmin):
search_fields = ["name"] search_fields = ["name"]
search_help_text = "Search by domain name." search_help_text = "Search by domain name."
change_form_template = "django/admin/domain_change_form.html" change_form_template = "django/admin/domain_change_form.html"
change_list_template = "django/admin/domain_change_list.html"
readonly_fields = ["state"] readonly_fields = ["state"]
# actions = ['export_data']
# Define a custom view for data export
def export_static_data(self, request):
# Your data export logic here
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="data_export.csv"'
writer = csv.writer(response)
# Write your data to the CSV here
writer.writerow(['Name', 'State', ...]) # Include the appropriate headers
# Loop through and write your data rows
for data_row in Domain.objects.all():
writer.writerow([data_row.name, data_row.state, ...]) # Include the appropriate fields
return response
def get_urls(self):
from django.urls import path
urlpatterns = super().get_urls()
info = self.model._meta.app_label, self.model._meta.model_name
my_url = [
path('export-static-data/', self.export_static_data, name='%s_%s_export_static_data' % info),
]
return my_url + urlpatterns
def response_change(self, request, obj): def response_change(self, request, obj):
# Create dictionary of action functions # Create dictionary of action functions

View file

@ -0,0 +1,25 @@
{% extends "admin/change_list.html" %}
{% block object-tools %}
<ul class="object-tools">
{% if has_add_permission %}
<li>
<a href="{% url 'admin:registrar_domain_add' %}" class="addlink">
Add Domain
</a>
</li>
<li>
{% comment %} <button type="button" class="button" id="custom-button-1">Custom Button 1</button>
<input type="submit" value="Place hold" name="_place_client_hold"> {% endcomment %}
<a href="{% url 'admin:registrar_domain_export_static_data' %}" class="button">Export Data</a>
</li>
<li>
<button type="button" class="button" id="custom-button-2">Custom Button 2</button>
</li>
<li>
<button type="button" class="button" id="custom-button-3">Custom Button 3</button>
</li>
{% endif %}
</ul>
{% endblock %}