from django.views.generic import FormView, View from django.http import JsonResponse from django.contrib.auth import get_user_model, logout from .forms import LearnerRegistrationForm User = get_user_model() class LearnerRegistrationView(FormView): form_class = LearnerRegistrationForm template_name = "auth/registration-learner.html" def form_valid(self, form): first_name = form.cleaned_data['first_name'] last_name = form.cleaned_data['last_name'] email = form.cleaned_data['email'] password = form.cleaned_data['password'] User.objects.create_user(username=email, email=email, first_name=first_name, last_name=last_name, password=password) return JsonResponse({"success": True}, status=201) def form_invalid(self, form): return JsonResponse(form.errors.get_json_data(escape_html=True), status=400) class LogoutView(View): def post(self, request, *args, **kwargs): logout(request) return JsonResponse({"success": True})