Fakultas Ilmu Komputer UI

Commit 42a42d4c authored by Joshua Casey's avatar Joshua Casey
Browse files

[#140654507] #16 Added endpoint for company to create new user

parent c2eecf47
......@@ -46,3 +46,10 @@ class LoginSerializer(serializers.HyperlinkedModelSerializer):
model = User
fields = ('url', 'username', 'email', 'is_staff', 'company', 'supervisor', 'student')
class RegisterSerializer(serializers.HyperlinkedModelSerializer):
company = CompanySerializer()
class Meta:
model = User
fields = ('url', 'username', 'email', 'is_staff', 'company')
......@@ -6,11 +6,12 @@ from rest_framework.decorators import list_route
from rest_framework.permissions import AllowAny
from rest_framework.permissions import IsAdminUser, IsAuthenticated
from rest_framework.response import Response
from rest_framework.parsers import FormParser,MultiPartParser
from core.lib.permissions import IsAdminOrStudent, IsAdminOrSelfOrReadOnly, IsAdminOrCompany, IsAdminOrSupervisor
from core.models.accounts import Student, Company, Supervisor
from core.serializers.accounts import UserSerializer, StudentSerializer, CompanySerializer, SupervisorSerializer, \
LoginSerializer
LoginSerializer, RegisterSerializer
class UserViewSet(viewsets.ModelViewSet):
......@@ -129,3 +130,42 @@ class LoginViewSet(viewsets.GenericViewSet):
return Response(status=status.HTTP_401_UNAUTHORIZED)
else:
return Response(status=status.HTTP_400_BAD_REQUEST)
class CompanyRegisterViewSet(viewsets.GenericViewSet):
permission_classes = (AllowAny,)
serializer_class = RegisterSerializer
queryset = Company.objects.all()
parser_classes = (MultiPartParser, FormParser,)
def create(self, request):
username = request.data.get('username')
password = request.data.get('password')
email = request.data.get('email')
company_name = request.data.get('name')
description = request.data.get('description')
logo = request.data.get('logo')
address = request.data.get('address')
if username is not None and password is not None and email is not None and company_name is not None and description is not None:
user, created = User.objects.get_or_create(
username=username,
email=email,
first_name=company_name,
last_name=""
)
if created:
user.set_password(password)
company = Company.objects.create(
user=user,
description=description,
logo=logo,
address=address
)
user.save()
company.save()
serializer = RegisterSerializer(user, context={'request': request})
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(status=status.HTTP_409_CONFLICT)
return Response(status=status.HTTP_400_BAD_REQUEST)
\ No newline at end of file
......@@ -22,7 +22,7 @@ from rest_framework import routers
from rest_framework_swagger.views import get_swagger_view
from core import apps
from core.views.accounts import StudentViewSet, CompanyViewSet, SupervisorViewSet, UserViewSet, LoginViewSet
from core.views.accounts import StudentViewSet, CompanyViewSet, SupervisorViewSet, UserViewSet, LoginViewSet, CompanyRegisterViewSet
from core.views.vacancies import VacancyViewSet, BookmarkedVacancyByStudentViewSet, ApplicationViewSet
schema_view = get_swagger_view()
......@@ -32,6 +32,7 @@ router.register(r'students', StudentViewSet)
router.register(r'companies', CompanyViewSet)
router.register(r'supervisors', SupervisorViewSet)
router.register(r'login', LoginViewSet)
router.register(r'register', CompanyRegisterViewSet)
router.register(r'vacancies', VacancyViewSet)
router.register(r'students/(?P<student_id>\d+)/bookmarked-vacancies', BookmarkedVacancyByStudentViewSet,
base_name='bookmarked-vacancy-list')
......
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