diff --git a/OpenBench/models.py b/OpenBench/models.py index f52ef68..75d0c15 100644 --- a/OpenBench/models.py +++ b/OpenBench/models.py @@ -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) diff --git a/OpenBench/urls.py b/OpenBench/urls.py index 4cf6d5a..68ab564 100644 --- a/OpenBench/urls.py +++ b/OpenBench/urls.py @@ -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), diff --git a/OpenBench/views.py b/OpenBench/views.py index db8c8c0..8882bda 100644 --- a/OpenBench/views.py +++ b/OpenBench/views.py @@ -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 # diff --git a/Templates/OpenBench/profile.html b/Templates/OpenBench/profile.html index 63aa8b2..18bb3b9 100644 --- a/Templates/OpenBench/profile.html +++ b/Templates/OpenBench/profile.html @@ -7,42 +7,87 @@