def test_authorize_run(self): """Test to see if function that authorizes runs works correctly.""" user1 = self.owner recipe = self.recipe # Current user can run the recipe self.assertTrue(auth.authorize_run(user1, recipe), "Authorized users can not run recipes.") user2 = models.User.objects.create_user(username=f"tested{get_uuid(10)}", email="*****@*****.**") self.assertFalse(auth.authorize_run(user2, recipe), "Unauthorized users can run recipes.") return
def recipe_run(request, uid): """ View used to execute recipes and start a 'Queued' job. """ analysis = Analysis.objects.filter(uid=uid).first() project = analysis.project # Form submission. if request.method == "POST": form = forms.RecipeInterface(request=request, analysis=analysis, json_data=analysis.json_data, data=request.POST, files=request.FILES) # The form validation will authorize the job. if form.is_valid(): # Create the job from the recipe and incoming json data. job = auth.create_job(analysis=analysis, user=request.user, fill_with=form.cleaned_data) # Spool the job right away if UWSGI exists. if tasks.HAS_UWSGI: # Spool via UWSGI. tasks.execute_job.spool(job_id=job.id) return redirect(reverse("job_list", kwargs=dict(uid=project.uid))) else: initial = dict(name=f"Results for: {analysis.name}") form = forms.RecipeInterface(request=request, analysis=analysis, json_data=analysis.json_data, initial=initial) is_runnable = auth.authorize_run(user=request.user, recipe=analysis) context = dict(project=project, analysis=analysis, form=form, is_runnable=is_runnable, activate='Run Recipe') context.update(get_counts(project)) return render(request, 'recipe_run.html', context)
def get_part(request, name, id): """ Return a template by name and with uid rendering """ user = request.user # The recipe that needs to be edited. recipe = Analysis.objects.filter(id=id).annotate( job_count=Count("job", filter=Q(job__deleted=False))).first() project = recipe.project if not auth.is_readable(project=project, user=user): message = str("Recipe is not writable by current user") return HttpResponse(message) # Fills in project level counts (results, data and recipe counts). counts = get_counts(recipe.project) if name == "run": initial = dict(name=f"Results for: {recipe.name}") form = forms.RecipeInterface(request=request, analysis=recipe, json_data=recipe.json_data, initial=initial) else: # Initial form loading via a GET request. form = forms.RecipeForm(instance=recipe, user=request.user, project=project) remap = dict( info="parts/recipe_info.html", code="parts/recipe_code.html", interface="parts/recipe_interface.html", run="parts/recipe_run.html", results="parts/recipe_results.html", ) name = remap.get(name, "parts/placeholder.html") # Check to see if this recipe is runnable by the user. is_runnable = auth.authorize_run(user=user, recipe=recipe) # Get the list of jobs required for recipe results jobs = recipe.job_set.filter( deleted=False).order_by("-lastedit_date").all() context = dict(recipe=recipe, form=form, is_runnable=is_runnable, job_list=jobs, rerun_btn=False) context.update(counts) html = render(request, name, context=context) return html
def recipe_view(request, uid): """ Edit meta-data associated with a recipe. """ # The recipe that needs to be edited. recipe = Analysis.objects.filter(uid=uid).first() # The project that recipe belongs to. project = recipe.project user = request.user if request.method == "POST": # Form has been submitted form = forms.RecipeForm(data=request.POST, instance=recipe, files=request.FILES, user=user, project=project) if form.is_valid(): form.save() messages.success(request, "Editted Recipe") return redirect(reverse("recipe_view", kwargs=dict(uid=recipe.uid))) else: # Initial form loading via a GET request. form = forms.RecipeForm(instance=recipe, user=request.user, project=project) action_url = reverse('recipe_edit', kwargs=dict(uid=uid)) initial = dict(name=f"Results for: {recipe.name}") run_form = forms.RecipeInterface(request=request, analysis=recipe, json_data=recipe.json_data, initial=initial) is_runnable = auth.authorize_run(user=request.user, recipe=recipe) recipe = Analysis.objects.filter(uid=uid).annotate( job_count=Count("job", filter=Q(job__deleted=False))).first() context = dict(recipe=recipe, project=project, form=form, is_runnable=is_runnable, name=recipe.name, activate='Recipe View', run_form=run_form, action_url=action_url) counts = get_counts(project) context.update(counts) return render(request, 'recipe_view.html', context)
def recipe_view(request, uid): """ Edit meta-data associated with a recipe. """ # The user making the request. user = request.user # The recipe that needs to be edited. recipe = Analysis.objects.filter(uid=uid).annotate( job_count=Count("job", filter=Q(job__deleted=False))).first() # The project that recipe belongs to. project = recipe.project # Initial form loading via a GET request. form = forms.RecipeForm(instance=recipe, user=request.user, project=project) # Fills in project level counts (results, data and recipe counts). counts = get_counts(project) # Disable buttons if project not writeable. btn_state = '' if auth.is_writable(user=user, project=project) else 'disabled' # Get the list of jobs required to view recipe results jobs = recipe.job_set.filter( deleted=False).order_by("-lastedit_date").all() # Check to see if this recipe is runnable by the user. is_runnable = auth.authorize_run(user=user, recipe=recipe) # Check to see if recipe is editable editable = auth.writeable_recipe(user=user, source=recipe) # Generate the context. context = dict(recipe=recipe, job_list=jobs, project=project, form=form, btn_state=btn_state, is_runnable=is_runnable, activate='Recipe View', rerun_btn=False, include_copy=False, editable=editable) # Update context with counts. context.update(counts) return render(request, 'recipe_view.html', context)