def register(request): """ Handles user registration using UserForm from forms.py Creates new User and new RedditUser models if appropriate data has been supplied. If account has been created user is redirected to login page. """ user_form = UserForm() if request.user.is_authenticated(): messages.warning(request, 'You are already registered and logged in.') return render(request, 'public/register.html', {'form': user_form}) if request.method == "POST": user_form = UserForm(request.POST) if user_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() reddit_user = RedditUser() reddit_user.user = user reddit_user.save() user = authenticate(username=request.POST['username'], password=request.POST['password']) login(request, user) return redirect('Frontpage') return render(request, 'public/register.html', {'form': user_form})
def get_or_create_author(username): try: user = User.objects.get(username=username) author = RedditUser.objects.get(user=user) except (User.DoesNotExist, RedditUser.DoesNotExist): print "Creating user {}".format(username) new_author = User(username=username) new_author.set_password(username) new_author.save() author = RedditUser(user=new_author) author.save() return author
def register(request): """ Handles user registration using UserForm from forms.py Creates new User and new RedditUser models if appropriate data has been supplied. If account has been created user is redirected to login page. """ user_form = UserForm() if request.user.is_authenticated(): messages.warning(request, 'You are already registered and logged in.') return render(request, 'public/register.html', {'form': user_form}) if request.method == "POST": user_form = UserForm(request.POST) if user_form.is_valid(): # check if registration code is valid cohorts = map(lambda x: x.registration_code, list(Cohort.objects.all())) if user_form.cleaned_data['cohort_registration_code'] not in cohorts: user_form = UserForm() messages.warning(request, 'Invalid registration code. Try again!') return render(request, 'public/register.html', {'form': user_form}) user = user_form.save() user.set_password(user.password) user.save() reddit_user = RedditUser() reddit_user.user = user reddit_user.save() # add user to cohort that corresponds to regitrations code cohort = Cohort.objects.get( registration_code=user_form.cleaned_data['cohort_registration_code'] ) reddit_user.cohorts.add(cohort) reddit_user.email = user.email reddit_user.save() user = authenticate(username=request.POST['username'], password=request.POST['password']) login(request, user) return redirect('Frontpage') return render(request, 'public/register.html', {'form': user_form})