Fakultas Ilmu Komputer UI

Commit 5266437e authored by Jonathan Christopher Jakub's avatar Jonathan Christopher Jakub
Browse files

[GREEN] Implement handler for existing user on account creation

parent 2089805a
Pipeline #43984 failed with stages
in 2 minutes and 38 seconds
......@@ -51,13 +51,27 @@ class AccountViewSet(viewsets.ModelViewSet):
return AccountRegisterSerializer
return AccountSerializer
def perform_create(self, serializer):
def create(self, request):
serializer_class = self.get_serializer_class()
serializer = serializer_class(data=request.data)
serializer.is_valid(raise_exception=True)
username = serializer.validated_data.pop("username").lower()
password = serializer.validated_data.pop("password")
if User.objects.filter(username=username).exists():
return Response(
{"username" : "User with that username already exists"},
status=status.HTTP_409_CONFLICT
)
user = User.objects.create_user(username=username, password=password)
Account.objects.create(user=user, **serializer.validated_data)
return Response(
serializer.data, status=status.HTTP_201_CREATED,
)
def perform_destroy(self, instance):
instance.delete(author=self.request.user.account)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment