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 = [
|
||||
# Program urls
|
||||
path("", program_view, name="programs"),
|
||||
path("", ProgramFilterView.as_view(), name="programs"),
|
||||
path("<int:pk>/detail/", program_detail, name="program_detail"),
|
||||
path("add/", program_add, name="add_program"),
|
||||
path("<int:pk>/edit/", program_edit, name="edit_program"),
|
||||
@ -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,
|
||||
|
||||
@ -7,6 +7,7 @@ from django.core.paginator import Paginator
|
||||
from django.conf import settings
|
||||
from django.utils.decorators import method_decorator
|
||||
from django.views.generic import ListView
|
||||
from django_filters.views import FilterView
|
||||
|
||||
from accounts.models import User, Student
|
||||
from core.models import Session, Semester
|
||||
@ -20,28 +21,19 @@ from .forms import (
|
||||
UploadFormFile,
|
||||
UploadFormVideo,
|
||||
)
|
||||
from .filters import ProgramFilter, CourseAllocationFilter
|
||||
from .models import Program, Course, CourseAllocation, Upload, UploadVideo
|
||||
|
||||
|
||||
# ########################################################
|
||||
# Program views
|
||||
# ########################################################
|
||||
@login_required
|
||||
def program_view(request):
|
||||
programs = Program.objects.all()
|
||||
@method_decorator([login_required], name="dispatch")
|
||||
class ProgramFilterView(FilterView):
|
||||
filterset_class = ProgramFilter
|
||||
template_name = "course/program_list.html"
|
||||
|
||||
program_filter = request.GET.get("program_filter")
|
||||
if program_filter:
|
||||
programs = Program.objects.filter(title__icontains=program_filter)
|
||||
|
||||
return render(
|
||||
request,
|
||||
"course/program_list.html",
|
||||
{
|
||||
"title": "Programs",
|
||||
"programs": programs,
|
||||
},
|
||||
)
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
context["title"] = "Programs"
|
||||
return context
|
||||
|
||||
|
||||
@login_required
|
||||
@ -268,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>
|
||||
|
||||
@ -27,9 +27,8 @@
|
||||
{% endif %}
|
||||
|
||||
<div class="content-center">
|
||||
<form class="search-form mx-auto" action="" method="GET">{% csrf_token %}
|
||||
<input class="au-input" type="text" name="program_filter" placeholder="Program name"
|
||||
value="{{ request.GET.program_filter }}" />
|
||||
<form class="search-form mx-auto" action="" method="GET">
|
||||
{{ filter.form }}
|
||||
<button class="btn btn-light" type="submit">
|
||||
<i class="fas fa-search"></i> Filter
|
||||
</button>
|
||||
@ -72,7 +71,7 @@
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for program in programs %}
|
||||
{% for program in filter.qs %}
|
||||
<tr>
|
||||
<td>{{ forloop.counter }}.</td>
|
||||
<td><a class="a-list" href="{{ program.get_absolute_url }}">
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user