def delete_workflow(request): if "id" not in request.POST: return HttpResponseBadRequest("Need id param") workflow = get_object_or_404(Workflow, id=request.POST["id"]) if workflow.user.user != request.user and not request.user.is_superuser: return json_error_response("That's not yours", status=403) if not workflow.is_finished: return json_error_response("Can't delete workflow before it finished running") workflow.delete_cascade() return json_response("deleted")
def abort_workflow(request): if "id" not in request.POST: return HttpResponseBadRequest("Need id param") workflow = get_object_or_404(Workflow, id=request.POST["id"]) if workflow.user.user != request.user and not request.user.is_superuser: return json_error_response("That's not yours", status=403) if workflow.is_finished: return json_error_response("Can't abort workflow after it finished running") yabiuser = User.objects.get(name=request.user.username) request_workflow_abort(workflow.pk, yabiuser) return json_response("abort requested")
def submit_workflow(request): try: yabiuser = User.objects.get(name=request.user.username) workflow_dict = _preprocess_workflow_json(yabiuser, request.POST["workflowjson"]) workflow = EngineWorkflow.objects.create( name=workflow_dict["name"], user=yabiuser, shared=workflow_dict.get("shared", False), original_json=json.dumps(workflow_dict), start_time=datetime.now() ) # always commit transactions before sending tasks depending on state from the current transaction # http://docs.celeryq.org/en/latest/userguide/tasks.html transaction.commit() # process the workflow via celery process_workflow(workflow.pk).apply_async() except Exception: transaction.rollback() logger.exception("Exception in submit_workflow()") return json_error_response("Workflow submission error") return json_response({"workflow_id": workflow.pk})
def share_workflow(request): if "id" not in request.POST: return HttpResponseBadRequest("Need id param") workflow = get_object_or_404(Workflow, id=request.POST["id"]) if workflow.user != request.user.user and not request.user.is_superuser: return json_error_response("That's not yours", status=403) workflow.share() return json_response("shared")
def delete_saved_workflow(request): if "id" not in request.POST: return HttpResponseBadRequest("Need id param") workflow = get_object_or_404(SavedWorkflow, id=request.POST["id"]) if workflow.creator.user != request.user and not request.user.is_superuser: return json_error_response("That's not yours", status=403) workflow.delete() return json_response("deleted")
def save_workflow(request): try: workflow_dict = json.loads(request.POST["workflowjson"]) except KeyError: return json_error_response("workflowjson param not posted", status=400) except ValueError: return json_error_response("Invalid workflow JSON") yabiuser = User.objects.get(name=request.user.username) # Check if the user already has a workflow with the same name, and if so, # munge the name appropriately. workflow_dict["name"] = munge_name(yabiuser.savedworkflow_set, workflow_dict["name"]) workflow_json = json.dumps(workflow_dict) workflow = SavedWorkflow.objects.create( name=workflow_dict["name"], creator=yabiuser, created_on=datetime.now(), json=workflow_json ) return json_response({"saved_workflow_id": workflow.pk})
def workflow_change_tags(request, id=None): id = int(id) yabiusername = request.user.username logger.debug(yabiusername) workflow = get_object_or_404(EngineWorkflow, pk=id) if workflow.user.user != request.user and not request.user.is_superuser: return json_error_response("That's not yours", status=403) if 'taglist' not in request.POST: return HttpResponseBadRequest("taglist needs to be passed in\n") taglist = request.POST['taglist'].split(',') taglist = [t.strip() for t in taglist if t.strip()] workflow.change_tags(taglist) return HttpResponse("Success")
def get_workflow(request, workflow_id): yabiusername = request.user.username logger.debug(yabiusername) if not (workflow_id and yabiusername): return JsonMessageResponseNotFound('No workflow_id or no username supplied') workflow_id = int(workflow_id) workflows = EngineWorkflow.objects.filter(id=workflow_id) if len(workflows) != 1: msg = 'Workflow %d not found' % workflow_id logger.critical(msg) return JsonMessageResponseNotFound(msg) workflow = workflows[0] if not (workflow.user.user == request.user or workflow.shared): return json_error_response("The workflow %s isn't yours and the owner '%s' didn't share it with you." % (workflow.id, workflow.user.user), status=403) response = workflow_to_response(workflow) return HttpResponse(json.dumps(response), content_type='application/json')