Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Always create active user on `get_or_create`

Krzysiek Szularz5f7e73fd602a
diff --git a/registration/models.py b/registration/models.py
index a608b75d6e..42260226b2 100644
--- a/registration/models.py
+++ b/registration/models.py
@@ -32,13 +32,11 @@ class EmailConfirmation(models.Model):
default=lambda: now() + timedelta(settings.ACCOUNT_ACTIVATION_DAYS))
def get_or_create_user(self):
- """Confirm that user owns this email address and return User insatnce.
- """
+ 'Confirms that user owns this email address and returns User insatnce'
if self.external_user and self.external_user.user:
return self.external_user.user
- user, _created = User.objects.get_or_create(
- email=self.email, defaults={'is_active': True})
+ user, _created = User.objects.get_or_create(email=self.email)
if self.external_user:
self.external_user.user = user
diff --git a/userprofile/models.py b/userprofile/models.py
index 09613ad2cd..f09eea8b72 100644
--- a/userprofile/models.py
+++ b/userprofile/models.py
@@ -78,17 +78,22 @@ class Address(models.Model):
class UserManager(BaseUserManager):
+ def get_or_create(self, **kwargs):
+ defaults = kwargs.pop('defaults', {})
+ try:
+ return self.get_query_set().get(**kwargs), False
+ except self.model.DoesNotExist:
+ defaults.update(kwargs)
+ return self.create_user(**defaults), True
+
def create_user(self, email, password=None, is_staff=False,
is_active=True, **extra_fields):
- '''
- Creates and saves a User with the given username, email and password.
- '''
- now = timezone.now()
+ 'Creates a User with the given username, email and password'
email = UserManager.normalize_email(email)
- user = self.model(email=email, is_active=is_active, is_staff=is_staff,
- last_login=now, date_joined=now, **extra_fields)
-
- user.set_password(password)
+ user = self.model(
+ email=email, is_active=is_active, is_staff=is_staff, **extra_fields)
+ if password:
+ user.set_password(password)
user.save()
return user
@@ -170,7 +175,5 @@ class User(models.Model):
return is_password_usable(self.password)
def email_user(self, subject, message, from_email=None):
- """
- Sends an email to this User.
- """
+ 'Sends an email to this User'
send_mail(subject, message, from_email, [self.email])