utils.py 747 B

123456789101112131415161718192021222324
  1. from string import ascii_letters, digits, printable
  2. def auth_with_username_or_email(username, password):
  3. password = encode_password(username, password)
  4. from account.models import User
  5. if '@' in username:
  6. user = User.objects.get(email=username, password=password)
  7. else:
  8. user = User.objects.get(username=username, password=password)
  9. return user
  10. def check_password(password):
  11. if set(password).isdisjoint(ascii_letters) and set(password).isdisjoint(digits):
  12. return False
  13. return set(password).issubset(printable) and len(password) >= 8
  14. def encode_password(username, password):
  15. import hashlib
  16. md5 = hashlib.md5()
  17. md5.update((username + password).encode())
  18. return md5.hexdigest()