Example #1
0
    def save(self, supporter):
        user = None
        user_profile = None
        if self.user_profile:
            user_profile = self.user_profile
            user = user_profile.user

        username = self.cleaned_data.get('username')
        email = self.cleaned_data.get('email')
        password = self.cleaned_data.get('password')
        administrator = self.cleaned_data.get('administrator')

        # Disable automatic Supporter creation when adding user
        post_save.disconnect(create_user_profile_and_supporter, sender=User)
        
        if not user:
            user = User.objects.create_user(username, email, password)
            if administrator:
                user.is_staff = True
                user.is_superuser = True
            user.save()
        else:
            changed = False
            if username != user.username:
                user.username = username
                changed = True
            if email != user.email:
                user.email = email
                changed = True
            if password != user.password:
                user.set_password(password)
                changed = True
            if administrator != user.is_superuser:
                user.is_superuser = administrator
                user.is_staff = administrator
                changed = True
            if changed:
                user.save()
    
        if not user_profile:
            user_profile = UserProfile(user=user)
            user_profile.save()
            self.user_profile = user_profile

        supporter.user_profile = user_profile
        supporter.save()
          
        # Re-enable signal 
        post_save.connect(create_user_profile_and_supporter, sender=User)

        return True 
Example #2
0
def create_user_profile_and_supporter(sender, instance, created, **kw):
    if not created:
        return None
    profile = UserProfile(user=instance)
    profile.save()

    first_name = instance.first_name
    if not first_name:
        first_name = instance.username

    supporter = Supporter(first_name = first_name, last_name = instance.last_name, user_profile = profile, creator = instance)

    if instance.email:
        email = EmailAddress(email = instance.email, type = EmailAddressType.objects.all()[0])
        email.save()
        contact_profile = ContactProfile(primary_email = email)
        contact_profile.save()
        supporter.contact_profile = contact_profile

    supporter.save()