Ticket #13588: newstyle_admin.diff

File newstyle_admin.diff, 14.2 KB (added by Florian Apolloner, 14 years ago)
  • django/contrib/admin/actions.py

    diff --git a/django/contrib/admin/actions.py b/django/contrib/admin/actions.py
    index b75c91b..e98d4eb 100644
    a b def delete_selected(modeladmin, request, queryset):  
    3232
    3333    # Populate deletable_objects, a data structure of all related objects that
    3434    # will also be deleted.
    35     deletable_objects, perms_needed = get_deleted_objects(queryset, opts, request.user, modeladmin.admin_site, levels_to_root=2)
     35    deletable_objects, perms_needed = get_deleted_objects(queryset, opts, request.user, modeladmin.admin_site)
    3636
    3737    # The user has already confirmed the deletion.
    3838    # Do the deletion and return a None to display the change list view again.
  • django/contrib/admin/options.py

    diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
    index 1f8ff6d..97210e5 100644
    a b from django.contrib.admin.util import unquote, flatten_fieldsets, get_deleted_ob  
    99from django.contrib import messages
    1010from django.views.decorators.csrf import csrf_protect
    1111from django.core.exceptions import PermissionDenied, ValidationError
     12from django.core.urlresolvers import reverse
    1213from django.db import models, transaction
    1314from django.db.models.fields import BLANK_CHOICE_DASH
    1415from django.http import Http404, HttpResponse, HttpResponseRedirect
    class ModelAdmin(BaseModelAdmin):  
    663664            # redirect to the change-list page for this object. Otherwise,
    664665            # redirect to the admin index.
    665666            if self.has_change_permission(request, None):
    666                 post_url = '../'
     667                post_url = reverse('admin:%s_%s_changelist' %
     668                                   (opts.app_label, opts.module_name))
    667669            else:
    668                 post_url = '../../../'
     670                post_url = reverse('admin:index')
    669671            return HttpResponseRedirect(post_url)
    670672
    671673    def response_change(self, request, obj):
    class ModelAdmin(BaseModelAdmin):  
    685687        elif request.POST.has_key("_saveasnew"):
    686688            msg = _('The %(name)s "%(obj)s" was added successfully. You may edit it again below.') % {'name': force_unicode(opts.verbose_name), 'obj': obj}
    687689            self.message_user(request, msg)
    688             return HttpResponseRedirect("../%s/" % pk_value)
     690            return HttpResponseRedirect(reverse('admin:%s_%s_change' %
     691                                        (opts.app_label, opts.module_name),
     692                                        args=pk_value))
     693
    689694        elif request.POST.has_key("_addanother"):
    690695            self.message_user(request, msg + ' ' + (_("You may add another %s below.") % force_unicode(opts.verbose_name)))
    691             return HttpResponseRedirect("../add/")
     696            return HttpResponseRedirect(reverse('admin:%s_%s_add' %
     697                                        (opts.app_label, opts.module_name)))
    692698        else:
    693699            self.message_user(request, msg)
    694             return HttpResponseRedirect("../")
     700            return HttpResponseRedirect(reverse('admin:%s_%s_changelist' %
     701                                        (opts.app_label, opts.module_name)))
    695702
    696703    def response_action(self, request, queryset):
    697704        """
    class ModelAdmin(BaseModelAdmin):  
    866873            raise Http404(_('%(name)s object with primary key %(key)r does not exist.') % {'name': force_unicode(opts.verbose_name), 'key': escape(object_id)})
    867874
    868875        if request.method == 'POST' and request.POST.has_key("_saveasnew"):
    869             return self.add_view(request, form_url='../add/')
     876            return self.add_view(request, form_url=reverse('admin:%s_%s_add' %
     877                                    (opts.app_label, opts.module_name)))
    870878
    871879        ModelForm = self.get_form(request, obj)
    872880        formsets = []
    class ModelAdmin(BaseModelAdmin):  
    11181126            self.message_user(request, _('The %(name)s "%(obj)s" was deleted successfully.') % {'name': force_unicode(opts.verbose_name), 'obj': force_unicode(obj_display)})
    11191127
    11201128            if not self.has_change_permission(request, None):
    1121                 return HttpResponseRedirect("../../../../")
    1122             return HttpResponseRedirect("../../")
     1129                return HttpResponseRedirect(reverse('admin:index'))
     1130            return HttpResponseRedirect(reverse('admin:%s_%s_changelist' %
     1131                                        (opts.app_label, opts.module_name)))
    11231132
    11241133        context = {
    11251134            "title": _("Are you sure?"),
  • django/contrib/admin/sites.py

    diff --git a/django/contrib/admin/sites.py b/django/contrib/admin/sites.py
    index 4446490..e451f8f 100644
    a b class AdminSite(object):  
    456456            context_instance=context_instance
    457457        )
    458458
    459     def root(self, request, url):
    460         """
    461         DEPRECATED. This function is the old way of handling URL resolution, and
    462         is deprecated in favor of real URL resolution -- see ``get_urls()``.
    463 
    464         This function still exists for backwards-compatibility; it will be
    465         removed in Django 1.3.
    466         """
    467         import warnings
    468         warnings.warn(
    469             "AdminSite.root() is deprecated; use include(admin.site.urls) instead.",
    470             DeprecationWarning
    471         )
    472 
    473         #
    474         # Again, remember that the following only exists for
    475         # backwards-compatibility. Any new URLs, changes to existing URLs, or
    476         # whatever need to be done up in get_urls(), above!
    477         #
    478 
    479         if request.method == 'GET' and not request.path.endswith('/'):
    480             return http.HttpResponseRedirect(request.path + '/')
    481 
    482         if settings.DEBUG:
    483             self.check_dependencies()
    484 
    485         # Figure out the admin base URL path and stash it for later use
    486         self.root_path = re.sub(re.escape(url) + '$', '', request.path)
    487 
    488         url = url.rstrip('/') # Trim trailing slash, if it exists.
    489 
    490         # The 'logout' view doesn't require that the person is logged in.
    491         if url == 'logout':
    492             return self.logout(request)
    493 
    494         # Check permission to continue or display login form.
    495         if not self.has_permission(request):
    496             return self.login(request)
    497 
    498         if url == '':
    499             return self.index(request)
    500         elif url == 'password_change':
    501             return self.password_change(request)
    502         elif url == 'password_change/done':
    503             return self.password_change_done(request)
    504         elif url == 'jsi18n':
    505             return self.i18n_javascript(request)
    506         # URLs starting with 'r/' are for the "View on site" links.
    507         elif url.startswith('r/'):
    508             from django.contrib.contenttypes.views import shortcut
    509             return shortcut(request, *url.split('/')[1:])
    510         else:
    511             if '/' in url:
    512                 return self.model_page(request, *url.split('/', 2))
    513             else:
    514                 return self.app_index(request, url)
    515 
    516         raise http.Http404('The requested admin page does not exist.')
    517 
    518     def model_page(self, request, app_label, model_name, rest_of_url=None):
    519         """
    520         DEPRECATED. This is the old way of handling a model view on the admin
    521         site; the new views should use get_urls(), above.
    522         """
    523         from django.db import models
    524         model = models.get_model(app_label, model_name)
    525         if model is None:
    526             raise http.Http404("App %r, model %r, not found." % (app_label, model_name))
    527         try:
    528             admin_obj = self._registry[model]
    529         except KeyError:
    530             raise http.Http404("This model exists but has not been registered with the admin site.")
    531         return admin_obj(request, rest_of_url)
    532     model_page = never_cache(model_page)
    533459
    534460# This global object represents the default admin site, for the common case.
    535461# You can instantiate AdminSite in your own code to create a custom admin site.
  • django/contrib/admin/util.py

    diff --git a/django/contrib/admin/util.py b/django/contrib/admin/util.py
    index 776a6f0..74c4a18 100644
    a b from django.utils.safestring import mark_safe  
    77from django.utils.text import capfirst
    88from django.utils.encoding import force_unicode, smart_unicode, smart_str
    99from django.utils.translation import ungettext, ugettext as _
    10 from django.core.urlresolvers import reverse, NoReverseMatch
     10from django.core.urlresolvers import reverse
    1111from django.utils.datastructures import SortedDict
    1212
    1313def quote(s):
    def flatten_fieldsets(fieldsets):  
    5858                field_names.append(field)
    5959    return field_names
    6060
    61 def _format_callback(obj, user, admin_site, levels_to_root, perms_needed):
     61def _format_callback(obj, user, admin_site, perms_needed):
    6262    has_admin = obj.__class__ in admin_site._registry
    6363    opts = obj._meta
    64     try:
     64    if has_admin:
    6565        admin_url = reverse('%s:%s_%s_change'
    6666                            % (admin_site.name,
    6767                               opts.app_label,
    6868                               opts.object_name.lower()),
    6969                            None, (quote(obj._get_pk_val()),))
    70     except NoReverseMatch:
    71         admin_url = '%s%s/%s/%s/' % ('../'*levels_to_root,
    72                                      opts.app_label,
    73                                      opts.object_name.lower(),
    74                                      quote(obj._get_pk_val()))
    75     if has_admin:
    7670        p = '%s.%s' % (opts.app_label,
    7771                       opts.get_delete_permission())
    7872        if not user.has_perm(p):
    def _format_callback(obj, user, admin_site, levels_to_root, perms_needed):  
    8882        return u'%s: %s' % (capfirst(opts.verbose_name),
    8983                            force_unicode(obj))
    9084
    91 def get_deleted_objects(objs, opts, user, admin_site, levels_to_root=4):
     85def get_deleted_objects(objs, opts, user, admin_site, levels_to_root=0):
    9286    """
    9387    Find all objects related to ``objs`` that should also be
    9488    deleted. ``objs`` should be an iterable of objects.
    def get_deleted_objects(objs, opts, user, admin_site, levels_to_root=4):  
    9690    Returns a nested list of strings suitable for display in the
    9791    template with the ``unordered_list`` filter.
    9892
    99     `levels_to_root` defines the number of directories (../) to reach
    100     the admin root path. In a change_view this is 4, in a change_list
    101     view 2.
    102 
    103     This is for backwards compatibility since the options.delete_selected
    104     method uses this function also from a change_list view.
    105     This will not be used if we can reverse the URL.
     93    `levels_to_root` is for backwards compatibility api compability, it is not
     94    used at all.
    10695    """
    10796    collector = NestedObjects()
    10897    for obj in objs:
    def get_deleted_objects(objs, opts, user, admin_site, levels_to_root=4):  
    114103    to_delete = collector.nested(_format_callback,
    115104                                 user=user,
    116105                                 admin_site=admin_site,
    117                                  levels_to_root=levels_to_root,
    118106                                 perms_needed=perms_needed)
    119107
    120108    return to_delete, perms_needed
  • django/contrib/admin/widgets.py

    diff --git a/django/contrib/admin/widgets.py b/django/contrib/admin/widgets.py
    index 1d321d0..66a84f2 100644
    a b from django.utils.translation import ugettext as _  
    1313from django.utils.safestring import mark_safe
    1414from django.utils.encoding import force_unicode
    1515from django.conf import settings
    16 from django.core.urlresolvers import reverse, NoReverseMatch
     16from django.core.urlresolvers import reverse
    1717
    1818class FilteredSelectMultiple(forms.SelectMultiple):
    1919    """
    class ForeignKeyRawIdWidget(forms.TextInput):  
    113113    def render(self, name, value, attrs=None):
    114114        if attrs is None:
    115115            attrs = {}
     116        # TODO, only add link if related object is in the admin...
    116117        related_url = '../../../%s/%s/' % (self.rel.to._meta.app_label, self.rel.to._meta.object_name.lower())
    117118        params = self.url_parameters()
    118119        if params:
    class RelatedFieldWidgetWrapper(forms.Widget):  
    229230    def render(self, name, value, *args, **kwargs):
    230231        rel_to = self.rel.to
    231232        info = (rel_to._meta.app_label, rel_to._meta.object_name.lower())
    232         try:
    233             related_url = reverse('admin:%s_%s_add' % info, current_app=self.admin_site.name)
    234         except NoReverseMatch:
    235             info = (self.admin_site.root_path, rel_to._meta.app_label, rel_to._meta.object_name.lower())
    236             related_url = '%s%s/%s/add/' % info
    237233        self.widget.choices = self.choices
    238234        output = [self.widget.render(name, value, *args, **kwargs)]
    239235        if rel_to in self.admin_site._registry: # If the related object has an admin interface:
     236            related_url = reverse('admin:%s_%s_add' % info, current_app=self.admin_site.name)
    240237            # TODO: "id_" is hard-coded here. This should instead use the correct
    241238            # API to determine the ID dynamically.
    242239            output.append(u'<a href="%s" class="add-another" id="add_id_%s" onclick="return showAddAnotherPopup(this);"> ' % \
  • tests/regressiontests/admin_inlines/models.py

    diff --git a/tests/regressiontests/admin_inlines/models.py b/tests/regressiontests/admin_inlines/models.py
    index 5a12e07..2998373 100644
    a b class BookInline(admin.TabularInline):  
    4343class AuthorAdmin(admin.ModelAdmin):
    4444    inlines = [BookInline]
    4545
     46admin.site.register(Book)
    4647admin.site.register(Author, AuthorAdmin)
    4748
    4849class Holder(models.Model):
  • tests/regressiontests/admin_views/tests.py

    diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py
    index 1385e5e..f60bb04 100644
    a b class SaveAsTests(TestCase):  
    324324        self.assert_(response.context['save_as'])
    325325        post_data = {'_saveasnew':'', 'name':'John M', 'gender':3, 'alive':'checked'}
    326326        response = self.client.post('/test_admin/admin/admin_views/person/1/', post_data)
    327         self.assertEqual(response.context['form_url'], '../add/')
     327        self.assertEqual(response.context['form_url'], '/test_admin/admin/admin_views/person/add/')
    328328
    329329class CustomModelAdminTest(AdminViewBasicTest):
    330330    urlbit = "admin2"
Back to Top