Re: Integrating User Profile with Auth

2010-09-12 Thread darren
Thanks.

I will keep this in mind.

On Sun, Sep 12, 2010 at 12:51 PM, Justin Myers  wrote:

> Another idea would be to connect to User's post-save signal. You can
> set it up to check whether the User instance being saved is just being
> created, and if so, you can create its UserProfile instance at the
> same time. That'd make it so all future users have profiles as soon as
> they're created; once you create UserProfile instances for each of
> your existing users, you could probably avoid the check altogether.
>
> Example:
> from django.db.models.signals import post_save
> from django.contrib.auth.models import User
>
> def create_profile(sender, **kwargs):
>if 'created' in kwargs and 'instance' in kwargs:
>if kwargs['created']:
>profile = UserProfile(user=kwargs['instance'])
>profile.save()
> post_save.connect(create_profile, sender=User)
>
> HTH,
> Justin
>
> On Sep 11, 8:45 pm, darren  wrote:
> > I think that my main problem was that I was expecting the save to the
> model
> > form to actually create the profile.
> >
> > Here's what I ended up with in my view.
> >
> >  86 @login_required
> >  87 def createProfile(request):
> >  88 UserProfile.objects.get_or_create(user=request.user)[0]
> >  89 if request.method == 'POST':
> >  90 form = UserProfileForm(request.POST,
> > instance=request.user.get_profile())
> >  91 if form.is_valid():
> >  92 form.save()
> >  93 return HttpResponseRedirect("/")
> >  94 else:
> >  95 return render_to_response('fav/createProfile.tpl', {
> 'form'
> > : form  }, RequestContext(request) )
> >  96 else:
> >  97 form = UserProfileForm(instance=request.user.get_profile())
> >  98 return render_to_response('fav/createProfile.tpl', { 'form' :
> form}, RequestContext(request))
> >
> >  99
> >
> > On Sat, Sep 11, 2010 at 12:37 AM, Shawn Milochik 
> wrote:
> > > I think you just may be missing a call to get_profile() in this view.
> > > You can just do that and do a try block with an except block for
> > > DoesNotExist. That will let you know the situation you're in, whether
> > > the profile already existed or not.
> >
> > > Also, unless I'm misreading something you're trying to pass a User
> > > instance as the instance for a user profile, which will not work. If
> > > anything, you should be doing a .get() on the user profile where user
> > > = request.user, but that doesn't matter anyway because get_profile()
> > > does the same thing and is the correct way to do this.
> >
> > > Shawn
> >
> > > --
> > > You received this message because you are subscribed to the Google
> Groups
> > > "Django users" group.
> > > To post to this group, send email to django-us...@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-us...@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-us...@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.



Re: Integrating User Profile with Auth

2010-09-12 Thread Justin Myers
Another idea would be to connect to User's post-save signal. You can
set it up to check whether the User instance being saved is just being
created, and if so, you can create its UserProfile instance at the
same time. That'd make it so all future users have profiles as soon as
they're created; once you create UserProfile instances for each of
your existing users, you could probably avoid the check altogether.

Example:
from django.db.models.signals import post_save
from django.contrib.auth.models import User

def create_profile(sender, **kwargs):
if 'created' in kwargs and 'instance' in kwargs:
if kwargs['created']:
profile = UserProfile(user=kwargs['instance'])
profile.save()
post_save.connect(create_profile, sender=User)

HTH,
Justin

On Sep 11, 8:45 pm, darren  wrote:
> I think that my main problem was that I was expecting the save to the model
> form to actually create the profile.
>
> Here's what I ended up with in my view.
>
>  86 @login_required
>  87 def createProfile(request):
>  88     UserProfile.objects.get_or_create(user=request.user)[0]
>  89     if request.method == 'POST':
>  90         form = UserProfileForm(request.POST,
> instance=request.user.get_profile())
>  91         if form.is_valid():
>  92             form.save()
>  93             return HttpResponseRedirect("/")
>  94         else:
>  95             return render_to_response('fav/createProfile.tpl', { 'form'
> : form  }, RequestContext(request) )
>  96     else:
>  97         form = UserProfileForm(instance=request.user.get_profile())
>  98     return render_to_response('fav/createProfile.tpl', { 'form' : form}, 
> RequestContext(request))
>
>  99
>
> On Sat, Sep 11, 2010 at 12:37 AM, Shawn Milochik  wrote:
> > I think you just may be missing a call to get_profile() in this view.
> > You can just do that and do a try block with an except block for
> > DoesNotExist. That will let you know the situation you're in, whether
> > the profile already existed or not.
>
> > Also, unless I'm misreading something you're trying to pass a User
> > instance as the instance for a user profile, which will not work. If
> > anything, you should be doing a .get() on the user profile where user
> > = request.user, but that doesn't matter anyway because get_profile()
> > does the same thing and is the correct way to do this.
>
> > Shawn
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Django users" group.
> > To post to this group, send email to django-us...@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-us...@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.



Re: Integrating User Profile with Auth

2010-09-11 Thread darren
I think that my main problem was that I was expecting the save to the model
form to actually create the profile.

Here's what I ended up with in my view.

 86 @login_required
 87 def createProfile(request):
 88 UserProfile.objects.get_or_create(user=request.user)[0]
 89 if request.method == 'POST':
 90 form = UserProfileForm(request.POST,
instance=request.user.get_profile())
 91 if form.is_valid():
 92 form.save()
 93 return HttpResponseRedirect("/")
 94 else:
 95 return render_to_response('fav/createProfile.tpl', { 'form'
: form  }, RequestContext(request) )
 96 else:
 97 form = UserProfileForm(instance=request.user.get_profile())
 98 return render_to_response('fav/createProfile.tpl', { 'form' : form
}, RequestContext(request))
 99


On Sat, Sep 11, 2010 at 12:37 AM, Shawn Milochik  wrote:

> I think you just may be missing a call to get_profile() in this view.
> You can just do that and do a try block with an except block for
> DoesNotExist. That will let you know the situation you're in, whether
> the profile already existed or not.
>
> Also, unless I'm misreading something you're trying to pass a User
> instance as the instance for a user profile, which will not work. If
> anything, you should be doing a .get() on the user profile where user
> = request.user, but that doesn't matter anyway because get_profile()
> does the same thing and is the correct way to do this.
>
> Shawn
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-us...@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-us...@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.



Re: Integrating User Profile with Auth

2010-09-11 Thread darren
Thanks.

That helped me figure it out.  I'll post my solution back later
today/tonight.


On Sat, Sep 11, 2010 at 12:37 AM, Shawn Milochik  wrote:

> I think you just may be missing a call to get_profile() in this view.
> You can just do that and do a try block with an except block for
> DoesNotExist. That will let you know the situation you're in, whether
> the profile already existed or not.
>
> Also, unless I'm misreading something you're trying to pass a User
> instance as the instance for a user profile, which will not work. If
> anything, you should be doing a .get() on the user profile where user
> = request.user, but that doesn't matter anyway because get_profile()
> does the same thing and is the correct way to do this.
>
> Shawn
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-us...@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-us...@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.



Re: Integrating User Profile with Auth

2010-09-10 Thread Shawn Milochik
I think you just may be missing a call to get_profile() in this view.
You can just do that and do a try block with an except block for
DoesNotExist. That will let you know the situation you're in, whether
the profile already existed or not.

Also, unless I'm misreading something you're trying to pass a User
instance as the instance for a user profile, which will not work. If
anything, you should be doing a .get() on the user profile where user
= request.user, but that doesn't matter anyway because get_profile()
does the same thing and is the correct way to do this.

Shawn

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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.



Integrating User Profile with Auth

2010-09-10 Thread darren
I am following the information in the docs to add fields to maintain, in
addition to the ones Auth already maintains.  I'm starting with the docs
here:


So far, I have a model form I've created based on the additional fields.
 And, I'm displaying my form in my template.  But, I can't seem to get the
form to work.  My plan is to use the exception encountered when
get_profile() is called in another view, but the profile doesn't yet exist.
I want to catch the error and send the user to a form to fill out the extra
field I want to maintain and pre-populate another.

Mostly I think my problem arises when I try to save my form.  But, it could
be when I create it.  The form is displaying a drop down list box of all of
the users.  So, something odd is going on there.  Also, every time I submit
the form, the submit method is GET, even though the method is set to POST in
the form's attributes.

I believe I may be stumbling over this part of the docs:

"The method 
get_profile()
 does not create the profile, if it does not exist. You need to register a
handler for the
signaldjango.db.models.signals.post_save
 on the User model, and, in the handler, if created=True, create the
associated user profile"

A suggestion is made here
to create the
profile.

*User.profile = property(lambda u:
UserProfile.objects.get_or_create(user=u)[0])*

But, if I automatically create the profile, I can't catch the exception for
a missing profile.

Here's some code:

In my template, I have this code (line 93 seems to be instrumental in my
problem).  I can get a post if I use line 92 instead of line 93.  But, I
still have 2 problems.  First, it's letting me choose the user (I was hoping
the code on line 90 would eliminate that).  And, second, the form simply
won't save.:

 86 @login_required
 87 def createProfile(request):
 88 err_msg = [request.method] # I have this so I can see the method
 89 messages = []
 90 a = User.objects.get(username=request.user.username)
 91 # err_msg = [request.method, a]
 92 # form = UserProfileForm(instance=a)
 93 form = UserProfileForm(request.POST, instance=a)
 94 if request.method == 'POST':
 95 if form.is_valid():
 96 form.save()
 97 return HttpResponseRedirect("/")
 98 else:
 99 err_msg.append("Error Processing File")
100 return render_to_response('fav/createProfile.tpl', {
'ERROR_MSG' : err_msg, 'messages' : messages, 'form' : form  },
RequestContext(request) )
101 return render_to_response('fav/createProfile.tpl', { 'ERROR_MSG' :
err_msg, 'form' : form }, RequestContext(request))

My Model:

  1 from django.db import models
  2 from django.forms import ModelForm
  3 from django.contrib.auth.models import User
  4
  5 class UserProfile(models.Model):
  6 user = models.ForeignKey(User, unique=True)
  7 screenname = models.CharField('Screen Name', max_length=25,
unique=True)
  8 credits = models.PositiveSmallIntegerField('Credits', default=3)
  9
 10 class UserProfileForm(ModelForm):
 11 class Meta:
 12 model = UserProfile
 13 #fields = ['screenname'] # I've tried uncommenting this.
~

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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.