Fakultas Ilmu Komputer UI

Commit 9ef20995 authored by Zamil Majdy's avatar Zamil Majdy
Browse files

[#140654507] #12 #15 Implement CRUD application

parent 8772aba4
from django.contrib import admin from django.contrib import admin
from core.models.vacancies import Application, Vacancy
from core.models.accounts import Company, Supervisor, Student from core.models.accounts import Company, Supervisor, Student
from core.models.vacancies import Vacancy
admin.site.register(Company) admin.site.register(Company)
admin.site.register(Student) admin.site.register(Student)
admin.site.register(Supervisor) admin.site.register(Supervisor)
admin.site.register(Vacancy) admin.site.register(Vacancy)
admin.site.register(Application)
# -*- coding: utf-8 -*-
# Generated by Django 1.10.5 on 2017-03-28 04:31
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('core', '0001_initial'),
]
operations = [
migrations.RemoveField(
model_name='application',
name='student_npm',
),
migrations.RemoveField(
model_name='application',
name='vacancy_id',
),
migrations.AddField(
model_name='student',
name='applied_vacancies',
field=models.ManyToManyField(blank=True, related_name='applied_vacancies', to='core.Vacancy'),
),
migrations.AlterField(
model_name='student',
name='bookmarked_vacancies',
field=models.ManyToManyField(blank=True, related_name='bookmarked_vacancies', to='core.Vacancy'),
),
migrations.DeleteModel(
name='Application',
),
]
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
from core.models.accounts import Company from core.models.accounts import Company
from core.models.accounts import Student from core.models.accounts import Student
from core.models.accounts import Supervisor from core.models.accounts import Supervisor
from core.models.vacancies import Application
from core.models.vacancies import Vacancy from core.models.vacancies import Vacancy
......
...@@ -48,7 +48,8 @@ class Student(models.Model): ...@@ -48,7 +48,8 @@ class Student(models.Model):
npm = models.IntegerField(validators=[MinValueValidator(100000000), MaxValueValidator(9999999999)], unique=True) npm = models.IntegerField(validators=[MinValueValidator(100000000), MaxValueValidator(9999999999)], unique=True)
resume = models.FileField(upload_to=get_student_resume_file_path, null=True, blank=True) resume = models.FileField(upload_to=get_student_resume_file_path, null=True, blank=True)
phone_number = models.CharField(max_length=100, blank=True, db_index=True, null=True) phone_number = models.CharField(max_length=100, blank=True, db_index=True, null=True)
bookmarked_vacancies = models.ManyToManyField('core.Vacancy', blank=True) bookmarked_vacancies = models.ManyToManyField('core.Vacancy', related_name="bookmarked_vacancies", blank=True)
applied_vacancies = models.ManyToManyField('core.Vacancy', related_name="applied_vacancies", blank=True)
@property @property
def name(self): def name(self):
......
from django.db import models from django.db import models
from core.models.accounts import Company, Student from core.models.accounts import Company
class Vacancy(models.Model): class Vacancy(models.Model):
...@@ -9,8 +9,3 @@ class Vacancy(models.Model): ...@@ -9,8 +9,3 @@ class Vacancy(models.Model):
open_time = models.DateTimeField() open_time = models.DateTimeField()
description = models.TextField(blank=True) description = models.TextField(blank=True)
close_time = models.DateTimeField() close_time = models.DateTimeField()
class Application(models.Model):
student_npm = models.OneToOneField(Student)
vacancy_id = models.OneToOneField(Vacancy)
\ No newline at end of file
from rest_framework import serializers from rest_framework import serializers
from core.serializers.accounts import CompanySerializer from core.serializers.accounts import CompanySerializer
from core.models.vacancies import Vacancy, Application from core.models.vacancies import Vacancy
class VacancySerializer(serializers.ModelSerializer): class VacancySerializer(serializers.ModelSerializer):
...@@ -11,8 +11,3 @@ class VacancySerializer(serializers.ModelSerializer): ...@@ -11,8 +11,3 @@ class VacancySerializer(serializers.ModelSerializer):
fields = '__all__' fields = '__all__'
class ApplicationSerializer(serializers.ModelSerializer):
class Meta:
model = Application
fields = '__all__'
...@@ -4,8 +4,8 @@ from rest_framework.response import Response ...@@ -4,8 +4,8 @@ from rest_framework.response import Response
from core.lib.permissions import IsAdminOrStudent, IsAdminOrCompany from core.lib.permissions import IsAdminOrStudent, IsAdminOrCompany
from core.models import Student from core.models import Student
from core.models.vacancies import Vacancy, Application from core.models.vacancies import Vacancy
from core.serializers.vacancies import VacancySerializer, ApplicationSerializer from core.serializers.vacancies import VacancySerializer
class VacancyViewSet(viewsets.ModelViewSet): class VacancyViewSet(viewsets.ModelViewSet):
...@@ -14,11 +14,27 @@ class VacancyViewSet(viewsets.ModelViewSet): ...@@ -14,11 +14,27 @@ class VacancyViewSet(viewsets.ModelViewSet):
permission_classes = [IsAdminOrCompany] permission_classes = [IsAdminOrCompany]
class ApplicationViewSet(viewsets.ModelViewSet): class ApplicationViewSet(viewsets.GenericViewSet):
queryset = Application.objects.all() serializer_class = VacancySerializer
serializer_class = ApplicationSerializer
permission_classes = [IsAdminOrStudent] permission_classes = [IsAdminOrStudent]
def list(self, request, student_id):
student = get_object_or_404(Student.objects.all(), pk=student_id)
vacancies = self.serializer_class(student.applied_vacancies, many=True, context={'request': request})
return Response(vacancies.data)
def create(self, request, student_id):
vacancy = get_object_or_404(Vacancy.objects.all(), pk=request.data['vacancy_id'])
student = get_object_or_404(Student.objects.all(), pk=student_id)
student.applied_vacancies.add(vacancy)
return Response(self.serializer_class(student.applied_vacancies, many=True, context={'request': request}).data)
def destroy(self, request, student_id, pk):
vacancy = get_object_or_404(Vacancy.objects.all(), pk=pk)
student = get_object_or_404(Student.objects.all(), pk=student_id)
student.applied_vacancies.remove(vacancy)
return Response(self.serializer_class(student.applied_vacancies, many=True, context={'request': request}).data)
class BookmarkedVacancyByStudentViewSet(viewsets.GenericViewSet): class BookmarkedVacancyByStudentViewSet(viewsets.GenericViewSet):
serializer_class = VacancySerializer serializer_class = VacancySerializer
......
...@@ -23,7 +23,7 @@ from rest_framework_swagger.views import get_swagger_view ...@@ -23,7 +23,7 @@ from rest_framework_swagger.views import get_swagger_view
from core import apps from core import apps
from core.views.accounts import StudentViewSet, CompanyViewSet, SupervisorViewSet, UserViewSet, LoginViewSet from core.views.accounts import StudentViewSet, CompanyViewSet, SupervisorViewSet, UserViewSet, LoginViewSet
from core.views.vacancies import VacancyViewSet, ApplicationViewSet from core.views.vacancies import VacancyViewSet, BookmarkedVacancyByStudentViewSet, ApplicationViewSet
schema_view = get_swagger_view() schema_view = get_swagger_view()
router = routers.DefaultRouter() router = routers.DefaultRouter()
...@@ -33,7 +33,11 @@ router.register(r'companies', CompanyViewSet) ...@@ -33,7 +33,11 @@ router.register(r'companies', CompanyViewSet)
router.register(r'supervisors', SupervisorViewSet) router.register(r'supervisors', SupervisorViewSet)
router.register(r'login', LoginViewSet) router.register(r'login', LoginViewSet)
router.register(r'vacancies', VacancyViewSet) router.register(r'vacancies', VacancyViewSet)
router.register(r'applications', ApplicationViewSet) router.register(r'students/(?P<student_id>\d+)/bookmarked-vacancies', BookmarkedVacancyByStudentViewSet,
base_name='bookmarked-vacancy-list')
router.register(r'students/(?P<student_id>\d+)/applications', ApplicationViewSet,
base_name='applications')
urlpatterns = static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) urlpatterns = static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += [ urlpatterns += [
......
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