Add Django integration example
authorScot Hacker <redacted>
Thu, 1 Oct 2015 04:50:32 +0000 (21:50 -0700)
committerScot Hacker <redacted>
Thu, 1 Oct 2015 04:50:32 +0000 (21:50 -0700)
xkcdpass/example_json.py [new file with mode: 0644]

diff --git a/xkcdpass/example_json.py b/xkcdpass/example_json.py
new file mode 100644 (file)
index 0000000..bd24f2a
--- /dev/null
@@ -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}
+            )
git clone https://git.99rst.org/PROJECT