From 13186e96f349f73d26963f580841935e307b5293 Mon Sep 17 00:00:00 2001 From: y938 Date: Tue, 2 Apr 2024 12:08:03 +0300 Subject: [PATCH] adding pdf download table feature for lecturer and student list --- accounts/urls.py | 5 ++ accounts/views.py | 43 ++++++++++++++++ requirements/base.txt | 1 + templates/accounts/lecturer_list.html | 1 + templates/accounts/student_list.html | 1 + templates/pdf/lecturer_list.html | 70 +++++++++++++++++++++++++++ templates/pdf/student_list.html | 68 ++++++++++++++++++++++++++ 7 files changed, 189 insertions(+) create mode 100644 templates/pdf/lecturer_list.html create mode 100644 templates/pdf/student_list.html diff --git a/accounts/urls.py b/accounts/urls.py index edfc697..fd0737d 100644 --- a/accounts/urls.py +++ b/accounts/urls.py @@ -25,6 +25,8 @@ from .views import ( ParentAdd, validate_username, register, + render_lecturer_pdf_list, #new + render_student_pdf_list #new ) # from .forms import EmailValidationOnForgotPassword @@ -48,6 +50,9 @@ urlpatterns = [ path("parents/add/", ParentAdd.as_view(), name="add_parent"), path("ajax/validate-username/", validate_username, name="validate_username"), path("register/", register, name="register"), + #paths to pdf + path("create_lecturers_pdf_list/", render_lecturer_pdf_list, name="lecturer_list_pdf"), #new + path("create_students_pdf_list/", render_student_pdf_list, name="student_list_pdf"), #new # path('add-student/', StudentAddView.as_view(), name='add_student'), # path('programs/course/delete//', course_delete, name='delete_course'), # Setting urls diff --git a/accounts/views.py b/accounts/views.py index 83f7501..557fcb2 100644 --- a/accounts/views.py +++ b/accounts/views.py @@ -16,6 +16,10 @@ from .forms import StaffAddForm, StudentAddForm, ProfileUpdateForm, ParentAddFor from .models import User, Student, Parent from .filters import LecturerFilter, StudentFilter +#to generate pdf from template we need the following +from django.http import HttpResponse +from django.template.loader import get_template # to get template which render as pdf +from xhtml2pdf import pisa def validate_username(request): username = request.GET.get("username", None) @@ -275,6 +279,26 @@ class LecturerFilterView(FilterView): return context +#lecturers list pdf +def render_lecturer_pdf_list(request): + lecturers = User.objects.filter(is_lecturer=True) + template_path = 'pdf/lecturer_list.html' + context = {'lecturers':lecturers} + response = HttpResponse(content_type='application/pdf') # convert the response to pdf + response['Content-Disposition'] = 'filename="lecturers_list.pdf"' + # find the template and render it. + template = get_template(template_path) + html = template.render(context) + # create a pdf + pisa_status = pisa.CreatePDF( + html, dest=response) + # if error then show some funny view + if pisa_status.err: + return HttpResponse('We had some errors
' + html + '
') + return response + + + # @login_required # @lecturer_required # def delete_staff(request, pk): @@ -365,6 +389,25 @@ class StudentListView(FilterView): return context +#student list pdf +def render_student_pdf_list(request): + students = Student.objects.all() + template_path = 'pdf/student_list.html' + context = {'students':students} + response = HttpResponse(content_type='application/pdf') # convert the response to pdf + response['Content-Disposition'] = 'filename="students_list.pdf"' + # find the template and render it. + template = get_template(template_path) + html = template.render(context) + # create a pdf + pisa_status = pisa.CreatePDF( + html, dest=response) + # if error then show some funny view + if pisa_status.err: + return HttpResponse('We had some errors
' + html + '
') + return response + + @login_required @admin_required def delete_student(request, pk): diff --git a/requirements/base.txt b/requirements/base.txt index ff15489..4aec2f0 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -16,6 +16,7 @@ django-cors-headers==3.13.0 # https://github.com/adamchainz/django-cors-headers # PDF generator reportlab==4.0.4 +xhtml2pdf==0.2.15 # Customize django admin django-jet-reboot==1.3.5 diff --git a/templates/accounts/lecturer_list.html b/templates/accounts/lecturer_list.html index bbd5d46..016803f 100644 --- a/templates/accounts/lecturer_list.html +++ b/templates/accounts/lecturer_list.html @@ -13,6 +13,7 @@ {% if request.user.is_superuser %} {% endif %} diff --git a/templates/accounts/student_list.html b/templates/accounts/student_list.html index 455720c..db3d947 100644 --- a/templates/accounts/student_list.html +++ b/templates/accounts/student_list.html @@ -16,6 +16,7 @@ {% if request.user.is_superuser %} {% endif %} diff --git a/templates/pdf/lecturer_list.html b/templates/pdf/lecturer_list.html new file mode 100644 index 0000000..fcd0709 --- /dev/null +++ b/templates/pdf/lecturer_list.html @@ -0,0 +1,70 @@ +{% block content %} + + +

Lecturers

+ +
+ + + + + + + + + + + + + {% for lecturer in lecturers %} + + + + + + + + + {% empty %} + + + + {% endfor %} + +
#ID No.Full NameEmailMob No.Address/City
{{ forloop.counter }}.{{ lecturer.username }}{{ lecturer.get_full_name }}{{ lecturer.email }}{{ lecturer.phone }}{{ lecturer.address }}
+ + No Lecturer(s). + +
+
+{% endblock content %} diff --git a/templates/pdf/student_list.html b/templates/pdf/student_list.html new file mode 100644 index 0000000..8539513 --- /dev/null +++ b/templates/pdf/student_list.html @@ -0,0 +1,68 @@ +{% block content %} + + +

Students

+ +
+ + + + + + + + + + + + {% for student in students %} + + + + + + + + {% empty %} + + + + {% endfor %} + +
ID No.Full NameEmailMob No.Program
{{ forloop.counter }}.{{ student.student.username }}{{ student.student.get_full_name }}{{ student.student.email }}{{ student.program }}
+ + No Lecturer(s). + +
+
+{% endblock content %}