[FIX] website: slug: fallback to positive ID when slug appears to contain a missing...
authorOlivier Dony <odo@openerp.com>
Fri, 1 Aug 2014 08:50:25 +0000 (10:50 +0200)
committerOlivier Dony <odo@openerp.com>
Fri, 1 Aug 2014 13:08:33 +0000 (15:08 +0200)
In some rare cases database records have negative IDs,
so the slug URL could look like /foo--20. This could
be mistaken for a slug ending with a `-` and a positive ID.
The latter is not supposed to happned as final hyphens
are stripped by slugify, but has been used in the past
and may be used in old links.

addons/website/models/ir_http.py

index 8c09c99..76386f0 100644 (file)
@@ -241,8 +241,13 @@ class ModelConverter(ir.ir_http.ModelConverter):
     def to_python(self, value):
         m = re.match(self.regex, value)
         _uid = RequestUID(value=value, match=m, converter=self)
+        record_id = int(m.group(2))
+        if record_id < 0:
+            # limited support for negative IDs due to our slug pattern, assume abs() if not found
+            if not request.registry[self.model].exists(request.cr, _uid, [record_id]):
+                record_id = abs(record_id)
         return request.registry[self.model].browse(
-            request.cr, _uid, int(m.group(2)), context=request.context)
+            request.cr, _uid, record_id, context=request.context)
 
     def generate(self, cr, uid, query=None, args=None, context=None):
         obj = request.registry[self.model]