david / django-modelviews
Backup of an old repository with useful ideas. Initial goal: integrating REST to django admin (class-based views).
Clone this repository (size: 85.8 KB): HTTPS / SSH
$ hg clone http://code.welldev.org/django-modelviews
| commit 44: | add8532b04a8 |
| parent 43: | 82f1d682d560 |
| branch: | default |
Add more authentication backends
2 years ago
Changed (Δ2.6 KB):
raw changeset »
generic/rest_views.py (4 lines added, 0 lines removed)
tests/test_modelview/tests.py (37 lines added, 1 lines removed)
tests/urls.py (4 lines added, 1 lines removed)
Up to file-list generic/rest_views.py:
| … | … | @@ -12,7 +12,11 @@ except ImportError: |
12 |
12 |
def django_authentication(request, **kwargs): |
13 |
13 |
return request.user.is_authenticated() |
14 |
14 |
|
15 |
def django_superuser_authentication(request, **kwargs): |
|
16 |
return request.user.is_superuser |
|
15 |
17 |
|
18 |
def django_staff_authentication(request, **kwargs): |
|
19 |
return request.user.is_staff |
|
16 |
20 |
|
17 |
21 |
|
18 |
22 |
Up to file-list tests/test_modelview/tests.py:
| … | … | @@ -45,7 +45,13 @@ class ModelViewTest(TestCase): |
45 |
45 |
|
46 |
46 |
|
47 |
47 |
def test_restricting_authentication(self): |
48 |
|
|
48 |
a = Article.objects.all()[0] |
|
49 |
david = User.objects.all()[0] |
|
50 |
david.is_superuser = True |
|
51 |
david.save() |
|
52 |
will = User.objects.all()[1] |
|
53 |
will.is_staff = True |
|
54 |
will.save() |
|
49 |
55 |
response = self.client.get('/auth-articles/') |
50 |
56 |
self.assertEqual(response.status_code, 405) |
51 |
57 |
response = self.client.post('/auth-articles/', {}) |
| … | … | @@ -53,6 +59,36 @@ class ModelViewTest(TestCase): |
53 |
59 |
response = self.client.login(username='david', password='baz') |
54 |
60 |
response = self.client.get('/auth-articles/') |
55 |
61 |
self.assertEqual(response.status_code, 200) |
62 |
response = self.client.post('/auth-articles/', {'name': "Another article", |
|
63 |
'slug': "another-article", |
|
64 |
'body': "some other text"}) |
|
65 |
self.assertEqual(response.status_code, 302) |
|
66 |
self.assertEqual(Article.objects.count(), 2) |
|
67 |
response = self.client.login(username='will', password='baz') |
|
68 |
response = self.client.get('/auth-articles/') |
|
69 |
self.assertEqual(response.status_code, 200) |
|
70 |
response = self.client.post('/auth-articles/', {'name': "Another article", |
|
71 |
'slug': "another-article", |
|
72 |
'body': "some other text"}) |
|
73 |
self.assertEqual(response.status_code, 405) |
|
74 |
self.assertEqual(Article.objects.count(), 2) |
|
75 |
a2 = Article.objects.all()[1] |
|
76 |
response = self.client.login(username='david', password='baz') |
|
77 |
response = self.client.post('/auth-articles/%s/' % a2.slug, {'name': "Yet another article", |
|
78 |
'slug': "yet-another-article", |
|
79 |
'body': "some other text"}, |
|
80 |
REQUEST_METHOD='PUT') |
|
81 |
self.assertEqual(response.status_code, 405) |
|
82 |
a2 = Article.objects.all()[1] |
|
83 |
self.assertEqual(a2.name, u'Another article') |
|
84 |
response = self.client.login(username='will', password='baz') |
|
85 |
response = self.client.post('/auth-articles/%s/' % a2.slug, {'name': "Yet another article again", |
|
86 |
'slug': "yet-another-article-again", |
|
87 |
'body': "some other text"}, |
|
88 |
REQUEST_METHOD='PUT') |
|
89 |
self.assertEqual(response.status_code, 302) |
|
90 |
a2 = Article.objects.all()[1] |
|
91 |
self.assertEqual(a2.name, u'Yet another article again') |
|
56 |
92 |
|
57 |
93 |
|
58 |
94 |
def test_default_responder(self): |
Up to file-list tests/urls.py:
| … | … | @@ -12,7 +12,10 @@ rw_articles = ModelView(Article.objects. |
12 |
12 |
methods=('GET', 'POST', 'PUT', 'DELETE')) |
13 |
13 |
auth_articles = ModelView(Article.objects.all(), |
14 |
14 |
responders=(HtmlResponder,), |
15 |
methods={ |
|
15 |
methods={ |
|
16 |
'GET': django_authentication, |
|
17 |
'POST': django_superuser_authentication, |
|
18 |
'PUT': django_staff_authentication}) |
|
16 |
19 |
comments = ModelView(Comment.objects.all(), |
17 |
20 |
responders=(HtmlResponder,), |
18 |
21 |
methods=('GET',)) |
