adding the ability to download user profile in pdf
This commit is contained in:
parent
13186e96f3
commit
2ec865a285
@ -20,6 +20,7 @@ from .filters import LecturerFilter, StudentFilter
|
|||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
from django.template.loader import get_template # to get template which render as pdf
|
from django.template.loader import get_template # to get template which render as pdf
|
||||||
from xhtml2pdf import pisa
|
from xhtml2pdf import pisa
|
||||||
|
from django.template.loader import render_to_string #to render a template into a string
|
||||||
|
|
||||||
def validate_username(request):
|
def validate_username(request):
|
||||||
username = request.GET.get("username", None)
|
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
|
@login_required
|
||||||
@admin_required
|
@admin_required
|
||||||
@ -109,6 +126,49 @@ def profile_single(request, id):
|
|||||||
).first()
|
).first()
|
||||||
|
|
||||||
user = User.objects.get(pk=id)
|
user = User.objects.get(pk=id)
|
||||||
|
"""
|
||||||
|
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:
|
||||||
if user.is_lecturer:
|
if user.is_lecturer:
|
||||||
courses = Course.objects.filter(allocated_course__lecturer__pk=id).filter(
|
courses = Course.objects.filter(allocated_course__lecturer__pk=id).filter(
|
||||||
semester=current_semester
|
semester=current_semester
|
||||||
|
|||||||
@ -56,6 +56,7 @@
|
|||||||
</button>
|
</button>
|
||||||
<ul class="dropdown-menu position-fixed">
|
<ul class="dropdown-menu position-fixed">
|
||||||
<li><a class="dropdown-item" href="{% url 'staff_edit' pk=lecturer.pk %}"><i class="fas fa-edit"></i> Update</a></li>
|
<li><a class="dropdown-item" href="{% url 'staff_edit' pk=lecturer.pk %}"><i class="fas fa-edit"></i> Update</a></li>
|
||||||
|
<li><a class="dropdown-item" target="_blank" href="{% url 'profile_single' lecturer.id %}?download_pdf=1"><i class="fas fa-download"></i> Download PDF</a></li>
|
||||||
<li><a class="dropdown-item text-danger" href="{% url 'lecturer_delete' pk=lecturer.pk %}"><i class="fas fa-trash-alt"></i> Delete</a></li>
|
<li><a class="dropdown-item text-danger" href="{% url 'lecturer_delete' pk=lecturer.pk %}"><i class="fas fa-trash-alt"></i> Delete</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@ -16,7 +16,7 @@
|
|||||||
{% if request.user.is_superuser %}
|
{% if request.user.is_superuser %}
|
||||||
<div class="manage-wrap">
|
<div class="manage-wrap">
|
||||||
<a class="btn btn-sm btn-primary" href="{% url 'add_student' %}"><i class="fas fa-plus"></i> Add Student</a>
|
<a class="btn btn-sm btn-primary" href="{% url 'add_student' %}"><i class="fas fa-plus"></i> Add Student</a>
|
||||||
<a class="btn btn-sm btn-primary" href="{% url 'student_list_pdf' %}"><i class="fas fa-download"></i> Download pdf</a> <!--new-->
|
<a class="btn btn-sm btn-primary" target="_blank" href="{% url 'student_list_pdf' %}"><i class="fas fa-download"></i> Download pdf</a> <!--new-->
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
@ -58,6 +58,7 @@
|
|||||||
</button>
|
</button>
|
||||||
<ul class="dropdown-menu position-fixed">
|
<ul class="dropdown-menu position-fixed">
|
||||||
<li><a class="dropdown-item" href="{% url 'student_edit' student.student.pk %}"><i class="fas fa-edit"></i> Update</a></li>
|
<li><a class="dropdown-item" href="{% url 'student_edit' student.student.pk %}"><i class="fas fa-edit"></i> Update</a></li>
|
||||||
|
<li><a class="dropdown-item" target="_blank" href="{% url 'profile_single' student.student.id %}?download_pdf=1"><i class="fas fa-download"></i> Download PDF</a></li>
|
||||||
<li><a class="dropdown-item text-danger" href="{% url 'student_delete' student.pk %}"><i class="fas fa-trash-alt"></i> Delete</a></li>
|
<li><a class="dropdown-item text-danger" href="{% url 'student_delete' student.pk %}"><i class="fas fa-trash-alt"></i> Delete</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
102
templates/pdf/profile_single.html
Normal file
102
templates/pdf/profile_single.html
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
{% block content %}
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.user-picture {
|
||||||
|
width: 100px;
|
||||||
|
height: 100px;
|
||||||
|
border: 3px solid #fff;
|
||||||
|
margin-top: -50px;
|
||||||
|
object-fit: cover;
|
||||||
|
}
|
||||||
|
|
||||||
|
table .info{
|
||||||
|
margin-left: -240px;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Specific to the .dashboard-description class */
|
||||||
|
.dashboard-description strong {
|
||||||
|
font-weight: 600;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Adjustments for headers within cards */
|
||||||
|
.card .h5 {
|
||||||
|
font-size: 1.25rem;
|
||||||
|
color: #333;
|
||||||
|
margin-top: 15px;
|
||||||
|
margin-bottom: 15px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|
||||||
|
{% if user.is_authenticated %}
|
||||||
|
<div class="row">
|
||||||
|
<div class="card-header">
|
||||||
|
<table>
|
||||||
|
<tr>
|
||||||
|
<td>
|
||||||
|
<img src="{{ user.picture.path }}" class="user-picture">
|
||||||
|
</td>
|
||||||
|
<td class="info">
|
||||||
|
<p>{{ user.get_full_name|title }}</p>
|
||||||
|
<p><strong>Last login:</strong> {{ user.last_login|date }}</p>
|
||||||
|
<p><strong>Role:</strong> {{ user.get_user_role }}</p>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
<hr>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="card">
|
||||||
|
<div class="card-body">
|
||||||
|
{% if user.is_lecturer %}
|
||||||
|
<p class="h5">My Courses</p>
|
||||||
|
{% if courses %}
|
||||||
|
<ul class="list-group">
|
||||||
|
{% for course in courses %}
|
||||||
|
<li class="list-group-item">{{ course }}</li>
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
|
{% else %}
|
||||||
|
<div class="text-danger">No courses assigned!</div>
|
||||||
|
{% endif %}
|
||||||
|
<hr class="my-0">
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
<p class="h5">Personal Info</p>
|
||||||
|
<div class="dashboard-description">
|
||||||
|
<p><strong>First Name:</strong> {{ user.first_name|title }}</p>
|
||||||
|
<p><strong>Last Name:</strong> {{ user.last_name|title }}</p>
|
||||||
|
<p><strong>ID No.:</strong> {{ user.username }}</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{% if user.is_student %}
|
||||||
|
<hr>
|
||||||
|
<p class="h5">Applicant Info</p>
|
||||||
|
<div class="dashboard-description">
|
||||||
|
<p><strong>School:</strong> Hawas Preparatory School</p>
|
||||||
|
<p><strong>Level:</strong> {{ level.level }}</p>
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
<hr>
|
||||||
|
<p class="h5">Contact Info</p>
|
||||||
|
<div class="dashboard-description">
|
||||||
|
<p><strong>Email:</strong> {{ user.email }}</p>
|
||||||
|
<p><strong>Tel No.:</strong> {{ user.phone }}</p>
|
||||||
|
<p><strong>Address/city:</strong> {{ user.address }}</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<hr>
|
||||||
|
<p class="h5">Important Dates</p>
|
||||||
|
<div class="dashboard-description">
|
||||||
|
<p><strong>Last login:</strong> {{ user.last_login }}</p>
|
||||||
|
{% if current_semester and current_session %}
|
||||||
|
<p><strong>Academic Year:</strong> {{ current_semester }} Semester {{ current_session }}</p>
|
||||||
|
{% endif %}
|
||||||
|
<p><strong>Registered Date:</strong> {{ user.date_joined|date }}</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
{% endblock content %}
|
||||||
Loading…
x
Reference in New Issue
Block a user