from__future__importannotationsfromdjangoimporthttpfromdjango.shortcutsimportget_object_or_404,redirect,renderfrom..auth.decoratorsimportteacher_or_superuser_requiredfrom.formsimportVenvFormfrom.modelsimportVenvfrom.tasksimportcreate_venv,install_packages# Create your views here.
[docs]@teacher_or_superuser_requireddefindex_view(request):"""Show all venvs visible to a user Args: request: The request """venvs=Venv.objects.filter_visible(request.user)returnrender(request,"venvs/index.html",{"venvs_app":True,"venvs":venvs,},)
[docs]@teacher_or_superuser_requireddefshow_view(request,venv_id):"""Show information about a venv Args: request: The request venv_id: The primary key of an instance of the :class:`.Venv` model """venv:Venv=get_object_or_404(Venv.objects.filter_visible(request.user),id=venv_id)returnrender(request,"venvs/show.html",{"venvs_app":True,"nav_item":venv.name,"venv":venv,},)
[docs]@teacher_or_superuser_requireddefcreate_view(request):"""Create a venv Args: request: The request """ifrequest.method=="POST":form=VenvForm(request.POST)ifform.is_valid():venv=form.save(commit=False)venv.fully_created=Falsevenv.save()create_venv.delay(venv.id)returnredirect("venvs:show",venv.id)else:form=VenvForm()returnrender(request,"venvs/edit_create.html",{"venvs_app":True,"nav_item":"Create virtual environment","form":form,},)
[docs]@teacher_or_superuser_requireddefedit_view(request,venv_id):"""Edit a venv Args: request: The request venv_id: The primary key of an instance of the :class:`.Venv` model """venv=get_object_or_404(Venv.objects.filter_editable(request.user),id=venv_id)ifrequest.method=="POST":form=VenvForm(data=request.POST,instance=venv)ifform.is_valid():venv=form.save()returnredirect("venvs:show",venv.id)else:form=VenvForm(instance=venv)returnrender(request,"courses/edit_create.html",{"venvs_app":True,"nav_item":"Edit","venv":venv,"form":form,},)
[docs]@teacher_or_superuser_requireddefinstall_packages_view(request,venv_id):"""Install packages in a venv Args: request: The request venv_id: The primary key of an instance of the :class:`.Venv` model """ifrequest.method=="POST":venv=get_object_or_404(Venv.objects.filter_editable(request.user),id=venv_id,installing_packages=False)venv.installing_packages=Truevenv.save()packages=list(filter(bool,request.POST.getlist("packages[]")))install_packages.delay(venv.id,packages)returnredirect("venvs:show",venv.id)raisehttp.Http404