separate user registration, login, logout from project/task logic
This commit is contained in:
parent
3b93dd8466
commit
912340593c
0
accounts/__init__.py
Normal file
0
accounts/__init__.py
Normal file
3
accounts/admin.py
Normal file
3
accounts/admin.py
Normal file
@ -0,0 +1,3 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
||||
6
accounts/apps.py
Normal file
6
accounts/apps.py
Normal file
@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class AccountsConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'accounts'
|
||||
0
accounts/migrations/__init__.py
Normal file
0
accounts/migrations/__init__.py
Normal file
3
accounts/models.py
Normal file
3
accounts/models.py
Normal file
@ -0,0 +1,3 @@
|
||||
from django.db import models
|
||||
|
||||
# Create your models here.
|
||||
3
accounts/tests.py
Normal file
3
accounts/tests.py
Normal file
@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
8
accounts/urls.py
Normal file
8
accounts/urls.py
Normal file
@ -0,0 +1,8 @@
|
||||
from django.urls import path
|
||||
from .views import UserRegistrationView, UserLoginView, UserLogoutView
|
||||
|
||||
urlpatterns = [
|
||||
path('api/login/', UserLoginView.as_view(), name='login'),
|
||||
path('api/logout/', UserLogoutView.as_view(), name='logout'),
|
||||
path('api/register/', UserRegistrationView.as_view(), name='welcome'),
|
||||
]
|
||||
31
accounts/views.py
Normal file
31
accounts/views.py
Normal file
@ -0,0 +1,31 @@
|
||||
# main/views.py
|
||||
from django.shortcuts import render, redirect, get_object_or_404
|
||||
from django.contrib.auth import authenticate, login, logout
|
||||
from rest_framework import status
|
||||
from rest_framework.views import APIView
|
||||
from rest_framework.response import Response
|
||||
|
||||
|
||||
class UserRegistrationView(APIView):
|
||||
def post(self, request):
|
||||
serializer = UserRegistrationSerializer(data=request.data)
|
||||
if serializer.is_valid():
|
||||
user = serializer.save()
|
||||
return Response({"id": user.id, "username": user.username}, status=status.HTTP_201_CREATED)
|
||||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
class UserLoginView(APIView):
|
||||
def post(self, request):
|
||||
username = request.data.get('username')
|
||||
password = request.data.get('password')
|
||||
user = authenticate(request, username=username, password=password)
|
||||
if user is not None:
|
||||
login(request, user)
|
||||
return Response({"message": "Login successful"}, status=status.HTTP_200_OK)
|
||||
return Response({error: "Invalid credentials"}, status=status.HTTP_401_UNAUTHORIZED)
|
||||
|
||||
class UserLogoutView(APIView):
|
||||
def post(self, request):
|
||||
logout(request)
|
||||
return Response({"message": "Logout successful"}, status=status.HTTP_200_OK)
|
||||
|
||||
@ -10,9 +10,6 @@ router.register(r'tasks', TaskViewSet)
|
||||
|
||||
urlpatterns = [
|
||||
path('api/', include(router.urls)),
|
||||
path('login/', UserLoginView.as_view(), name='login'),
|
||||
path('logout/', UserLogoutView.as_view(), name='logout'),
|
||||
path('register/', UserRegistrationView.as_view(), name='welcome'),
|
||||
path('projects/create/', create_project, name='create_project'),
|
||||
path('projects/<int:project_id>/', project_dashboard, name='project_dashboard'),
|
||||
path('projects/<int:project_id>/tasks/create/', create_task, name='create_task'),
|
||||
|
||||
@ -1,36 +1,10 @@
|
||||
# main/views.py
|
||||
from django.shortcuts import render, redirect, get_object_or_404
|
||||
from django.contrib.auth import authenticate, login, logout
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from .models import Project, Task, RegisteredUser
|
||||
from .forms import ProjectForm, TaskForm
|
||||
from rest_framework import status
|
||||
from rest_framework.views import APIView
|
||||
from rest_framework.response import Response
|
||||
|
||||
|
||||
class UserRegistrationView(APIView):
|
||||
def post(self, request):
|
||||
serializer = UserRegistrationSerializer(data=request.data)
|
||||
if serializer.is_valid():
|
||||
user = serializer.save()
|
||||
return Response({"id": user.id, "username": user.username}, status=status.HTTP_201_CREATED)
|
||||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
class UserLoginView(APIView):
|
||||
def post(self, request):
|
||||
username = request.data.get('username')
|
||||
password = request.data.get('password')
|
||||
user = authenticate(request, username=username, password=password)
|
||||
if user is not None:
|
||||
login(request, user)
|
||||
return Response({"message": "Login successful"}, status=status.HTTP_200_OK)
|
||||
return Response({error: "Invalid credentials"}, status=status.HTTP_401_UNAUTHORIZED)
|
||||
|
||||
class UserLogoutView(APIView):
|
||||
def post(self, request):
|
||||
logout(request)
|
||||
return Response({"message": "Logout successful"}, status=status.HTTP_200_OK)
|
||||
|
||||
# create project
|
||||
@login_required
|
||||
|
||||
@ -40,6 +40,7 @@ INSTALLED_APPS = [
|
||||
'django.contrib.sessions',
|
||||
'django.contrib.messages',
|
||||
'django.contrib.staticfiles',
|
||||
'accounts',
|
||||
'main',
|
||||
]
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user