Ticket #1375: main2.diff

File main2.diff, 2.7 KB (added by Malcolm Tredinnick <malcolm@…>, 18 years ago)

The previous main.diff was broken for the common (default) case. :-( Sorry about that.

  • contrib/admin/views/main.py

     
    4141class IncorrectLookupParameters(Exception):
    4242    pass
    4343
     44def quote(s):
     45    """
     46    Ensure that primary key values do not confuse the admin URLs by escaping
     47    any '/', '_' and ':' characters. Similar to urllib.quote, except that the
     48        quoting is slightly different so that it doesn't get autoamtically
     49        unquoted by the web browser.
     50    """
     51    if type(s) != type(''):
     52        return s
     53    res = list(s)
     54    for i in range(len(res)):
     55        c = res[i]
     56        if c in ':/_':
     57            res[i] = '_%02X' % ord(c)
     58    return ''.join(res)
     59
     60def unquote(s):
     61    """
     62    Undo the effects of quote(). Based heavily on urllib.unquote().
     63    """
     64    mychr = chr
     65    myatoi = int
     66    list = s.split('_')
     67    res = [list[0]]
     68    myappend = res.append
     69    del list[0]
     70    for item in list:
     71        if item[1:2]:
     72            try:
     73                myappend(mychr(myatoi(item[:2], 16))
     74                     + item[2:])
     75            except ValueError:
     76                myappend('_' + item)
     77        else:
     78            myappend('_' + item)
     79    return "".join(res)
     80
    4481def get_javascript_imports(opts, auto_populated_fields, field_sets):
    4582# Put in any necessary JavaScript imports.
    4683    js = ['js/core.js', 'js/admin/RelatedObjectLookups.js']
     
    250287
    251288def change_stage(request, app_label, model_name, object_id):
    252289    model = models.get_model(app_label, model_name)
     290    object_id = unquote(object_id)
    253291    if model is None:
    254292        raise Http404, "App %r, model %r, not found" % (app_label, model_name)
    255293    opts = model._meta
     
    433471def delete_stage(request, app_label, model_name, object_id):
    434472    import sets
    435473    model = models.get_model(app_label, model_name)
     474    object_id = unquote(object_id)
    436475    if model is None:
    437476        raise Http404, "App %r, model %r, not found" % (app_label, model_name)
    438477    opts = model._meta
     
    465504
    466505def history(request, app_label, model_name, object_id):
    467506    model = models.get_model(app_label, model_name)
     507    object_id = unquote(object_id)
    468508    if model is None:
    469509        raise Http404, "App %r, model %r, not found" % (app_label, model_name)
    470510    action_list = LogEntry.objects.filter(object_id=object_id,
     
    655695        return qs
    656696
    657697    def url_for_result(self, result):
    658         return "%s/" % getattr(result, self.pk_attname)
     698        return "%s/" % quote(getattr(result, self.pk_attname))
    659699
    660700def change_list(request, app_label, model_name):
    661701    model = models.get_model(app_label, model_name)
Back to Top