summaryrefslogtreecommitdiff
path: root/django/db/backends/utils.py
diff options
context:
space:
mode:
authorJon Dufresne <jon.dufresne@gmail.com>2018-10-02 09:41:43 -0500
committerTim Graham <timograham@gmail.com>2018-10-02 10:41:43 -0400
commit0bf7b25f8f667d3710de91e91ae812efde05187c (patch)
treec19992c37342e341a7a5848493ff53d88c23b620 /django/db/backends/utils.py
parentc37b84434937925149fd4626beedc2de62dc446e (diff)
downloaddjango-0bf7b25f8f667d3710de91e91ae812efde05187c.tar.gz
Added django.db.backends.utils.names_digest() to remove redundant code.
Diffstat (limited to 'django/db/backends/utils.py')
-rw-r--r--django/db/backends/utils.py13
1 files changed, 12 insertions, 1 deletions
diff --git a/django/db/backends/utils.py b/django/db/backends/utils.py
index a762175025..2062af91f1 100644
--- a/django/db/backends/utils.py
+++ b/django/db/backends/utils.py
@@ -213,10 +213,21 @@ def truncate_name(identifier, length=None, hash_len=4):
if length is None or len(name) <= length:
return identifier
- digest = hashlib.md5(name.encode()).hexdigest()[:hash_len]
+ digest = names_digest(name, length=hash_len)
return '%s%s%s' % ('%s"."' % namespace if namespace else '', name[:length - hash_len], digest)
+def names_digest(*args, length):
+ """
+ Generate a 32-bit digest of a set of arguments that can be used to shorten
+ identifying names.
+ """
+ h = hashlib.md5()
+ for arg in args:
+ h.update(arg.encode())
+ return h.hexdigest()[:length]
+
+
def format_number(value, max_digits, decimal_places):
"""
Format a number into a string with the requisite number of digits and