example_json.py 864 B

12345678910111213141516171819202122232425262728
  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(
  12. wordfile=wordfile,
  13. min_length=3,
  14. max_length=8)
  15. suggestion = xp.generate_xkcdpassword(words, acrostic=acrostic)
  16. if titlecase:
  17. # Convert "foo bar" to "Foo Bar"
  18. suggestion = suggestion.title()
  19. return JsonResponse({
  20. 'suggestion': suggestion}
  21. )