Django

Code

Changeset 2418

Show
Ignore:
Timestamp:
02/27/06 15:36:52 (2 years ago)
Author:
jacob
Message:

Fixed #1375: primary_key values with "bad" characters are now escaped in the admin; thanks, Malcom Tredinnick

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/magic-removal/django/contrib/admin/views/main.py

    r2416 r2418  
    4242class IncorrectLookupParameters(Exception): 
    4343    pass 
     44 
     45def quote(s): 
     46    """ 
     47    Ensure that primary key values do not confuse the admin URLs by escaping 
     48    any '/', '_' and ':' characters. Similar to urllib.quote, except that the 
     49        quoting is slightly different so that it doesn't get autoamtically 
     50        unquoted by the web browser. 
     51    """ 
     52    if type(s) != type(''): 
     53        return s 
     54    res = list(s) 
     55    for i in range(len(res)): 
     56        c = res[i] 
     57        if c in ':/_': 
     58            res[i] = '_%02X' % ord(c) 
     59    return ''.join(res) 
     60 
     61def unquote(s): 
     62    """ 
     63    Undo the effects of quote(). Based heavily on urllib.unquote(). 
     64    """ 
     65    mychr = chr 
     66    myatoi = int 
     67    list = s.split('_') 
     68    res = [list[0]] 
     69    myappend = res.append 
     70    del list[0] 
     71    for item in list: 
     72        if item[1:2]: 
     73            try: 
     74                myappend(mychr(myatoi(item[:2], 16)) 
     75                     + item[2:]) 
     76            except ValueError: 
     77                myappend('_' + item) 
     78        else: 
     79            myappend('_' + item) 
     80    return "".join(res) 
    4481 
    4582def get_javascript_imports(opts, auto_populated_fields, field_sets): 
     
    253290def change_stage(request, app_label, model_name, object_id): 
    254291    model = models.get_model(app_label, model_name) 
     292    object_id = unquote(object_id) 
    255293    if model is None: 
    256294        raise Http404, "App %r, model %r, not found" % (app_label, model_name) 
     
    436474    import sets 
    437475    model = models.get_model(app_label, model_name) 
     476    object_id = unquote(object_id) 
    438477    if model is None: 
    439478        raise Http404, "App %r, model %r, not found" % (app_label, model_name) 
     
    471510def history(request, app_label, model_name, object_id): 
    472511    model = models.get_model(app_label, model_name) 
     512    object_id = unquote(object_id) 
    473513    if model is None: 
    474514        raise Http404, "App %r, model %r, not found" % (app_label, model_name) 
     
    664704 
    665705    def url_for_result(self, result): 
    666         return "%s/" % getattr(result, self.pk_attname
     706        return "%s/" % quote(getattr(result, self.pk_attname)
    667707 
    668708def change_list(request, app_label, model_name):