Fakultas Ilmu Komputer UI

Commit 469792af authored by Ibnu Sofian Firdaus's avatar Ibnu Sofian Firdaus
Browse files

121 Modularize vacancy filters

parent 6cbefb96
......@@ -45,26 +45,21 @@ class VacancyViewSet(MultiSerializerViewSetMixin, viewsets.ModelViewSet):
return super(VacancyViewSet, self).get_permissions()
def list(self, request, *args, **kwargs):
verified = request.query_params['verified'] if 'verified' in request.query_params else "True"
search = request.query_params['search'] if 'search' in request.query_params else None
verified = request.query_params['verified'] if 'verified' in request.query_params else "True"
opened_only = str(request.query_params['opened_only']) \
if 'opened_only' in request.query_params else "False"
companies = [int(x) for x in request.query_params.getlist('company', [])]
if search is not None:
vacancies = Vacancy.objects.filter(Q(name__icontains=search) | Q(company__user__username__icontains=search))
else:
vacancies = Vacancy.objects.all()
companies = [int(x) for x in request.query_params.getlist('company', [])]
if verified.lower() in ("yes", "true", "t", "1"):
vacancies = vacancies.filter(verified=True)
if verified.lower() in {"no", "false", "f", "0"}:
vacancies = vacancies.filter(verified=False)
opened_only = str(request.query_params['opened_only']) \
if 'opened_only' in request.query_params else "False"
if opened_only.lower() in ("yes", "true", "t", "1"):
today = datetime.combine(datetime.now().date(), time())
vacancies = vacancies.filter(close_time__gte=today)
if len(companies) > 0:
vacancies = vacancies.filter(company__id__in=companies)
# Filter vacancies here
vacancies = self.verifiedFilter(vacancies, verified)
vacancies = self.openedJobFilter(vacancies, opened_only)
vacancies = self.companyFilter(vacancies, companies)
page = self.paginate_queryset(vacancies)
print(vacancies)
......@@ -74,6 +69,24 @@ class VacancyViewSet(MultiSerializerViewSetMixin, viewsets.ModelViewSet):
vacancies = Vacancy.objects.all()
return Response(VacancySerializer(vacancies, many=True, context={'request': request}).data)
def verifiedFilter(self, vacancies, flag):
if flag.lower() in ("yes", "true", "t", "1"):
vacancies = vacancies.filter(verified=True)
if flag.lower() in {"no", "false", "f", "0"}:
vacancies = vacancies.filter(verified=False)
return vacancies
def openedJobFilter(self, vacancies, flag):
if flag.lower() in ("yes", "true", "t", "1"):
today = datetime.combine(datetime.now().date(), time())
vacancies = vacancies.filter(close_time__gte=today)
return vacancies
def companyFilter(self, vacancies, companies):
if len(companies) > 0:
vacancies = vacancies.filter(company__id__in=companies)
return vacancies
def name_position_validator(self, names):
for name in names.split(" "):
if not name.isalpha():
......
Supports Markdown
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