2010-05-01 63 views
3

我正在处理的网站涉及教师创建学生对象。教师可以选择让学生登录网站(查看日历等),或者教师可以选择仅将学生对象用于记录保存,而不允许学生登录。在学生创建形式,如果教师提供用户名和密码,它应该创建第一种对象 - 可以登录的对象,即常规用户对象。如果教师不提供用户名/密码,则应创建第二种类型。另一个要求是,教师应该能够晚点入学,并将非登录学生转为其他种类。设计这种情况的最佳方式是什么?子类用户并且不需要用户名和密码?这会影响到什么?Django设计问题:扩展用户使无法登录的用户

编辑: 我结束了使用User.set_unusable_password()。下面的代码 - 我已经离开了其他形式,等等,那我也用我的观点:

形式

class StudentForm(forms.ModelForm): 
    username = forms.RegexField(regex=r'^\w+$', 
           required=False, 
           max_length=30, 
           label=("Username"), 
           error_messages={ 'invalid': ("This value must contain only letters, numbers and underscores.") }) 
    password = forms.CharField(widget=forms.PasswordInput(), 
            label="Password", required=False) 

    class Meta: 
     model = User 
     fields = ('first_name', 'last_name', 'username', 'email', 'password') 

注意,用户名和密码没有形式要求。

查看

def create_student(request): 
    if request.method == "POST": 
     student_form = StudentForm(request.POST) 
     if student_form.is_valid(): 
      user = student_form.save(commit=False) 
      if student_form.cleaned_data['username'] == '': 
       user.username = generate_random_username() 
       user.set_unusable_password() 
      else: 
       user.set_password(user.password) 
      user.save() 

      return HttpResponseRedirect(reverse('student_list', args=['active'])) 

    #GET an empty form 
    else: 
     student_form = StudentForm() 

return render_to_response('priviostudio/create_student.html', { 
    'student_form': student_form, 
}) 

而且在编辑一个学生(这很可能会与create_student视景)我有这样的GET观点:

student_form_initial = { 
     'username': user_instance.username if user_instance.has_usable_password() else '', 
     'password': user_instance.password if user_instance.has_usable_password() else '', 
    } 
    student_form = StudentForm(instance=user_instance, initial=student_form_initial) 

,并在后期,如果教师提交了新的用户名和有效的密码,我只需在User实例上进行设置。

感谢您的想法大家。

回答

3

中的验证应用程序的User模型具有set_unusable_password方法;这可能是你想要的,而不需要扩展模型。

+0

问题是我还必须生成一个随机的用户名(因为用户名有unique = True且没有空白= True),这也会显示在表单上,​​并且可能会让教师感到困惑。 – UrLicht 2010-05-01 20:42:55

1

你可能会考虑把所有的学生都一个样的对象 - 而不是用户 - 然后补充与何老师,使学生以用户身份登录的对象模型,这两个模型对象的组成将解决你要相当干净。

+0

这似乎是一个很好的做法,但是看起来形式可能会有点混乱。我会去试试这个。 – UrLicht 2010-05-01 20:30:09

+0

我遇到了麻烦:我仍然想在用户上使用first_name,last_name和email。这样做需要将这些数据保存在非用户对象中并进行同步。 – UrLicht 2010-05-01 21:29:22

0

我woud避免子类用户。相反,您可能需要创建一个自定义身份验证器,以允许您检查组成员的登录能力。

""" 
DummyBackend.py 

""" 

from django.contrib.auth.models import User, check_password 
from django.contrib.auth.backends import RemoteUserBackend 
from lecture_feedback.daily.models import Student 
class DummyBackend(RemoteUserBackend): 
    """ 
    Dummy authentication module that takes a username and password. The password must match the username. 
    """ 

    def authenticate(self, username=None, password=None): 
     """ 
     The username passed as ``remote_user`` is considered trusted. This 
     method simply returns the ``User`` object with the given username, 
     creating a new ``User`` object if ``create_unknown_user`` is ``True``. 

     Returns None if ``create_unknown_user`` is ``False`` and a ``User`` 
     object with the given username is not found in the database. 

     """ 

     try: 
      student = Student.objects.get(globalid=username) 
     except Student.DoesNotExist: 
      return None 

     if username != password: 
      return 
     user = None 

     # Note that this could be accomplished in one try-except clause, but 
     # instead we use get_or_create when creating unknown users since it has 
     # built-in safeguards for multiple threads. 
     if self.create_unknown_user: 
      user, created = User.objects.get_or_create(username=username) 
      if created: 
       user = self.configure_user(user) 
     else: 
      try: 
       user = User.objects.get(username=username) 
      except User.DoesNotExist: 
       pass 
     return user 


    def configure_user(self, user): 
     """ 
     Configures a user after creation and returns the updated user. 

     By default, returns the user unmodified. 
     """ 
     student = Student.objects.get(globalid=user.username) 
     user.first_name = student.first_name 
     user.last_name = student.last_name 
     return user 

学生模型可以包含一个字段,表示如果学生被允许登录。 也看看http://docs.djangoproject.com/en/dev/howto/auth-remote-user/#attributes

2

Django的默认的用户模型有一个IS_ACTIVE场。

http://docs.djangoproject.com/en/dev/topics/auth/#django.contrib.auth.models.User.is_active

你可能想使用它。

当老师决定,他们希望用户能够登录,您的代码将只设置学生用户IS_ACTIVE =真,反之亦然这样。

另外根据上面的文档链接,Django的默认身份验证表单和权限方法检查is_active标志,这是已经为您完成的一些工作。

您可能还需要为该学生生成一个用户名,但如果提供该名称,则可以使用名称中的子弹轻松地执行此操作。

对我来说,让学生与教师区分的最明显的方式是真正的组合,Django也为此提供了机制。