From: Scot Hacker Date: Thu, 1 Oct 2015 04:50:32 +0000 (-0700) Subject: Add Django integration example X-Git-Url: http://git.99rst.org/?a=commitdiff_plain;h=8b89fd02ff4a13f03874597601b7eba021379520;p=redacted-XKCD-password-generator.git Add Django integration example --- diff --git a/xkcdpass/example_json.py b/xkcdpass/example_json.py new file mode 100644 index 0000000..bd24f2a --- /dev/null +++ b/xkcdpass/example_json.py @@ -0,0 +1,24 @@ +from xkcdpass import xkcd_password as xp +from django.http import JsonResponse + + +def json_password_generator(request): + # Example Django view to generate passphrase suggestions via xkcd library + # Called with optional params e.g. + # /json_password_generator/?tc=true&separator=|&acrostic=face + + if request.method == 'GET': + acrostic = request.GET.get("acrostic", None) + titlecase = request.GET.get("tc", None) + + wordfile = xp.locate_wordfile() + words = xp.generate_wordlist(wordfile=wordfile, min_length=3, max_length=8) + suggestion = xp.generate_xkcdpassword(words, acrostic=acrostic) + + if titlecase: + # Convert "foo bar" to "Foo Bar" + suggestion = suggestion.title() + + return JsonResponse({ + 'suggestion': suggestion} + )