def post(self, request): from automation.models import Policy from automation.tasks import generate_agent_autotasks_task from autotasks.tasks import create_win_task_schedule data = request.data script = get_object_or_404(Script, pk=data["autotask"]["script"]) # Determine if adding check to Policy or Agent if "policy" in data: policy = get_object_or_404(Policy, id=data["policy"]) # Object used for filter and save parent = {"policy": policy} else: agent = get_object_or_404(Agent, pk=data["agent"]) parent = {"agent": agent} check = None if data["autotask"]["assigned_check"]: check = get_object_or_404(Check, pk=data["autotask"]["assigned_check"]) bit_weekdays = None if data["autotask"]["run_time_days"]: bit_weekdays = get_bit_days(data["autotask"]["run_time_days"]) del data["autotask"]["run_time_days"] serializer = TaskSerializer(data=data["autotask"], partial=True, context=parent) serializer.is_valid(raise_exception=True) task = serializer.save( **parent, script=script, win_task_name=AutomatedTask.generate_task_name(), assigned_check=check, run_time_bit_weekdays=bit_weekdays, ) if task.agent: create_win_task_schedule.delay(pk=task.pk) elif task.policy: generate_agent_autotasks_task.delay(policy=task.policy.pk) return Response("Task will be created shortly!")
def handle_pending_actions(self): pending_actions = self.pendingactions.filter(status="pending") for action in pending_actions: if action.action_type == "taskaction": from autotasks.tasks import ( create_win_task_schedule, enable_or_disable_win_task, delete_win_task_schedule, ) task_id = action.details["task_id"] if action.details["action"] == "taskcreate": create_win_task_schedule.delay(task_id, pending_action=action.id) elif action.details["action"] == "tasktoggle": enable_or_disable_win_task.delay( task_id, action.details["value"], pending_action=action.id ) elif action.details["action"] == "taskdelete": delete_win_task_schedule.delay(task_id, pending_action=action.id)