def temp_account(request):
    # Use the RegistrationForm to validate the generated user for safety
    charspace = u'abcdefghijklmnopqrstuvwxyz0123456789'
    username = "******"+get_random_string(length=4, allowed_chars=charspace)

    while User.objects.filter(username__exact=username):
        if attempts >= 100:
            # this should be extremely unlikely, probably indicates a bug
            return Http404()
        username="******"+get_random_string(length=4, allowed_chars=charspace)

    form = RegistrationForm({"username": username,
                             "password1": username,
                             "password2": username,
                             "first_name": "Temporary",
                             "last_name": "User",
                             "email": username+"@mailinator.com"
                            })

    # Validates the form.
    if not form.is_valid():
         # this should never happen, and means that something is broken
        return Http404()

    new_user = User.objects.create_user(username=form.cleaned_data['username'], 
                                        password=form.cleaned_data['password1'],
                                        first_name=form.cleaned_data['first_name'],
                                        last_name=form.cleaned_data['last_name'],
                                        email=form.cleaned_data['email'])
    new_user.save()


    new_user_profile = UserProfile(user=new_user)
    new_user_profile.save()

    new_script = Script(userprofile=new_user_profile)
    new_script.save()

    # Logs in the new user and redirects to global stream
    new_user = authenticate(username=form.cleaned_data['username'],
                            password=form.cleaned_data['password1'])
    login(request, new_user)
    
    return redirect(reverse('stream'))
def register(request):
    context = {}

    # Just display the registration form if this is a GET request
    if request.method == 'GET':
        context['form']= RegistrationForm()
        return render(request, 'socialnetwork/register.html',context)

    form = RegistrationForm(request.POST)
    context['form'] = form

    # Validates the form.
    if not form.is_valid():
        return render(request, 'socialnetwork/register.html', context)

    # At this point, the form data is valid.  Register and login the user.
    new_user = User.objects.create_user(username=form.cleaned_data['username'], 
                                        password=form.cleaned_data['password1'],
                                        first_name=form.cleaned_data['first_name'],
                                        last_name=form.cleaned_data['last_name'],
                                        email=form.cleaned_data['email'])
    # new_user.is_active = False
    new_user.save()


    new_user_profile = UserProfile(user=new_user)
    new_user_profile.save()

    new_script = Script(userprofile=new_user_profile)
    new_script.save()


    #### Email verification
    # 
    #  # Generate a one-time use token and an email message body
    # token = default_token_generator.make_token(new_user)

    # email_body = """
    # Welcome to the Social Network.  Please click the link below to
    # verify your email address and complete the registration of your account:
    # http://%s%s
    # """ % (request.get_host(), 
    #    reverse('confirm_registration', args=[new_user.username, token]))

    # send_mail(subject="Verify your email address",
    #           message= email_body,
    #           from_email="*****@*****.**",
    #           recipient_list=[new_user.email])

    # context['email'] = form.cleaned_data['email']

    # return render(request, 'socialnetwork/needs-confirmation.html',context)



    # Logs in the new user and redirects to global stream
    new_user = authenticate(username=form.cleaned_data['username'],
                            password=form.cleaned_data['password1'])
    login(request, new_user)


    return redirect(reverse('stream'))