Esempio n. 1
0
        def form_valid(self, form):
            self.form = form
            user = self.request.user
            org = user.get_org()

            step_uuid = self.form.cleaned_data.get("step_node", None)
            text = self.form.cleaned_data["text"]
            has_schedule = False

            if step_uuid:
                from .tasks import send_to_flow_node

                get_params = {k: v for k, v in self.request.GET.items()}
                get_params.update({"s": step_uuid})
                send_to_flow_node.delay(org.pk, user.pk, text, **get_params)
            else:

                omnibox = omnibox_deserialize(org, self.form.cleaned_data["omnibox"])
                has_schedule = self.form.cleaned_data["schedule"]

                groups = list(omnibox["groups"])
                contacts = list(omnibox["contacts"])
                urns = list(omnibox["urns"])

                schedule = Schedule.create_blank_schedule(org, user) if has_schedule else None
                broadcast = Broadcast.create(
                    org,
                    user,
                    text,
                    groups=groups,
                    contacts=contacts,
                    urns=urns,
                    schedule=schedule,
                    status=QUEUED,
                    template_state=Broadcast.TEMPLATE_STATE_UNEVALUATED,
                )

                if not has_schedule:
                    self.post_save(broadcast)
                    super().form_valid(form)

                analytics.track(
                    self.request.user.username,
                    "temba.broadcast_created",
                    dict(contacts=len(contacts), groups=len(groups), urns=len(urns)),
                )

            if "HTTP_X_PJAX" in self.request.META:
                success_url = "hide"
                if has_schedule:
                    success_url = reverse("msgs.broadcast_schedule_read", args=[broadcast.pk])

                response = self.render_to_response(self.get_context_data())
                response["Temba-Success"] = success_url
                return response

            return HttpResponseRedirect(self.get_success_url())
Esempio n. 2
0
 def clean_omnibox(self):
     return omnibox_deserialize(self.user.get_org(), self.cleaned_data["omnibox"])