Coders-HQ / CodersHQ

Social platform for developers to network, create and challenge themselves
https://codershq.ae
MIT License
139 stars 46 forks source link

Use `requests` library to validate the user #194

Open delrius-euphoria opened 2 years ago

delrius-euphoria commented 2 years ago

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
delrius-euphoria commented 2 years ago

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