multiple repos

This commit is contained in:
rafid-dev committed 2023-06-09 09:49:56 +06:00
1 parent 9faa5c51aa
commit 5ae64025aa
4 files changed
+118 -26

No files matched your search

+1 -1
View File
@@ -38,7 +38,7 @@ class Profile(Model):
user = ForeignKey(User, PROTECT, related_name='user')
games = IntegerField(default=0)
tests = IntegerField(default=0)
repo = CharField(max_length=256, blank=True)
repos = JSONField()
engine = CharField(max_length=128, blank=True)
enabled = BooleanField(default=False)
approver = BooleanField(default=False)
+1
View File
@@ -27,6 +27,7 @@ urlpatterns = [
django.urls.path(r'login/', OpenBench.views.login),
django.urls.path(r'logout/', OpenBench.views.logout),
django.urls.path(r'profile/', OpenBench.views.profile),
django.urls.path(r'profileConfig/', OpenBench.views.profileConfig),
# Links for viewing test tables
django.urls.path(r'index/', OpenBench.views.index),
+46
View File
@@ -200,6 +200,52 @@ def profile(request):
return django.http.HttpResponseRedirect('/index/')
def profileConfig(request):
if not request.user.is_authenticated:
return django.http.HttpResponseRedirect('/login/')
if request.method == 'GET':
return render(request, 'profile.html')
profile = Profile.objects.filter(user=request.user)
profile = profile.first()
changes = False # To check if any changes are made.
default_status = request.POST.get('default-status', '')
if default_status != '':
# update the default engine
profile.engine = default_status
changes = True
deleted_repos = request.POST.get('deleted-repos', '[]')
deleted_repos_array = json.loads(deleted_repos)
for engine in deleted_repos_array:
if engine in profile.repos:
del profile.repos[engine]
if len(deleted_repos_array) != 0:
changes = True
if 'new-engine-name' in request.POST:
if request.POST['new-engine-name'] == "None":
return index(request, error="Choose engine can't be None.")
elif request.POST['new-engine-name'] != "None" and not request.POST['new-engine-repo'].startswith('https://github.com/'):
return index(request, error="Not a valid repository!")
elif request.POST['new-engine-name'] != "None" and request.POST['new-engine-repo'].startswith('https://github.com/'):
engine_name = request.POST['new-engine-name']
engine_repo = request.POST['new-engine-repo']
# Append the new engine and repo to the profile's repos field
profile.repos[engine_name] = engine_repo
changes = True
if changes:
profile.save() # Save if any changes are made
return django.http.HttpResponseRedirect('/index/')
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# TEST LIST VIEWS #
+70 -25
View File
@@ -7,42 +7,87 @@
<div class="form">
<div class="col">
<div class="row">
<label>Username</label><input readonly value="{{profile.user.username}}">
<label>Username</label><input readonly value="{{ profile.user.username }}">
</div>
<div class="row">
<label>Email</label><input value="{{profile.user.email}}" name="email">
</div>
<div class="row">
<label>Source Repo</label><input value="{{profile.repo}}" name="repo">
<label>Email</label><input value="{{ profile.user.email }}" name="email">
</div>
<div class="row">
<label>New Password</label><input type="password" name="password1">
</div>
<div class="row">
<label>New Password</label><input type="password" name="password2">
</div>
<div class="row">
<label>Engine</label>
<select id="engine" name="engine">
{% for engine in config.engines %}
{% if profile.engine == engine %}
<option selected value="{{engine}}">{{engine}}</option>
{% else %}
<option value="{{engine}}">{{engine}}</option>
{% endif %}
{% endfor %}
{% if not profile.engine %}
<option selected value=""></option>
{% endif %}
</select>
</div>
<input class="anchorbutton btn-blue w-100" type="submit" value="Update Profile">
</div>
</div>
</form>
<hr>
<br>
<form method="POST" action="/profileConfig/">
{% csrf_token %}
<div class="col" id="engines-container">
{% for engine, repo in profile.repos.items %}
<div class="row" id="engine-row-{{ engine }}">
<label>{{ engine }}</label>
<input type="text" name="engine-repo-{{ engine }}" value="{{ repo }}">
<input id="radio-{{engine}}" type="radio" name="default-status" value="{{ engine }}" {% if profile.engine == engine %}checked{% endif %}>
<span onclick="deleteRow(event)">
<i class="fa-solid fa-trash-can" style="color: #ff0000;" data-engine="{{ engine }}"></i>
</span>
</div>
{% endfor %}
<div class="row">
<select name="new-engine-name">
<option value="None">None</option>
<option selected disabled hidden>Choose an engine</option>
{% for engine in config.engines.keys %}
{% if engine not in profile.repos %}
<option value="{{ engine }}">{{ engine }}</option>
{% endif %}
{% endfor %}
</select>
<input name="new-engine-repo">
</div>
{% endblock %}
<input type="hidden" name="deleted-repos" id="deleted-repos" value="[]">
<input class="anchorbutton btn-blue w-100" type="submit" name="add-engine" value="Update Engine">
</div>
</form>
<script>
function deleteRow(event) {
const button = event.currentTarget.querySelector('i');
const engine = button.getAttribute('data-engine');
const radioId = 'radio-' + engine;
const radio = document.getElementById(radioId);
if (engine && radio && radio.checked) {
// Don't delete default engine.
return;
}
if (engine) {
// Perform the delete action here
console.log(`Deleting engine: ${engine}`);
// Remove the corresponding row from the DOM
const row = button.parentNode.parentNode;
row.parentNode.removeChild(row);
// Update the deleted-repos hidden input field with the deleted engine
const deletedReposInput = document.getElementById('deleted-repos');
let deletedRepos = JSON.parse(deletedReposInput.value);
deletedRepos.push(engine);
deletedReposInput.value = JSON.stringify(deletedRepos);
console.log(deletedRepos);
}
}
</script>
{% endblock %}