Changeset - 6d04883b0a8b
[Not reviewed]
0 2 0
Branko Majic (branko) - 10 years ago 2014-01-04 12:14:20
branko@majic.rs
DJPYD-1: Make the digest algorithm configurable. Fix generator initialisation to use custom settings.
2 files changed with 11 insertions and 1 deletions:
0 comments (0 inline, 0 general)
django_pydenticon/settings.py
Show inline comments
 
# Standard Python library imports.
 
import collections
 
import hashlib
 

	
 
# Django imports
 
from django.conf import settings
 
from django.core.exceptions import ImproperlyConfigured
 
@@ -17,6 +21,7 @@ PYDENTICON_FOREGROUND = getattr(settings, "PYDENTICON_FOREGROUND", ( "rgb(45,79,
 
                                                                     "rgb(49,203,115)",
 
                                                                     "rgb(141,69,170)" ))
 
PYDENTICON_BACKGROUND = getattr(settings, "PYDENTICON_BACKGROUND", "rgb(224,224,224)")
 
PYDENTICON_DIGEST = getattr(settings, "PYDENTICON_DIGEST", hashlib.md5)
 

	
 
# Validate the settings.
 
if not isinstance(PYDENTICON_ROWS, int) or PYDENTICON_ROWS <= 0:
 
@@ -43,3 +48,5 @@ if not all([isinstance(f, str) for f in PYDENTICON_FOREGROUND]):
 
if not isinstance(PYDENTICON_BACKGROUND, str):
 
    raise ImproperlyConfigured("Setting PYDENTICON_BACKGROUND must be a string representation of colour")
 

	
 
if not isinstance(PYDENTICON_DIGEST, collections.Callable):
 
    raise ImproperlyConfigured("Setting PYDENTICON_DIGEST must be a callable digest (usually from hashlib module).")
django_pydenticon/views.py
Show inline comments
 
@@ -9,6 +9,7 @@ from django.http import HttpResponseBadRequest
 
# Application imports.
 
from .settings import PYDENTICON_ROWS, PYDENTICON_COLUMNS, PYDENTICON_WIDTH, PYDENTICON_HEIGHT
 
from .settings import PYDENTICON_PADDING, PYDENTICON_FORMAT, PYDENTICON_FOREGROUND, PYDENTICON_BACKGROUND
 
from .settings import PYDENTICON_DIGEST
 

	
 
def image(request, data):
 
    """
 
@@ -61,7 +62,9 @@ def image(request, data):
 
        raise SuspiciousOperation("Unsupported identicon format requested - '%s' % output_format")
 

	
 
    # Initialise a generator.
 
    generator = Generator(PYDENTICON_ROWS, PYDENTICON_COLUMNS)
 
    generator = Generator(PYDENTICON_ROWS, PYDENTICON_COLUMNS,
 
                          foreground = PYDENTICON_FOREGROUND, background = PYDENTICON_BACKGROUND,
 
                          digest = PYDENTICON_DIGEST)
 

	
 
    # Generate the identicion.
 
    content = generator.generate(data, width, height, padding=padding, output_format=output_format)
0 comments (0 inline, 0 general)