Student program edit form and other minor updates
This commit is contained in:
parent
9d5ef21f5c
commit
9d132440f9
@ -324,14 +324,6 @@ class ProfileUpdateForm(UserChangeForm):
|
||||
label="Address / city",
|
||||
)
|
||||
|
||||
program = forms.ModelChoiceField(
|
||||
queryset=Program.objects.all(),
|
||||
widget=forms.Select(
|
||||
attrs={"class": "browser-default custom-select form-control"}
|
||||
),
|
||||
label="Program",
|
||||
)
|
||||
|
||||
class Meta:
|
||||
model = User
|
||||
fields = [
|
||||
@ -356,9 +348,8 @@ class ProgramUpdateForm(UserChangeForm):
|
||||
|
||||
class Meta:
|
||||
model = Student
|
||||
fields = [
|
||||
"program"
|
||||
]
|
||||
fields = ["program"]
|
||||
|
||||
|
||||
class EmailValidationOnForgotPassword(PasswordResetForm):
|
||||
def clean_email(self):
|
||||
|
||||
@ -22,12 +22,12 @@ from .views import (
|
||||
student_add_view,
|
||||
edit_student,
|
||||
delete_student,
|
||||
edit_program,
|
||||
edit_student_program,
|
||||
ParentAdd,
|
||||
validate_username,
|
||||
register,
|
||||
render_lecturer_pdf_list, #new
|
||||
render_student_pdf_list #new
|
||||
render_lecturer_pdf_list, # new
|
||||
render_student_pdf_list, # new
|
||||
)
|
||||
|
||||
# from .forms import EmailValidationOnForgotPassword
|
||||
@ -48,13 +48,21 @@ urlpatterns = [
|
||||
path("student/add/", student_add_view, name="add_student"),
|
||||
path("student/<int:pk>/edit/", edit_student, name="student_edit"),
|
||||
path("students/<int:pk>/delete/", delete_student, name="student_delete"),
|
||||
path("edit_program/<int:pk>/", edit_program, name="student_program_edit"),
|
||||
path(
|
||||
"edit_student_program/<int:pk>/",
|
||||
edit_student_program,
|
||||
name="student_program_edit",
|
||||
),
|
||||
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
|
||||
# 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/<int:pk>/', course_delete, name='delete_course'),
|
||||
# Setting urls
|
||||
|
||||
@ -12,15 +12,24 @@ from core.models import Session, Semester
|
||||
from course.models import Course
|
||||
from result.models import TakenCourse
|
||||
from .decorators import admin_required
|
||||
from .forms import StaffAddForm, StudentAddForm, ProfileUpdateForm, ParentAddForm, ProgramUpdateForm
|
||||
from .forms import (
|
||||
StaffAddForm,
|
||||
StudentAddForm,
|
||||
ProfileUpdateForm,
|
||||
ParentAddForm,
|
||||
ProgramUpdateForm,
|
||||
)
|
||||
from .models import User, Student, Parent
|
||||
from .filters import LecturerFilter, StudentFilter
|
||||
|
||||
#to generate pdf from template we need the following
|
||||
# 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 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
|
||||
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)
|
||||
@ -96,19 +105,17 @@ def profile(request):
|
||||
},
|
||||
)
|
||||
|
||||
#function that generate pdf by taking Django template and its context,
|
||||
|
||||
# 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
|
||||
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
|
||||
)
|
||||
pdf = pisa.CreatePDF(template, dest=response)
|
||||
if pdf.err:
|
||||
return HttpResponse('We had some problems generating the PDF')
|
||||
return HttpResponse("We had some problems generating the PDF")
|
||||
|
||||
return response
|
||||
|
||||
@ -131,7 +138,7 @@ def profile_single(request, id):
|
||||
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 request.GET.get("download_pdf"):
|
||||
if user.is_lecturer:
|
||||
courses = Course.objects.filter(allocated_course__lecturer__pk=id).filter(
|
||||
semester=current_semester
|
||||
@ -339,26 +346,26 @@ class LecturerFilterView(FilterView):
|
||||
return context
|
||||
|
||||
|
||||
#lecturers list pdf
|
||||
# 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"'
|
||||
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)
|
||||
pisa_status = pisa.CreatePDF(html, dest=response)
|
||||
# if error then show some funny view
|
||||
if pisa_status.err:
|
||||
return HttpResponse('We had some errors <pre>' + html + '</pre>')
|
||||
return HttpResponse("We had some errors <pre>" + html + "</pre>")
|
||||
return response
|
||||
|
||||
|
||||
|
||||
# @login_required
|
||||
# @lecturer_required
|
||||
# def delete_staff(request, pk):
|
||||
@ -420,10 +427,6 @@ def edit_student(request, pk):
|
||||
if form.is_valid():
|
||||
form.save()
|
||||
|
||||
student = Student.objects.get(student_id=instance.id)
|
||||
student.program = form.cleaned_data["program"]
|
||||
student.save()
|
||||
|
||||
messages.success(request, ("Student " + full_name + " has been updated."))
|
||||
return redirect("student_list")
|
||||
else:
|
||||
@ -453,22 +456,23 @@ class StudentListView(FilterView):
|
||||
return context
|
||||
|
||||
|
||||
#student list pdf
|
||||
# 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"'
|
||||
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)
|
||||
pisa_status = pisa.CreatePDF(html, dest=response)
|
||||
# if error then show some funny view
|
||||
if pisa_status.err:
|
||||
return HttpResponse('We had some errors <pre>' + html + '</pre>')
|
||||
return HttpResponse("We had some errors <pre>" + html + "</pre>")
|
||||
return response
|
||||
|
||||
|
||||
@ -481,10 +485,10 @@ def delete_student(request, pk):
|
||||
messages.success(request, "Student has been deleted.")
|
||||
return redirect("student_list")
|
||||
|
||||
# REDUNDANT
|
||||
|
||||
@login_required
|
||||
@admin_required
|
||||
def edit_program(request, pk):
|
||||
def edit_student_program(request, pk):
|
||||
|
||||
instance = get_object_or_404(Student, student_id=pk)
|
||||
user = get_object_or_404(User, pk=pk)
|
||||
@ -493,8 +497,10 @@ def edit_program(request, pk):
|
||||
full_name = user.get_full_name
|
||||
if form.is_valid():
|
||||
form.save()
|
||||
messages.success(request, message=full_name + " has been updated.")
|
||||
url = "/accounts/profile/" + user.id.__str__() + "/detail/" # Botched job, must optimize
|
||||
messages.success(request, message=full_name + " program has been updated.")
|
||||
url = (
|
||||
"/accounts/profile/" + user.id.__str__() + "/detail/"
|
||||
) # Botched job, must optimize
|
||||
return redirect(to=url)
|
||||
else:
|
||||
messages.error(request, "Please correct the error(s) below.")
|
||||
@ -502,11 +508,8 @@ def edit_program(request, pk):
|
||||
form = ProgramUpdateForm(instance=instance)
|
||||
return render(
|
||||
request,
|
||||
"accounts/edit_program.html",
|
||||
context={
|
||||
"title": "Edit-program",
|
||||
"form": form
|
||||
},
|
||||
"accounts/edit_student_program.html",
|
||||
context={"title": "Edit-program", "form": form, "student": instance},
|
||||
)
|
||||
|
||||
|
||||
|
||||
@ -30,7 +30,6 @@
|
||||
{{ form.gender|as_crispy_field }}
|
||||
{{ form.phone|as_crispy_field }}
|
||||
{{ form.address|as_crispy_field }}
|
||||
{{ form.program|as_crispy_field }}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@ -24,6 +24,8 @@
|
||||
<p class="form-title">Program</p>
|
||||
|
||||
<div class="card-body">
|
||||
<p>Change <a href="{{ student.student.get_absolute_url }}">{{ student.student.get_full_name }}'s</a> program</p>
|
||||
<hr>
|
||||
{{ form.program|as_crispy_field }}
|
||||
</div>
|
||||
</div>
|
||||
@ -261,15 +261,15 @@
|
||||
<h1 class="mb-5">🚀 Confirm your account</h1>
|
||||
<h4>Dear <b>{{ user.get_full_name }}</b>,</h4>
|
||||
<p>
|
||||
A new lecturer account with ID of <b>{user.username}</b> has been
|
||||
A new lecturer account with ID of <b>{{ user.username }}</b> has been
|
||||
created for you. <br />
|
||||
You're receiving this e-mail because the Dj-LMS admin has given your
|
||||
e-mail address to register an account on djlms.com. <br />
|
||||
</p>
|
||||
<h5>Login credentials for your DJ LMS account:</h5>
|
||||
<ul>
|
||||
<li>ID: {user.username}</li>
|
||||
<li>Your password: {password}</li>
|
||||
<li>ID: {{ user.username }}</li>
|
||||
<li>Your password: {{ password }}</li>
|
||||
</ul>
|
||||
<p>To secure your account be sure to change your password.</p>
|
||||
<p>
|
||||
|
||||
@ -32,9 +32,9 @@
|
||||
</ul>
|
||||
</div>
|
||||
<hr>
|
||||
<a class="mb-2" href="{% url 'edit_profile' %}"><i class="fas fa-user-edit"></i>
|
||||
<a class="mb-2" href="{% url 'edit_profile' %}"><i class="fas fa-user-edit unstyled"></i>
|
||||
<span class="mobile-hide">Edit Profile</span></a>
|
||||
<a href="{% url 'change_password' %}"><i class="fas fa-lock"></i><span class="mobile-hide">
|
||||
<a href="{% url 'change_password' %}"><i class="fas fa-lock unstyled"></i><span class="mobile-hide">
|
||||
Change password</span></a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@ -33,19 +33,18 @@
|
||||
</div>
|
||||
<hr>
|
||||
{% if request.user.is_superuser %}
|
||||
<div class="btn-flex">
|
||||
<div class="d-flex flex-column">
|
||||
{% if user.is_student %}
|
||||
<a class="edit-btn" href="{% url 'student_edit' pk=user.id %}">
|
||||
<i class="fas fa-user-edit"></i><span class="mobile-hide">Edit Profile</span>
|
||||
<a href="{% url 'student_edit' pk=user.id %}">
|
||||
<i class="fas fa-user-edit unstyled"></i><span class="mobile-hide">Edit Profile</span>
|
||||
</a>
|
||||
<a href="{% url 'student_program_edit' user.id %}">
|
||||
<i class="fas fa-pen-to-square unstyled"></i><span class="mobile-hide">Change Program</span>
|
||||
</a>
|
||||
{# had to name out of convention because it clashes with 'edit_[whatever]program', which directs to 'program/[id]/edit/ #}
|
||||
{# <a class="edit-btn" href="{% url 'student_program_edit' user.id %}">#}
|
||||
{# <i class="fas fa-user-edit"></i><span class="mobile-hide">Change Program</span>#}
|
||||
{# </a>#}
|
||||
{% endif %}
|
||||
{% if user.is_lecturer %}
|
||||
<a class="edit-btn" href="{% url 'staff_edit' pk=user.id %}">
|
||||
<i class="fas fa-user-edit"></i><span class="mobile-hide">Edit Profile</span>
|
||||
<a href="{% url 'staff_edit' pk=user.id %}">
|
||||
<i class="fas fa-user-edit unstyled"></i><span class="mobile-hide">Edit Profile</span>
|
||||
</a>
|
||||
{% endif %}
|
||||
</div>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user