Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cc5c992

Browse files
25 - Register API View
1 parent a44f9d2 commit cc5c992

File tree

4 files changed

+44
-8
lines changed

4 files changed

+44
-8
lines changed

scripts/cfe_rest_framework_api.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,22 @@
33
import os
44

55

6-
AUTH_ENDPOINT = "http://127.0.0.1:8000/api/auth/"
6+
AUTH_ENDPOINT = "http://127.0.0.1:8000/api/auth/register/"
77
REFRESH_ENDPOINT = AUTH_ENDPOINT + "refresh/"
88
ENDPOINT = "http://127.0.0.1:8000/api/status/"
99

1010
image_path = os.path.join(os.getcwd(), "logo.jpg")
1111

1212
headers = {
13-
"Content-Type": "application/json"
13+
"Content-Type": "application/json",
14+
#"Authorization": "JWT " + 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoxLCJ1c2VybmFtZSI6ImNmZSIsImV4cCI6MTUxMzIwNjEwOSwiZW1haWwiOiIiLCJvcmlnX2lhdCI6MTUxMzIwNTgwOX0.JCIM7Es7-pJpKVv4-OrEjCFVYsIegRxELu6YATayu7k',
1415
}
1516

1617
data = {
17-
'username': 'cfe',
18-
'password': 'learncode'
18+
'username': 'cfe4',
19+
'email': '[email protected]',
20+
'password': 'learncode',
21+
'password2': 'learncode'
1922
}
2023

2124
r = requests.post(AUTH_ENDPOINT, data=json.dumps(data), headers=headers)

src/accounts/api/urls.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
from rest_framework_jwt.views import refresh_jwt_token, obtain_jwt_token # accounts app
66

7-
from .views import AuthView
7+
from .views import AuthAPIView, RegisterAPIView
88
urlpatterns = [
9-
url(r'^$', AuthView.as_view()),
9+
url(r'^$', AuthAPIView.as_view()),
10+
url(r'^register/$', RegisterAPIView.as_view()),
1011
url(r'^jwt/$', obtain_jwt_token),
1112
url(r'^jwt/refresh/$', refresh_jwt_token),
1213
]

src/accounts/api/views.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
User = get_user_model()
1616

17-
class AuthView(APIView):
17+
class AuthAPIView(APIView):
1818
permission_classes = [permissions.AllowAny]
1919
def post(self, request, *args, **kwargs):
2020
#print(request.user)
@@ -23,7 +23,6 @@ def post(self, request, *args, **kwargs):
2323
data = request.data
2424
username = data.get('username') # username or email address
2525
password = data.get('password')
26-
user = authenticate(username=username, password=password)
2726
qs = User.objects.filter(
2827
Q(username__iexact=username)|
2928
Q(email__iexact=username)
@@ -39,3 +38,36 @@ def post(self, request, *args, **kwargs):
3938
return Response({"detail": "Invalid credentials"}, status=401)
4039

4140

41+
42+
class RegisterAPIView(APIView):
43+
permission_classes = [permissions.AllowAny]
44+
def post(self, request, *args, **kwargs):
45+
if request.user.is_authenticated():
46+
return Response({'detail': 'You are already registered and are authenticated.'}, status=400)
47+
data = request.data
48+
username = data.get('username') # username or email address
49+
email = data.get('username')
50+
password = data.get('password')
51+
password2 = data.get('password2')
52+
qs = User.objects.filter(
53+
Q(username__iexact=username)|
54+
Q(email__iexact=username)
55+
)
56+
if password != password2:
57+
return Response({"password": "Password must match."}, status=401)
58+
if qs.exists():
59+
return Response({"detail": "This user already exists"}, status=401)
60+
else:
61+
user = User.objects.create(username=username, email=email)
62+
user.set_password(password)
63+
user.save()
64+
# payload = jwt_payload_handler(user)
65+
# token = jwt_encode_handler(payload)
66+
# response = jwt_response_payload_handler(token, user, request=request)
67+
# return Response(response, status=201)
68+
return Response({'detail': "Thank you for registering. Please verify your email."}, status=201)
69+
return Response({"detail": "Invalid Request"}, status=400)
70+
71+
72+
73+

src/db.sqlite3

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)