On 1/27/06, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:

I`ve got couple of questions about auth system. Just a directions..

1. How do I extend base User class? I`ve read about subclassing, but it
was two month ago and it was not very clear.. Is there any new method?

I think you are talking about http://code.djangoproject.com/wiki/ExtendedUserModel . I have not tried it, and do not know the actual benefit, but if you follow the following steps you may not have to worry about it. Some one please correct me if I am wrong.

2. I want to have login form on each page. How should I process form,
if I want to stay inside built-in auth system? Dunno what should my
View be.

One option is to use the django login page for form processing, but if you do that you will miss the django's super cool error reporting in the same form feature. Also may be you want to have a remember me kind of check box in the login page. This is what I recommend:

from django.parts.auth.formfields import AuthenticationForm

class LoginManipulator(AuthenticationForm):
    def __init__(self, request):
        AuthenticationForm.__init__(self, request)
        self.fields.append(
            formfields.CheckboxField(field_name="remember")
        )

def index(request):
    manipulator = LoginManipulator(request)
    redirect_to = request.REQUEST.get ('next','')
    if request.POST:
        # If data was POSTed, we're trying to create a new Place.
        new_data = request.POST.copy()
        # Check for errors.
        errors = manipulator.get_validation_errors (new_data)
        if not errors:
            # No errors. This means we can save the data!
            request.session[users.SESSION_KEY] = manipulator.get_user_id()
            request.session.delete_test_cookie ()
            if request.REQUEST.has_key('next'): return HttpResponseRedirect(request.REQUEST['next'])
            else: return HttpResponseRedirect("../home/")
    else:
        request.session.set_test_cookie ()
        errors = new_data = {}
        # Create the FormWrapper, template, context, response.
    form = formfields.FormWrapper(manipulator, new_data, errors)
    return render_to_response('myapp/index',
                              {'form': form, 'signin_page': True, 'redirect_to': redirect_to},
                              context_instance=DjangoContext(request))
Note: You can see I have just ignored the remember me checkbox for now, here you can set permanent cookie etc, which I havent done so far.

Relevant snippet from my template:

{% if user.is_anonymous %}
<div id="signon">
        <form method="post"
                {% if redirect_to %}
                action="" redirect_to }}">
                {% else %}
                action="">                {% endif %}
                <fieldset>
                        <legend>Sign in</legend>
{% if form.has_errors %}
<center><font color="red">Email or password incorrect</font></center>
{% endif %}
                <table>
                        <tr>
                                <td align="right">
                                        <label for="">                                </td>
                                <td>
                                        {{ form.username }}
                                </td>
                        </tr>
                        <tr>
                                <td align="right">
                                        <label for=""
                                </td>
                                <td>
                                        {{ form.password }}
                                </td>
                        </tr>
                        <tr>
                                <td align="right">
                                        {{ form.remember }}
                                </td>
                                <td>
                                        <label for="" me on this computer.</label>
                                </td>
                        </tr>
                        <tr>
                                <td>&nbsp;</td>
                                <td>
                                <td>
                                        <input type="submit" value="Sign in"/>
                                </td>
                </table>
                <center><a href="" your email or password?</a></center>

        </fieldset>
        </form>
</div>

And the logout view is:

def logout(request):
    del request.session[users.SESSION_KEY]
    return HttpResponseRedirect("../")

Finally change the file django_src/django/views/auth/login.py and edit the following line:

LOGIN_URL = '/accounts/login/'

to make it read:

LOGIN_URL = '/my_login_page/'

where my_login_page is the page where you want you login page to be, if your login form is in all the pages, you may still have a page devoted to login, explaining why one should login to your system.

HTH,

PS: Should be wikified I guess.

--
Amit Upadhyay
Blog: http://www.rootshell.be/~upadhyay
+91-9867-359-701

Reply via email to