Re: django registration custom backend

2012-10-27 Thread Bill Freeman
If I were doing this, I would create a view, written (or decorated) to
require a new permission (or allowing superuser without the permission)
to use, containing a form to collect minimal information, certainly
email and username, probably also human name.  The form class
would have validation that attempts to create a new auth.User with
that information and an unusable password (validation failing, for
example, if the username already exists).  If the form succeeds, the
view triggers a password reset email for that user.

All the hard parts are already in there, best to use them.

Bill

On Sat, Oct 27, 2012 at 9:46 AM, Ian Foote  wrote:
> Hi,
>
> I'm trying to write a custom backend for django registration.
> (http://docs.b-list.org/django-registration/0.8/backend-api.html)
> I'm using python 2.7 and django 1.4.
>
> What I want is for an existing user with suitable permissions to be able to
> register accounts for new users. The new user will get an email with an
> activation link, which will redirect the new user to a form where they set a
> password for their account.
>
> I do not want the existing user to be required to set a password manually
> for the new user to change once they first log in.
>
> This is what I have so far:
>
> from django.conf import settings
> from django.contrib.sites.models import RequestSite
> from django.contrib.sites.models import Site
> from django.contrib.auth import login
>
> from registration import signals
> from registration.models import RegistrationProfile
> from registration.backends.default import DefaultBackend
>
> class CustomBackend(DefaultBackend):
> def register(self, request, **kwargs):
> username, email = kwargs['email'], kwargs['email']
> # username is email address
> password = '' # User will have no password set.
> if Site._meta.installed:
> site = Site.objects.get_current()
> else:
> site = RequestSite(request)
> new_user = RegistrationProfile.objects.create_inactive_user(
> username,
> email,
> password,
> site)
> signals.user_registered.send(sender=self.__class__,
>  user=new_user,
>  request=request)
> return new_user
>
> def activate(self, request, activation_key):
> activated = RegistrationProfile.objects.activate_user(
> activation_key)
> if activated:
> login(request, activated)
> signals.user_activated.send(sender=self.__class__,
> user=activated,
> request=request)
> return activated
>
> def post_activation_redirect(self, request, user):
> return ('set_password', (), {})
>
> Unfortunately, when I try to log the new user in during account activation,
> I get an AttributeError: 'User' object has no attribute 'backend'
>
> I know this is because I'm not calling authenticate before login, but
> authenticate
> (https://docs.djangoproject.com/en/1.4/topics/auth/#django.contrib.auth.authenticate)
> requires a password, which I don't want to set at this stage. I want the
> user to be logged in when they are redirected to the set_password form.
>
> Advice would be appreciated.
>
> Regards,
> Ian
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



django registration custom backend

2012-10-27 Thread Ian Foote

Hi,

I'm trying to write a custom backend for django registration. 
(http://docs.b-list.org/django-registration/0.8/backend-api.html)

I'm using python 2.7 and django 1.4.

What I want is for an existing user with suitable permissions to be able 
to register accounts for new users. The new user will get an email with 
an activation link, which will redirect the new user to a form where 
they set a password for their account.


I do not want the existing user to be required to set a password 
manually for the new user to change once they first log in.


This is what I have so far:

from django.conf import settings
from django.contrib.sites.models import RequestSite
from django.contrib.sites.models import Site
from django.contrib.auth import login

from registration import signals
from registration.models import RegistrationProfile
from registration.backends.default import DefaultBackend

class CustomBackend(DefaultBackend):
def register(self, request, **kwargs):
username, email = kwargs['email'], kwargs['email']
# username is email address
password = '' # User will have no password set.
if Site._meta.installed:
site = Site.objects.get_current()
else:
site = RequestSite(request)
new_user = RegistrationProfile.objects.create_inactive_user(
username,
email,
password,
site)
signals.user_registered.send(sender=self.__class__,
 user=new_user,
 request=request)
return new_user

def activate(self, request, activation_key):
activated = RegistrationProfile.objects.activate_user(
activation_key)
if activated:
login(request, activated)
signals.user_activated.send(sender=self.__class__,
user=activated,
request=request)
return activated

def post_activation_redirect(self, request, user):
return ('set_password', (), {})

Unfortunately, when I try to log the new user in during account 
activation, I get an AttributeError: 'User' object has no attribute 
'backend'


I know this is because I'm not calling authenticate before login, but 
authenticate 
(https://docs.djangoproject.com/en/1.4/topics/auth/#django.contrib.auth.authenticate) 
requires a password, which I don't want to set at this stage. I want the 
user to be logged in when they are redirected to the set_password form.


Advice would be appreciated.

Regards,
Ian

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.