Changeset 3360
- Timestamp:
- 07/18/06 21:09:26 (2 years ago)
- Files:
-
- django/trunk/django/contrib/admin/templates/admin/base.html (modified) (1 diff)
- django/trunk/django/contrib/admin/views/decorators.py (modified) (1 diff)
- django/trunk/django/contrib/auth/decorators.py (modified) (1 diff)
- django/trunk/django/contrib/auth/models.py (modified) (2 diffs)
- django/trunk/django/contrib/comments/templates/comments/form.html (modified) (1 diff)
- django/trunk/django/contrib/comments/templatetags/comments.py (modified) (1 diff)
- django/trunk/django/contrib/comments/views/comments.py (modified) (1 diff)
- django/trunk/django/contrib/comments/views/karma.py (modified) (1 diff)
- django/trunk/django/contrib/flatpages/views.py (modified) (1 diff)
- django/trunk/django/views/generic/create_update.py (modified) (6 diffs)
- django/trunk/docs/authentication.txt (modified) (7 diffs)
- django/trunk/docs/request_response.txt (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/contrib/admin/templates/admin/base.html
r3343 r3360 21 21 {% block branding %}{% endblock %} 22 22 </div> 23 {% if not user.is_anonymous %}{% ifuser.is_staff %}23 {% if user.is_authenticated and user.is_staff %} 24 24 <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 %} 26 26 {% block nav-global %}{% endblock %} 27 27 </div> django/trunk/django/contrib/admin/views/decorators.py
r3226 r3360 47 47 """ 48 48 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: 50 50 # The user is valid. Continue to the admin page. 51 51 if request.POST.has_key('post_data'): django/trunk/django/contrib/auth/decorators.py
r2954 r3360 18 18 return _dec 19 19 20 login_required = user_passes_test(lambda u: not u.is_anonymous())20 login_required = user_passes_test(lambda u: u.is_authenticated()) 21 21 login_required.__doc__ = ( 22 22 """ django/trunk/django/contrib/auth/models.py
r3328 r3360 126 126 "Always returns False. This is a way of comparing User objects to anonymous users." 127 127 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 128 133 129 134 def get_full_name(self): … … 294 299 def is_anonymous(self): 295 300 return True 301 302 def is_authenticated(self): 303 return False django/trunk/django/contrib/comments/templates/comments/form.html
r3283 r3360 3 3 <form {% if photos_optional or photos_required %}enctype="multipart/form-data" {% endif %}action="/comments/post/" method="post"> 4 4 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 %} 6 8 <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>9 9 {% endif %} 10 10 django/trunk/django/contrib/comments/templatetags/comments.py
r2809 r3360 115 115 116 116 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(): 118 118 user_id = context['user'].id 119 119 context['user_can_moderate_comments'] = Comment.objects.user_is_moderator(context['user']) django/trunk/django/contrib/comments/views/comments.py
r3226 r3360 64 64 ), 65 65 ]) 66 if not user.is_anonymous():66 if user.is_authenticated(): 67 67 self["username"].is_required = False 68 68 self["username"].validator_list = [] django/trunk/django/contrib/comments/views/karma.py
r2809 r3360 16 16 if not rating: 17 17 raise Http404, "Invalid vote" 18 if request.user.is_anonymous():18 if not request.user.is_authenticated(): 19 19 raise Http404, _("Anonymous users cannot vote") 20 20 try: django/trunk/django/contrib/flatpages/views.py
r2809 r3360 23 23 # If registration is required for accessing this page, and the user isn't 24 24 # 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(): 26 26 from django.contrib.auth.views import redirect_to_login 27 27 return redirect_to_login(request.path) django/trunk/django/views/generic/create_update.py
r3171 r3360 21 21 """ 22 22 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(): 24 24 return redirect_to_login(request.path) 25 25 … … 40 40 new_object = manipulator.save(new_data) 41 41 42 if not request.user.is_anonymous():42 if request.user.is_authenticated(): 43 43 request.user.message_set.create(message="The %s was created successfully." % model._meta.verbose_name) 44 44 … … 87 87 """ 88 88 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(): 90 90 return redirect_to_login(request.path) 91 91 … … 114 114 object = manipulator.save(new_data) 115 115 116 if not request.user.is_anonymous():116 if request.user.is_authenticated(): 117 117 request.user.message_set.create(message="The %s was updated successfully." % model._meta.verbose_name) 118 118 … … 163 163 """ 164 164 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(): 166 166 return redirect_to_login(request.path) 167 167 … … 181 181 if request.method == 'POST': 182 182 object.delete() 183 if not request.user.is_anonymous():183 if request.user.is_authenticated(): 184 184 request.user.message_set.create(message="The %s was deleted." % model._meta.verbose_name) 185 185 return HttpResponseRedirect(post_delete_redirect) django/trunk/docs/authentication.txt
r3256 r3360 96 96 97 97 * ``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. 99 103 100 104 * ``get_full_name()`` -- Returns the ``first_name`` plus the ``last_name``, … … 220 224 * ``id`` is always ``None``. 221 225 * ``is_anonymous()`` returns ``True`` instead of ``False``. 226 * ``is_authenticated()`` returns ``False`` instead of ``True``. 222 227 * ``has_perm()`` always returns ``False``. 223 228 * ``set_password()``, ``check_password()``, ``save()``, ``delete()``, … … 255 260 representing the currently logged-in user. If a user isn't currently logged in, 256 261 ``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(): 262 previous 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: 260 267 # Do something for anonymous users. 261 else:262 # Do something for logged-in users.263 268 264 269 .. _request objects: http://www.djangoproject.com/documentation/request_response/#httprequest-objects … … 324 329 325 330 The simple, raw way to limit access to pages is to check 326 ``request.user.is_a nonymous()`` and either redirect to a login page::331 ``request.user.is_authenticated()`` and either redirect to a login page:: 327 332 328 333 from django.http import HttpResponseRedirect 329 334 330 335 def my_view(request): 331 if request.user.is_anonymous():336 if not request.user.is_authenticated(): 332 337 return HttpResponseRedirect('/login/?next=%s' % request.path) 333 338 # ... … … 336 341 337 342 def my_view(request): 338 if request.user.is_anonymous():343 if not request.user.is_authenticated(): 339 344 return render_to_response('myapp/login_error.html') 340 345 # ... … … 440 445 441 446 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')): 443 448 return HttpResponse("You can't vote in this poll.") 444 449 # ... … … 606 611 instance, is stored in the template variable ``{{ user }}``:: 607 612 608 {% if user.is_anonymous %} 613 {% if user.is_authenticated %} 614 <p>Welcome, {{ user.username }}. Thanks for logging in.</p> 615 {% else %} 609 616 <p>Welcome, new user. Please log in.</p> 610 {% else %}611 <p>Welcome, {{ user.username }}. Thanks for logging in.</p>612 617 {% endif %} 613 618 django/trunk/docs/request_response.txt
r3192 r3360 107 107 logged-in user. If the user isn't currently logged in, ``user`` will be set 108 108 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: 112 114 # Do something for anonymous users. 113 else:114 # Do something for logged-in users.115 115 116 116 ``user`` is only available if your Django installation has the
