Added function to export to pdf
This commit is contained in:
parent
e5549cfc24
commit
c9f9af6103
3 changed files with 42 additions and 7 deletions
|
|
@ -1,9 +1,15 @@
|
|||
from django.contrib import admin
|
||||
from django.db.models import F, Value
|
||||
from django.db.models.functions import Concat
|
||||
from django.http import HttpResponse
|
||||
from django.http import HttpResponse, FileResponse
|
||||
|
||||
from reportlab.pdfgen import canvas
|
||||
from reportlab.lib.pagesizes import letter
|
||||
from reportlab.lib.units import cm
|
||||
from reportlab.platypus import Table, TableStyle, SimpleDocTemplate
|
||||
from reportlab.lib.colors import black
|
||||
import csv
|
||||
import io
|
||||
|
||||
from ..models import UserEntry
|
||||
|
||||
|
|
@ -41,7 +47,7 @@ class UserEntryAdmin(admin.ModelAdmin):
|
|||
list_filter = ['house', 'grade', MentorListFilter, 'activity1',
|
||||
'activity1__teacher', 'activity2', 'activity2__teacher']
|
||||
|
||||
actions = ['export_to_csv']
|
||||
actions = ['export_to_csv', 'export_to_pdf']
|
||||
|
||||
@admin.display(description='Activity 1 Teacher')
|
||||
def activity1_teacher(self, obj):
|
||||
|
|
@ -76,14 +82,39 @@ class UserEntryAdmin(admin.ModelAdmin):
|
|||
values = []
|
||||
for field in field_names:
|
||||
value = getattr(row, field)
|
||||
if callable(value):
|
||||
try:
|
||||
value = value() or ''
|
||||
except:
|
||||
value = 'Error retrieving value'
|
||||
if value is None:
|
||||
value = ''
|
||||
values.append(value)
|
||||
writer.writerow(values)
|
||||
|
||||
return response
|
||||
|
||||
@admin.action(description='Export selected to pdf')
|
||||
def export_to_pdf(self, request, queryset):
|
||||
buffer = io.BytesIO()
|
||||
doc = SimpleDocTemplate(buffer, pagesize=letter)
|
||||
|
||||
elements = []
|
||||
|
||||
field_names = [field.name for field in queryset.model._meta.fields]
|
||||
data = []
|
||||
data.append(field_names)
|
||||
for row in queryset:
|
||||
values = []
|
||||
for field in field_names:
|
||||
value = getattr(row, field)
|
||||
if value is None:
|
||||
value = ''
|
||||
values.append(value)
|
||||
data.append(values)
|
||||
|
||||
table = Table(data, repeatRows=1)
|
||||
table.setStyle(TableStyle(
|
||||
[('INNERGRID', (0, 0), (-1, -1), 0.25, black), ('BOX', (0, 0), (-1, -1), 0.25, black)]))
|
||||
elements.append(table)
|
||||
|
||||
doc.build(elements)
|
||||
|
||||
buffer.seek(0)
|
||||
|
||||
return FileResponse(buffer, as_attachment=True, filename='download.pdf')
|
||||
|
|
|
|||
|
|
@ -35,6 +35,8 @@ class UserEntryForm(Form):
|
|||
|
||||
def clean(self):
|
||||
email = self.cleaned_data['email']
|
||||
if ('@slps.org' not in email):
|
||||
raise ValidationError("Please use your school email")
|
||||
if UserEntry.objects.filter(email=email).exists():
|
||||
raise ValidationError("A signup with this email already exists")
|
||||
|
||||
|
|
|
|||
|
|
@ -5,8 +5,10 @@ django-formtools==2.3
|
|||
django-smart-selects==1.6.0
|
||||
gunicorn==20.1.0
|
||||
mysqlclient==2.1.1
|
||||
Pillow==9.2.0
|
||||
pycodestyle==2.8.0
|
||||
python-dotenv==0.20.0
|
||||
reportlab==3.6.11
|
||||
sqlparse==0.4.2
|
||||
toml==0.10.2
|
||||
tzdata==2022.1
|
||||
|
|
|
|||
Reference in a new issue