Fakultas Ilmu Komputer UI

Commit ee274406 authored by Farhan Farasdak's avatar Farhan Farasdak
Browse files

[#140653601] [50] JS Logic Supervisor Verify and Unverify Vacancy

parents 5eafdc95 5d407635
......@@ -100,7 +100,7 @@ export default class VacancyPage extends React.Component {
<Pane label="Lowongan Belum Terverifikasi" >
<Pagination
key={1}
url="/vacancies/"
url="/vacancies/?verified=false"
child={
<VacancyList
user={this.props.user}
......@@ -113,7 +113,7 @@ export default class VacancyPage extends React.Component {
<Pane label="Lowongan Terverifikasi" >
<Pagination
key={2}
url={`/vacancies/`}
url={`/vacancies/?verified=true`}
child={
<VacancyList
user={this.props.user}
......
import React from 'react';
import { Item, Grid, Icon, Button } from 'semantic-ui-react';
import VerifyAdminModal from './VerifyAdminModal';
import Server from '../lib/Server';
export default class AdminVacancy extends React.Component {
static propTypes = {
data: React.PropTypes.object.isRequired,
updateStatus : React.PropTypes.func.isRequired,
};
verifyVacancy(){
//define here
}
unverifyVacancy(){
//define here
changeVerifiedStatus(){
let data = {};
if(this.props.data.verified){
data = {"verified" : false};
}
else{
data = {"verified" : true};
}
Server.patch(`/vacancies/${this.props.data.id}/verify/`, data).then((status) => {
this.props.updateStatus(this.props.data.id, status.status);
});
}
generateButton(){
const unverifyButton = <Button floated="right" color="red" onClick='{this.unverifyVacancy}'>Batalkan Verifikasi</Button>;
const verifyButton = <Button floated="right" color="blue" onClick='{this.unverifyVacancy}'>Verifikasi</Button>;
const unverifyButton = <Button floated="right" color="red" onClick={this.changeVerifiedStatus.bind(this)}>Batalkan Verifikasi</Button>;
const verifyButton = <Button floated="right" color="blue" onClick={this.changeVerifiedStatus.bind(this)}>Verifikasi</Button>;
if(this.props.data.verified){
return unverifyButton;
......
......@@ -18,6 +18,15 @@ export default class VacancyList extends React.Component {
items: [],
};
updateStatus(id, status) {
const obj = [];
this.state.vacancies.map((vacancy) => {
if (vacancy.id !== id) return obj.push(vacancy);
return null;
});
this.setState({ vacancies: obj });
}
constructor(props) {
super(props);
/* istanbul ignore next */
......@@ -28,6 +37,7 @@ export default class VacancyList extends React.Component {
loading: true,
};
this.generateVacancies = this.generateVacancies.bind(this);
this.updateStatus = this.updateStatus.bind(this);
}
deleteVacancy = id => Server.delete(`/vacancies/${id}/`, this.state).then(() => {
......@@ -58,6 +68,7 @@ export default class VacancyList extends React.Component {
return this.state.vacancies.map(vacancy => (<AdminVacancy
key={vacancy.id}
data={vacancy}
updateStatus={this.updateStatus}
/>),
);
}
......
......@@ -76,7 +76,10 @@ export default class App extends React.Component {
replace({ pathname: '/lowongan' }); cb();
} else if (App.getRole() === 'admin') {
replace({ pathname: '/perusahaan' }); cb();
} else if (App.getRole() === 'supervisor'){
replace({ pathname:'/lowongan'}); cb();
}
}
replace({ pathname: '/login' }); cb();
};
......
......@@ -5,6 +5,7 @@ from core.models import Company
from core.models import Student
from core.models import Supervisor
from core.models import Application
from core.models import Vacancy
def is_admin_or_student(user):
......@@ -145,3 +146,11 @@ class IsAdminOrVacancyOwner(permissions.BasePermission):
raise PermissionDenied(
"Checking owner permission on non-application object"
)
class VacancyApprovalPermission(permissions.BasePermission):
def has_permission(self, request, view):
return is_admin_or_supervisor(request.user)
def has_object_permission(self, request, view, obj):
return isinstance(obj, Vacancy)
......@@ -58,3 +58,17 @@ class ApplicationStatusSerializer(serializers.ModelSerializer):
class Meta:
model = Application
fields = ['status']
class VacancyApplicationSerializer(serializers.ModelSerializer):
vacancy = VacancySerializer()
class Meta:
model = Application
fields = ['cover_letter', 'vacancy', 'status']
class VacancyVerifiedSerializer(serializers.ModelSerializer):
class Meta:
model = Vacancy
fields = ['verified']
......@@ -5,7 +5,7 @@ from django.contrib.auth.models import User
from rest_framework import status
from rest_framework.test import APITestCase
from core.models.accounts import Company
from core.models.accounts import Company, Supervisor
from core.models.vacancies import Vacancy
......@@ -186,3 +186,38 @@ class CompanyListsTests(APITestCase):
url = '/api/companies/' + str(new_company.pk) + '/applications'
response = self.client.post(url, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
class SupervisorApprovalTests(APITestCase):
def test_supervisor_approve_vacancy(self):
new_user = User.objects.create_user('dummy.supervisor', 'dummy.supervisor@asd.asd', 'lalala123')
new_supervisor = Supervisor.objects.create(user=new_user, nip=1212121212)
self.client.force_authenticate(user=new_user)
new_user2 = User.objects.create_user('dummy.company2', 'dummy.compan2y@company.com', 'lalala123')
new_company2 = Company.objects.create(user=new_user2, description="lalala", status=Company.VERIFIED, logo=None,
address=None)
new_vacancy2 = Vacancy.objects.create(company=new_company2, verified=False, open_time=datetime.fromtimestamp(0),
description="lalala", close_time=datetime.today())
url = '/api/vacancies/' + str(new_vacancy2.pk) + '/verify/'
response = self.client.patch(url, {'verified' : True}, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
retrieve_vacancy = Vacancy.objects.get(pk=new_vacancy2.pk)
self.assertEqual(retrieve_vacancy.verified, True)
def test_unauthorized_approve_vacancy(self):
new_user = User.objects.create_user('dummy.companyz', 'dummy.companyz@company.com', 'lalala123')
new_company = Company.objects.create(user=new_user, description="lalalaz", status=Company.VERIFIED, logo=None,
address=None)
self.client.force_authenticate(user=new_user)
new_vacancy = Vacancy.objects.create(company=new_company, verified=False, open_time=datetime.fromtimestamp(0),
description="lalala", close_time=datetime.today())
url = '/api/vacancies/' + str(new_vacancy.pk) + '/verify/'
response = self.client.patch(url, format='json')
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
self.assertEqual(new_vacancy.verified, False)
......@@ -8,11 +8,11 @@ from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.pagination import PageNumberPagination
from core.lib.mixins import MultiSerializerViewSetMixin
from core.lib.permissions import IsAdminOrStudent, IsAdminOrCompany, IsAdminOrVacancyOwner
from core.lib.permissions import IsAdminOrStudent, IsAdminOrCompany, IsAdminOrVacancyOwner, VacancyApprovalPermission
from core.models import Student, Company
from core.models.vacancies import Vacancy, Application
from core.serializers.vacancies import VacancySerializer, ApplicationSerializer, ApplicationStatusSerializer, \
PostVacancySerializer
VacancyApplicationSerializer, PostVacancySerializer, VacancyVerifiedSerializer
class VacancyViewSet(MultiSerializerViewSetMixin, viewsets.ModelViewSet):
......@@ -34,6 +34,8 @@ class VacancyViewSet(MultiSerializerViewSetMixin, viewsets.ModelViewSet):
verified = request.query_params['verified'] if 'verified' in request.query_params else "True"
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)
page = self.paginate_queryset(vacancies)
if page is not None:
return self.get_paginated_response(VacancySerializer(page, many=True, context={'request': request}).data)
......@@ -46,6 +48,15 @@ class VacancyViewSet(MultiSerializerViewSetMixin, viewsets.ModelViewSet):
count_new = Application.objects.filter(vacancy=vacancy, status=Application.NEW).count()
return Response({"count": count, "count_new": count_new}, status=status.HTTP_200_OK)
@detail_route(methods=['patch'], permission_classes=[VacancyApprovalPermission], serializer_class=VacancyVerifiedSerializer)
def verify(self, request, pk=None):
vacancy = self.get_object()
serializer = self.get_serializer_class()(vacancy, data=request.data, partial=True)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK)
return Response({"error" : "bad request"}, status=status.HTTP_400_BAD_REQUEST)
class ApplicationViewSet(viewsets.GenericViewSet):
serializer_class = ApplicationSerializer
......
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