Skip to content

Commit

Permalink
Show more info about user and in profile
Browse files Browse the repository at this point in the history
test_repo added to signup and profile
  • Loading branch information
peregrineshahin authored and ppigazzini committed May 22, 2024
1 parent 9875a87 commit 5e8653a
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 25 deletions.
26 changes: 26 additions & 0 deletions server/fishtest/templates/signup.mak
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,32 @@
<label for="email" class="d-flex align-items-end">Email</label>
</div>

<div class="input-group mb-3">
<div class="form-floating">
<input
class="form-control"
id="tests_repo"
name="tests_repo"
placeholder="GitHub Stockfish fork URL"
>
<label for="tests_repo" class="d-flex align-items-end">Tests Repository</label>
</div>
<span class="input-group-text" role="button" data-bs-toggle="modal" data-bs-target="#tests_repo_info_modal">
<i class="fas fa-question-circle fa-lg pe-none" style="width: 30px"></i>
</span>
</div>

<div id="tests_repo_info_modal" class="modal fade" tabindex="-1" aria-labelledby="tests_repo_info_modal_label" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-body">
This Github fork URL will be the default fork URL for users who want to contribute code when creating runs,
it is not needed for resources contribution.
</div>
</div>
</div>
</div>

<div class="g-recaptcha mb-3"
data-sitekey="6LePs8YUAAAAABMmqHZVyVjxat95Z1c_uHrkugZM"></div>

Expand Down
80 changes: 61 additions & 19 deletions server/fishtest/templates/user.mak
Original file line number Diff line number Diff line change
@@ -1,41 +1,56 @@
<%inherit file="base.mak"/>

<script>
document.title = "User Management | Stockfish Testing";
</script>

% if profile:
<script>
async function handleGitHubToken() {
await DOMContentLoaded();
document.getElementById("github_token").value = localStorage.getItem("github_token") || "";
document.getElementById("profile_form").addEventListener("submit", (e) => {
e.preventDefault();
const githubToken = document.getElementById("github_token").value;
localStorage.setItem("github_token", githubToken);
e.target.submit();
});
}
handleGitHubToken();
document.title = "Profile | Stockfish Testing";
async function handleGitHubToken() {
await DOMContentLoaded();
document.getElementById("github_token").value = localStorage.getItem("github_token") || "";
document.getElementById("profile_form").addEventListener("submit", (e) => {
e.preventDefault();
const githubToken = document.getElementById("github_token").value;
localStorage.setItem("github_token", githubToken);
e.target.submit();
});
}
handleGitHubToken();
</script>
% else:
<script>
document.title = "User Management | Stockfish Testing";
</script>
% endif

<div class="col-limited-size">
<header class="text-md-center py-2">
<h2>User Management</h2>
% if profile:
<h2>Profile</h2>
% else:
<h2>User Management</h2>
% endif
<div class="alert alert-info">
<h4 class="alert-heading">
<a href="/tests/user/${user['username']}" class="alert-link col-6 text-break">${user['username']}</a>
</h4>
<ul class="list-group list-group-flush">
<li class="list-group-item bg-transparent text-break">Registered: ${format_date(user['registration_time'] if 'registration_time' in user else 'Unknown')}</li>
% if not profile:
<li class="list-group-item bg-transparent text-break">Tests Repository:
% if user['tests_repo']:
<a class="alert-link" href="${user['tests_repo']}">${extract_repo_from_link(user['tests_repo'])}</a>
% else:
<span>-</span>
% endif
</li>
<li class="list-group-item bg-transparent text-break">Email:
<a href="mailto:${user['email']}?Subject=Fishtest%20Account" class="alert-link">
${user['email']}
</a>
</li>
% endif
<li class="list-group-item bg-transparent text-break">Registered: ${format_date(user['registration_time'] if 'registration_time' in user else 'Unknown')}</li>
<li class="list-group-item bg-transparent text-break">
Groups: ${', '.join([group.replace('group:', '') for group in user['groups']]) if user['groups'] and len(user['groups']) > 0 else "No group"}
</li>
<li class="list-group-item bg-transparent text-break">Machine Limit: ${limit}</li>
<li class="list-group-item bg-transparent text-break">CPU-Hours: ${hours}</li>
</ul>
Expand Down Expand Up @@ -113,6 +128,33 @@
</span>
</div>

<div class="input-group mb-3">
<div class="form-floating">
<input
class="form-control"
id="tests_repo"
name="tests_repo"
value="${user['tests_repo']}"
placeholder="GitHub Stockfish fork URL"
>
<label for="tests_repo" class="d-flex align-items-end">Tests Repository</label>
</div>
<span class="input-group-text" role="button" data-bs-toggle="modal" data-bs-target="#tests_repo_info_modal">
<i class="fas fa-question-circle fa-lg pe-none" style="width: 30px"></i>
</span>
</div>

<div id="tests_repo_info_modal" class="modal fade" tabindex="-1" aria-labelledby="tests_repo_info_modal_label" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-body">
This Github fork URL will be the default fork URL for users who want to contribute code when creating runs,
it is not needed for resources contribution.
</div>
</div>
</div>
</div>

<div class="input-group mb-3">
<div class="form-floating">
<input
Expand All @@ -124,12 +166,12 @@
/>
<label for="github_token" class="d-flex align-items-end">GitHub's fine-grained personal access token</label>
</div>
<span class="input-group-text" role="button" data-bs-toggle="modal" data-bs-target="#info_modal">
<span class="input-group-text" role="button" data-bs-toggle="modal" data-bs-target="#github_token_info_modal">
<i class="fas fa-question-circle fa-lg pe-none" style="width: 30px"></i>
</span>
</div>

<div id="info_modal" class="modal fade" tabindex="-1" aria-labelledby="info_modal_label" aria-hidden="true">
<div id="github_token_info_modal" class="modal fade" tabindex="-1" aria-labelledby="github_token_info_modal_label" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-body">
Expand Down
4 changes: 2 additions & 2 deletions server/fishtest/userdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def add_user_group(self, username, group):
self.users.replace_one({"_id": user["_id"]}, user)
self.clear_cache()

def create_user(self, username, password, email):
def create_user(self, username, password, email, tests_repo):
try:
if self.find_by_username(username) or self.find_by_email(email):
return False
Expand All @@ -118,7 +118,7 @@ def create_user(self, username, password, email):
"blocked": False,
"email": email,
"groups": [],
"tests_repo": "",
"tests_repo": tests_repo,
"machine_limit": DEFAULT_MACHINE_LIMIT,
}
validate_user(user)
Expand Down
22 changes: 22 additions & 0 deletions server/fishtest/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,28 @@ def email_valid(email):
return False, str(e)


def github_repo_valid(url):
# Accept no repo for resources contribution
if not url:
return True

# Regular expression to match the GitHub repository URL pattern, with optional 'www.'
pattern = r"^https:\/\/(www\.)?github\.com\/[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+\/?$"
return re.match(pattern, url) is not None


def extract_repo_from_link(url):
# Validate the URL
if not github_repo_valid(url):
return None

# Regular expression to capture the username/repository part of the URL
match = re.search(r"github\.com\/([A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+)\/?", url)
if match:
return match.group(1)
return None


def get_hash(s):
h = re.search("Hash=([0-9]+)", s)
if h:
Expand Down
18 changes: 17 additions & 1 deletion server/fishtest/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
from fishtest.schemas import short_worker_name
from fishtest.util import (
email_valid,
extract_repo_from_link,
format_bounds,
format_date,
format_results,
get_chi2,
get_hash,
get_tc_ratio,
github_repo_valid,
password_strength,
update_residuals,
)
Expand Down Expand Up @@ -370,6 +372,7 @@ def signup(request):
signup_password = request.POST.get("password", "").strip()
signup_password_verify = request.POST.get("password2", "").strip()
signup_email = request.POST.get("email", "").strip()
tests_repo = request.POST.get("tests_repo", "").strip()

strong_password, password_err = password_strength(
signup_password, signup_username, signup_email
Expand All @@ -385,6 +388,8 @@ def signup(request):
errors.append("Error! Username required")
if not signup_username.isalnum():
errors.append("Error! Alphanumeric username required")
if not github_repo_valid(tests_repo):
errors.append("Error! Invalid tests repo: " + tests_repo)
if errors:
for error in errors:
request.session.flash(error, "error")
Expand All @@ -411,7 +416,10 @@ def signup(request):
return {}

result = request.userdb.create_user(
username=signup_username, password=signup_password, email=validated_email
username=signup_username,
password=signup_password,
email=validated_email,
tests_repo=tests_repo,
)
if result is None:
request.session.flash("Error! Invalid username or password", "error")
Expand Down Expand Up @@ -598,6 +606,7 @@ def user(request):
new_password = request.params.get("password").strip()
new_password_verify = request.params.get("password2", "").strip()
new_email = request.params.get("email").strip()
tests_repo = request.params.get("tests_repo").strip()

# Temporary comparison until passwords are hashed.
if old_password != user_data["password"].strip():
Expand Down Expand Up @@ -626,6 +635,12 @@ def user(request):
)
return home(request)

if not github_repo_valid(tests_repo):
request.session.flash("Error! Invalid test repo", "error")
return home(request)
else:
user_data["tests_repo"] = tests_repo

if len(new_email) > 0 and user_data["email"] != new_email:
email_is_valid, validated_email = email_valid(new_email)
if not email_is_valid:
Expand Down Expand Up @@ -673,6 +688,7 @@ def user(request):
"limit": request.userdb.get_machine_limit(user_name),
"hours": hours,
"profile": profile,
"extract_repo_from_link": extract_repo_from_link,
}


Expand Down
14 changes: 12 additions & 2 deletions server/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,12 @@ def setUpClass(cls):
"ARCH": "?",
"nps": 0.0,
}
cls.rundb.userdb.create_user(cls.username, cls.password, "email@email.email")
cls.rundb.userdb.create_user(
cls.username,
cls.password,
"email@email.email",
"https://github.com/official-stockfish/Stockfish",
)
user = cls.rundb.userdb.get_user(cls.username)
user["pending"] = False
user["machine_limit"] = 50
Expand Down Expand Up @@ -500,7 +505,12 @@ def setUpClass(cls):
"ARCH": "?",
"nps": 0.0,
}
cls.rundb.userdb.create_user(cls.username, cls.password, "email@email.email")
cls.rundb.userdb.create_user(
cls.username,
cls.password,
"email@email.email",
"https://github.com/official-stockfish/Stockfish",
)
user = cls.rundb.userdb.get_user(cls.username)
user["pending"] = False
user["machine_limit"] = 50
Expand Down
8 changes: 7 additions & 1 deletion server/tests/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def test_create_user(self):
"password": "secret",
"password2": "secret",
"email": "joe@user.net",
"tests_repo": "https://github.com/official-stockfish/Stockfish",
},
)
response = signup(request)
Expand All @@ -38,7 +39,12 @@ def test_create_user(self):
class Create50LoginTest(unittest.TestCase):
def setUp(self):
self.rundb = util.get_rundb()
self.rundb.userdb.create_user("JoeUser", "secret", "email@email.email")
self.rundb.userdb.create_user(
"JoeUser",
"secret",
"email@email.email",
"https://github.com/official-stockfish/Stockfish",
)
self.config = testing.setUp()
self.config.add_route("login", "/login")

Expand Down

0 comments on commit 5e8653a

Please sign in to comment.