Changeset 8236
- Timestamp:
- 08/08/08 13:07:33 (1 month ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/contrib/admin/options.py
r8192 r8236 359 359 pk_value = new_object._get_pk_val() 360 360 LogEntry.objects.log_action(request.user.id, ContentType.objects.get_for_model(self.model).id, pk_value, force_unicode(new_object), ADDITION) 361 msg = _('The %(name)s "%(obj)s" was added successfully.') % {'name': force_unicode(opts.verbose_name), 'obj': new_object}361 msg = _('The %(name)s "%(obj)s" was added successfully.') % {'name': force_unicode(opts.verbose_name), 'obj': force_unicode(new_object)} 362 362 # Here, we distinguish between different save types by checking for 363 363 # the presence of keys in request.POST. … … 429 429 LogEntry.objects.log_action(request.user.id, ContentType.objects.get_for_model(self.model).id, pk_value, force_unicode(new_object), CHANGE, change_message) 430 430 431 msg = _('The %(name)s "%(obj)s" was changed successfully.') % {'name': force_unicode(opts.verbose_name), 'obj': new_object}431 msg = _('The %(name)s "%(obj)s" was changed successfully.') % {'name': force_unicode(opts.verbose_name), 'obj': force_unicode(new_object)} 432 432 if request.POST.has_key("_continue"): 433 433 request.user.message_set.create(message=msg + ' ' + _("You may edit it again below.")) django/trunk/tests/regressiontests/admin_views/models.py
r7967 r8236 4 4 class Section(models.Model): 5 5 """ 6 A simple section that links to articles, to test linking to related items 6 A simple section that links to articles, to test linking to related items 7 7 in admin views. 8 8 """ … … 13 13 A simple article to test admin views. Test backwards compatibility. 14 14 """ 15 title = models.CharField(max_length=100) 15 16 content = models.TextField() 16 17 date = models.DateTimeField() 17 18 section = models.ForeignKey(Section) 18 19 20 def __unicode__(self): 21 return self.title 22 19 23 class ArticleAdmin(admin.ModelAdmin): 20 24 list_display = ('content', 'date') 21 25 list_filter = ('date',) 22 26 23 27 def changelist_view(self, request): 24 28 "Test that extra_context works" … … 41 45 object_history_template = 'custom_admin/object_history.html' 42 46 delete_confirmation_template = 'custom_admin/delete_confirmation.html' 43 47 44 48 def changelist_view(self, request): 45 49 "Test that extra_context works" … … 52 56 class ModelWithStringPrimaryKey(models.Model): 53 57 id = models.CharField(max_length=255, primary_key=True) 54 58 55 59 def __unicode__(self): 56 60 return self.id 57 61 58 62 admin.site.register(Article, ArticleAdmin) 59 63 admin.site.register(CustomArticle, CustomArticleAdmin) django/trunk/tests/regressiontests/admin_views/tests.py
r8055 r8236 1 # coding: utf-8 1 2 2 3 from django.test import TestCase … … 18 19 class AdminViewPermissionsTest(TestCase): 19 20 """Tests for Admin Views Permissions.""" 20 21 21 22 fixtures = ['admin-views-users.xml'] 22 23 23 24 def setUp(self): 24 25 """Test setup.""" 25 # Setup permissions, for our users who can add, change, and delete. 26 # Setup permissions, for our users who can add, change, and delete. 26 27 # We can't put this into the fixture, because the content type id 27 28 # and the permission id could be different on each run of the test. 28 29 29 30 opts = Article._meta 30 31 31 32 # User who can add Articles 32 33 add_user = User.objects.get(username='adduser') 33 34 add_user.user_permissions.add(get_perm(Article, 34 35 opts.get_add_permission())) 35 36 36 37 # User who can change Articles 37 38 change_user = User.objects.get(username='changeuser') 38 39 change_user.user_permissions.add(get_perm(Article, 39 40 opts.get_change_permission())) 40 41 41 42 # User who can delete Articles 42 43 delete_user = User.objects.get(username='deleteuser') 43 44 delete_user.user_permissions.add(get_perm(Article, 44 45 opts.get_delete_permission())) 45 46 46 47 delete_user.user_permissions.add(get_perm(Section, 47 48 Section._meta.get_delete_permission())) 48 49 49 50 # login POST dicts 50 51 self.super_login = {'post_data': _encode_post_data({}), … … 82 83 """ 83 84 self.client.post('/test_admin/admin/', self.super_login) 84 85 85 86 request = self.client.get( 86 87 '/test_admin/admin/admin_views/article/add' … … 93 94 """ 94 95 Make sure only staff members can log in. 95 96 96 97 Successful posts to the login page will redirect to the orignal url. 97 Unsuccessfull attempts will continue to render the login page with 98 Unsuccessfull attempts will continue to render the login page with 98 99 a 200 status code. 99 100 """ … … 105 106 self.failIf(login.context) 106 107 self.client.get('/test_admin/admin/logout/') 107 108 108 109 # Test if user enters e-mail address 109 110 request = self.client.get('/test_admin/admin/') … … 119 120 login = self.client.post('/test_admin/admin/', self.super_email_login) 120 121 self.assertContains(login, "Usernames cannot contain the '@' character") 121 122 122 123 # Add User 123 124 request = self.client.get('/test_admin/admin/') … … 127 128 self.failIf(login.context) 128 129 self.client.get('/test_admin/admin/logout/') 129 130 130 131 # Change User 131 132 request = self.client.get('/test_admin/admin/') … … 135 136 self.failIf(login.context) 136 137 self.client.get('/test_admin/admin/logout/') 137 138 138 139 # Delete User 139 140 request = self.client.get('/test_admin/admin/') … … 143 144 self.failIf(login.context) 144 145 self.client.get('/test_admin/admin/logout/') 145 146 146 147 # Regular User should not be able to login. 147 148 request = self.client.get('/test_admin/admin/') … … 154 155 def testAddView(self): 155 156 """Test add view restricts access and actually adds items.""" 156 157 add_dict = {'content': '<p>great article</p>', 157 158 add_dict = {'title' : 'Døm ikke', 159 'content': '<p>great article</p>', 158 160 'date_0': '2008-03-18', 'date_1': '10:54:39', 159 161 'section': 1} 160 162 161 163 # Change User should not have access to add articles 162 164 self.client.get('/test_admin/admin/') … … 169 171 self.failUnlessEqual(Article.objects.all().count(), 1) 170 172 self.client.get('/test_admin/admin/logout/') 171 173 172 174 # Add user may login and POST to add view, then redirect to admin root 173 175 self.client.get('/test_admin/admin/') … … 177 179 self.failUnlessEqual(Article.objects.all().count(), 2) 178 180 self.client.get('/test_admin/admin/logout/') 179 181 180 182 # Super can add too, but is redirected to the change list view 181 183 self.client.get('/test_admin/admin/') … … 185 187 self.failUnlessEqual(Article.objects.all().count(), 3) 186 188 self.client.get('/test_admin/admin/logout/') 187 189 188 190 # Check and make sure that if user expires, data still persists 189 191 post = self.client.post('/test_admin/admin/admin_views/article/add/', add_dict) … … 197 199 def testChangeView(self): 198 200 """Change view should restrict access and allow users to edit items.""" 199 200 change_dict = {'content': '<p>edited article</p>', 201 'date_0': '2008-03-18', 'date_1': '10:54:39', 202 'section': 1} 203 201 202 change_dict = {'title' : 'Ikke fordømt', 203 'content': '<p>edited article</p>', 204 'date_0': '2008-03-18', 'date_1': '10:54:39', 205 'section': 1} 206 204 207 # add user shoud not be able to view the list of article or change any of them 205 208 self.client.get('/test_admin/admin/') … … 212 215 self.failUnlessEqual(post.status_code, 403) 213 216 self.client.get('/test_admin/admin/logout/') 214 217 215 218 # change user can view all items and edit them 216 219 self.client.get('/test_admin/admin/') … … 228 231 self.client.get('/test_admin/admin/') 229 232 self.client.post('/test_admin/admin/', self.super_login) 230 233 231 234 # Test custom change list template with custom extra context 232 235 request = self.client.get('/test_admin/admin/admin_views/customarticle/') … … 234 237 self.assert_("var hello = 'Hello!';" in request.content) 235 238 self.assertTemplateUsed(request, 'custom_admin/change_list.html') 236 239 237 240 # Test custom change form template 238 241 request = self.client.get('/test_admin/admin/admin_views/customarticle/add/') 239 242 self.assertTemplateUsed(request, 'custom_admin/change_form.html') 240 243 241 244 # Add an article so we can test delete and history views 242 245 post = self.client.post('/test_admin/admin/admin_views/customarticle/add/', { … … 247 250 self.assertRedirects(post, '/test_admin/admin/admin_views/customarticle/') 248 251 self.failUnlessEqual(CustomArticle.objects.all().count(), 1) 249 252 250 253 # Test custom delete and object history templates 251 254 request = self.client.get('/test_admin/admin/admin_views/customarticle/1/delete/') … … 253 256 request = self.client.get('/test_admin/admin/admin_views/customarticle/1/history/') 254 257 self.assertTemplateUsed(request, 'custom_admin/object_history.html') 255 258 256 259 self.client.get('/test_admin/admin/logout/') 257 260 … … 260 263 self.assertEqual(admin.site.index_template, None) 261 264 self.assertEqual(admin.site.login_template, None) 262 265 263 266 self.client.get('/test_admin/admin/logout/') 264 267 request = self.client.get('/test_admin/admin/') … … 267 270 request = self.client.get('/test_admin/admin/') 268 271 self.assertTemplateUsed(request, 'admin/index.html') 269 272 270 273 self.client.get('/test_admin/admin/logout/') 271 274 admin.site.login_template = 'custom_admin/login.html' … … 278 281 self.assertTemplateUsed(request, 'custom_admin/index.html') 279 282 self.assert_('Hello from a custom index template' in request.content) 280 283 281 284 # Finally, using monkey patching check we can inject custom_context arguments in to index 282 285 original_index = admin.site.index … … 288 291 self.assertTemplateUsed(request, 'custom_admin/index.html') 289 292 self.assert_('Hello from a custom index template *bar*' in request.content) 290 293 291 294 self.client.get('/test_admin/admin/logout/') 292 295 del admin.site.index # Resets to using the original … … 296 299 def testDeleteView(self): 297 300 """Delete view should restrict access and actually delete items.""" 298 301 299 302 delete_dict = {'post': 'yes'} 300 303 301 304 # add user shoud not be able to delete articles 302 305 self.client.get('/test_admin/admin/') … … 308 311 self.failUnlessEqual(Article.objects.all().count(), 1) 309 312 self.client.get('/test_admin/admin/logout/') 310 313 311 314 # Delete user can delete 312 315 self.client.get('/test_admin/admin/') … … 315 318 # test response contains link to related Article 316 319 self.assertContains(response, "admin_views/article/1/") 317 320 318 321 response = self.client.get('/test_admin/admin/admin_views/article/1/delete/') 319 322 self.failUnlessEqual(response.status_code, 200) … … 325 328 class AdminViewStringPrimaryKeyTest(TestCase): 326 329 fixtures = ['admin-views-users.xml', 'string-primary-key.xml'] 327 330 328 331 def __init__(self, *args): 329 332 super(AdminViewStringPrimaryKeyTest, self).__init__(*args) 330 333 self.pk = """abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 -_.!~*'() ;/?:@&=+$, <>#%" {}|\^[]`""" 331 334 332 335 def setUp(self): 333 336 self.client.login(username='super', password='secret') 334 337 content_type_pk = ContentType.objects.get_for_model(ModelWithStringPrimaryKey).pk 335 338 LogEntry.objects.log_action(100, content_type_pk, self.pk, self.pk, 2, change_message='') 336 339 337 340 def tearDown(self): 338 341 self.client.logout() 339 342 340 343 def test_get_change_view(self): 341 344 "Retrieving the object using urlencoded form of primary key should work" … … 343 346 self.assertContains(response, escape(self.pk)) 344 347 self.failUnlessEqual(response.status_code, 200) 345 348 346 349 def test_changelist_to_changeform_link(self): 347 350 "The link from the changelist referring to the changeform of the object should be quoted" … … 349 352 should_contain = """<tr class="row1"><th><a href="%s/">%s</a></th></tr>""" % (quote(self.pk), escape(self.pk)) 350 353 self.assertContains(response, should_contain) 351 354 352 355 def test_recentactions_link(self): 353 356 "The link from the recent actions list referring to the changeform of the object should be quoted" … … 355 358 should_contain = """<a href="admin_views/modelwithstringprimarykey/%s/">%s</a>""" % (quote(self.pk), escape(self.pk)) 356 359 self.assertContains(response, should_contain) 357 360 358 361 def test_deleteconfirmation_link(self): 359 362 "The link from the delete confirmation page referring back to the changeform of the object should be quoted"
