From 2ec865a2859845b77e81574a06ea407ff3df405f Mon Sep 17 00:00:00 2001 From: y938 Date: Wed, 3 Apr 2024 11:44:33 +0300 Subject: [PATCH] adding the ability to download user profile in pdf --- accounts/views.py | 132 +++++++++++++++++++------- templates/accounts/lecturer_list.html | 1 + templates/accounts/student_list.html | 3 +- templates/pdf/profile_single.html | 102 ++++++++++++++++++++ 4 files changed, 201 insertions(+), 37 deletions(-) create mode 100644 templates/pdf/profile_single.html diff --git a/accounts/views.py b/accounts/views.py index 557fcb2..47fbead 100644 --- a/accounts/views.py +++ b/accounts/views.py @@ -20,6 +20,7 @@ from .filters import LecturerFilter, StudentFilter from django.http import HttpResponse from django.template.loader import get_template # to get template which render as pdf from xhtml2pdf import pisa +from django.template.loader import render_to_string #to render a template into a string def validate_username(request): username = request.GET.get("username", None) @@ -95,6 +96,22 @@ def profile(request): }, ) +#function that generate pdf by taking Django template and its context, +def render_to_pdf(template_name, context): + """Renders a given template to PDF format.""" + response = HttpResponse(content_type='application/pdf') + response['Content-Disposition'] = 'filename="profile.pdf"' # Set default filename + + template = render_to_string(template_name, context) + pdf = pisa.CreatePDF( + template, + dest=response + ) + if pdf.err: + return HttpResponse('We had some problems generating the PDF') + + return response + @login_required @admin_required @@ -109,43 +126,86 @@ def profile_single(request, id): ).first() user = User.objects.get(pk=id) - if user.is_lecturer: - courses = Course.objects.filter(allocated_course__lecturer__pk=id).filter( - semester=current_semester - ) - context = { - "title": user.get_full_name, - "user": user, - "user_type": "Lecturer", - "courses": courses, - "current_session": current_session, - "current_semester": current_semester, - } - return render(request, "accounts/profile_single.html", context) - elif user.is_student: - student = Student.objects.get(student__pk=id) - courses = TakenCourse.objects.filter( - student__student__id=id, course__level=student.level - ) - context = { - "title": user.get_full_name, - "user": user, - "user_type": "student", - "courses": courses, - "student": student, - "current_session": current_session, - "current_semester": current_semester, - } - return render(request, "accounts/profile_single.html", context) + """ + If download_pdf exists, instead of calling render_to_pdf directly, + pass the context dictionary built for the specific user type + (lecturer, student, or superuser) to the render_to_pdf function. + """ + if request.GET.get('download_pdf'): + if user.is_lecturer: + courses = Course.objects.filter(allocated_course__lecturer__pk=id).filter( + semester=current_semester + ) + context = { + "title": user.get_full_name, + "user": user, + "user_type": "Lecturer", + "courses": courses, + "current_session": current_session, + "current_semester": current_semester, + } + elif user.is_student: + student = Student.objects.get(student__pk=id) + courses = TakenCourse.objects.filter( + student__student__id=id, course__level=student.level + ) + context = { + "title": user.get_full_name, + "user": user, + "user_type": "student", + "courses": courses, + "student": student, + "current_session": current_session, + "current_semester": current_semester, + } + else: + context = { + "title": user.get_full_name, + "user": user, + "user_type": "superuser", + "current_session": current_session, + "current_semester": current_semester, + } + return render_to_pdf("pdf/profile_single.html", context) + else: - context = { - "title": user.get_full_name, - "user": user, - "user_type": "superuser", - "current_session": current_session, - "current_semester": current_semester, - } - return render(request, "accounts/profile_single.html", context) + if user.is_lecturer: + courses = Course.objects.filter(allocated_course__lecturer__pk=id).filter( + semester=current_semester + ) + context = { + "title": user.get_full_name, + "user": user, + "user_type": "Lecturer", + "courses": courses, + "current_session": current_session, + "current_semester": current_semester, + } + return render(request, "accounts/profile_single.html", context) + elif user.is_student: + student = Student.objects.get(student__pk=id) + courses = TakenCourse.objects.filter( + student__student__id=id, course__level=student.level + ) + context = { + "title": user.get_full_name, + "user": user, + "user_type": "student", + "courses": courses, + "student": student, + "current_session": current_session, + "current_semester": current_semester, + } + return render(request, "accounts/profile_single.html", context) + else: + context = { + "title": user.get_full_name, + "user": user, + "user_type": "superuser", + "current_session": current_session, + "current_semester": current_semester, + } + return render(request, "accounts/profile_single.html", context) @login_required diff --git a/templates/accounts/lecturer_list.html b/templates/accounts/lecturer_list.html index 016803f..c4a73b2 100644 --- a/templates/accounts/lecturer_list.html +++ b/templates/accounts/lecturer_list.html @@ -56,6 +56,7 @@ diff --git a/templates/accounts/student_list.html b/templates/accounts/student_list.html index db3d947..c46dc7d 100644 --- a/templates/accounts/student_list.html +++ b/templates/accounts/student_list.html @@ -16,7 +16,7 @@ {% if request.user.is_superuser %}
Add Student - Download pdf + Download pdf
{% endif %} @@ -58,6 +58,7 @@ diff --git a/templates/pdf/profile_single.html b/templates/pdf/profile_single.html new file mode 100644 index 0000000..49f79ca --- /dev/null +++ b/templates/pdf/profile_single.html @@ -0,0 +1,102 @@ +{% block content %} + + + +{% if user.is_authenticated %} +
+
+ + + + + +
+ + +

{{ user.get_full_name|title }}

+

Last login: {{ user.last_login|date }}

+

Role: {{ user.get_user_role }}

+
+
+
+ +
+
+ {% if user.is_lecturer %} +

My Courses

+ {% if courses %} +
    + {% for course in courses %} +
  • {{ course }}
  • + {% endfor %} +
+ {% else %} +
No courses assigned!
+ {% endif %} +
+ {% endif %} + +

Personal Info

+
+

First Name: {{ user.first_name|title }}

+

Last Name: {{ user.last_name|title }}

+

ID No.: {{ user.username }}

+
+ + {% if user.is_student %} +
+

Applicant Info

+
+

School: Hawas Preparatory School

+

Level: {{ level.level }}

+
+ {% endif %} + +
+

Contact Info

+
+

Email: {{ user.email }}

+

Tel No.: {{ user.phone }}

+

Address/city: {{ user.address }}

+
+ +
+

Important Dates

+
+

Last login: {{ user.last_login }}

+ {% if current_semester and current_session %} +

Academic Year: {{ current_semester }} Semester {{ current_session }}

+ {% endif %} +

Registered Date: {{ user.date_joined|date }}

+
+
+
+
+{% endif %} + +{% endblock content %}