Esempio n. 1
0
def make_group_add_form(user):
    """Makes a form for use with group_add.  Only shows options which are
    relevant to user's permissions.
    @param user  The User who will be shown this form"""
    pos_choices = list(permissions.get_allowed_positions(user))
    class GroupAddForm(forms.Form):
        group_name = forms.CharField(label='Group name', max_length=30)
        pos        = forms.ChoiceField(label='Position', choices=pos_choices)
        login      = forms.ChoiceField(label='Password allocation',
                                       choices=((0, 'Raven login only'),
                                                (1, 'Email random password')),
                                       widget=forms.widgets.RadioSelect)
        users      = forms.CharField(label='Users, e.g. fs123,[email protected],Fred,Smith',
                                     widget=forms.Textarea)

        # See if we are allowed to create users at different organizations, and if
        # we are, make a field for it
        if permissions.allowed_user_access_create_different_org(user):

            # Get a list of organizations
            orgs = db.Organization.objects.all()
            org_choices = [(o.name, o.name) for o in orgs]
            org = forms.ChoiceField(label='Organization',
                                    choices=org_choices)

        def clean_group_name(self):
            """Ensure the group name is valid"""
            group_name = self.cleaned_data['group_name']
            if re.match(r'^\w+$', group_name) is None:
                raise forms.ValidationError("Group names can contain only alphanumeric characters and underscores")
            return group_name
    
    return GroupAddForm
Esempio n. 2
0
def make_registration_form(user):
    pos_choices = [p for p in permissions.get_allowed_positions(user)]
    class RegistrationForm(forms.Form):
        username   = forms.CharField(label='Username', max_length=30)
        first_name = forms.CharField(label='First Name', max_length=30)
        last_name  = forms.CharField(label='Last Name', max_length=30)
        email      = forms.CharField(label='E-mail Address', max_length=75)
        pw_method  = forms.ChoiceField(label='Password assignment',
                                       choices=(('email','Email random password'),
                                                ('raven','Require raven login'),
                                                ('given','Set password given below')),
                                       widget=forms.widgets.RadioSelect)
        pw         = forms.CharField(label='Password',
                                     widget=forms.PasswordInput(render_value=False),
                                     required=False)
        pos        = forms.ChoiceField(label='Position', choices=pos_choices)

        # See if we're allowed to add users at different organizations; if we
        # are, show a choice of organizations
        if permissions.allowed_user_access_create_different_org(user):
            orgs = db.Organization.objects.all()
            org_choices = [(o.name, o.name) for o in orgs]
            org = forms.ChoiceField(label='Organization',
                                    choices=org_choices)

        def clean_username(self):
            un = self.cleaned_data['username']
            if not re.match('^\w+$', un):
                raise forms.ValidationError("Only alphanumeric characters and underscores are allowed in a user's name.")
            return un

        def clean(self):
            """Makes sure that the form data is valid; in particular, that a
            password has been supplied if the choice of login method requires
            it."""
            login = self.cleaned_data['pw_method']
            pw = self.cleaned_data['pw']
            if login == 'given' and len(pw) < 6:
                raise forms.ValidationError("Password must be at least 6 characters")
            return self.cleaned_data

    return RegistrationForm