def post_save_create_or_update_profile(sender, **kwargs):
    """
    This ``post_save`` signal handler for ``User`` and user profile objects is connected
    automatically. For new users, it creates an associated user profile
    instance. For existing users, it checks whether such an instance already
    exists, and creates one if it doesn't.
    
    Notice that this function also keeps fields of the ``User`` model
    synchronized with fields of the user profile model that have the same name.
    That is, if your user profile model contains fields named ``email`` or
    ``last_name``, their value will automatically be set to the corresponding
    attributes of the ``User`` instance when it is saved, and vice versa.
    """
    from user_profiles.utils import create_profile_for_new_user
    if sender == User and kwargs['instance'].is_authenticated():
        profile = None
        if not kwargs['created']:
            # If profile exists, copy identical field names from user to
            # profile. This assures that changing e.g. `last_name` in the admin
            # changes that field in the profile instance as well.
            try:
                profile = kwargs['instance'].get_profile()
                if len(sync_profile_fields(kwargs['instance'], profile)):
                    profile.save()
            except ObjectDoesNotExist:
                pass
        if not profile:
            # Create profile if it doesn't exist yet. This implies copying all
            # identical field names
            profile = create_profile_for_new_user(kwargs['instance'])
    if not kwargs['created'] and sender == get_user_profile_model():
        # Copy identical fields names from profile to user
        if len(sync_profile_fields(kwargs['instance'], kwargs['instance'].user)):
            kwargs['instance'].user.save()
示例#2
0
def _user_change(request, user, template_name):
    profile_model = get_user_profile_model()
    if request.method == 'POST':
        try:
            profile = user.get_profile()
            form = PROFILE_FORM_CLASS(request.POST, instance=profile)
        except profile_model.DoesNotExist:
            form = PROFILE_FORM_CLASS(request.POST)        
        if form.is_valid():
            form.save()
            messages.add_message(request, messages.SUCCESS, _('Your changes were saved.'))
            if user != request.user:
                return HttpResponseRedirect(reverse('user_detail', args=[getattr_field_lookup(user, app_settings.URL_FIELD)]))
            else:
                return HttpResponseRedirect(reverse('current_user_detail'))
        else:
            messages.error(request, _('Please correct the errors below.'))
    else:
        try:
            profile = user.get_profile()
            form = PROFILE_FORM_CLASS(instance=profile)
        except profile_model.DoesNotExist:
            profile = None
            form = PROFILE_FORM_CLASS()
    
    context_dict = {
        'form' : form,
        'profile' : profile,
    }
    
    return render_to_response(template_name,
        context_dict, context_instance=RequestContext(request))
 def __init__(self, *args, **kwargs):
     super(UserProfileBase, self).__init__(*args, **kwargs)
     # As explained in docstring of this class: ``is_default`` attribute
     # is always set to ``True`` for user profile model.
     if self.__class__ == get_user_profile_model():
         self.is_default = True
 class Meta:
     model = get_user_profile_model()