example_json.py 827 B

12345678910111213141516171819202122232425
  1. from xkcdpass import xkcd_password as xp
  2. from django.http import JsonResponse
  3. def json_password_generator(request):
  4. # Example Django view to generate passphrase suggestions via xkcd library
  5. # Called with optional params e.g.
  6. # /json_password_generator/?tc=true&separator=|&acrostic=face
  7. if request.method == 'GET':
  8. acrostic = request.GET.get("acrostic", None)
  9. titlecase = request.GET.get("tc", None)
  10. wordfile = xp.locate_wordfile()
  11. words = xp.generate_wordlist(wordfile=wordfile, min_length=3, max_length=8)
  12. suggestion = xp.generate_xkcdpassword(words, acrostic=acrostic)
  13. if titlecase:
  14. # Convert "foo bar" to "Foo Bar"
  15. suggestion = suggestion.title()
  16. return JsonResponse({
  17. 'suggestion': suggestion}
  18. )