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

    for user in get_user_model().objects.filter(slack_user_id__isnull=False,
                                                role=0):
        local_datetime = user.get_local_time()
        if local_datetime.hour == 8 and local_datetime.weekday(
        ) < 5 and local_datetime.date() >= user.start_date:
            s.set_user(user)
            # overdue items
            tasks = ToDoUser.objects.filter(
                user=user,
                completed=False,
                to_do__due_on_day__lt=user.workday()).exclude(
                    to_do__due_on_day=0)
            if tasks.exists():
                blocks = s.format_to_do_block(pre_message=_(
                    "Some to do items are overdue. Please complete those as "
                    "soon as possible!"),
                                              items=tasks)
                s.send_message(blocks=blocks)

            # to do items for today
            tasks = ToDoUser.objects.filter(user=user,
                                            completed=False,
                                            to_do__due_on_day=user.workday())
            if tasks.exists():
                blocks = s.format_to_do_block(pre_message=_(
                    "Good morning! These are the tasks you need to complete "
                    "today:"),
                                              items=tasks)
                s.send_message(blocks=blocks)
    return
Ejemplo n.º 2
0
 def post(self, request, id):
     # reminding someone
     t_u = ToDoUser.objects.get(id=id)
     t_u.reminded = datetime.now()
     t_u.save()
     if t_u.user.slack_user_id:
         s = SlackBot()
         s.set_user(t_u.user)
         blocks = s.format_to_do_block(pre_message=_('Don\'t forget this to do item!'), items=[t_u])
         s.send_message(blocks=blocks)
     else:
         send_reminder_email(t_u)
     return Response()
Ejemplo n.º 3
0
 def put(self, request, id):
     # reopen task
     t_u = ToDoUser.objects.get(id=id)
     t_u.completed = False
     t_u.form = []
     t_u.save()
     if t_u.user.slack_user_id:
         s = SlackBot()
         s.set_user(t_u.user)
         blocks = s.format_to_do_block(pre_message=_('This task has just been reopened! ' + request.data['message']),
                                       items=[t_u])
         s.send_message(blocks=blocks)
     else:
         email_reopen_task(t_u, request.data['message'], t_u.user)
     return Response()
Ejemplo n.º 4
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.º 5
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