Ticket #7466: fix-relative-paths-in-admin-doc.diff

File fix-relative-paths-in-admin-doc.diff, 23.2 KB (added by shnur, 16 years ago)
  • django/contrib/admin/options.py

     
    353353        for inline in self.inline_instances:
    354354            yield inline.get_formset(request, obj)
    355355
    356     def save_add(self, request, model, form, formsets, post_url_continue):
    357         """
    358         Saves the object in the "add" stage and returns an HttpResponseRedirect.
     356    def save(self, request, form, formsets, original_obj, commit=True):
     357        """ Save new or changed object and its inlines. """
     358        new_obj = form.save(commit=commit)
     359        for formset in formsets:
     360            # HACK: it seems like the parent object should be passed into
     361            # a method of something, not just set as an attribute
     362            formset.instance = new_obj
     363            formset.save()
     364        return new_obj
    359365
    360         `form` is a bound Form instance that's verified to be valid.
    361         """
     366    def log_add(self, request, form, formsets, new_obj):
     367        """ Log saving of new object and its inlines. """
    362368        from django.contrib.admin.models import LogEntry, ADDITION
    363         from django.contrib.contenttypes.models import ContentType
    364         opts = model._meta
    365         new_object = form.save(commit=True)
     369        user_id = request.user.id
     370        content_type_id = ContentType.objects.get_for_model(self.model).id
     371        object_id = new_obj._get_pk_val()
     372        LogEntry.objects.log_action(user_id, content_type_id, object_id, force_unicode(new_obj), ADDITION)
    366373
    367         if formsets:
    368             for formset in formsets:
    369                 # HACK: it seems like the parent obejct should be passed into
    370                 # a method of something, not just set as an attribute
    371                 formset.instance = new_object
    372                 formset.save()
    373 
    374         pk_value = new_object._get_pk_val()
    375         LogEntry.objects.log_action(request.user.id, ContentType.objects.get_for_model(model).id, pk_value, force_unicode(new_object), ADDITION)
    376         msg = _('The %(name)s "%(obj)s" was added successfully.') % {'name': opts.verbose_name, 'obj': new_object}
    377         # Here, we distinguish between different save types by checking for
    378         # the presence of keys in request.POST.
    379         if request.POST.has_key("_continue"):
    380             request.user.message_set.create(message=msg + ' ' + _("You may edit it again below."))
    381             if request.POST.has_key("_popup"):
    382                 post_url_continue += "?_popup=1"
    383             return HttpResponseRedirect(post_url_continue % pk_value)
     374    def log_change(self, request, form, formsets, original_obj, new_obj):
     375        """ Log saving of changed object and its inlines. """
     376        from django.contrib.admin.models import LogEntry, CHANGE
     377        user_id = request.user.id
     378        content_type_id = ContentType.objects.get_for_model(self.model).id
     379        object_id = new_obj._get_pk_val() 
     380        # Construct the change message. TODO: Temporarily commented-out,
     381        # as manipulator object doesn't exist anymore, and we don't yet
     382        # have a way to get fields_added, fields_changed, fields_deleted.
     383        #change_message = ' '.join(change_message)
     384        change_message = None
     385        if not change_message:
     386            change_message = _('No fields changed.')
     387        LogEntry.objects.log_action(user_id, content_type_id, object_id, force_unicode(new_obj), CHANGE, change_message)
    384388       
    385         if request.POST.has_key("_popup"):
    386             return HttpResponse('<script type="text/javascript">opener.dismissAddAnotherPopup(window, "%s", "%s");</script>' % \
     389    def redirect_after_save(self, request, new_obj, add=False, change=False):
     390        """ Redirect after saving of new or changed object. """
     391        opts = self.opts
     392        pk_value = new_obj._get_pk_val()
     393
     394        # popup
     395        if request.POST.has_key('_popup'):
     396            if isinstance(pk_value, str): # Quote if string, so JavaScript doesn't think it's a variable.
     397                pk_value = '"%s"' % pk_value.replace('"', '\\"')
     398            return HttpResponse('<script type="text/javascript">opener.dismissAddAnotherPopup(window, %s, "%s");</script>' % \
    387399                # escape() calls force_unicode.
    388                 (escape(pk_value), escape(new_object)))
    389         elif request.POST.has_key("_addanother"):
    390             request.user.message_set.create(message=msg + ' ' + (_("You may add another %s below.") % opts.verbose_name))
    391             return HttpResponseRedirect(request.path)
     400                (escape(pk_value), escape(new_obj)))
     401
     402        # default message       
     403        if add:
     404            msg = _('The %(name)s "%(obj)s" was added successfully.') % {'name': opts.verbose_name, 'obj': new_obj}
    392405        else:
    393             request.user.message_set.create(message=msg)
    394             # Figure out where to redirect. If the user has change permission,
    395             # redirect to the change-list page for this object. Otherwise,
    396             # redirect to the admin index.
    397             if self.has_change_permission(request, None):
    398                 post_url = '../'
     406            msg = _('The %(name)s "%(obj)s" was changed successfully.') % {'name': opts.verbose_name, 'obj': new_obj}
     407
     408        # save as new
     409        if request.POST.has_key('_saveasnew'):
     410            if self.has_change_permission(request, new_obj):
     411                msg += ' ' + _('You may edit it again below.')
     412                redirect_url = '../%s/' % pk_value
     413            elif self.has_change_permission(request):
     414                redirect_url = '../'
    399415            else:
    400                 post_url = '../../../'
    401             return HttpResponseRedirect(post_url)
    402     save_add = transaction.commit_on_success(save_add)
     416                redirect_url = '../../../'
     417        # save and add another
     418        elif request.POST.has_key('_addanother'):
     419            msg += ' ' + (_('You may add another %s below.') % opts.verbose_name)
     420            if add:
     421                redirect_url = request.path
     422            else:
     423                redirect_url = '../add/'
     424        # save and continue editing
     425        elif request.POST.has_key('_continue'):
     426            if self.has_change_permission(request, new_obj):
     427                msg += ' ' + _('You may edit it again below.')
     428                if add:
     429                    redirect_url = '../%s/' % pk_value
     430                else:
     431                    redirect_url = request.path
     432            elif self.has_change_permission(request):
     433                redirect_url = '../'
     434            else:
     435                redirect_url = '../../../'
     436        # save
     437        else:
     438            if self.has_change_permission(request):
     439                redirect_url = '../'
     440            else:
     441                redirect_url = '../../../'
    403442
    404     def save_change(self, request, model, form, formsets=None):
    405         """
    406         Saves the object in the "change" stage and returns an HttpResponseRedirect.
    407 
    408         `form` is a bound Form instance that's verified to be valid.
     443        # create message and redirect
     444        request.user.message_set.create(message=msg)
     445        return HttpResponseRedirect(redirect_url)
    409446       
    410         `formsets` is a sequence of InlineFormSet instances that are verified to be valid.
    411         """
    412         from django.contrib.admin.models import LogEntry, CHANGE
    413         from django.contrib.contenttypes.models import ContentType
    414         opts = model._meta
    415         new_object = form.save(commit=True)
    416         pk_value = new_object._get_pk_val()
     447    def save_add(self, request, form, formsets, commit=True):
     448        """ Save changed object and its inlines. """
     449        return self.save(request, form, formsets, None, commit)
     450    save_add = transaction.commit_on_success(save_add)
    417451
    418         if formsets:
    419             for formset in formsets:
    420                 formset.save()
    421 
    422         # Construct the change message.                 
    423         change_message = []
    424         if form.changed_data:
    425             change_message.append(_('Changed %s.') % get_text_list(form.changed_data, _('and')))
    426            
    427         if formsets:
    428             for formset in formsets:
    429                 for added_object in formset.new_objects:
    430                     change_message.append(_('Added %(name)s "%(object)s".')
    431                                           % {'name': added_object._meta.verbose_name,
    432                                              'object': added_object})
    433                 for changed_object, changed_fields in formset.changed_objects:
    434                     change_message.append(_('Changed %(list)s for %(name)s "%(object)s".')
    435                                           % {'list': get_text_list(changed_fields, _('and')),
    436                                              'name': changed_object._meta.verbose_name,
    437                                              'object': changed_object})
    438                 for deleted_object in formset.deleted_objects:
    439                     change_message.append(_('Deleted %(name)s "%(object)s".')
    440                                           % {'name': deleted_object._meta.verbose_name,
    441                                              'object': deleted_object})
    442         change_message = ' '.join(change_message)
    443         if not change_message:
    444             change_message = _('No fields changed.')
    445         LogEntry.objects.log_action(request.user.id, ContentType.objects.get_for_model(model).id, pk_value, force_unicode(new_object), CHANGE, change_message)
    446 
    447         msg = _('The %(name)s "%(obj)s" was changed successfully.') % {'name': opts.verbose_name, 'obj': new_object}
    448         if request.POST.has_key("_continue"):
    449             request.user.message_set.create(message=msg + ' ' + _("You may edit it again below."))
    450             if request.REQUEST.has_key('_popup'):
    451                 return HttpResponseRedirect(request.path + "?_popup=1")
    452             else:
    453                 return HttpResponseRedirect(request.path)
    454         elif request.POST.has_key("_saveasnew"):
    455             request.user.message_set.create(message=_('The %(name)s "%(obj)s" was added successfully. You may edit it again below.') % {'name': opts.verbose_name, 'obj': new_object})
    456             return HttpResponseRedirect("../%s/" % pk_value)
    457         elif request.POST.has_key("_addanother"):
    458             request.user.message_set.create(message=msg + ' ' + (_("You may add another %s below.") % opts.verbose_name))
    459             return HttpResponseRedirect("../add/")
    460         else:
    461             request.user.message_set.create(message=msg)
    462             return HttpResponseRedirect("../")
     452    def save_change(self, request, form, formsets, original_obj, commit=True):
     453        """ Save changed object and its inlines. """
     454        return self.save(request, form, formsets, original_obj, commit)
    463455    save_change = transaction.commit_on_success(save_change)
    464456
    465457    def render_change_form(self, request, model, context, add=False, change=False, form_url='', obj=None):
     
    514506                    instance=obj, save_as_new=request.POST.has_key("_saveasnew"))
    515507                inline_formsets.append(inline_formset)
    516508            if all_valid(inline_formsets) and form.is_valid():
    517                 return self.save_add(request, model, form, inline_formsets, '../%s/')
     509                new_obj = self.save_add(request, form, inline_formsets)
     510                self.log_add(request, form, inline_formsets, new_obj)
     511                return self.redirect_after_save(request, new_obj, add=True)
    518512        else:
    519513            form = ModelForm(initial=dict(request.GET.items()))
    520514            for FormSet in self.get_formsets(request):
     
    576570                inline_formsets.append(inline_formset)
    577571
    578572            if all_valid(inline_formsets) and form.is_valid():
    579                 return self.save_change(request, model, form, inline_formsets)
     573                new_obj = self.save_change(request, form, inline_formsets, obj)
     574                self.log_change(request, form, inline_formsets, obj, new_obj)
     575                return self.redirect_after_save(request, new_obj, change=True)
    580576        else:
    581577            form = ModelForm(instance=obj)
    582578            for FormSet in self.get_formsets(request, obj):
  • django/contrib/admin/templates/admin_doc/view_detail.html

     
    1 {% extends "admin/base_site.html" %}
     1{% extends "admin_doc/base.html" %}
    22{% load i18n %}
    33{% block breadcrumbs %}<div class="breadcrumbs"><a href="../../../">Home</a> &rsaquo; <a href="../../">Documentation</a> &rsaquo; <a href="../">Views</a> &rsaquo; {{ name }}</div>{% endblock %}
    44{% block userlinks %}<a href="../../../password_change/">{% trans 'Change password' %}</a> / <a href="../../../logout/">{% trans 'Log out' %}</a>{% endblock %}
  • django/contrib/admin/templates/admin_doc/bookmarklets.html

     
    1 {% extends "admin/base_site.html" %}
     1{% extends "admin_doc/base.html" %}
    22
    3 {% block breadcrumbs %}{% load i18n %}<div class="breadcrumbs"><a href="../../">{% trans "Home" %}</a> &rsaquo; <a href="../">{% trans "Documentation" %}</a> &rsaquo; {% trans "Bookmarklets" %}</div>{% endblock %}
    4 {% block userlinks %}<a href="../../password_change/">{% trans 'Change password' %}</a> / <a href="../../logout/">{% trans 'Log out' %}</a>{% endblock %}
     3{% block breadcrumbs %}{% load i18n %}<div class="breadcrumbs"><a href="{{ root_path }}">{% trans "Home" %}</a> &rsaquo; <a href="{{ root_path }}doc/">{% trans "Documentation" %}</a> &rsaquo; {% trans "Bookmarklets" %}</div>{% endblock %}
     4{% block userlinks %}<a href="{{ root_path }}password_change/">{% trans 'Change password' %}</a> / <a href="{{ root_path }}logout/">{% trans 'Log out' %}</a>{% endblock %}
    55{% block title %}{% trans "Documentation bookmarklets" %}{% endblock %}
    66
    77{% block content %}
  • django/contrib/admin/templates/admin_doc/template_detail.html

     
    1 {% extends "admin/base_site.html" %}
     1{% extends "admin_doc/base.html" %}
    22{% load i18n %}
    33{% block breadcrumbs %}<div class="breadcrumbs"><a href="../../../">Home</a> &rsaquo; <a href="../../">Documentation</a> &rsaquo; Templates &rsaquo; {{ name|escape }}</div>{% endblock %}
    44{% block userlinks %}<a href="../../../password_change/">{% trans 'Change password' %}</a> / <a href="../../../logout/">{% trans 'Log out' %}</a>{% endblock %}
  • django/contrib/admin/templates/admin_doc/template_tag_index.html

     
    1 {% extends "admin/base_site.html" %}
     1{% extends "admin_doc/base.html" %}
    22{% load i18n %}
    33{% block coltype %}colSM{% endblock %}
    44{% block breadcrumbs %}<div class="breadcrumbs"><a href="../../">Home</a> &rsaquo; <a href="../">Documentation</a> &rsaquo; Tags</div>{% endblock %}
  • django/contrib/admin/templates/admin_doc/missing_docutils.html

     
    1 {% extends "admin/base_site.html" %}
     1{% extends "admin_doc/base.html" %}
    22{% load i18n %}
    33{% block breadcrumbs %}<div class="breadcrumbs"><a href="../">Home</a> &rsaquo; Documentation</div>{% endblock %}
    4 {% block userlinks %}<a href="../password_change/">{% trans 'Change password' %}</a> / <a href="../logout/">{% trans 'Log out' %}</a>{% endblock %}
    54{% block title %}Please install docutils{% endblock %}
    65
    76{% block content %}
  • django/contrib/admin/templates/admin_doc/view_index.html

     
    1 {% extends "admin/base_site.html" %}
     1{% extends "admin_doc/base.html" %}
    22{% load i18n %}
    33{% block coltype %}colSM{% endblock %}
    44{% block breadcrumbs %}<div class="breadcrumbs"><a href="../../">Home</a> &rsaquo; <a href="../">Documentation</a> &rsaquo; Views</div>{% endblock %}
  • django/contrib/admin/templates/admin_doc/model_detail.html

     
    1 {% extends "admin/base_site.html" %}
     1{% extends "admin_doc/base.html" %}
    22{% load i18n %}
    33{% block userlinks %}<a href="../../../password_change/">{% trans 'Change password' %}</a> / <a href="../../../logout/">{% trans 'Log out' %}</a>{% endblock %}
    44{% block extrahead %}
  • django/contrib/admin/templates/admin_doc/index.html

     
    1 {% extends "admin/base_site.html" %}
     1{% extends "admin_doc/base.html" %}
    22{% load i18n %}
    33{% block breadcrumbs %}<div class="breadcrumbs"><a href="../">Home</a> &rsaquo; Documentation</div>{% endblock %}
    44{% block title %}Documentation{% endblock %}
  • django/contrib/admin/templates/admin_doc/model_index.html

     
    1 {% extends "admin/base_site.html" %}
     1{% extends "admin_doc/base.html" %}
    22{% load i18n %}
    33{% block coltype %}colSM{% endblock %}
    44{% block breadcrumbs %}<div class="breadcrumbs"><a href="../../">Home</a> &rsaquo; <a href="../">Documentation</a> &rsaquo; Models</div>{% endblock %}
  • django/contrib/admin/templates/admin_doc/template_filter_index.html

     
    1 {% extends "admin/base_site.html" %}
     1{% extends "admin_doc/base.html" %}
    22{% load i18n %}
    33{% block coltype %}colSM{% endblock %}
    44{% block breadcrumbs %}<div class="breadcrumbs"><a href="../../">Home</a> &rsaquo; <a href="../">Documentation</a> &rsaquo; filters</div>{% endblock %}
  • django/contrib/admindocs/views.py

     
    88from django.http import Http404, get_host
    99from django.core import urlresolvers
    1010from django.contrib.admindocs import utils
     11from django.contrib import admin
    1112from django.contrib.sites.models import Site
    1213from django.utils.translation import ugettext as _
    1314from django.utils.safestring import mark_safe
     
    2324def doc_index(request):
    2425    if not utils.docutils_is_available:
    2526        return missing_docutils_page(request)
    26     return render_to_response('admin_doc/index.html', context_instance=RequestContext(request))
     27    return render_to_response('admin_doc/index.html', context_instance=RequestContext(request),
     28                              dictionary={'root_path': admin_root(request, 'doc/')})
    2729doc_index = staff_member_required(doc_index)
    2830
    2931def bookmarklets(request):
    3032    # Hack! This couples this view to the URL it lives at.
    31     admin_root = request.path[:-len('doc/bookmarklets/')]
    3233    return render_to_response('admin_doc/bookmarklets.html', {
    33         'admin_url': mark_safe("%s://%s%s" % (request.is_secure() and 'https' or 'http', request.get_host(), admin_root)),
     34        'root_path': admin_root(request, 'doc/bookmarklets/'),
    3435    }, context_instance=RequestContext(request))
    3536bookmarklets = staff_member_required(bookmarklets)
    3637
     
    3940        return missing_docutils_page(request)
    4041
    4142    load_all_installed_template_libraries()
    42 
    4343    tags = []
    4444    for module_name, library in template.libraries.items():
    4545        for tag_name, tag_func in library.tags.items():
     
    6262                'library': tag_library,
    6363            })
    6464
    65     return render_to_response('admin_doc/template_tag_index.html', {'tags': tags}, context_instance=RequestContext(request))
     65    return render_to_response('admin_doc/template_tag_index.html', {'tags': tags, 'root_path': admin_root(request, 'doc/tags/')}, context_instance=RequestContext(request))
    6666template_tag_index = staff_member_required(template_tag_index)
    6767
    6868def template_filter_index(request):
     
    9292                'meta': metadata,
    9393                'library': tag_library,
    9494            })
    95     return render_to_response('admin_doc/template_filter_index.html', {'filters': filters}, context_instance=RequestContext(request))
     95    return render_to_response('admin_doc/template_filter_index.html', {'filters': filters, 'root_path': admin_root(request, 'doc/filters/')}, context_instance=RequestContext(request))
    9696template_filter_index = staff_member_required(template_filter_index)
    9797
    9898def view_index(request):
     
    120120                'site': site_obj,
    121121                'url': simplify_regex(regex),
    122122            })
    123     return render_to_response('admin_doc/view_index.html', {'views': views}, context_instance=RequestContext(request))
     123    return render_to_response('admin_doc/view_index.html', {'views': views, 'root_path': admin_root(request, 'doc/views/')}, context_instance=RequestContext(request))
    124124view_index = staff_member_required(view_index)
    125125
    126126def view_detail(request, view):
     
    152152        return missing_docutils_page(request)
    153153
    154154    m_list = [m._meta for m in models.get_models()]
    155     return render_to_response('admin_doc/model_index.html', {'models': m_list}, context_instance=RequestContext(request))
     155    return render_to_response('admin_doc/model_index.html', {'models': m_list, 'root_path': admin_root(request, 'doc/models/')}, context_instance=RequestContext(request))
    156156model_index = staff_member_required(model_index)
    157157
    158158def model_detail(request, app_label, model_name):
     
    262262# Helper functions #
    263263####################
    264264
     265def admin_root(request, current_path):
     266    return request.path[:-len(current_path)]
     267
    265268def missing_docutils_page(request):
    266269    """Display an error message for people without docutils"""
    267     return render_to_response('admin_doc/missing_docutils.html')
     270    return render_to_response('admin_doc/missing_docutils.html', dictionary={'root_path': admin_root(request, 'doc/')})
    268271
    269272def load_all_installed_template_libraries():
    270273    # Load/register all template tag libraries from installed apps.
Back to Top