CodersHQ
CodersHQ copied to clipboard
Use `requests` library to validate the user
https://github.com/Coders-HQ/CodersHQ/blob/40400f7c866bbb16ed0064e2ee180a5241f86d84/codershq/users/validators.py#L15
Why not just use an additional step that uses request to check if the userprofile exists or not? Like:
>>> import requests
>>> req = requests.get('https://www.github.com/SomeUserThatDoesNotExistsHopefully')
>>> req.status_code
404
>>> req = requests.get('https://www.github.com/delrius-euphoria')
>>> req.status_code
200
A possible way is something like:
import requests
def validate_github_profile(value):
"""validate github profile"""
pattern = r"^github.com\/[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*\/?$"
if re.search(pattern, value) is None:
raise ValidationError(
_("%(value)s is not a valid GitHub profile."),
params={"value": value},
)
status_code = requests.get(value).status_code
if status_code == 404:
raise ValidationError(
_("%(value)s is not a valid GitHub profile."),
params={"value": value},
)
Or something along the lines