mixr-api/api_v1/auth/registration_view.py

26 lines
914 B
Python
Raw Normal View History

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")