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(self, 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