Moved some views around
This commit is contained in:
parent
18033eeb34
commit
62138e0d26
@ -1,8 +1,5 @@
|
|||||||
# main/api_views.py
|
# main/api_views.py
|
||||||
from rest_framework import viewsets, status
|
from rest_framework import viewsets
|
||||||
from rest_framework.views import APIView
|
|
||||||
from rest_framework.response import Response
|
|
||||||
from django.contrib.auth import authenticate, login, logout
|
|
||||||
from .models import Project, Task, RegisteredUser
|
from .models import Project, Task, RegisteredUser
|
||||||
from .serializers import ProjectSerializer, TaskSerializer, UserRegistrationSerializer
|
from .serializers import ProjectSerializer, TaskSerializer, UserRegistrationSerializer
|
||||||
|
|
||||||
@ -19,25 +16,4 @@ class AccountViewSet(viewsets.ModelViewSet):
|
|||||||
queryset = RegisteredUser.objects.all()
|
queryset = RegisteredUser.objects.all()
|
||||||
serializer_class = UserRegistrationSerializer
|
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)
|
|
||||||
|
|||||||
10
main/urls.py
10
main/urls.py
@ -1,8 +1,8 @@
|
|||||||
# main/urls.py
|
# 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 django.urls import path, include
|
||||||
from rest_framework.routers import DefaultRouter
|
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 = DefaultRouter()
|
||||||
router.register(r'projects', ProjectViewSet)
|
router.register(r'projects', ProjectViewSet)
|
||||||
@ -11,9 +11,9 @@ router.register(r'accounts', AccountViewSet)
|
|||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('api/', include(router.urls)),
|
path('api/', include(router.urls)),
|
||||||
path('accounts/login/', UserLoginView, name='login'),
|
path('accounts/login/', UserLoginView.as_view(), name='login'),
|
||||||
path('accounts/logout/', UserLogoutView, name='logout'),
|
path('accounts/logout/', UserLogoutView.as_view(), name='logout'),
|
||||||
path('accounts/register/', UserRegistrationView, name='welcome'),
|
path('accounts/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'),
|
||||||
|
|||||||
@ -1,10 +1,37 @@
|
|||||||
# 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
|
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
|
||||||
def create_project(request):
|
def create_project(request):
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user