def register(request):
    if request.method == 'POST':
        # return HttpResponse(request.POST.get('password1'))
        form = UserRegistrationForm(request.POST)
        # profileForm = UserUpdateForm(request.POST)
        if form.is_valid():
            userTable = models.User()
            userTable.first_name = request.POST['first_name']
            userTable.last_name = request.POST['last_name']
            userTable.username = request.POST['username']
            userTable.email = request.POST['email']
            userTable.password = make_password(request.POST.get('password1'))
            userTable.save()

            friendTable = Friends()
            friendTable.current_user = userTable
            friendTable.save()

            username = form.cleaned_data.get('username')
            messages.success(
                request,
                f'Your account has been created! You are now able to login!')
            messages.warning(
                request,
                f'Please login to your account and update profile using [Edit Profile] option!'
            )
            return redirect('login')
    else:
        form = UserRegistrationForm()
        # profileForm = UserUpdateForm()
    return render(request, 'user/registration.html', {'form': form})
Exemplo n.º 2
0
 def create(cls, validated_data):
     # create friend
     
     try:
         author = validated_data["author"]   # person who received the request
         friend = validated_data["friend"]   # person who made the request
         
         if (Friends.objects.filter(author=author, friend=friend).exists()):
             # the relation already exists in the database
             raise ValueError("Author is already friends with this friend")
         
         if ((User.objects.filter(id=author).exists()) and (User.objects.filter(id=friend).exists())):
             # verify users (author + friend) exists
             authorUser = User.objects.get(id=author)
             friendUser = User.objects.get(id=friend)
             friend1 = Friends(author=authorUser, friend=friendUser)
             friend2 = Friends(author=friendUser, friend=authorUser)
             friend1.save()
             friend2.save()
     except:
         raise RuntimeError("Unable to create friend")