Django

Code

Changeset 3360

Show
Ignore:
Timestamp:
07/18/06 21:09:26 (2 years ago)
Author:
mtredinnick
Message:

Fixed #2332 -- Introduced is_authenticated() method on User and AnonymousUser? classes. Recommended its use over is_anonymous in the docs. Changed internal Django use to match this recommendation. Thanks to SmileyChris? and Gary Wilson for the patch.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/contrib/admin/templates/admin/base.html

    r3343 r3360  
    2121        {% block branding %}{% endblock %} 
    2222        </div> 
    23         {% if not user.is_anonymous %}{% if user.is_staff %} 
     23        {% if user.is_authenticated and user.is_staff %} 
    2424        <div id="user-tools">{% trans 'Welcome,' %} <strong>{% if user.first_name %}{{ user.first_name|escape }}{% else %}{{ user.username }}{% endif %}</strong>. {% block userlinks %}<a href="doc/">{% trans 'Documentation' %}</a> / <a href="password_change/">{% trans 'Change password' %}</a> / <a href="logout/">{% trans 'Log out' %}</a>{% endblock %}</div> 
    25         {% endif %}{% endif %} 
     25        {% endif %} 
    2626        {% block nav-global %}{% endblock %} 
    2727    </div> 
  • django/trunk/django/contrib/admin/views/decorators.py

    r3226 r3360  
    4747    """ 
    4848    def _checklogin(request, *args, **kwargs): 
    49         if not request.user.is_anonymous() and request.user.is_staff: 
     49        if request.user.is_authenticated() and request.user.is_staff: 
    5050            # The user is valid. Continue to the admin page. 
    5151            if request.POST.has_key('post_data'): 
  • django/trunk/django/contrib/auth/decorators.py

    r2954 r3360  
    1818    return _dec 
    1919 
    20 login_required = user_passes_test(lambda u: not u.is_anonymous()) 
     20login_required = user_passes_test(lambda u: u.is_authenticated()) 
    2121login_required.__doc__ = ( 
    2222    """ 
  • django/trunk/django/contrib/auth/models.py

    r3328 r3360  
    126126        "Always returns False. This is a way of comparing User objects to anonymous users." 
    127127        return False 
     128     
     129    def is_authenticated(self): 
     130        """Always return True. This is a way to tell if the user has been authenticated in templates. 
     131        """ 
     132        return True 
    128133 
    129134    def get_full_name(self): 
     
    294299    def is_anonymous(self): 
    295300        return True 
     301     
     302    def is_authenticated(self): 
     303        return False 
  • django/trunk/django/contrib/comments/templates/comments/form.html

    r3283 r3360  
    33<form {% if photos_optional or photos_required %}enctype="multipart/form-data" {% endif %}action="/comments/post/" method="post"> 
    44 
    5 {% if user.is_anonymous %} 
     5{% if user.is_authenticated %} 
     6<p>{% trans "Username:" %} <strong>{{ user.username }}</strong> (<a href="/accounts/logout/">{% trans "Log out" %}</a>)</p> 
     7{% else %} 
    68<p><label for="id_username">{% trans "Username:" %}</label> <input type="text" name="username" id="id_username" /><br />{% trans "Password:" %} <input type="password" name="password" id="id_password" /> (<a href="/accounts/password_reset/">{% trans "Forgotten your password?" %}</a>)</p> 
    7 {% else %} 
    8 <p>{% trans "Username:" %} <strong>{{ user.username }}</strong> (<a href="/accounts/logout/">{% trans "Log out" %}</a>)</p> 
    99{% endif %} 
    1010 
  • django/trunk/django/contrib/comments/templatetags/comments.py

    r2809 r3360  
    115115 
    116116        if not self.free: 
    117             if context.has_key('user') and not context['user'].is_anonymous(): 
     117            if context.has_key('user') and context['user'].is_authenticated(): 
    118118                user_id = context['user'].id 
    119119                context['user_can_moderate_comments'] = Comment.objects.user_is_moderator(context['user']) 
  • django/trunk/django/contrib/comments/views/comments.py

    r3226 r3360  
    6464            ), 
    6565        ]) 
    66         if not user.is_anonymous(): 
     66        if user.is_authenticated(): 
    6767            self["username"].is_required = False 
    6868            self["username"].validator_list = [] 
  • django/trunk/django/contrib/comments/views/karma.py

    r2809 r3360  
    1616    if not rating: 
    1717        raise Http404, "Invalid vote" 
    18     if request.user.is_anonymous(): 
     18    if not request.user.is_authenticated(): 
    1919        raise Http404, _("Anonymous users cannot vote") 
    2020    try: 
  • django/trunk/django/contrib/flatpages/views.py

    r2809 r3360  
    2323    # If registration is required for accessing this page, and the user isn't 
    2424    # logged in, redirect to the login page. 
    25     if f.registration_required and request.user.is_anonymous(): 
     25    if f.registration_required and not request.user.is_authenticated(): 
    2626        from django.contrib.auth.views import redirect_to_login 
    2727        return redirect_to_login(request.path) 
  • django/trunk/django/views/generic/create_update.py

    r3171 r3360  
    2121    """ 
    2222    if extra_context is None: extra_context = {} 
    23     if login_required and request.user.is_anonymous(): 
     23    if login_required and not request.user.is_authenticated(): 
    2424        return redirect_to_login(request.path) 
    2525 
     
    4040            new_object = manipulator.save(new_data) 
    4141 
    42             if not request.user.is_anonymous(): 
     42            if request.user.is_authenticated(): 
    4343                request.user.message_set.create(message="The %s was created successfully." % model._meta.verbose_name) 
    4444 
     
    8787    """ 
    8888    if extra_context is None: extra_context = {} 
    89     if login_required and request.user.is_anonymous(): 
     89    if login_required and not request.user.is_authenticated(): 
    9090        return redirect_to_login(request.path) 
    9191 
     
    114114            object = manipulator.save(new_data) 
    115115 
    116             if not request.user.is_anonymous(): 
     116            if request.user.is_authenticated(): 
    117117                request.user.message_set.create(message="The %s was updated successfully." % model._meta.verbose_name) 
    118118 
     
    163163    """ 
    164164    if extra_context is None: extra_context = {} 
    165     if login_required and request.user.is_anonymous(): 
     165    if login_required and not request.user.is_authenticated(): 
    166166        return redirect_to_login(request.path) 
    167167 
     
    181181    if request.method == 'POST': 
    182182        object.delete() 
    183         if not request.user.is_anonymous(): 
     183        if request.user.is_authenticated(): 
    184184            request.user.message_set.create(message="The %s was deleted." % model._meta.verbose_name) 
    185185        return HttpResponseRedirect(post_delete_redirect) 
  • django/trunk/docs/authentication.txt

    r3256 r3360  
    9696 
    9797    * ``is_anonymous()`` -- Always returns ``False``. This is a way of 
    98       comparing ``User`` objects to anonymous users. 
     98      differentiating ``User`` and ``AnonymousUser`` objects. Generally, you 
     99      should prefer using ``is_authenticated()`` to this method. 
     100 
     101    * ``is_authenticated()`` -- Always returns ``True``. This is a way to 
     102      tell if the user has been authenticated. 
    99103 
    100104    * ``get_full_name()`` -- Returns the ``first_name`` plus the ``last_name``, 
     
    220224    * ``id`` is always ``None``. 
    221225    * ``is_anonymous()`` returns ``True`` instead of ``False``. 
     226    * ``is_authenticated()`` returns ``False`` instead of ``True``. 
    222227    * ``has_perm()`` always returns ``False``. 
    223228    * ``set_password()``, ``check_password()``, ``save()``, ``delete()``, 
     
    255260representing the currently logged-in user. If a user isn't currently logged in, 
    256261``request.user`` will be set to an instance of ``AnonymousUser`` (see the 
    257 previous section). You can tell them apart with ``is_anonymous()``, like so:: 
    258  
    259     if request.user.is_anonymous(): 
     262previous section). You can tell them apart with ``is_authenticated()``, like so:: 
     263 
     264    if request.user.is_authenticated(): 
     265        # Do something for authenticated users. 
     266    else: 
    260267        # Do something for anonymous users. 
    261     else: 
    262         # Do something for logged-in users. 
    263268 
    264269.. _request objects: http://www.djangoproject.com/documentation/request_response/#httprequest-objects 
     
    324329 
    325330The simple, raw way to limit access to pages is to check 
    326 ``request.user.is_anonymous()`` and either redirect to a login page:: 
     331``request.user.is_authenticated()`` and either redirect to a login page:: 
    327332 
    328333    from django.http import HttpResponseRedirect 
    329334 
    330335    def my_view(request): 
    331         if request.user.is_anonymous(): 
     336        if not request.user.is_authenticated(): 
    332337            return HttpResponseRedirect('/login/?next=%s' % request.path) 
    333338        # ... 
     
    336341 
    337342    def my_view(request): 
    338         if request.user.is_anonymous(): 
     343        if not request.user.is_authenticated(): 
    339344            return render_to_response('myapp/login_error.html') 
    340345        # ... 
     
    440445 
    441446    def my_view(request): 
    442         if request.user.is_anonymous() or not request.user.has_perm('polls.can_vote'): 
     447        if not (request.user.is_authenticated() and request.user.has_perm('polls.can_vote')): 
    443448            return HttpResponse("You can't vote in this poll.") 
    444449        # ... 
     
    606611instance, is stored in the template variable ``{{ user }}``:: 
    607612 
    608     {% if user.is_anonymous %} 
     613    {% if user.is_authenticated %} 
     614        <p>Welcome, {{ user.username }}. Thanks for logging in.</p>     
     615    {% else %} 
    609616        <p>Welcome, new user. Please log in.</p> 
    610     {% else %} 
    611         <p>Welcome, {{ user.username }}. Thanks for logging in.</p> 
    612617    {% endif %} 
    613618 
  • django/trunk/docs/request_response.txt

    r3192 r3360  
    107107    logged-in user. If the user isn't currently logged in, ``user`` will be set 
    108108    to an instance of ``django.contrib.auth.models.AnonymousUser``. You 
    109     can tell them apart with ``is_anonymous()``, like so:: 
    110  
    111         if request.user.is_anonymous(): 
     109    can tell them apart with ``is_authenticated()``, like so:: 
     110 
     111        if request.user.is_authenticated(): 
     112            # Do something for logged-in users. 
     113        else: 
    112114            # Do something for anonymous users. 
    113         else: 
    114             # Do something for logged-in users. 
    115115 
    116116    ``user`` is only available if your Django installation has the