Changeset 2507

Show
Ignore:
Timestamp:
01/27/10 16:56:14 (7 weeks ago)
Author:
JensDiemer
Message:

django updates: After model validation lands, we can't easy use ModelForm?

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/0.9/pylucid_project/pylucid_plugins/auth/forms.py

    r2001 r2507  
    55from django.contrib import auth 
    66from django.contrib.auth.models import User 
     7from django.utils.translation import ugettext as _ 
    78 
    89from pylucid_project.utils import crypt 
     
    3637    ) 
    3738    sha_b = forms.CharField( 
    38         min_length=crypt.HASH_LEN/2, max_length=crypt.HASH_LEN/2 
     39        min_length=crypt.HASH_LEN / 2, max_length=crypt.HASH_LEN / 2 
    3940    ) 
    4041 
     
    5354 
    5455        # Fill with null, to match the full SHA1 hexdigest length. 
    55         fill_len = crypt.HASH_LEN - (crypt.HASH_LEN/2) 
     56        fill_len = crypt.HASH_LEN - (crypt.HASH_LEN / 2) 
    5657        temp_value = ("0" * fill_len) + sha_value 
    5758 
     
    6970    # Should normaly never be send back! 
    7071    raw_password = forms.CharField( 
    71         help_text="(required)", required=False, widget = forms.PasswordInput() 
     72        help_text="(required)", required=False, widget=forms.PasswordInput() 
    7273    ) 
    7374 
     
    7576        label="SHA1 for django", 
    7677        help_text="(automatic generated with JavaScript.)", 
    77         widget = forms.TextInput(attrs={"readonly":"readonly", "size":"40"}), 
     78        widget=forms.TextInput(attrs={"readonly":"readonly", "size":"40"}), 
    7879        min_length=crypt.HASH_LEN, max_length=crypt.HASH_LEN 
    7980    ) 
     
    8182        label="SHA1 for PyLucid", 
    8283        help_text="(automatic generated with JavaScript.)", 
    83         widget = forms.TextInput(attrs={"readonly":"readonly", "size":"40"}), 
     84        widget=forms.TextInput(attrs={"readonly":"readonly", "size":"40"}), 
    8485        min_length=crypt.HASH_LEN, max_length=crypt.HASH_LEN 
    8586    ) 
     
    9899# FORMS 
    99100 
    100 class BaseModelForm(forms.ModelForm): 
    101     """ 
    102     A model form witch don't validate unique fields. 
    103  
    104     This ModelForm is only for generating the forms and not for create/update 
    105     any database data. So a field unique Test would like generate Errors like: 
    106         User with this Username already exists. 
    107  
    108     see also: 
    109     http://www.jensdiemer.de/_command/118/blog/detail/30/ (de) 
    110     http://www.python-forum.de/topic-16000.html (de) 
    111     """ 
    112     def validate_unique(self): 
    113         pass 
    114  
    115  
    116 class UsernameForm(BaseModelForm): 
     101#class BaseModelForm(forms.ModelForm): 
     102#    """ 
     103#    A model form witch don't validate unique fields. 
     104# 
     105#    This ModelForm is only for generating the forms and not for create/update 
     106#    any database data. So a field unique Test would like generate Errors like: 
     107#        User with this Username already exists. 
     108# 
     109#    see also: 
     110#    http://www.jensdiemer.de/_command/118/blog/detail/30/ (de) 
     111#    http://www.python-forum.de/topic-16000.html (de) 
     112#    """ 
     113#    def __init__(self, *args, **kwargs): 
     114#        """ Change field meta in a DRY way """ 
     115#        super(BaseModelForm, self).__init__(*args, **kwargs) 
     116# 
     117#        self.model.full_validate = self._skip 
     118# 
     119#    def _skip(self, *args, **kwargs): 
     120#        pass 
     121# 
     122#    def validate_unique(self): 
     123#        pass 
     124 
     125class UsernameForm(forms.Form): 
    117126    """ 
    118127    form for input the username, used in auth.login() 
    119     """    
     128     
     129    FIXME: This is not DRY. 
     130    """ 
     131    username = forms.CharField(max_length=_('30'), label=_('Username'), 
     132        help_text=_('Required. 30 characters or fewer. Alphanumeric characters only (letters, digits and underscores).') 
     133    ) 
     134 
    120135    def is_valid(self): 
    121136        """ do a secont validation: try to get the user from database and 
    122         check if he is active """         
     137        check if he is active """ 
    123138        is_valid = super(UsernameForm, self).is_valid() 
    124139        if not is_valid: 
    125140            return False 
    126          
     141 
    127142        username = self.cleaned_data["username"] 
    128143        try: 
    129             self.user = User.objects.get(username = username) 
     144            self.user = User.objects.get(username=username) 
    130145        except User.DoesNotExist, e: 
    131146            self._errors["username"] = ("User doesn't exist!",) 
    132147            return False 
     148 
     149        return True 
     150 
     151#    class Meta: 
     152#        model = User 
     153#        fields = ("username",) 
     154 
     155 
     156class PasswordForm(forms.Form): 
     157    """ 
     158    form for input the username, used in auth._sha_login() 
    133159     
    134         return True 
    135          
    136     class Meta: 
    137         model = User 
    138         fields=("username",) 
    139  
    140  
    141 class PasswordForm(BaseModelForm): 
    142     """ 
    143     form for input the username, used in auth._sha_login() 
    144     """ 
    145     def __init__(self, *args, **kwargs): 
    146         """ Change field meta in a DRY way """ 
    147         super(PasswordForm, self).__init__(*args, **kwargs) 
    148  
    149         self.fields['password'].widget = forms.PasswordInput() 
    150         self.fields['password'].help_text = "" 
     160    FIXME: This is not DRY. 
     161    """ 
     162    password = forms.CharField(max_length=_('128'), label=_('Password'), widget=forms.PasswordInput() 
     163    ) 
     164 
     165#    def __init__(self, *args, **kwargs): 
     166#        """ Change field meta in a DRY way """ 
     167#        super(PasswordForm, self).__init__(*args, **kwargs) 
     168# 
     169#        self.fields['password'].widget = forms.PasswordInput() 
     170#        self.fields['password'].help_text = "" 
    151171 
    152172    def is_valid(self, username): 
     
    154174        if not is_valid: 
    155175            return False 
    156          
     176 
    157177        password = self.cleaned_data["password"] 
    158178        self.user = auth.authenticate(username=username, password=password) 
     
    160180            self._errors["password"] = ("Wrong password!",) 
    161181            return False 
    162          
     182 
    163183        return True 
    164184 
    165     class Meta: 
    166         model = User 
    167         fields=("password",) 
    168  
    169  
    170 class ResetForm(BaseModelForm): 
     185#    class Meta: 
     186#        model = User 
     187#        fields = ("password",) 
     188 
     189 
     190class ResetForm(forms.Form): 
    171191    """ 
    172192    from for input username and email, used in auth.pass_reset() 
    173193    """ 
    174     def __init__(self, *args, **kwargs): 
    175         super(ResetForm, self).__init__(*args, **kwargs) 
    176         # User.email is normaly a not required field, here it's required! 
    177         self.fields['email'].required = True 
    178          
    179     class Meta: 
    180         model = User 
    181         fields=("username","email") 
     194    username = forms.CharField(max_length=_('30'), label=_('Username'), 
     195        help_text=_('Required. 30 characters or fewer. Alphanumeric characters only (letters, digits and underscores).') 
     196    ) 
     197    email = forms.EmailField(max_length=_('75'), label=_('E-mail address')) 
     198 
     199#    def __init__(self, *args, **kwargs): 
     200#        super(ResetForm, self).__init__(*args, **kwargs) 
     201#        # User.email is normaly a not required field, here it's required! 
     202#        self.fields['email'].required = True 
     203 
     204#    class Meta: 
     205#        model = User 
     206#        fields = ("username", "email")