from django.shortcuts import render, redirect from django.views.generic import View from django.contrib import messages from .forms import UserRegisterForm from .models import UserAccount from gauth.models import Location class RegistrationView(View): def get(self, request, *args, **kwargs): form = UserRegisterForm() return render(request, 'signup.html', {'form': form}) def post(self, request, *args, **kwargs): form = UserRegisterForm(request.POST) if form.is_valid(): email = form.cleaned_data.get('email') location = Location.objects.filter(recipient_email=email).first() if location: user = form.save() UserAccount.objects.create(user=user, location=location) messages.success(request, f'Your account has been created! You are now able to log in') return redirect('login') else: messages.warning(request, f'Sorry, We don\'t have any location with this email!') return render(request, 'signup.html', {'form': form})