-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #285 from smu-nity/feature/284
Feature/284
- Loading branch information
Showing
4 changed files
with
55 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from rest_framework import serializers | ||
|
||
|
||
class UserSerializer(serializers.Serializer): | ||
username = serializers.CharField(max_length=100) | ||
password = serializers.CharField(max_length=100) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
from django.urls import path | ||
|
||
from api import views | ||
|
||
app_name = 'api' | ||
|
||
urlpatterns = [ | ||
path('auth', views.authenticate, name='authenticate'), | ||
path('userinfo', views.userinfo, name='userinfo'), | ||
path('courses', views.courses, name='courses'), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,50 @@ | ||
from rest_framework import status | ||
from rest_framework.response import Response | ||
from rest_framework.decorators import api_view | ||
from sangmyung_univ_auth import auth | ||
from rest_framework.response import Response | ||
from sangmyung_univ_auth import auth, completed_courses | ||
|
||
from api.serializers import UserSerializer | ||
|
||
|
||
@api_view(['POST']) | ||
def authenticate(request): | ||
if 'username' and 'password' in request.data: | ||
username, password = request.data['username'], request.data['password'] | ||
return authenticate_user(request) | ||
|
||
|
||
@api_view(['POST']) | ||
def userinfo(request): | ||
return authenticate_user(request, response_body=False) | ||
|
||
|
||
@api_view(['POST']) | ||
def courses(request): | ||
serializer = UserSerializer(data=request.data) | ||
if serializer.is_valid(): | ||
username, password = serializer.validated_data['username'], serializer.validated_data['password'] | ||
result = completed_courses(username, password) | ||
status_code = status.HTTP_200_OK if result.is_auth else status.HTTP_401_UNAUTHORIZED | ||
return Response(filter_data(result.body), status=status_code) | ||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) | ||
|
||
|
||
def authenticate_user(request, response_body=True): | ||
serializer = UserSerializer(data=request.data) | ||
if serializer.is_valid(): | ||
username, password = serializer.validated_data['username'], serializer.validated_data['password'] | ||
result = auth(username, password) | ||
res = status.HTTP_200_OK if result.is_auth else status.HTTP_401_UNAUTHORIZED | ||
return Response(result._asdict(), status=res) | ||
return Response(status=status.HTTP_400_BAD_REQUEST) | ||
status_code = status.HTTP_200_OK if result.is_auth else status.HTTP_401_UNAUTHORIZED | ||
return Response(result._asdict() if response_body else result.body, status=status_code) | ||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) | ||
|
||
|
||
def filter_data(original_data): | ||
return list(map(lambda item: { | ||
'number': item['SBJ_NO'], | ||
'name': item['SBJ_NM'], | ||
'credit': item['CDT'], | ||
'type': item['CMP_DIV_NM'], | ||
'grade': item['GRD_NM'], | ||
'year': item['SCH_YEAR'], | ||
'semester': item['SMT_NM'], | ||
'domain': item['CULT_ARA_NM'] if item['CULT_ARA_NM'] != '*' else None | ||
}, original_data)) |