(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango »
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango » pwd
/Users/marie/mynewdjango
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango » tree
.
├── comment
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-37.pyc
│ │ ├── models.cpython-37.pyc
│ │ ├── urls.cpython-37.pyc
│ │ └── views.cpython-37.pyc
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ │ ├── 0001_initial.py
│ │ ├── __init__.py
│ │ └── __pycache__
│ │ ├── 0001_initial.cpython-37.pyc
│ │ └── __init__.cpython-37.pyc
│ ├── models.py
│ ├── tests.py
│ ├── urls.py
│ └── views.py
├── db.sqlite3
├── manage.py
├── mynewjangodirec
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-37.pyc
│ │ ├── settings.cpython-37.pyc
│ │ ├── urls.cpython-37.pyc
│ │ └── wsgi.cpython-37.pyc
│ ├── asgi.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── user
├── __init__.py
├── __pycache__
│ ├── __init__.cpython-37.pyc
│ ├── admin.cpython-37.pyc
│ ├── models.cpython-37.pyc
│ ├── urls.cpython-37.pyc
│ └── views.cpython-37.pyc
├── admin.py
├── apps.py
├── migrations
│ ├── 0001_initial.py
│ ├── __init__.py
│ └── __pycache__
│ ├── 0001_initial.cpython-37.pyc
│ └── __init__.cpython-37.pyc
├── models.py
├── tests.py
├── urls.py
└── views.py
10 directories, 42 files
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango »
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango »
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango » cat ./user/views.py
from django.shortcuts import render
# Create your views here.
import json #json은 자바스크립트의 오브젝트를 나열한 데이터타입! 파이썬은 json을 모르기때문에 import 필수!
from django.views import View #http 렌더링 및 http response의 메소드 구별을 위해 import 필수!
from django.http import JsonResponse #파이썬은 json을 모름. 그래서! http response를json으로 응답하기 위해서 import 필수!
from .models import Users #생성한"Users"라는 클래스에 지금의 views.py을 적용하고자 import!
class UserView(View):
def post(self, request):
data = json.loads(request.body)
Users(
name = data['name'],
email = data['email'],
password = data['password'],
).save()
return JsonResponse({'message':'It\'s success!'}, status = 200)
class AuthView(View):
def post(self, request):
data = json.loads(request.body)
user = Users.objects.get(email=data['email'])
if data['password'] == user.password:
return JsonResponse({'message':'Thank you for Login!'}, status = 200)
else:
return JsonResponse({'message':'Invalid Passowrd!!'}, status = 401)
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango »
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango » vi ./user/views.py
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango »
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango » cat ./user/views.py
import jwt
import json
import bcrypt
from django.views import View
from django.http import JsonResponse
from django.db import IntegrityError
from mynewdjangodirec.settings import SECRET_KEY
from .models import Users
class UserView(View):
def post(self, request):
data = json.loads(request.body)
#print(data)
try:
hashed_password = bcrypt.hashpw(data['password'].encode('utf-8'),bcrypt.gensalt())
Users(
name = data['name'],
email = data['email'],
password = hashed_password.decode('utf-8')
).save()
return JsonResponse({'message':'SUCCESS'}, status = 200)
except KeyError: #키에러
return JsonResponse({'message':'INVALID_KEYS'}, status = 400)
except IntegrityError: #유니크한 데이터 필드에 중복값을 입력시 에러 처리
return JsonResponse({'message': 'EXCEPTED_DATA'}, status = 401)
def get(self, request, user_id):
try:
user = list(Users.objects.filter(id = user_id).values())
return JsonResponse({'data':user}, status=200)
except Users.DoesNotExist:#넘긴 유저아이디가 존재하지 않는경우 에러처리
return JsonResponse({'message':'INVALID_USER'}, status = 400)
class AuthView(View):
def post(self, request):
data = json.loads(request.body)
try:
user = Users.objects.get(email = data['email'])
if bcrypt.checkpw(data['password'].encode('utf-8'),user.password.encode('utf-8')):
access_token = jwt.encode({'id':user.id},SECRET_KEY,algorithm='HS256')
return JsonResponse({'access_token':access_token.decode('utf-8')}, status = 200)
else:
return JsonResponse({'message':'INVALID_PASSWORD'}, status = 401)
except KeyError: #키에러
return JsonResponse({'message':'INVALID_KEYS'}, status = 400)
except Users.DoesNotExist: #유저가 존재하지 않을 경우
return JsonResponse({'message':'INVALID_USER'}, status = 400)
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango »