separate user registration, login, logout from project/task logic

This commit is contained in:
= 2025-01-12 11:06:42 -05:00
parent 3b93dd8466
commit 912340593c
11 changed files with 55 additions and 29 deletions

0
accounts/__init__.py Normal file
View File

3
accounts/admin.py Normal file
View File

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

6
accounts/apps.py Normal file
View File

@ -0,0 +1,6 @@
from django.apps import AppConfig
class AccountsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'accounts'

View File

3
accounts/models.py Normal file
View File

@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

3
accounts/tests.py Normal file
View File

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

8
accounts/urls.py Normal file
View 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
View 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)

View File

@ -10,9 +10,6 @@ router.register(r'tasks', TaskViewSet)
urlpatterns = [ urlpatterns = [
path('api/', include(router.urls)), 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/create/', create_project, name='create_project'),
path('projects/<int:project_id>/', project_dashboard, name='project_dashboard'), path('projects/<int:project_id>/', project_dashboard, name='project_dashboard'),
path('projects/<int:project_id>/tasks/create/', create_task, name='create_task'), path('projects/<int:project_id>/tasks/create/', create_task, name='create_task'),

View File

@ -1,36 +1,10 @@
# main/views.py # main/views.py
from django.shortcuts import render, redirect, get_object_or_404 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 django.contrib.auth.decorators import login_required
from .models import Project, Task, RegisteredUser from .models import Project, Task, RegisteredUser
from .forms import ProjectForm, TaskForm 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 # create project
@login_required @login_required

View File

@ -40,6 +40,7 @@ INSTALLED_APPS = [
'django.contrib.sessions', 'django.contrib.sessions',
'django.contrib.messages', 'django.contrib.messages',
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'accounts',
'main', 'main',
] ]