mixr-api/api_v1/auth/registration_view.py

25 lines
908 B
Python

from django.views import View
from django.contrib.auth.models import User
from django.http import HttpResponse
from api_v1.profile.profile_services import acreate_profile
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 profile already exists
if User.objects.filter(username=username).exists():
return HttpResponse(status=400, content="User already exists")
# Create profile
user = await User.objects.acreate_user(username, email, password)
# Create UserInformation
await acreate_profile(user, display_name=display_name)
return HttpResponse(status=201, content="User created")