Changeset - 9b6778938da5
[Not reviewed]
0 2 0
Branko Majic (branko) - 10 years ago 2014-01-04 22:48:08
branko@majic.rs
DJPYD-1: Added support for configuring and passing the inversion parameter.
2 files changed with 16 insertions and 2 deletions:
0 comments (0 inline, 0 general)
django_pydenticon/settings.py
Show inline comments
 
@@ -22,6 +22,7 @@ PYDENTICON_FOREGROUND = getattr(settings, "PYDENTICON_FOREGROUND", ( "rgb(45,79,
 
                                                                     "rgb(141,69,170)" ))
 
PYDENTICON_BACKGROUND = getattr(settings, "PYDENTICON_BACKGROUND", "rgb(224,224,224)")
 
PYDENTICON_DIGEST = getattr(settings, "PYDENTICON_DIGEST", hashlib.md5)
 
PYDENTICON_INVERT = getattr(settings, "PYDENTICON_INVERT", False)
 

	
 
# Validate the settings.
 
if not isinstance(PYDENTICON_ROWS, int) or PYDENTICON_ROWS <= 0:
 
@@ -50,3 +51,6 @@ if not isinstance(PYDENTICON_BACKGROUND, str):
 

	
 
if not isinstance(PYDENTICON_DIGEST, collections.Callable):
 
    raise ImproperlyConfigured("Setting PYDENTICON_DIGEST must be a callable digest (usually from hashlib module).")
 

	
 
if not isinstance(PYDENTICON_INVERT, bool):
 
    raise ImproperlyConfigured("Setting PYDENTICON_INVERT must be a boolean.")
django_pydenticon/views.py
Show inline comments
 
@@ -9,7 +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
 
from .settings import PYDENTICON_DIGEST, PYDENTICON_INVERT
 

	
 
def image(request, data):
 
    """
 
@@ -44,6 +44,16 @@ def image(request, data):
 
        padding = PYDENTICON_PADDING
 
    except ValueError:
 
        raise SuspiciousOperation("Identicon padding must consist out of 4 positive integers separated with commas.")
 
    if "i" in request.GET:
 
        inverted = request.GET.get("i")
 
        if inverted.lower() == "true":
 
            inverted = True
 
        elif inverted.lower() == "false":
 
            inverted = False
 
        else:
 
            raise SuspiciousOperation("Inversion parameter must be a boolean (true/false).")
 
    else:
 
        inverted = PYDENTICON_INVERT
 

	
 
    # Validate the input parameters.
 
    if not isinstance(width, int) or width <= 0:
 
@@ -67,7 +77,7 @@ def image(request, data):
 
                          digest = PYDENTICON_DIGEST)
 

	
 
    # Generate the identicion.
 
    content = generator.generate(data, width, height, padding=padding, output_format=output_format)
 
    content = generator.generate(data, width, height, padding=padding, output_format=output_format, inverted=inverted)
 

	
 
    # Create and return the response.
 
    response = HttpResponse(content, content_type=content_type)
0 comments (0 inline, 0 general)