From a103ac22f5999fab628efad891d08559450b5d30 Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 28 Feb 2024 17:33:16 +0100 Subject: [PATCH] Working on registration endpoint and authentication in general --- api_v1/admin.py | 2 +- api_v1/auth/registration_view.py | 25 +++++++++++++++++++++++++ api_v1/models.py | 2 +- api_v1/urls.py | 8 ++++++++ api_v1/user/__init__.py | 2 -- api_v1/user/models/__init__.py | 1 - api_v1/user/user_services.py | 2 +- mixr_api/urls.py | 3 ++- 8 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 api_v1/auth/registration_view.py create mode 100644 api_v1/urls.py diff --git a/api_v1/admin.py b/api_v1/admin.py index c818c3f..c4fee19 100644 --- a/api_v1/admin.py +++ b/api_v1/admin.py @@ -1,5 +1,5 @@ from django.contrib import admin -from .user.models import * +from .user.models.user_information import UserInformation # Register your models here. admin.site.register(UserInformation) diff --git a/api_v1/auth/registration_view.py b/api_v1/auth/registration_view.py new file mode 100644 index 0000000..22a6653 --- /dev/null +++ b/api_v1/auth/registration_view.py @@ -0,0 +1,25 @@ +from django.views import View +from django.contrib.auth.models import User +from django.http import HttpResponse +from api_v1.user.user_services import acreate_user_information + + +class RegistrationView(View): + async def post(self, request): + # Read parameters from body + username = request.POST.get('username') + password = request.POST.get('password') + email = request.POST.get('email') + display_name = request.POST.get('display_name') + + # Check if user already exists + if User.objects.filter(username=username).exists(): + return HttpResponse(status=400, content="User already exists") + + # Create user + user = await User.objects.acreate_user(username, email, password) + + # Create UserInformation + await acreate_user_information(user, display_name=display_name) + + return HttpResponse(status=201, content="User created") diff --git a/api_v1/models.py b/api_v1/models.py index 152f52c..460966d 100644 --- a/api_v1/models.py +++ b/api_v1/models.py @@ -1 +1 @@ -from .user.models import * +from .user.models.user_information import UserInformation diff --git a/api_v1/urls.py b/api_v1/urls.py new file mode 100644 index 0000000..97b223c --- /dev/null +++ b/api_v1/urls.py @@ -0,0 +1,8 @@ +from django.urls import path, include +from .auth.registration_view import RegistrationView + +# Assign register url +urlpatterns = [ + path("auth/", include("django.contrib.auth.urls")), + path('auth/register/', RegistrationView.as_view(), name='register'), +] diff --git a/api_v1/user/__init__.py b/api_v1/user/__init__.py index 1e010ce..e69de29 100644 --- a/api_v1/user/__init__.py +++ b/api_v1/user/__init__.py @@ -1,2 +0,0 @@ -from .models import * -from .user_services import * \ No newline at end of file diff --git a/api_v1/user/models/__init__.py b/api_v1/user/models/__init__.py index 130f2ae..e69de29 100644 --- a/api_v1/user/models/__init__.py +++ b/api_v1/user/models/__init__.py @@ -1 +0,0 @@ -from .user_information import UserInformation \ No newline at end of file diff --git a/api_v1/user/user_services.py b/api_v1/user/user_services.py index 7972292..ea2141f 100644 --- a/api_v1/user/user_services.py +++ b/api_v1/user/user_services.py @@ -1,4 +1,4 @@ -from .models import UserInformation +from .models.user_information import UserInformation from django.contrib.auth.models import User from typing import Optional diff --git a/mixr_api/urls.py b/mixr_api/urls.py index dc54bff..ddcdb0c 100644 --- a/mixr_api/urls.py +++ b/mixr_api/urls.py @@ -15,8 +15,9 @@ Including another URLconf 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin -from django.urls import path +from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), + path("v1/", include("api_v1.urls")), ]