Пример #1
0
def create(request):
    class NewAccountForm(forms.Form):
        _CHOICE = ((1, "Nauczyciel"), (2, "Adminstrator"))
        login = forms.EmailField(label=u"E-mail")
        name = forms.CharField(label=u"Imię", required=False)
        surname = forms.CharField(label=u"Nazwisko", required=False)
        status = forms.ChoiceField(choices=_CHOICE, initial=1, label=u"Typ")

    if request.method == "POST":
        form = NewAccountForm(request.POST)

        if form.is_valid():

            # grab a random password
            from random import choice

            randompass = "".join([choice("1234567890qwertyupasdfghjklzxcvbnm") for i in range(7)])

            u = User(
                login=form.cleaned_data["login"],
                name=form.cleaned_data["name"],
                surname=form.cleaned_data["surname"],
                status=form.cleaned_data["status"],
            )
            u.save()
            u.set_password(randompass)

            messages.add_message(request, messages.SUCCESS, u"Konto stworzone. Nowe hasło to %s" % (randompass,))

            return redirect("/admin/accounts/%s/" % (u.id,))

    else:
        form = NewAccountForm()

    return render_to_response("radmin/manage_accounts_add.html", request, **klist(selected_user_id="create", form=form))
Пример #2
0
def register(request):
    """Handles registering new students only"""

    class RegisterForm(forms.Form):

        name = forms.CharField(label=u'Imię')
        surname = forms.CharField(label=u'Nazwisko')

        login = forms.EmailField(max_length=254, label=u'Email')

        album = forms.IntegerField(label=u'Nr albumu')

        password = forms.CharField(widget=forms.PasswordInput, label=u'Hasło', required=True)
        password2 = forms.CharField(widget=forms.PasswordInput, label=u'Powtórz hasło', required=True)


        def clean_login(self):
            """Assure login is unique"""
            lgn = self.cleaned_data['login']
            try:
                usr = User.objects.get(login=lgn)
            except User.DoesNotExist:
                return lgn
            raise forms.ValidationError(u'Taki e-mail jest już używany!')

        def clean(self):
            """Assure passwords match"""
            cleaned_data = super(RegisterForm, self).clean()

            pwd1 = cleaned_data.get('password')
            pwd2 = cleaned_data.get('password2')

            if pwd1 != pwd2:
                self._errors['password'] = self.error_class(['Niezgodne hasła'])
                self._errors['password2'] = self.error_class(['Niezgodne hasła'])

                del cleaned_data['password']
                del cleaned_data['password2']

            return cleaned_data


    if request.method == 'POST':
        rf = RegisterForm(request.POST)

        if rf.is_valid():

            # Create an account
            u = User(name=rf.cleaned_data['name'],
                     surname=rf.cleaned_data['surname'],
                     login=rf.cleaned_data['login'],
                     number=rf.cleaned_data['album'])
            u.save()
            u.set_password(rf.cleaned_data['password'])

            request.login(u.login)      # Log in the user

            messages.add_message(request, messages.INFO, u'Utworzono nowe konto. Zostałeś zalogowany.')
            return redirect('/')
    else:
        rf = RegisterForm()

    return render_to_response('front/register.html', request, form=rf)