Skip to content

Commit

Permalink
Remove staticviews. Use standard login/logout
Browse files Browse the repository at this point in the history
  • Loading branch information
tomchristie committed Feb 21, 2012
1 parent b074754 commit 66eabe8
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 31 deletions.
4 changes: 1 addition & 3 deletions djangorestframework/renderers.py
Expand Up @@ -335,7 +335,7 @@ def render(self, obj=None, media_type=None):
context = RequestContext(self.view.request, {
'content': content,
'view': self.view,
'request': self.view.request, # TODO: remove
'request': self.view.request,
'response': self.view.response,
'description': description,
'name': name,
Expand All @@ -344,8 +344,6 @@ def render(self, obj=None, media_type=None):
'available_formats': self.view._rendered_formats,
'put_form': put_form_instance,
'post_form': post_form_instance,
'login_url': login_url,
'logout_url': logout_url,
'FORMAT_PARAM': self._FORMAT_QUERY_PARAM,
'METHOD_PARAM': getattr(self.view, '_METHOD_PARAM', None),
})
Expand Down
11 changes: 9 additions & 2 deletions djangorestframework/templates/djangorestframework/base.html
Expand Up @@ -20,8 +20,15 @@
<h1 id="site-name">{% block branding %}<a href='http://django-rest-framework.org'>Django REST framework</a> <span class="version"> v {{ version }}</span>{% endblock %}</h1>
</div>
<div id="user-tools">
{% if user.is_active %}Welcome, {{ user }}.{% if logout_url %} <a href='{{ logout_url }}'>Log out</a>{% endif %}{% else %}Anonymous {% if login_url %}<a href='{{ login_url }}'>Log in</a>{% endif %}{% endif %}
{% block userlinks %}{% endblock %}
{% block userlinks %}
{% if user.is_active %}
Welcome, {{ user }}.
<a href='{% url djangorestframework:logout %}?next={{ request.path }}'>Log out</a>
{% else %}
Anonymous
<a href='{% url djangorestframework:login %}?next={{ request.path }}'>Log in</a>
{% endif %}
{% endblock %}
</div>
{% block nav-global %}{% endblock %}
</div>
Expand Down
Expand Up @@ -17,7 +17,7 @@ <h1 id="site-name">Django REST framework</h1>

<div id="content" class="colM">
<div id="content-main">
<form method="post" action="{% url djangorestframework.utils.staticviews.api_login %}" id="login-form">
<form method="post" action="{% url djangorestframework:login %}" id="login-form">
{% csrf_token %}
<div class="form-row">
<label for="id_username">Username:</label> {{ form.username }}
Expand Down
15 changes: 13 additions & 2 deletions djangorestframework/tests/accept.py
@@ -1,3 +1,4 @@
from django.conf.urls.defaults import patterns, url, include
from django.test import TestCase
from djangorestframework.compat import RequestFactory
from djangorestframework.views import View
Expand All @@ -13,9 +14,19 @@
OPERA_11_0_MSIE_USER_AGENT = 'Mozilla/4.0 (compatible; MSIE 8.0; X11; Linux x86_64; pl) Opera 11.00'
OPERA_11_0_OPERA_USER_AGENT = 'Opera/9.80 (X11; Linux x86_64; U; pl) Presto/2.7.62 Version/11.00'


urlpatterns = patterns('',
url(r'^api', include('djangorestframework.urls', namespace='djangorestframework'))
)


class UserAgentMungingTest(TestCase):
"""We need to fake up the accept headers when we deal with MSIE. Blergh.
http://www.gethifi.com/blog/browser-rest-http-accept-headers"""
"""
We need to fake up the accept headers when we deal with MSIE. Blergh.
http://www.gethifi.com/blog/browser-rest-http-accept-headers
"""

urls = 'djangorestframework.tests.accept'

def setUp(self):

Expand Down
2 changes: 1 addition & 1 deletion djangorestframework/tests/oauthentication.py
Expand Up @@ -27,7 +27,7 @@ def get(self, request):
urlpatterns = patterns('',
url(r'^$', oauth_required(ClientView.as_view())),
url(r'^oauth/', include('oauth_provider.urls')),
url(r'^accounts/login/$', 'djangorestframework.utils.staticviews.api_login'),
url(r'^restframework/', include('djangorestframework.urls', namespace='djangorestframework')),
)


Expand Down
3 changes: 2 additions & 1 deletion djangorestframework/tests/renderers.py
@@ -1,6 +1,6 @@
import re

from django.conf.urls.defaults import patterns, url
from django.conf.urls.defaults import patterns, url, include
from django.test import TestCase

from djangorestframework import status
Expand Down Expand Up @@ -73,6 +73,7 @@ def get(self, request, **kwargs):
url(r'^jsonp/nojsonrenderer$', MockGETView.as_view(renderers=[JSONPRenderer])),
url(r'^html$', HTMLView.as_view()),
url(r'^html1$', HTMLView1.as_view()),
url(r'^api', include('djangorestframework.urls', namespace='djangorestframework'))
)


Expand Down
12 changes: 6 additions & 6 deletions djangorestframework/tests/views.py
@@ -1,4 +1,5 @@
from django.conf.urls.defaults import patterns, url
from django.core.urlresolvers import reverse
from django.conf.urls.defaults import patterns, url, include
from django.http import HttpResponse
from django.test import TestCase
from django.test import Client
Expand Down Expand Up @@ -45,14 +46,13 @@ class MockResourceModel(models.Model):
model = MockResourceModel
fields = ('foo', 'bar', 'baz')

urlpatterns = patterns('djangorestframework.utils.staticviews',
url(r'^accounts/login$', 'api_login'),
url(r'^accounts/logout$', 'api_logout'),
urlpatterns = patterns('',
url(r'^mock/$', MockView.as_view()),
url(r'^mock/final/$', MockViewFinal.as_view()),
url(r'^resourcemock/$', ResourceMockView.as_view()),
url(r'^model/$', ListOrCreateModelView.as_view(resource=MockResource)),
url(r'^model/(?P<pk>[^/]+)/$', InstanceModelView.as_view(resource=MockResource)),
url(r'^restframework/', include('djangorestframework.urls', namespace='djangorestframework')),
)

class BaseViewTests(TestCase):
Expand Down Expand Up @@ -123,13 +123,13 @@ class ExtraViewsTests(TestCase):

def test_login_view(self):
"""Ensure the login view exists"""
response = self.client.get('/accounts/login')
response = self.client.get(reverse('djangorestframework:login'))
self.assertEqual(response.status_code, 200)
self.assertEqual(response['Content-Type'].split(';')[0], 'text/html')

def test_logout_view(self):
"""Ensure the logout view exists"""
response = self.client.get('/accounts/logout')
response = self.client.get(reverse('djangorestframework:logout'))
self.assertEqual(response.status_code, 200)
self.assertEqual(response['Content-Type'].split(';')[0], 'text/html')

Expand Down
11 changes: 7 additions & 4 deletions djangorestframework/urls.py
@@ -1,6 +1,9 @@
from django.conf.urls.defaults import patterns
from django.conf.urls.defaults import patterns, url

urlpatterns = patterns('djangorestframework.utils.staticviews',
(r'^accounts/login/$', 'api_login'),
(r'^accounts/logout/$', 'api_logout'),

template_name = {'template_name': 'djangorestframework/login.html'}

urlpatterns = patterns('django.contrib.auth.views',
url(r'^login/$', 'login', template_name, name='login'),
url(r'^logout/$', 'logout', template_name, name='logout'),
)
17 changes: 9 additions & 8 deletions docs/howto/setup.rst
Expand Up @@ -53,16 +53,17 @@ YAML support is optional, and requires `PyYAML`_.
Login / Logout
--------------

Django REST framework includes login and logout views that are useful if
you're using the self-documenting API::
Django REST framework includes login and logout views that are needed if
you're using the self-documenting API.

from django.conf.urls.defaults import patterns
Make sure you include the following in your `urlconf`::

urlpatterns = patterns('djangorestframework.views',
# Add your resources here
(r'^accounts/login/$', 'api_login'),
(r'^accounts/logout/$', 'api_logout'),
)
from django.conf.urls.defaults import patterns, url

urlpatterns = patterns('',
...
url(r'^restframework', include('djangorestframework.urls', namespace='djangorestframework'))
)

.. _django.contrib.staticfiles: https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/
.. _django-staticfiles: http://pypi.python.org/pypi/django-staticfiles/
Expand Down
6 changes: 6 additions & 0 deletions docs/index.rst
Expand Up @@ -64,6 +64,12 @@ To add Django REST framework to a Django project:

* Ensure that the ``djangorestframework`` directory is on your ``PYTHONPATH``.
* Add ``djangorestframework`` to your ``INSTALLED_APPS``.
* Add the following to your URLconf. (To include the REST framework Login/Logout views.)::

urlpatterns = patterns('',
...
url(r'^restframework', include('djangorestframework.urls', namespace='djangorestframework'))
)

For more information on settings take a look at the :ref:`setup` section.

Expand Down
5 changes: 2 additions & 3 deletions examples/urls.py
@@ -1,4 +1,4 @@
from django.conf.urls.defaults import patterns, include
from django.conf.urls.defaults import patterns, include, url
from sandbox.views import Sandbox
try:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
Expand All @@ -15,8 +15,7 @@
(r'^pygments/', include('pygments_api.urls')),
(r'^blog-post/', include('blogpost.urls')),
(r'^permissions-example/', include('permissionsexample.urls')),

(r'^', include('djangorestframework.urls')),
url(r'^restframework/', include('djangorestframework.urls', namespace='djangorestframework')),
)

urlpatterns += staticfiles_urlpatterns()

0 comments on commit 66eabe8

Please sign in to comment.