From 6ab11188d5714a0330bb9ad5011fd5dfbbf1ff54 Mon Sep 17 00:00:00 2001 From: Boulder Sprinters Date: Sat, 4 Nov 2006 20:18:55 +0000 Subject: [PATCH] Add a truncate_name method to shorten DB identifiers for Oracle git-svn-id: http://code.djangoproject.com/svn/django/branches/boulder-oracle-sprint@3972 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/backends/util.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/django/db/backends/util.py b/django/db/backends/util.py index 3ec1b41485..961e50e91b 100644 --- a/django/db/backends/util.py +++ b/django/db/backends/util.py @@ -1,4 +1,5 @@ import datetime +import md5 from time import time class CursorDebugWrapper(object): @@ -92,6 +93,16 @@ def typecast_boolean(s): def rev_typecast_boolean(obj, d): return obj and '1' or '0' +def truncate_name(name, length=None): + """Shortens a string to a repeatable mangled version with the given length. + """ + if length is None or len(name) <= length: + return name + + hash = md5.md5(name).hexdigest()[:4] + + return '%s%s' % (name[:length-4], hash) + ################################################################################## # Helper functions for dictfetch* for databases that don't natively support them # ##################################################################################