Merge pull request #15 from benaissazaki/add-remaining-filters
Add filters for Course Allocation and Program
This commit is contained in:
commit
28b5501917
48
course/filters.py
Normal file
48
course/filters.py
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
from django.db.models import Q
|
||||||
|
import django_filters
|
||||||
|
from .models import Program, CourseAllocation, Course
|
||||||
|
|
||||||
|
|
||||||
|
class ProgramFilter(django_filters.FilterSet):
|
||||||
|
title = django_filters.CharFilter(lookup_expr="icontains", label="")
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = Program
|
||||||
|
fields = ["title"]
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
# Change html classes and placeholders
|
||||||
|
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)
|
||||||
@ -4,7 +4,7 @@ from .views import *
|
|||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
# Program urls
|
# Program urls
|
||||||
path("", program_view, name="programs"),
|
path("", ProgramFilterView.as_view(), name="programs"),
|
||||||
path("<int:pk>/detail/", program_detail, name="program_detail"),
|
path("<int:pk>/detail/", program_detail, name="program_detail"),
|
||||||
path("add/", program_add, name="add_program"),
|
path("add/", program_add, name="add_program"),
|
||||||
path("<int:pk>/edit/", program_edit, name="edit_program"),
|
path("<int:pk>/edit/", program_edit, name="edit_program"),
|
||||||
@ -18,7 +18,11 @@ urlpatterns = [
|
|||||||
path(
|
path(
|
||||||
"course/assign/", CourseAllocationFormView.as_view(), name="course_allocation"
|
"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(
|
path(
|
||||||
"allocated_course/<int:pk>/edit/",
|
"allocated_course/<int:pk>/edit/",
|
||||||
edit_allocated_course,
|
edit_allocated_course,
|
||||||
|
|||||||
@ -7,6 +7,7 @@ from django.core.paginator import Paginator
|
|||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.utils.decorators import method_decorator
|
from django.utils.decorators import method_decorator
|
||||||
from django.views.generic import ListView
|
from django.views.generic import ListView
|
||||||
|
from django_filters.views import FilterView
|
||||||
|
|
||||||
from accounts.models import User, Student
|
from accounts.models import User, Student
|
||||||
from core.models import Session, Semester
|
from core.models import Session, Semester
|
||||||
@ -20,28 +21,19 @@ from .forms import (
|
|||||||
UploadFormFile,
|
UploadFormFile,
|
||||||
UploadFormVideo,
|
UploadFormVideo,
|
||||||
)
|
)
|
||||||
|
from .filters import ProgramFilter, CourseAllocationFilter
|
||||||
from .models import Program, Course, CourseAllocation, Upload, UploadVideo
|
from .models import Program, Course, CourseAllocation, Upload, UploadVideo
|
||||||
|
|
||||||
|
|
||||||
# ########################################################
|
@method_decorator([login_required], name="dispatch")
|
||||||
# Program views
|
class ProgramFilterView(FilterView):
|
||||||
# ########################################################
|
filterset_class = ProgramFilter
|
||||||
@login_required
|
template_name = "course/program_list.html"
|
||||||
def program_view(request):
|
|
||||||
programs = Program.objects.all()
|
|
||||||
|
|
||||||
program_filter = request.GET.get("program_filter")
|
def get_context_data(self, **kwargs):
|
||||||
if program_filter:
|
context = super().get_context_data(**kwargs)
|
||||||
programs = Program.objects.filter(title__icontains=program_filter)
|
context["title"] = "Programs"
|
||||||
|
return context
|
||||||
return render(
|
|
||||||
request,
|
|
||||||
"course/program_list.html",
|
|
||||||
{
|
|
||||||
"title": "Programs",
|
|
||||||
"programs": programs,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
@ -268,17 +260,15 @@ class CourseAllocationFormView(CreateView):
|
|||||||
return context
|
return context
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
@method_decorator([login_required], name="dispatch")
|
||||||
def course_allocation_view(request):
|
class CourseAllocationFilterView(FilterView):
|
||||||
allocated_courses = CourseAllocation.objects.all()
|
filterset_class = CourseAllocationFilter
|
||||||
return render(
|
template_name = "course/course_allocation_view.html"
|
||||||
request,
|
|
||||||
"course/course_allocation_view.html",
|
def get_context_data(self, **kwargs):
|
||||||
{
|
context = super().get_context_data(**kwargs)
|
||||||
"title": "Course Allocations",
|
context["title"] = "Course Allocations"
|
||||||
"allocated_courses": allocated_courses,
|
return context
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
|
|||||||
@ -27,11 +27,10 @@
|
|||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
<div class="content-center">
|
<div class="content-center">
|
||||||
<form class="search-form" action="" method="POST">{% csrf_token %}
|
<form class="search-form" action="" method="GET">
|
||||||
<input class="au-input" type="text" name="lecturer" placeholder="Lecturer" value="{{ request.GET.lecturer }}"/>
|
{{ filter.form }}
|
||||||
<input class="au-input" type="text" name="course" placeholder="Course" value="{{ request.GET.course }}"/>
|
|
||||||
<button class="btn btn-light" type="submit">
|
<button class="btn btn-light" type="submit">
|
||||||
<i class="fas fa-search"></i> filter
|
<i class="fas fa-search"></i> Filter
|
||||||
</button>
|
</button>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
@ -48,7 +47,7 @@
|
|||||||
{% endif %}
|
{% endif %}
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
{% for course in allocated_courses %}
|
{% for course in filter.qs %}
|
||||||
<tbody>
|
<tbody>
|
||||||
<tr>
|
<tr>
|
||||||
<td> {{ forloop.counter }}.</td>
|
<td> {{ forloop.counter }}.</td>
|
||||||
|
|||||||
@ -27,9 +27,8 @@
|
|||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
<div class="content-center">
|
<div class="content-center">
|
||||||
<form class="search-form mx-auto" action="" method="GET">{% csrf_token %}
|
<form class="search-form mx-auto" action="" method="GET">
|
||||||
<input class="au-input" type="text" name="program_filter" placeholder="Program name"
|
{{ filter.form }}
|
||||||
value="{{ request.GET.program_filter }}" />
|
|
||||||
<button class="btn btn-light" type="submit">
|
<button class="btn btn-light" type="submit">
|
||||||
<i class="fas fa-search"></i> Filter
|
<i class="fas fa-search"></i> Filter
|
||||||
</button>
|
</button>
|
||||||
@ -72,7 +71,7 @@
|
|||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
{% for program in programs %}
|
{% for program in filter.qs %}
|
||||||
<tr>
|
<tr>
|
||||||
<td>{{ forloop.counter }}.</td>
|
<td>{{ forloop.counter }}.</td>
|
||||||
<td><a class="a-list" href="{{ program.get_absolute_url }}">
|
<td><a class="a-list" href="{{ program.get_absolute_url }}">
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user