...
  View open merge request
Commits (2)
......@@ -4,13 +4,17 @@ import logging
import os
import re
# Third party imports
import natsort
import redis
import IPy
from django.conf import settings
from django.db.models import Q
import config
from .models import Job, Webapp, AllgoUser
from .models import Job, Webapp, AllgoUser, WebappVersion
log = logging.getLogger('allgo')
......@@ -223,4 +227,15 @@ def query_webapps_for_user(user):
return Webapp.objects.filter(Q(private=False) | Q(user_id=user.id))
def get_sorted_webapp_versions(webapp, allowed_states):
"""
:param webapp: the webapp object <-- I think the webapp id is enough
:param allowed_states: the WebappVersion.states allowed (SANDBOX_STATE_CHOICES).
:type allowed_states: an iterable
:return: the sorted list of webapp version objects, sorted on their number attribute.
"""
queryset_versions = WebappVersion.objects \
.filter(webapp=webapp, state__in=allowed_states)
versions = natsort.versorted(queryset_versions, key=lambda v: v.number)
return versions
......@@ -69,7 +69,16 @@ from .forms import (
)
# Local imports
import config
from .helpers import get_base_url, get_ssh_data, upload_data, notify_controller, lookup_job_file, get_request_user, query_webapps_for_user
from .helpers import (
get_base_url,
get_ssh_data,
upload_data,
notify_controller,
lookup_job_file,
get_request_user,
query_webapps_for_user,
get_sorted_webapp_versions,
)
from .mixins import UserAccessMixin, ProviderAccessMixin, AllAccessMixin, JobAuthMixin
from .models import (
AllgoUser,
......@@ -624,11 +633,8 @@ class WebappSandboxPanel(UserAccessMixin, TemplateView):
context['docker_os_list'] = DockerOs.objects.all()
# candidate versions (start from an existing version)
versions = {}
for state in (WebappVersion.READY, WebappVersion.COMMITTED):
versions.update((v.number, v) for v in WebappVersion.objects.filter(
webapp=context["webapp"], state=state))
context['versions'] = natsort.versorted(versions.values(), key=lambda v: v.number)
states = (WebappVersion.READY, WebappVersion.COMMITTED)
context['versions'] = get_sorted_webapp_versions(context["webapp"], states)
context['versions'].reverse()
return context
......@@ -1183,11 +1189,10 @@ class JobCreate(AllAccessMixin, SuccessMessageMixin, CreateView):
readme_file = None
# select the list of versions to be displayed
versions = natsort.versorted(set(v for v, in WebappVersion.objects
.filter(webapp=webapp, state__in=( WebappVersion.SANDBOX,
WebappVersion.COMMITTED,
WebappVersion.READY))
.values_list("number")))
states = (WebappVersion.SANDBOX,
WebappVersion.COMMITTED,
WebappVersion.READY)
versions = get_sorted_webapp_versions(webapp, states)
# also list 'sandbox' if the sandbox is running and if the current user
# is allowed to use the sandbox
......