Ejemplo n.º 1
0
def link_slack_users():
    if not AccessToken.objects.filter(integration=0).exists():
        return
    s = Slack()

    for user in get_user_model().objects.filter(slack_user_id__isnull=True,
                                                role=0):
        response = s.find_by_email(email=user.email.lower())
        if response:
            translation.activate(user.language)
            user.slack_user_id = response['user']['id']
            user.save()
            s.set_user(user)
            blocks = [{
                "type": "section",
                "text": {
                    "type":
                    "mrkdwn",
                    "text":
                    WelcomeMessage.objects.get(language=user.language,
                                               message_type=3).message
                },
            }]
            # check if extra buttons need to be send with it as well
            if s.org.slack_buttons:
                blocks.extend([{
                    "type": "section",
                    "text": {
                        "type": "mrkdwn",
                        "text": _("Click a button to see more information :)")
                    }
                }, {
                    "type":
                    "actions",
                    "elements": [{
                        "type": "button",
                        "text": {
                            "type": "plain_text",
                            "text": "To do items"
                        },
                        "value": "to_do"
                    }, {
                        "type": "button",
                        "text": {
                            "type": "plain_text",
                            "text": "Resources"
                        },
                        "value": "resources"
                    }]
                }])

            # adding introduction items
            introductions = user.introductions.all()
            if introductions.exists():
                for i in introductions:
                    text = '*' + i.name + ':* ' + i.intro_person.full_name(
                    ) + '\n'
                    if i.intro_person.position is not None and i.intro_person.position != '':
                        text += i.intro_person.position + '\n'
                    if i.intro_person.message is not None and i.intro_person.message != "":
                        text += '_' + s.personalize(
                            i.intro_person.message) + '_\n'
                    if i.intro_person.email is not None and i.intro_person.email != "":
                        text += i.intro_person.email + ' '
                    if i.intro_person.phone is not None and i.intro_person.phone != "":
                        text += i.intro_person.phone
                    block = {
                        "type": "section",
                        "text": {
                            "type": "mrkdwn",
                            "text": text
                        }
                    }
                    if i.intro_person.profile_image is not None:
                        block["accessory"] = {
                            "type": "image",
                            "image_url":
                            i.intro_person.profile_image.get_url(),
                            "alt_text": "profile image"
                        }

                    blocks.append(block)
            res = s.send_message(blocks=blocks, channel=response['user']['id'])
            user.slack_channel_id = res['channel']
            user.save()
            # send user to do items for that day (and perhaps over due ones)
            tasks = ToDoUser.objects.filter(
                user=user,
                completed=False,
                to_do__due_on_day__lte=user.workday()).exclude(
                    to_do__due_on_day=0)

            if tasks.exists():
                blocks = s.format_to_do_block(
                    pre_message=_("These are the tasks you need to complete:"),
                    items=tasks)
                s.send_message(blocks=blocks)

    return True
Ejemplo n.º 2
0
def link_slack_users():
    if not AccessToken.objects.filter(integration=0).exists():
        return
    s = Slack()

    for user in get_user_model().objects.filter(slack_user_id__isnull=True,
                                                role=0):
        response = s.find_by_email(email=user.email.lower())
        if response:
            translation.activate(user.language)
            user.slack_user_id = response['user']['id']
            user.save()
            s.set_user(user)
            blocks = [{
                "type": "section",
                "text": {
                    "type":
                    "mrkdwn",
                    "text":
                    s.personalize(
                        WelcomeMessage.objects.get(language=user.language,
                                                   message_type=3).message)
                },
            }]
            # check if extra buttons need to be send with it as well
            if s.org.slack_buttons:
                blocks.extend([{
                    "type": "section",
                    "text": {
                        "type": "mrkdwn",
                        "text": _("Click a button to see more information :)")
                    }
                }, {
                    "type":
                    "actions",
                    "elements": [{
                        "type": "button",
                        "text": {
                            "type": "plain_text",
                            "text": "To do items"
                        },
                        "value": "to_do"
                    }, {
                        "type": "button",
                        "text": {
                            "type": "plain_text",
                            "text": "Resources"
                        },
                        "value": "resources"
                    }]
                }])

            # adding introduction items
            introductions = user.introductions.all()
            for i in introductions:
                blocks.append(s.format_intro_block(i))

            res = s.send_message(blocks=blocks, channel=response['user']['id'])
            user.slack_channel_id = res['channel']
            user.save()
            # send user to do items for that day (and perhaps over due ones)
            tasks = ToDoUser.objects.filter(
                user=user,
                completed=False,
                to_do__due_on_day__lte=user.workday()).exclude(
                    to_do__due_on_day=0)

            if tasks.exists():
                blocks = s.format_to_do_block(
                    pre_message=_("These are the tasks you need to complete:"),
                    items=tasks)
                s.send_message(blocks=blocks)

    return True