Moved some views around

This commit is contained in:
= 2025-01-11 21:25:12 -05:00
parent 18033eeb34
commit 62138e0d26
3 changed files with 34 additions and 31 deletions

View File

@ -1,8 +1,5 @@
# main/api_views.py
from rest_framework import viewsets, status
from rest_framework.views import APIView
from rest_framework.response import Response
from django.contrib.auth import authenticate, login, logout
from rest_framework import viewsets
from .models import Project, Task, RegisteredUser
from .serializers import ProjectSerializer, TaskSerializer, UserRegistrationSerializer
@ -19,25 +16,4 @@ class AccountViewSet(viewsets.ModelViewSet):
queryset = RegisteredUser.objects.all()
serializer_class = UserRegistrationSerializer
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

@ -1,8 +1,8 @@
# main/urls.py
from .views import create_project, project_dashboard, create_task
from .views import create_project, project_dashboard, create_task, UserRegistrationView, UserLoginView, UserLogoutView
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from .api_views import ProjectViewSet, TaskViewSet, AccountViewSet, UserRegistrationView, UserLoginView, UserLogoutView
from .api_views import ProjectViewSet, TaskViewSet, AccountViewSet
router = DefaultRouter()
router.register(r'projects', ProjectViewSet)
@ -11,9 +11,9 @@ router.register(r'accounts', AccountViewSet)
urlpatterns = [
path('api/', include(router.urls)),
path('accounts/login/', UserLoginView, name='login'),
path('accounts/logout/', UserLogoutView, name='logout'),
path('accounts/register/', UserRegistrationView, name='welcome'),
path('accounts/login/', UserLoginView.as_view(), name='login'),
path('accounts/logout/', UserLogoutView.as_view(), name='logout'),
path('accounts/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'),

View File

@ -1,10 +1,37 @@
# 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
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
def create_project(request):