Add filter to course allocation
This commit is contained in:
parent
3b743bff49
commit
a6da0a2f26
@ -1,5 +1,6 @@
|
||||
from django.db.models import Q
|
||||
import django_filters
|
||||
from .models import Program
|
||||
from .models import Program, CourseAllocation, Course
|
||||
|
||||
|
||||
class ProgramFilter(django_filters.FilterSet):
|
||||
@ -16,3 +17,32 @@ class ProgramFilter(django_filters.FilterSet):
|
||||
self.filters["title"].field.widget.attrs.update(
|
||||
{"class": "au-input", "placeholder": "Program name"}
|
||||
)
|
||||
|
||||
|
||||
class CourseAllocationFilter(django_filters.FilterSet):
|
||||
lecturer = django_filters.CharFilter(method="filter_by_lecturer", label="")
|
||||
course = django_filters.filters.CharFilter(method="filter_by_course", label="")
|
||||
|
||||
class Meta:
|
||||
model = CourseAllocation
|
||||
fields = []
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
# Change html classes and placeholders
|
||||
self.filters["lecturer"].field.widget.attrs.update(
|
||||
{"class": "au-input", "placeholder": "Lecturer"}
|
||||
)
|
||||
self.filters["course"].field.widget.attrs.update(
|
||||
{"class": "au-input", "placeholder": "Course"}
|
||||
)
|
||||
|
||||
def filter_by_lecturer(self, queryset, name, value):
|
||||
return queryset.filter(
|
||||
Q(lecturer__first_name__icontains=value)
|
||||
| Q(lecturer__last_name__icontains=value)
|
||||
)
|
||||
|
||||
def filter_by_course(self, queryset, name, value):
|
||||
return queryset.filter(courses__title__icontains=value)
|
||||
|
||||
@ -18,7 +18,11 @@ urlpatterns = [
|
||||
path(
|
||||
"course/assign/", CourseAllocationFormView.as_view(), name="course_allocation"
|
||||
),
|
||||
path("course/allocated/", course_allocation_view, name="course_allocation_view"),
|
||||
path(
|
||||
"course/allocated/",
|
||||
CourseAllocationFilterView.as_view(),
|
||||
name="course_allocation_view",
|
||||
),
|
||||
path(
|
||||
"allocated_course/<int:pk>/edit/",
|
||||
edit_allocated_course,
|
||||
|
||||
@ -21,7 +21,7 @@ from .forms import (
|
||||
UploadFormFile,
|
||||
UploadFormVideo,
|
||||
)
|
||||
from .filters import ProgramFilter
|
||||
from .filters import ProgramFilter, CourseAllocationFilter
|
||||
from .models import Program, Course, CourseAllocation, Upload, UploadVideo
|
||||
|
||||
|
||||
@ -260,17 +260,15 @@ class CourseAllocationFormView(CreateView):
|
||||
return context
|
||||
|
||||
|
||||
@login_required
|
||||
def course_allocation_view(request):
|
||||
allocated_courses = CourseAllocation.objects.all()
|
||||
return render(
|
||||
request,
|
||||
"course/course_allocation_view.html",
|
||||
{
|
||||
"title": "Course Allocations",
|
||||
"allocated_courses": allocated_courses,
|
||||
},
|
||||
)
|
||||
@method_decorator([login_required], name="dispatch")
|
||||
class CourseAllocationFilterView(FilterView):
|
||||
filterset_class = CourseAllocationFilter
|
||||
template_name = "course/course_allocation_view.html"
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
context["title"] = "Course Allocations"
|
||||
return context
|
||||
|
||||
|
||||
@login_required
|
||||
|
||||
@ -27,11 +27,10 @@
|
||||
{% endif %}
|
||||
|
||||
<div class="content-center">
|
||||
<form class="search-form" action="" method="POST">{% csrf_token %}
|
||||
<input class="au-input" type="text" name="lecturer" placeholder="Lecturer" value="{{ request.GET.lecturer }}"/>
|
||||
<input class="au-input" type="text" name="course" placeholder="Course" value="{{ request.GET.course }}"/>
|
||||
<form class="search-form" action="" method="GET">
|
||||
{{ filter.form }}
|
||||
<button class="btn btn-light" type="submit">
|
||||
<i class="fas fa-search"></i> filter
|
||||
<i class="fas fa-search"></i> Filter
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
@ -48,7 +47,7 @@
|
||||
{% endif %}
|
||||
</tr>
|
||||
</thead>
|
||||
{% for course in allocated_courses %}
|
||||
{% for course in filter.qs %}
|
||||
<tbody>
|
||||
<tr>
|
||||
<td> {{ forloop.counter }}.</td>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user