from string import ascii_letters, digits, printable def auth_with_username_or_email(username, password): password = encode_password(username, password) from account.models import User if '@' in username: user = User.objects.get(email=username, password=password) else: user = User.objects.get(username=username, password=password) return user def check_password(password): if set(password).isdisjoint(ascii_letters) and set(password).isdisjoint(digits): return False return set(password).issubset(printable) and len(password) >= 8 def encode_password(username, password): import hashlib md5 = hashlib.md5() md5.update((username + password).encode()) return md5.hexdigest()