109 lines
4.5 KiB
Python
109 lines
4.5 KiB
Python
from rest_framework import viewsets, permissions, authentication
|
|
from rest_framework.response import Response
|
|
from jobposting.models import JobListing, SkillLevels, CompanyLogo, Skill
|
|
from core.models import MyUser, AnonymousUserData
|
|
from rest_framework import status
|
|
from jobposting.permissions import ClientCredentialPermission
|
|
from oauth2_provider.contrib.rest_framework import TokenHasReadWriteScope, OAuth2Authentication
|
|
from .auth import OAuth2ClientCredentialAuthentication
|
|
from rest_framework.views import APIView
|
|
import requests
|
|
from django.conf import settings
|
|
|
|
from jobposting.serializers import (
|
|
JobListingSerializer,
|
|
SkillLevelsSerializer,
|
|
MyUserSerializer,
|
|
CompanyLogoSerializer,
|
|
SkillSerializer
|
|
)
|
|
|
|
class MyUserViewSet(viewsets.ModelViewSet):
|
|
authentication_classes = [OAuth2Authentication, authentication.SessionAuthentication]
|
|
permission_classes = [permissions.IsAuthenticated|TokenHasReadWriteScope]
|
|
queryset = MyUser.objects.all()
|
|
serializer_class = MyUserSerializer
|
|
required_scope = ['main']
|
|
# permission_classes = [permissions.IsAuthenticated]
|
|
|
|
class JobListingViewSet(viewsets.ModelViewSet):
|
|
authentication_classes = [OAuth2Authentication, authentication.SessionAuthentication]
|
|
permission_classes = [permissions.IsAuthenticated|TokenHasReadWriteScope] # test only
|
|
queryset = JobListing.objects.all()
|
|
serializer_class = JobListingSerializer
|
|
required_scope = ['main']
|
|
|
|
# permission_classes = [permissions.IsAuthenticatedOrReadOnly]
|
|
|
|
def perform_create(self, serializer):
|
|
if self.request.user.is_authenticated:
|
|
serializer.save(created_by=self.request.user)
|
|
else:
|
|
# Pobieranie danych z formularza
|
|
first_name = self.request.data.get('first_name')
|
|
last_name = self.request.data.get('last_name')
|
|
contact_email = self.request.data.get('contact_email')
|
|
|
|
# Tworzenie rekordu AnonymousUserData
|
|
anonymous_user_data = AnonymousUserData.objects.create(
|
|
first_name=first_name,
|
|
last_name=last_name,
|
|
contact_email=contact_email
|
|
)
|
|
|
|
serializer.save(anonymous_user_data=anonymous_user_data)
|
|
|
|
class SkillLevelsViewSet(viewsets.ModelViewSet):
|
|
authentication_classes = [OAuth2Authentication, authentication.SessionAuthentication]
|
|
permission_classes = [permissions.IsAuthenticated|TokenHasReadWriteScope] # test only
|
|
queryset = SkillLevels.objects.all()
|
|
serializer_class = SkillLevelsSerializer
|
|
required_scope = ['main']
|
|
|
|
# permission_classes = [permissions.IsAuthenticatedOrReadOnly]
|
|
|
|
def create(self, request, *args, **kwargs):
|
|
data = request.data
|
|
print(type(data))
|
|
if isinstance(data, list): # Sprawdzenie, czy dane to lista
|
|
serializers = [self.get_serializer(data=item) for item in data]
|
|
for serializer in serializers:
|
|
serializer.is_valid(raise_exception=True)
|
|
self.perform_create(serializer)
|
|
return Response([serializer.data for serializer in serializers], status=status.HTTP_201_CREATED)
|
|
else:
|
|
return super().create(request, *args, **kwargs)
|
|
|
|
class CompanyLogoViewSet(viewsets.ModelViewSet):
|
|
authentication_classes = [OAuth2Authentication, authentication.SessionAuthentication]
|
|
permission_classes = [permissions.IsAuthenticated|TokenHasReadWriteScope]
|
|
queryset = CompanyLogo.objects.all()
|
|
serializer_class = CompanyLogoSerializer
|
|
required_scope = ['main']
|
|
|
|
|
|
class SkillViewset(viewsets.ModelViewSet):
|
|
authentication_classes = [OAuth2Authentication, authentication.SessionAuthentication]
|
|
permission_classes = [permissions.IsAuthenticated|TokenHasReadWriteScope]
|
|
queryset = Skill.objects.all()
|
|
serializer_class = SkillSerializer
|
|
required_scope = ['main']
|
|
|
|
class GetAuthTokenView(APIView):
|
|
authentication_classes = []
|
|
permission_classes = []
|
|
def post(self, request):
|
|
print("GET ID")
|
|
client_id = settings.REACT_CLIENT_ID
|
|
client_secret = settings.REACT_CLIENT_SECRET
|
|
print("GET SECRET")
|
|
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
|
|
data = {
|
|
"grant_type": "client_credentials",
|
|
"client_id": client_id,
|
|
"client_secret": client_secret,
|
|
"redirect_uri": "http://izaac.izaac.pl/noexist/callback"
|
|
}
|
|
print("DATA READY")
|
|
response = requests.post('http://izaac.izaac.pl/o/token/', data=data, headers=headers)
|
|
return Response(response.json()) |