def update(self, request: request.Request, *args: Any, **kwargs: Any) -> Response: action = Action.objects.get(pk=kwargs['pk'], team=request.user.team_set.get()) # If there's no steps property at all we just ignore it # If there is a step property but it's an empty array [], we'll delete all the steps if 'steps' in request.data: steps = request.data.pop('steps') # remove steps not in the request step_ids = [step['id'] for step in steps if step.get('id')] action.steps.exclude(pk__in=step_ids).delete() for step in steps: if step.get('id'): db_step = ActionStep.objects.get(pk=step['id']) step_serializer = ActionStepSerializer(db_step) step_serializer.update(db_step, step) else: ActionStep.objects.create( action=action, **{key: value for key, value in step.items() if key not in ('isNew', 'selection')} ) serializer = ActionSerializer(action, context={'request': request}) serializer.update(action, request.data) action.is_calculating = True calculate_action.delay(action_id=action.pk) return Response(ActionSerializer(action, context={'request': request}).data)
def create(self, request: request.Request, *args: Any, **kwargs: Any) -> Response: action, created = Action.objects.get_or_create( name=request.data["name"], team=request.user.team_set.get(), deleted=False, defaults={ "post_to_slack": request.data.get("post_to_slack", False), "created_by": request.user, }, ) if not created: return Response( data={"detail": "action-exists", "id": action.pk}, status=400 ) if request.data.get("steps"): for step in request.data["steps"]: ActionStep.objects.create( action=action, **{ key: value for key, value in step.items() if key not in ("isNew", "selection") } ) calculate_action.delay(action_id=action.pk) return Response(ActionSerializer(action, context={"request": request}).data)
def create(self, request: request.Request, *args: Any, **kwargs: Any) -> Response: action, created = Action.objects.get_or_create( name=request.data['name'], team=request.user.team_set.get(), deleted=False, defaults={ 'post_to_slack': request.data.get('post_to_slack', False), 'created_by': request.user }) if not created: return Response(data={ 'detail': 'action-exists', 'id': action.pk }, status=400) if request.data.get('steps'): for step in request.data['steps']: ActionStep.objects.create(action=action, **{ key: value for key, value in step.items() if key not in ('isNew', 'selection') }) calculate_action.delay(action_id=action.pk) return Response( ActionSerializer(action, context={ 'request': request }).data)
def create(self, validated_data: Any) -> Any: steps = validated_data.pop("steps", []) validated_data["created_by"] = self.context["request"].user instance = super().create(validated_data) for step in steps: ActionStep.objects.create( action=instance, **{ key: value for key, value in step.items() if key not in ("isNew", "selection") }, ) calculate_action.delay(action_id=instance.pk) posthoganalytics.capture(validated_data["created_by"].distinct_id, "action created", instance.get_analytics_metadata()) return instance
def update(self, instance: Any, validated_data: Dict[str, Any]) -> Any: steps = validated_data.pop("steps", None) # If there's no steps property at all we just ignore it # If there is a step property but it's an empty array [], we'll delete all the steps if steps is not None: # remove steps not in the request step_ids = [step["id"] for step in steps if step.get("id")] instance.steps.exclude(pk__in=step_ids).delete() for step in steps: if step.get("id"): step_instance = ActionStep.objects.get(pk=step["id"]) step_serializer = ActionStepSerializer( instance=step_instance) step_serializer.update(step_instance, step) else: ActionStep.objects.create( action=instance, **{ key: value for key, value in step.items() if key not in ("isNew", "selection") }, ) instance = super().update(instance, validated_data) calculate_action.delay(action_id=instance.pk) instance.refresh_from_db() posthoganalytics.capture( self.context["request"].user.distinct_id, "action updated", { **instance.get_analytics_metadata(), "updated_by_creator": self.context["request"].user == instance.created_by, }, ) return instance
def update(self, request: request.Request, *args: Any, **kwargs: Any) -> Response: action = Action.objects.get(pk=kwargs["pk"], team=request.user.team) # If there's no steps property at all we just ignore it # If there is a step property but it's an empty array [], we'll delete all the steps if "steps" in request.data: steps = request.data.pop("steps") # remove steps not in the request step_ids = [step["id"] for step in steps if step.get("id")] action.steps.exclude(pk__in=step_ids).delete() for step in steps: if step.get("id"): db_step = ActionStep.objects.get(pk=step["id"]) step_serializer = ActionStepSerializer(db_step) step_serializer.update(db_step, step) else: ActionStep.objects.create( action=action, **{ key: value for key, value in step.items() if key not in ("isNew", "selection") }) serializer = ActionSerializer(action, context={"request": request}) if "created_by" in request.data: del request.data["created_by"] serializer.update(action, request.data) action.is_calculating = True calculate_action.delay(action_id=action.pk) return Response( ActionSerializer(action, context={ "request": request }).data)
def _calculate_action(self, action: Action) -> None: calculate_action.delay(action_id=action.pk)