Example #1
0
 def save(self, **kwargs):
     profile_user = super(DealerProfileForm, self).save(**kwargs)        
     profile = profile_user.get_profile()
     if profile:
         profile_org = profile.account
     else:
         profile_org = Dealer()
           
     profile_org.legal_name=self.cleaned_data['legal_name'] 
     profile_org.address_1=self.cleaned_data['address_1'] 
     profile_org.city=self.cleaned_data['city']
     profile_org.state=self.cleaned_data['state'] 
     profile_org.zip4=self.cleaned_data['zip4']
     profile_org.phone=self.cleaned_data['phone'] 
     profile_org.email=self.instance.email
     profile_org.save()
     
     if not profile:
         profile = UserProfile(
                         user=profile_user, 
                         account=profile_org, 
                         )
         profile.save()    
     return profile_user
Example #2
0
    def register(self, request, **kwargs):
        """
        Given core dealer information, create a new ``Dealer`` object 
        in 'PENDING' status.

        Along with the new ``Dealer`` object, a new
        ``registration.models.RegistrationProfile`` will be created,
        tied to that ``Dealer``, containing the activation key which
        will be used for this account.

        An email will be sent to the supplied email address; this
        email should contain an activation link. The email will be
        rendered using two templates. See the documentation for
        ``RegistrationProfile.send_activation_email()`` for
        information about these templates and the contexts provided to
        them.

        After the ``Dealer`` and ``RegistrationProfile`` are created and
        the activation email is sent, the signal
        ``registration.signals.user_registered`` will be sent, with
        the new ``User`` as the keyword argument ``user`` and the
        class of this backend as the sender.

        """        
        
        logger.debug('DealerRegistrationBackend: register(%s) called', kwargs)
        
        first_name, last_name, company_name, email = kwargs.pop('first_name'), kwargs.pop('last_name'), kwargs['legal_name'], kwargs['email']
        notes = {}
        for key in ('rush', 'product_type', 'revisions', 'expected_orders', 'tos' ):
            notes[key] = kwargs.pop(key)
        
        if Site._meta.installed:
            site = Site.objects.get_current()
        else:
            site = RequestSite(request)

        temp_username = sha1(company_name+str(datetime.now())).hexdigest()[:25]
        new_user = RegistrationProfile.objects.create_inactive_user(temp_username, email,
                                                                    None, site, send_email=False)
        try:
            new_user.first_name = first_name
            new_user.last_name = last_name
            new_user.save()
            
            new_dealer = Dealer(**kwargs)
            new_dealer.notes = '\n'.join(['%s: %s' % (k,v) for (k,v) in notes.items()])
            new_dealer.status = Dealer.Const.PENDING
            new_dealer.save()
            
            new_profile = UserProfile(user=new_user, account=new_dealer)
            new_profile.primary = True
            new_profile.save()        
            
            notify_new_dealer_registration(new_dealer)
                
        except Exception as ex:
            logger.error('DealerRegistrationBackend: registration failure: %s - rolling back', ex)
            transaction.rollback()
            new_user.delete()
            transaction.commit()
#            return False
            raise ex
        else:
            transaction.commit()
        
        user_registered.send(sender=self.__class__,
                                     user=new_user,
                                     request=request)
        logger.info('DealerRegistrationBackend: new registration for %s/%s', new_user, new_dealer)
        return new_user