2013-02-27 57 views
3

我有我的網站,我用omab/django-social-auth創建用戶閒置爲默認(IS_ACTIVE默認爲false)

我想將用戶重定向到另一個網站,讓他們填寫自己的詳細信息Facebook驗證。所以我想讓用戶在他們第一次使用他們的Facebook帳戶進行身份驗證時處於非活動狀態,然後在完成表單後將他們保存爲活動用戶。

我在我的環境下操作了django/contrib/auth/models.py,與is_active字段一樣,默認值爲False;但他們被保存爲活動用戶 但仍然是相同的結果,即使我從管理面板添加普通用戶。有什麼我失蹤?

class User(models.Model): 
""" 
Users within the Django authentication system are represented by this 
model. 

Username and password are required. Other fields are optional. 
""" 
username = models.CharField(_('username'), max_length=30, unique=True, 
    help_text=_('Required. 30 characters or fewer. Letters, numbers and ' 
       '@/./+/-/_ characters')) 
first_name = models.CharField(_('first name'), max_length=30, blank=True) 
last_name = models.CharField(_('last name'), max_length=30, blank=True) 
email = models.EmailField(_('e-mail address'), blank=True) 
password = models.CharField(_('password'), max_length=128) 
is_staff = models.BooleanField(_('staff status'), default=False, 
    help_text=_('Designates whether the user can log into this admin ' 
       'site.')) 
is_active = models.BooleanField(_('active'), default=False, 
    help_text=_('Designates whether this user should be treated as ' 
       'active. Unselect this instead of deleting accounts.')) 
is_superuser = models.BooleanField(_('superuser status'), default=False, 
    help_text=_('Designates that this user has all permissions without ' 
       'explicitly assigning them.')) 
last_login = models.DateTimeField(_('last login'), default=timezone.now) 
date_joined = models.DateTimeField(_('date joined'), default=timezone.now) 
groups = models.ManyToManyField(Group, verbose_name=_('groups'), 
    blank=True, help_text=_('The groups this user belongs to. A user will ' 
          'get all permissions granted to each of ' 
          'his/her group.')) 
user_permissions = models.ManyToManyField(Permission, 
    verbose_name=_('user permissions'), blank=True, 
    help_text='Specific permissions for this user.') 
objects = UserManager() 


def create_user(self, username, email=None, password=None): 
    """ 
    Creates and saves a User with the given username, email and password. 
    """ 
    now = timezone.now() 
    if not username: 
     raise ValueError('The given username must be set') 
    email = UserManager.normalize_email(email) 
    user = self.model(username=username, email=email, 
         is_staff=False, is_active=False, is_superuser=False, 
         last_login=now, date_joined=now) 

    user.set_password(password) 
    user.save(using=self._db) 
    return user 

回答

8
  1. 避免修改內置插件。有更好的方法來做事情。
  2. signals。信號真棒。
  3. 在這種情況下,我會附加到0123_s的pre_save信號,並手動更正模型實例的is_active屬性(如果對象是新的)。
  4. 這樣,您可以添加一些邏輯,以確保您正確地將用戶標記爲不活動。
  5. 因爲在Admin中添加的用戶可能應該是活動的,如果管理員將其標記爲活動的。
+0

這很酷..所以我必須操縱django-social-auth軟件包來添加一個信號......對吧? – tunaktunak 2013-02-27 21:48:31

+0

不是。你可以在你的Django項目中的任何應用程序中做到這一點。如果可以的話,應該避免修補軟件包。 – 2013-02-27 21:49:13

+1

如何檢查用戶是否正在創建,而不僅僅是被修改? – 2017-07-11 06:01:16

4

使用django-allauth時優雅的解決方案。

還有一個很好的解決方案..聽起來很像你想要的東西。

我已經創建了一個自定義表單(在我的情況下是一個ModelForm),我可以通過ACCOUNT_SIGNUP_FORM_CLASS設置將其交給django-allauth。這是什麼...請求新的潛在用戶在註冊過程中提供額外的字段。

這有一些非常不錯的優勢:

  1. 可以在除了默認的東西很優雅添加一些領域。
  2. 它適用於社交和「正常」註冊。
  3. 不需要修補第三方應用程序。
  4. 您仍然可以修改和維護管理中的所有內容。
  5. 在自定義表單中,您可以在將其保存到 數據庫之前訪問新的用戶實例。這意味着你甚至可以處理提供的信息,例如爲他創建一個配置文件對象,並將用戶設置爲非活動狀態。 這是可行的,因爲你可以檢查是否一切正常..只有然後承諾做所有這些步驟或拒絕帶有驗證錯誤的表單。 :)

好吧..聽起來不錯吧?
但它究竟如何工作(即看起來像)?
我很高興你問..^_^

爲您的使用情況下,它可能是這個樣子:

設置。PY

[...] 
ACCOUNT_SIGNUP_FORM_CLASS = "<your_app>.forms.SignupForm" 
[...] 

forms.py

class SignupForm(forms.Form): 
    first_name = forms.CharField(max_length=30) 
    last_name = forms.CharField(max_length=30) 

    def signup(self, request, user): 
     user.first_name = self.cleaned_data['first_name'] 
     user.last_name = self.cleaned_data['last_name'] 
     user.is_active = False 
     user.save() 
2

jack_shed提示信號,這讓我覺得把它的方向。但仍然有工作要弄清楚接收信號後如何進行測試和修改。

這是我的工作。

from django.dispatch import receiver 
from django.db.models.signals import pre_save 
from django.contrib.auth.models import User 

@receiver(pre_save, sender=User) 
def set_new_user_inactive(sender, instance, **kwargs): 
    if instance._state.adding is True: 
     print("Creating Inactive User") 
     instance.is_active = False 
    else: 
     print("Updating User Record") 

這將捕獲在保存發生之前創建用戶的操作,然後測試此實例狀態是否爲「添加」。區分創建和更新模型實例。

如果你不做這個測試,更新用戶設置is_active也爲False,並且最終無法通過django激活它們。