Login

django app name & breadcrumbs l10n

Author:
xormag
Posted:
July 14, 2011
Language:
Python
Version:
1.3
Score:
2 (after 2 ratings)

This approach allows you to rename you app name & app breadcrumbs within admin interface.

Paste the code in admin.py of your application and change AppLabelRenamer.main params.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from django.db.models.base import ModelBase
from django.core.urlresolvers import resolve

class AppLabelRenamer(object):
    ''' Rename app label and app breadcrumbs in admin. '''

    def __init__(self, native_app_label, app_label):
        self.native_app_label = native_app_label
        self.app_label = app_label
        self.module = '.'.join([native_app_label, 'models'])

    class string_with_realoaded_title(str):
        ''' tnx to Ionel Maries Cristian for http://ionelmc.wordpress.com/2011/06/24/custom-app-names-in-the-django-admin/'''
        def __new__(cls, value, title):
            instance = str.__new__(cls, value)
            instance._title = title
            return instance

        def title(self):
            return self._title

        __copy__ = lambda self: self
        __deepcopy__ = lambda self, memodict: self

    def rename_app_label(self, f):
        app_label = self.app_label

        def rename_breadcrumbs(f):
            def wrap(self, *args, **kwargs):
                extra_context = kwargs.get('extra_context', {})
                extra_context['app_label'] = app_label
                kwargs['extra_context'] = extra_context
                return f(self, *args, **kwargs)
            return wrap

        def wrap(model_or_iterable, admin_class=None, **option):
            if isinstance(model_or_iterable, ModelBase):
                model_or_iterable = [model_or_iterable]
            for model in model_or_iterable:
                if model.__module__ != self.module:
                    continue
                if admin_class is None:
                    admin_class = type(model.__name__+'Admin',
                                       (admin.ModelAdmin,), {})
                admin_class.add_view = rename_breadcrumbs(admin_class.add_view)
                admin_class.change_view = rename_breadcrumbs(admin_class.change_view)
                admin_class.changelist_view = rename_breadcrumbs(admin_class.changelist_view)
                model._meta.app_label = self.string_with_realoaded_title(
                                            self.native_app_label,
                                            self.app_label)
            return f(model, admin_class, **option)
        return wrap

    def rename_app_index(self, f):
        def wrap(request, app_label, extra_context=None):
            requested_app_label = resolve(request.path).kwargs.get('app_label', '')
            if requested_app_label and requested_app_label == self.native_app_label:
                app_label = self.string_with_realoaded_title(self.native_app_label,
                                                             self.app_label)
            else:
                app_label = requested_app_label
            return f(request, app_label, extra_context=None)
        return wrap

    def main(self):
        admin.site.register = self.rename_app_label(admin.site.register)
        admin.site.app_index = self.rename_app_index(admin.site.app_index)

# Example
AppLabelRenamer(native_app_label=u'exampleapp', app_label=u'your custom label').main()

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

mdsol (on July 29, 2011):

Thank you for the snippet.

Your code works only on the app list page (eg: /admin/appname/), doesn't work on main admin page (eg: /admin/) and on models pages (eg: /admin/appname/modelname/*).

Is this correct?

#

xormag (on August 11, 2011):

It works for me in both main (/admin/) and model (/admin/appname/modelname/). Can you describe your case?

#

Francisco (on September 22, 2011):

Thanks a million!

The only problem, for me, is that it isn't working at app index page (eg: /admin/appname/), I mean, the title immediateley bellow breadcrumbs is not working but the title inside the box with the models does work.

Any idea, please?

Francisco.

#

shrenik181986 (on February 21, 2013):

Thank you for the snippet.

There is a problem with "/admin/appname/". Not able to view app_label & get all models names. Any tip regarding how to use this with django-admin-tools

Thanks

#

Please login first before commenting.