Django

Code

Changeset 8236

Show
Ignore:
Timestamp:
08/08/08 13:07:33 (1 month ago)
Author:
mtredinnick
Message:

Added a few force_unicode() calls around objects in the admin. Required for
Python 2.3 compatibility. Patch from nfg.

Refs #8151, #8153.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/contrib/admin/options.py

    r8192 r8236  
    359359        pk_value = new_object._get_pk_val() 
    360360        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)
    362362        # Here, we distinguish between different save types by checking for 
    363363        # the presence of keys in request.POST. 
     
    429429        LogEntry.objects.log_action(request.user.id, ContentType.objects.get_for_model(self.model).id, pk_value, force_unicode(new_object), CHANGE, change_message) 
    430430 
    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)
    432432        if request.POST.has_key("_continue"): 
    433433            request.user.message_set.create(message=msg + ' ' + _("You may edit it again below.")) 
  • django/trunk/tests/regressiontests/admin_views/models.py

    r7967 r8236  
    44class Section(models.Model): 
    55    """ 
    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 
    77    in admin views. 
    88    """ 
     
    1313    A simple article to test admin views. Test backwards compatibility. 
    1414    """ 
     15    title = models.CharField(max_length=100) 
    1516    content = models.TextField() 
    1617    date = models.DateTimeField() 
    1718    section = models.ForeignKey(Section) 
    1819 
     20    def __unicode__(self): 
     21        return self.title 
     22 
    1923class ArticleAdmin(admin.ModelAdmin): 
    2024    list_display = ('content', 'date') 
    2125    list_filter = ('date',) 
    22      
     26 
    2327    def changelist_view(self, request): 
    2428        "Test that extra_context works" 
     
    4145    object_history_template = 'custom_admin/object_history.html' 
    4246    delete_confirmation_template = 'custom_admin/delete_confirmation.html' 
    43      
     47 
    4448    def changelist_view(self, request): 
    4549        "Test that extra_context works" 
     
    5256class ModelWithStringPrimaryKey(models.Model): 
    5357    id = models.CharField(max_length=255, primary_key=True) 
    54      
     58 
    5559    def __unicode__(self): 
    5660        return self.id 
    57          
     61 
    5862admin.site.register(Article, ArticleAdmin) 
    5963admin.site.register(CustomArticle, CustomArticleAdmin) 
  • django/trunk/tests/regressiontests/admin_views/tests.py

    r8055 r8236  
     1# coding: utf-8 
    12 
    23from django.test import TestCase 
     
    1819class AdminViewPermissionsTest(TestCase): 
    1920    """Tests for Admin Views Permissions.""" 
    20      
     21 
    2122    fixtures = ['admin-views-users.xml'] 
    22      
     23 
    2324    def setUp(self): 
    2425        """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. 
    2627        # We can't put this into the fixture, because the content type id 
    2728        # and the permission id could be different on each run of the test. 
    28          
     29 
    2930        opts = Article._meta 
    30          
     31 
    3132        # User who can add Articles 
    3233        add_user = User.objects.get(username='adduser') 
    3334        add_user.user_permissions.add(get_perm(Article, 
    3435            opts.get_add_permission())) 
    35          
     36 
    3637        # User who can change Articles 
    3738        change_user = User.objects.get(username='changeuser') 
    3839        change_user.user_permissions.add(get_perm(Article, 
    3940            opts.get_change_permission())) 
    40          
     41 
    4142        # User who can delete Articles 
    4243        delete_user = User.objects.get(username='deleteuser') 
    4344        delete_user.user_permissions.add(get_perm(Article, 
    4445            opts.get_delete_permission())) 
    45          
     46 
    4647        delete_user.user_permissions.add(get_perm(Section, 
    4748            Section._meta.get_delete_permission())) 
    48          
     49 
    4950        # login POST dicts 
    5051        self.super_login = {'post_data': _encode_post_data({}), 
     
    8283        """ 
    8384        self.client.post('/test_admin/admin/', self.super_login) 
    84          
     85 
    8586        request = self.client.get( 
    8687            '/test_admin/admin/admin_views/article/add' 
     
    9394        """ 
    9495        Make sure only staff members can log in. 
    95          
     96 
    9697        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 
    9899        a 200 status code. 
    99100        """ 
     
    105106        self.failIf(login.context) 
    106107        self.client.get('/test_admin/admin/logout/') 
    107          
     108 
    108109        # Test if user enters e-mail address 
    109110        request = self.client.get('/test_admin/admin/') 
     
    119120        login = self.client.post('/test_admin/admin/', self.super_email_login) 
    120121        self.assertContains(login, "Usernames cannot contain the '@' character") 
    121          
     122 
    122123        # Add User 
    123124        request = self.client.get('/test_admin/admin/') 
     
    127128        self.failIf(login.context) 
    128129        self.client.get('/test_admin/admin/logout/') 
    129          
     130 
    130131        # Change User 
    131132        request = self.client.get('/test_admin/admin/') 
     
    135136        self.failIf(login.context) 
    136137        self.client.get('/test_admin/admin/logout/') 
    137          
     138 
    138139        # Delete User 
    139140        request = self.client.get('/test_admin/admin/') 
     
    143144        self.failIf(login.context) 
    144145        self.client.get('/test_admin/admin/logout/') 
    145          
     146 
    146147        # Regular User should not be able to login. 
    147148        request = self.client.get('/test_admin/admin/') 
     
    154155    def testAddView(self): 
    155156        """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>', 
    158160                    'date_0': '2008-03-18', 'date_1': '10:54:39', 
    159161                    'section': 1} 
    160          
     162 
    161163        # Change User should not have access to add articles 
    162164        self.client.get('/test_admin/admin/') 
     
    169171        self.failUnlessEqual(Article.objects.all().count(), 1) 
    170172        self.client.get('/test_admin/admin/logout/') 
    171          
     173 
    172174        # Add user may login and POST to add view, then redirect to admin root 
    173175        self.client.get('/test_admin/admin/') 
     
    177179        self.failUnlessEqual(Article.objects.all().count(), 2) 
    178180        self.client.get('/test_admin/admin/logout/') 
    179          
     181 
    180182        # Super can add too, but is redirected to the change list view 
    181183        self.client.get('/test_admin/admin/') 
     
    185187        self.failUnlessEqual(Article.objects.all().count(), 3) 
    186188        self.client.get('/test_admin/admin/logout/') 
    187          
     189 
    188190        # Check and make sure that if user expires, data still persists 
    189191        post = self.client.post('/test_admin/admin/admin_views/article/add/', add_dict) 
     
    197199    def testChangeView(self): 
    198200        """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 
    204207        # add user shoud not be able to view the list of article or change any of them 
    205208        self.client.get('/test_admin/admin/') 
     
    212215        self.failUnlessEqual(post.status_code, 403) 
    213216        self.client.get('/test_admin/admin/logout/') 
    214          
     217 
    215218        # change user can view all items and edit them 
    216219        self.client.get('/test_admin/admin/') 
     
    228231        self.client.get('/test_admin/admin/') 
    229232        self.client.post('/test_admin/admin/', self.super_login) 
    230          
     233 
    231234        # Test custom change list template with custom extra context 
    232235        request = self.client.get('/test_admin/admin/admin_views/customarticle/') 
     
    234237        self.assert_("var hello = 'Hello!';" in request.content) 
    235238        self.assertTemplateUsed(request, 'custom_admin/change_list.html') 
    236          
     239 
    237240        # Test custom change form template 
    238241        request = self.client.get('/test_admin/admin/admin_views/customarticle/add/') 
    239242        self.assertTemplateUsed(request, 'custom_admin/change_form.html') 
    240          
     243 
    241244        # Add an article so we can test delete and history views 
    242245        post = self.client.post('/test_admin/admin/admin_views/customarticle/add/', { 
     
    247250        self.assertRedirects(post, '/test_admin/admin/admin_views/customarticle/') 
    248251        self.failUnlessEqual(CustomArticle.objects.all().count(), 1) 
    249          
     252 
    250253        # Test custom delete and object history templates 
    251254        request = self.client.get('/test_admin/admin/admin_views/customarticle/1/delete/') 
     
    253256        request = self.client.get('/test_admin/admin/admin_views/customarticle/1/history/') 
    254257        self.assertTemplateUsed(request, 'custom_admin/object_history.html') 
    255          
     258 
    256259        self.client.get('/test_admin/admin/logout/') 
    257260 
     
    260263        self.assertEqual(admin.site.index_template, None) 
    261264        self.assertEqual(admin.site.login_template, None) 
    262          
     265 
    263266        self.client.get('/test_admin/admin/logout/') 
    264267        request = self.client.get('/test_admin/admin/') 
     
    267270        request = self.client.get('/test_admin/admin/') 
    268271        self.assertTemplateUsed(request, 'admin/index.html') 
    269          
     272 
    270273        self.client.get('/test_admin/admin/logout/') 
    271274        admin.site.login_template = 'custom_admin/login.html' 
     
    278281        self.assertTemplateUsed(request, 'custom_admin/index.html') 
    279282        self.assert_('Hello from a custom index template' in request.content) 
    280          
     283 
    281284        # Finally, using monkey patching check we can inject custom_context arguments in to index 
    282285        original_index = admin.site.index 
     
    288291        self.assertTemplateUsed(request, 'custom_admin/index.html') 
    289292        self.assert_('Hello from a custom index template *bar*' in request.content) 
    290          
     293 
    291294        self.client.get('/test_admin/admin/logout/') 
    292295        del admin.site.index # Resets to using the original 
     
    296299    def testDeleteView(self): 
    297300        """Delete view should restrict access and actually delete items.""" 
    298          
     301 
    299302        delete_dict = {'post': 'yes'} 
    300          
     303 
    301304        # add user shoud not be able to delete articles 
    302305        self.client.get('/test_admin/admin/') 
     
    308311        self.failUnlessEqual(Article.objects.all().count(), 1) 
    309312        self.client.get('/test_admin/admin/logout/') 
    310          
     313 
    311314        # Delete user can delete 
    312315        self.client.get('/test_admin/admin/') 
     
    315318         # test response contains link to related Article 
    316319        self.assertContains(response, "admin_views/article/1/") 
    317          
     320 
    318321        response = self.client.get('/test_admin/admin/admin_views/article/1/delete/') 
    319322        self.failUnlessEqual(response.status_code, 200) 
     
    325328class AdminViewStringPrimaryKeyTest(TestCase): 
    326329    fixtures = ['admin-views-users.xml', 'string-primary-key.xml'] 
    327      
     330 
    328331    def __init__(self, *args): 
    329332        super(AdminViewStringPrimaryKeyTest, self).__init__(*args) 
    330333        self.pk = """abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 -_.!~*'() ;/?:@&=+$, <>#%" {}|\^[]`""" 
    331      
     334 
    332335    def setUp(self): 
    333336        self.client.login(username='super', password='secret') 
    334337        content_type_pk = ContentType.objects.get_for_model(ModelWithStringPrimaryKey).pk 
    335338        LogEntry.objects.log_action(100, content_type_pk, self.pk, self.pk, 2, change_message='') 
    336      
     339 
    337340    def tearDown(self): 
    338341        self.client.logout() 
    339      
     342 
    340343    def test_get_change_view(self): 
    341344        "Retrieving the object using urlencoded form of primary key should work" 
     
    343346        self.assertContains(response, escape(self.pk)) 
    344347        self.failUnlessEqual(response.status_code, 200) 
    345      
     348 
    346349    def test_changelist_to_changeform_link(self): 
    347350        "The link from the changelist referring to the changeform of the object should be quoted" 
     
    349352        should_contain = """<tr class="row1"><th><a href="%s/">%s</a></th></tr>""" % (quote(self.pk), escape(self.pk)) 
    350353        self.assertContains(response, should_contain) 
    351      
     354 
    352355    def test_recentactions_link(self): 
    353356        "The link from the recent actions list referring to the changeform of the object should be quoted" 
     
    355358        should_contain = """<a href="admin_views/modelwithstringprimarykey/%s/">%s</a>""" % (quote(self.pk), escape(self.pk)) 
    356359        self.assertContains(response, should_contain) 
    357      
     360 
    358361    def test_deleteconfirmation_link(self): 
    359362        "The link from the delete confirmation page referring back to the changeform of the object should be quoted"