1. Benoit Chesneau
  2. django-authopenid

Commits

benoitc  committed 8e9499c

add possibility to change password.

  • Participants
  • Parent commits b5bdd2e
  • Branches trunk

Comments (0)

Files changed (5)

File django_authopenid/models.py

View file
 from django.contrib.auth.models import User
 from django.db import models
 
-
-
 class Nonce(models.Model):
     """ openid nonce """
     server_url = models.CharField(max_length=255)
     def __unicode__(self):
         return u"Nonce: %s" % self.id
 
-    
 class Association(models.Model):
     """ association openid url and lifetime """
     server_url = models.TextField(max_length=2047)

File django_authopenid/urls.py

View file
 from django.conf.urls.defaults import patterns, url
 from django.utils.translation import ugettext as _
 
-urlpatterns = patterns('django_authopenid.views',
+from django.contrib.auth import views as auth_views
+from django_authopenid import views as oid_views
+
+urlpatterns = patterns('',
     # yadis rdf
-    url(r'^yadis.xrdf$', 'xrdf', name='yadis_xrdf'),
-     # manage account registration
-    url(r'^signin/$', 'signin', name='user_signin'),
-    url(r'^signout/$', 'signout', name='user_signout'),
-    url(r'^signin/complete/$', 'complete_signin', name='user_complete_signin'),
-    url(r'^register/$', 'register', name='user_register'),
+    url(r'^yadis.xrdf$', oid_views.xrdf, name='oid_views.yadis_xrdf'),
+    
+    # user profile
+    url(r'^password/$',oid_views.password_change, name='auth_password_change'),
+    
+    # manage account registration
+    url(r'^register/$', oid_views.register, name='user_register'),
+    url(r'^signin/$', oid_views.signin, name='user_signin'),
+    url(r'^signout/$', oid_views.signout, name='user_signout'),
+    url(r'^signin/complete/$', oid_views.complete_signin, name='user_complete_signin'),
+  
 )

File django_authopenid/views.py

View file
 #
 from django.conf import settings
 from django.contrib.auth import REDIRECT_FIELD_NAME
-from django.contrib.auth.forms import AuthenticationForm
+from django.contrib.auth.forms import *
 from django.contrib.auth.models import User
 from django.contrib.auth import login, logout
 from django.contrib.auth.decorators import login_required
     return is_exist
     
 def register_account(form):
-    tmp_pwd = User.objects.make_random_password()
-    user_ = User.objects.create_user(form.cleaned_data['username'],
-             form.cleaned_data['email'], tmp_pwd)
+    user_ = User.objects.create_user(form.cleaned_data['username'], form.cleaned_data['email'])
     user_.backend = "django.contrib.auth.backends.ModelBackend"
     return user_
 
     ]
     return render(template_name, { 
         'return_to': return_to 
-        }, context_instance=RequestContext(request))
+        }, context_instance=RequestContext(request))
+        
+        
+@login_required
+def password_change(request, template_name='authopenid/password_change_form.html', 
+        set_password_form=SetPasswordForm, change_password_form=PasswordChangeForm,
+        post_change_redirect=None):
+    """
+    View that allow the user to set a password. Only 
+    """
+    if post_change_redirect is None:
+        post_change_redirect = settings.LOGIN_REDIRECT_URL
+
+    set_password = False
+    if request.user.has_usable_password():
+        change_form = change_password_form
+    else:
+        set_password = True
+        change_form = set_password_form
+
+    if request.POST:
+        form = change_form(request.user, request.POST)
+        print form.__class__.__name__
+        print form.is_valid()
+        if form.is_valid():
+            form.save()
+            msg = urllib.quote(_("Password changed"))
+            redirect_to = "%s?%s" % (post_change_redirect, 
+                                urllib.urlencode({ "msg": msg }))
+            print redirect_to
+            return HttpResponseRedirect(redirect_to)
+    else:
+        
+        form = change_form(request.user)
+        print form.__class__.__name__
+
+    return render(template_name, {
+        'form': form,
+        'set_password': set_password
+    }, context_instance=RequestContext(request))

File djauthaupenid_example/settings.py

View file
     os.path.join(PROJECT_PATH, 'templates'),
 )
 
+LOGIN_REDIRECT_URL = '/'
+
 INSTALLED_APPS = (
     'django.contrib.auth',
     'django.contrib.contenttypes',

File djauthaupenid_example/templates/home.html

View file
 {% extends "base.html" %}
 
 {% block content %}
-    <h1>Django AuthOpenID test</h1>
-    
-    <p>To test go <a href="/account/signin">here</a>.</p>
+<h1>Django AuthOpenID test</h1>
+
+<p>Welcome {{ user }}</p>
+
+{% if user.is_authenticated%}
+<ul>
+	<li><a href="{% url auth_password_change %}">Change password</a></li>
+  	<li><a href="{% url user_signout %}">signout</a></li>
+</ul>
+{% else%}    
+<p> <a href="{% url user_signin %}">signin</a></p>
+{% endif %}
+
 {% endblock %}