Commits

offline committed b6a0244

confirmation is now seperate package. Please drop openauth_confirmation table and run syncdb

Comments (0)

Files changed (9)

 *.pyc
 django_openauth.egg-info
 *.kpf
+openauth/.project
+openauth/.pydevproject

openauth/__init__.py

+from openauth import signals
 from openauth import settings

openauth/admin.py

 from django.contrib import admin
-from openauth.models import Confirmation, OpenID
+from openauth.models import OpenID
 
-class ConfirmationAdmin(admin.ModelAdmin):
-    pass
+
 
 class OpenIDAdmin(admin.ModelAdmin):
     list_display = ['user', 'url']
 
+admin.site.register(OpenID, OpenIDAdmin)
 
-admin.site.register(OpenID, OpenIDAdmin)
-admin.site.register(Confirmation, ConfirmationAdmin)
 

openauth/forms.py

 from django import forms
 from django.contrib.auth.models import User
-from django.contrib.auth import authenticate, login
+from django.contrib.auth import login
 from django.utils.translation import ugettext as _
-from django.template import loader
 
 from annoying.functions import get_object_or_None
 from annoying.decorators import autostrip
 
 import openauth
-from openauth.utils import uri_to_username, str_to_class
-from openauth.models import Confirmation, OpenID
+from openauth.utils import uri_to_username
+from openauth.models import OpenID
 
 
 
 class RegistrationForm(forms.Form):
 
     if openauth.settings.DISPLAYNAME_REQUIRED:
-        username = UsernameField(login=False)
-    email = forms.EmailField(label=_('Email'))
+        username = forms.CharField()
+    email = UsernameField(login=True, label=_('Email'))
     password = PasswordField(label=_('Password'))
     password_dup = PasswordField(label=_('Password (confirmation)'))
 
 
     def save(self):
         self.user.username = self.cleaned_data['username']
-        user.save()
-        return user
+        self.user.save()
+        return self.user
 
 EditUserForm = autostrip(EditUserForm)

openauth/models.py

 from django.db import models
 from django.contrib.auth.models import User
 
-from annoying.fields import AutoOneToOneField
-
 import openauth
-from openauth.utils import unicode_urlencode, generate_hash
-
 
 
 User.username = models.CharField(max_length=30, unique=openauth.settings.UNIQUE_USERNAME)
 
-
-class Confirmation(models.Model):
-    """
-    Can be used for user activation, but also for
-    other purposes where confirmation is required.
-    """
-    user = models.ForeignKey(User)
-    key = models.CharField(max_length=32)
-    confirmation_type = models.IntegerField(default=1)
-
-    def __unicode__(self):
-        """
-        return:
-
-        vasya@pupkin: 36799bbfa2d074b5ffc1e8a55fb7f539
-        """
-        return "%s: %s" % (self.user.email, self.key)
-
-
-
-
-
 class OpenID(models.Model):
     user = models.ForeignKey(User)
     url = models.CharField(u'OpenID URL', max_length=255, unique=True)

openauth/settings.py

 
 
 MESSAGE_TYPE = get_config("MESSAGE_TYPE", "flash")
-CUSTOM_CONFIRMATION = get_config("CUSTOM_CONFIRMATION", None) # custom confirmation function
 REGISTRATION_ALLOWED = get_config("REGISTRATION_ALLOWED", True)
 REGISTRATION_FORM = get_config("REGISTRATION_FORM", "openauth.forms.RegistrationForm")
 EDIT_USER_FORM = get_config("EDIT_USER_FORM", "openauth.forms.EditUserForm")

openauth/signals.py

+from django.contrib import auth 
+
+from annoying.decorators import signals
+from confirmation.models import Confirmation
+from confirmation.signals import successfull_confirmation
+
+import openauth
+
+
+
+@signals(successfull_confirmation, sender=Confirmation)
+def sighandler(request, instance, confirmation_type, **kwargs):
+    if confirmation_type == openauth.settings.ACTIVATION_CONFIRMATION_TYPE:
+        user = instance
+        user.is_active = True
+        user.save()
+        if openauth.settings.ACTIVATION_AUTO_LOGIN:
+            user.backend = 'django.contrib.auth.backends.ModelBackend'
+            auth.login(request, user)
+            
+    elif confirmation_type == openauth.settings.PASSWORD_RESET_CONFIRMATION_TYPE:
+        user.backend = 'django.contrib.auth.backends.ModelBackend'
+        auth.login(request, user)    
+
 from django.conf.urls.defaults import *
-from django.views.generic.simple import direct_to_template
 
 from openauth import views
 
     url(r'^logout/$', views.logout, name='openauth-logout'),
     url(r'^reset_password/$', views.reset_password, name='openauth-reset-password'),
     url(r'^change_password/$', views.change_password, name='openauth-change-password'),
-    url(r'^confirm/$', views.confirm, name='openauth-confirm'),
     url(r'^user/$', views.show_user, name='openauth-show-user'),
     url(r'^user/(\d+)/$', views.show_user, name='openauth-show-user'),
     url(r'^users/$', views.users, name='openauth-show-users'),

openauth/views.py

 
 from openid.consumer import consumer, discover
 from openid.extensions import sreg, ax
-from openid import oidutil
-
 
 from django.contrib.auth.models import User
 from django.core.urlresolvers import reverse
 from django.utils.translation import ugettext as _
 
 from annoying.decorators import render_to
+from confirmation.models import Confirmation
 
 import openauth
 from openauth.forms import ResetPasswordForm, NewPasswordForm, LoginForm, \
-     EditUserForm, OpenIDLoginForm, OpenIDRegistrationForm
+     OpenIDLoginForm, OpenIDRegistrationForm
 from openauth.utils import email_template, build_redirect_url, str_to_class, \
     generate_hash, activation_required, message
-from openauth.models import Confirmation
+from openauth.models import OpenID
 
 
 
-@render_to()
-def confirm(request):
-    """
-    Check confirmation key, proccess confirmation and delete key.
-    """
-    key = request.GET.get("key")
-    try:
-        confirmation_type = int(request.GET.get("type", 1))
-    except ValueError:
-        return message(request, _("Confirmation error: incorrect type"), "error", "/")
-    confirmation = get_object_or_404(Confirmation, key=key, confirmation_type=confirmation_type)
-    if confirmation_type == openauth.settings.ACTIVATION_CONFIRMATION_TYPE:
-        user = confirmation.user
-        user.is_active = True
-        user.save()
-        if openauth.settings.ACTIVATION_AUTO_LOGIN:
-            confirmation.user.backend = 'django.contrib.auth.backends.ModelBackend'
-            auth.login(request, user)
-
-        confirmation.delete()
-        return message(request, _('Your account successfully activated'), 'success', 'openauth-show-user', args=[user.id])
-    elif confirmation_type == openauth.settings.PASSWORD_RESET_CONFIRMATION_TYPE:
-        confirmation.user.backend = 'django.contrib.auth.backends.ModelBackend'
-        auth.login(request, confirmation.user)      
-        confirmation.delete()
-        return message(request, _("Now you can change your password"), "notice", "openauth-change-password")
-    else:
-        return openauth.settings.CUSTOM_CONFIRMATION(request)
 
 
 @render_to('openauth/registration.html')
             user.save()
 
             if openauth.settings.ACTIVATION_REQUIRED:
-                confirmation = Confirmation(user=user, key=generate_hash(), confirmation_type=openauth.settings.ACTIVATION_CONFIRMATION_TYPE)
-                confirmation.save()
-                url = request.build_absolute_uri(reverse('openauth-confirm'))
+                confirmation = Confirmation.objects.create(user, confirmation_type=openauth.settings.ACTIVATION_CONFIRMATION_TYPE)
+                url = request.build_absolute_uri(reverse('confirmation'))
                 url += '?key=%s&type=%s' % (confirmation.key, confirmation.confirmation_type)
                 params = {'domain': request.get_host(), 'email': user.email, 'url': url}
                 if email_template(user.email, 'openauth/mail/registration.txt', **params):
                                                 return_to=return_to)
             return redirect(redirect_url)
 
-        except discover.DiscoveryFailure, ex:
+        except discover.DiscoveryFailure:
             error = _('Could not find OpenID server')
             form.errors['openid_url'] = [error]
 
             if user:
                 if user.is_active:
                     auth.login(request, user)
-                    error = u'You have saccessfully authorized via OpenID'
+                    error = u'You have successfully authorized via OpenID'
                     redirect_url = build_redirect_url(request, openauth.settings.LOGIN_REDIRECT_URL)
                     return redirect(redirect_url)
                 else:
 
 
 
-class EditUser(object):
-
-    def __call__(self, request, user_id):
-        self.request = request
-        self.user_id = user_id
-        self.user = get_object_or_404(User, pk=user_id)
-        if self.request.POST:
-            return self.proceed_form()
-        else:
-            return self.create_form()
-
-
-    def permissions(self):
-        if not self.request.user.is_authenticated() or self.user != self.request.user and not self.request.user.is_staff:
-            return HttpResponseReload(request)
-
-    def proceed_form(self):
-        form = EditUserForm(self.user, self.request.POST, self.request.FILES)
-        if form.is_valid():
-            form.save()
-            return message(request, _('User details successfully changed'), 'success', 'openauth-show-user', args=[user.id])
-
-    def create_form(self):
-        initial = self.get_initial_data()
-        form = EditUserForm(user, initial=initial)
-        return {'form': form, 'user': user}
-
-    def get_initial_data(self):
-        return {}
-
-
-
 @render_to('openauth/profile.html')
 def show_user(request, user_id=None):
     """