Unverified Commit f2cdf1ea authored by Estelle Veisemburger's avatar Estelle Veisemburger Committed by Florent Chehab
Browse files

feat(filter): apply filter to previous exchanges search

parent 9fc1fdda
from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.permissions import BasePermission
from django_filters import rest_framework as filters
from backend_app.models.abstract.essentialModule import (
EssentialModule,
......@@ -78,6 +80,23 @@ class ExchangePermission(BasePermission):
return request.user.pk == obj.exchange.student.pk
class CharInFilter(filters.BaseInFilter, filters.CharFilter):
pass
class ExchangeFeedbackFilter(filters.FilterSet):
exchange__student_major_in = CharInFilter(field_name='exchange__student_major', lookup_expr='in')
exchange__student_minor_in = CharInFilter(field_name='exchange__student_minor', lookup_expr='in')
class Meta:
model = ExchangeFeedback
fields = ['exchange__student_major_in', 'exchange__student_minor_in']
class ExchangeFeedbackFilterBackend(DjangoFilterBackend):
filterset_base = ExchangeFeedbackFilter
class ExchangeFeedbackViewSet(EssentialModuleViewSet):
permission_classes = (
NoDelete & NoPost & (ReadOnly | IsStaff | ExchangePermission),
......@@ -99,9 +118,8 @@ class ExchangeFeedbackViewSet(EssentialModuleViewSet):
end_point_route = "exchangeFeedbacks"
filterset_fields = (
"university",
"exchange__student_major",
"exchange__student_minor",
"untouched",
)
filter_backends = (filters.DjangoFilterBackend, ExchangeFeedbackFilterBackend,)
required_filterset_fields = ("university",)
pagination_class = CustomPagination
......@@ -19,6 +19,11 @@ import withUnivInfo from "../../../hoc/withUnivInfo";
import withNetworkWrapper, {
NetWrapParam,
} from "../../../hoc/withNetworkWrapper";
import {
getAllMajors,
getAllMinors,
getUpdatedUnivMajorMinors,
} from "../../../utils/majorMinorMappings";
const undefinedVal = uuidv4();
......@@ -52,10 +57,13 @@ function PreviousExchangesTab({
}) {
const classes = useStyles();
const univMajorMinorsUpdated = getUpdatedUnivMajorMinors(univMajorMinors);
const displayMinorSelect = major !== undefinedVal;
let minors;
if (displayMinorSelect)
minors = univMajorMinors.find((el) => el.major === major).minors;
if (displayMinorSelect) {
minors = univMajorMinorsUpdated.find((el) => el.major === major).minors;
}
return (
<>
......@@ -70,7 +78,7 @@ function PreviousExchangesTab({
}}
>
<MenuItem value={undefinedVal}>Pas de filtre</MenuItem>
{univMajorMinors
{univMajorMinorsUpdated
.filter((el) => el.major !== null)
.map((el) => (
<MenuItem key={el.major} value={el.major}>
......@@ -176,10 +184,14 @@ const buildExchangesParams = (
const params = RequestParams.Builder.withQueryParam("university", univId)
.withQueryParam("page_size", 8)
.withQueryParam("page", page);
if (typeof major !== "undefined" && major !== undefinedVal)
params.withQueryParam("exchange__student_major", major);
if (typeof minor !== "undefined" && minor !== undefinedVal)
params.withQueryParam("exchange__student_minor", minor);
if (typeof major !== "undefined" && major !== undefinedVal) {
const finalMajors = getAllMajors(major);
params.withQueryParam("exchange__student_major_in", finalMajors.join(","));
}
if (typeof minor !== "undefined" && minor !== undefinedVal) {
const finalMinors = getAllMinors(major, minor);
params.withQueryParam("exchange__student_minor_in", finalMinors.join(","));
}
if (showUntouched === false) params.withQueryParam("untouched", false);
return params.build();
};
......
......@@ -121,8 +121,8 @@ class FilterService {
);
const extraMinors = this.getMajorInUniv(univObj).map((major) => {
major = getUpdatedMajor(major);
return `${major} — Toutes filières confondues`;
const maj = getUpdatedMajor(major);
return `${maj} — Toutes filières confondues`;
});
return [...new Set(realMinors), ...extraMinors];
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment