示例#1
0
  def profile(request):
    '''The profile page containing

    * password change form;
    * forms to associate and dissociate OpenID's;
    * a button to delete account.

    **Authentication restrictions**

    If a user that is not logged in tries to visit this page, he will
    be redirected to the login page. If he successfully logs in there,
    he'll be redirected back here.

    **Templates**

    `"authentication/profile/profile.html"`
      Page template.

      **Context**

      ``form_change_password``
        A :class:`django.contrib.auth.forms.SetPasswordForm`.
      ``form_add_openid_msg``
        Form errors for ``form_add_openid`` (see below).
      ``form_add_openid``
        An :class:`django_authopenid.forms.AssociateOpenID`.
      ``form_delete_openid``
        An :class:`authentication.forms.DeleteOpenidForm`.
      ``form_delete``
        An :class:`authentication.forms.DeleteAccountForm`.

    `"authentication/profile/password_changed.html"`
      Templates rendered after the change password form was submitted and user's
      password was successfull changed.

      **Context**

      ``next``
        The link for previous page tracking or ``''`` if it is unknown
        (i.e., was not passed);
    '''
    from django.contrib.auth.forms import SetPasswordForm
    from django_authopenid.forms import AssociateOpenID
    from forms import DeleteOpenidForm, DeleteAccountForm

    redirect_to = next = request.GET.get('next')
    if not redirect_to or '://' in redirect_to:
      redirect_to = settings.LOGIN_REDIRECT_URL
      next = ''

    form_change_password = SetPasswordForm(request.user)
    form_add_openid_msg = ''
    form_add_openid = AssociateOpenID(request.user)
    form_delete_openid = DeleteOpenidForm.new(request.user)
    form_delete = DeleteAccountForm()
    if request.method == 'POST':
      if 'new_password1' in request.POST.keys():
        form_change_password = SetPasswordForm(request.user, request.POST)
        if form_change_password.is_valid():
          request.user.set_password(form_change_password.cleaned_data['new_password1'])
          request.user.save()
          return render_to_response2(request, 'authentication/profile/password_changed.html', {'next': next})
      elif 'openid_url' in request.POST.keys():
        form_add_openid = AssociateOpenID(request.user, request.POST)
        if form_add_openid.is_valid():
          openid_url = form_add_openid.cleaned_data['openid_url']
          redirect_url = "%s?%s" % (
            request.build_absolute_uri(request.path),
            urllib.urlencode({'next': redirect_to, 'openid_url': openid_url})
          )
          try:
            return Overrides.Authopenid.ask_openid(request, openid_url, redirect_url)
          except Overrides.Authopenid.OpenidError, e:
            form_add_openid_msg = unicode(e)

      elif 'openid_url_to_delete' in request.POST.keys():
        form_delete_openid = DeleteOpenidForm.process(request.user, request.POST)
      else:
        form_delete = DeleteAccountForm(request.POST)
        if form_delete.is_valid():
          request.user.delete()
          auth.logout(request)
          return HttpResponseRedirect(redirect_to)
示例#2
0
        if form_delete.is_valid():
          request.user.delete()
          auth.logout(request)
          return HttpResponseRedirect(redirect_to)

    elif 'openid_url' in request.GET:
      try:
        Overrides.Authopenid.complete(request)
      except Overrides.Authopenid.OpenidError, e:
        form_add_openid_msg = unicode(e)
        form_add_openid = AssociateOpenID(request.user,
          initial={'openid_url': request.GET['openid_url']})
      else:

        UserAssociation(openid_url=request.GET['openid_url'], user_id=request.user.id).save(send_email=False)
        form_delete_openid = DeleteOpenidForm.new(request.user)

    return render_to_response2(request, 'authentication/profile/profile.html', {
      'form_change_password': form_change_password,
      'form_add_openid_msg': form_add_openid_msg,
      'form_add_openid': form_add_openid,
      'form_delete_openid': form_delete_openid,
      'form_delete': form_delete,
    })

class Logout:

  @staticmethod
  def logout(request):
    '''Log out.