Ejemplo n.º 1
0
    def handle(self, *args, **options):
        unfired = EventFire.objects.filter(
            fired=None).select_related("event").order_by("scheduled",
                                                         "id")[:50]

        self.stdout.write(
            "Fire       | Event                            | Contact    | Scheduled"
        )
        self.stdout.write(
            "-----------|----------------------------------|------------|--------------"
        )

        now = timezone.now()

        for fire in unfired:
            event = truncate(f"{fire.event.id}: {fire.event.name}", 32)
            contact = fire.contact_id

            if fire.scheduled > now:
                scheduled = timeuntil(fire.scheduled, now=now)
            else:
                scheduled = f"{timesince(fire.scheduled, now=now)} ago"

            self.stdout.write(
                f"{fire.id:10} | {event:<32} | {contact:10} | {scheduled}")
Ejemplo n.º 2
0
    def form_valid(self, form):
        from .type import RocketChatType

        base_url = form.cleaned_data["base_url"]
        bot_username = form.cleaned_data["bot_username"]
        admin_auth_token = form.cleaned_data["admin_auth_token"]
        admin_user_id = form.cleaned_data["admin_user_id"]
        secret = form.cleaned_data["secret"]
        config = {
            RocketChatType.CONFIG_BASE_URL: base_url,
            RocketChatType.CONFIG_BOT_USERNAME: bot_username,
            RocketChatType.CONFIG_ADMIN_AUTH_TOKEN: admin_auth_token,
            RocketChatType.CONFIG_ADMIN_USER_ID: admin_user_id,
            RocketChatType.CONFIG_SECRET: secret,
        }

        rc_host = urlparse(base_url).netloc

        self.object = Channel(
            uuid=uuid4(),
            org=self.org,
            channel_type=RocketChatType.code,
            config=config,
            name=truncate(f"{RocketChatType.name}: {rc_host}",
                          Channel._meta.get_field("name").max_length),
            created_by=self.request.user,
            modified_by=self.request.user,
        )

        client = Client(config[RocketChatType.CONFIG_BASE_URL],
                        config[RocketChatType.CONFIG_SECRET])
        webhook_url = "https://" + self.object.callback_domain + reverse(
            "courier.rc", args=[self.object.uuid])

        try:
            client.settings(webhook_url, bot_username)
        except ClientError as err:
            messages.error(
                self.request,
                err.msg if err.msg else _("Configuration has failed"))
            return super().get(self.request, *self.args, **self.kwargs)
        else:
            self.request.session.pop(self.SESSION_KEY, None)

        self.object.save()
        return super().form_valid(form)
Ejemplo n.º 3
0
    def form_valid(self, form):
        from .type import RocketChatType

        base_url = form.cleaned_data["base_url"]
        config = {
            RocketChatType.CONFIG_BASE_URL:
            base_url,
            RocketChatType.CONFIG_SECRET:
            form.cleaned_data["secret"],
            RocketChatType.CONFIG_ADMIN_AUTH_TOKEN:
            form.cleaned_data["admin_auth_token"],
            RocketChatType.CONFIG_ADMIN_USER_ID:
            form.cleaned_data["admin_user_id"],
        }

        rc_host = urlparse(base_url).netloc

        self.object = Ticketer(
            uuid=uuid4(),
            org=self.org,
            ticketer_type=RocketChatType.slug,
            config=config,
            name=truncate(f"{RocketChatType.name}: {rc_host}",
                          Ticketer._meta.get_field("name").max_length),
            created_by=self.request.user,
            modified_by=self.request.user,
        )

        client = Client(config[RocketChatType.CONFIG_BASE_URL],
                        config[RocketChatType.CONFIG_SECRET])
        webhook_url = WEBHOOK_URL_TEMPLATE.format(
            domain=self.object.org.get_brand_domain(), uuid=self.object.uuid)

        try:
            client.settings(webhook_url)
            self.request.session.pop(self.SESSION_KEY, None)
        except ClientError as err:
            messages.error(
                self.request,
                err.msg if err.msg else _("Configuration has failed"))
            return super().get(self.request, *self.args, **self.kwargs)

        self.object.save()
        return super().form_valid(form)
Ejemplo n.º 4
0
 def format_query(q):
     return "Query [%s] %.3f secs" % (truncate(q['sql'],
                                               75), float(q['time']))
Ejemplo n.º 5
0
 def __str__(self):
     return truncate(self.statement, 79).replace('\n', ' ')