def access(self, request, pk=None): new_hire = self.get_object() if request.method == 'PUT': ScheduledAccess.objects.create( new_hire=new_hire, integration=request.data['integration'], email=request.data['email']) return Response({'status': 'pending'}) s = SlackBot() if request.data['integration'] == 1 else Google() if s.find_by_email(new_hire.email): return Response({'status': 'exists'}) items = ScheduledAccess.objects.filter( new_hire=new_hire, integration=request.data['integration']).exclude(status=1) if items.exists(): return Response({'status': 'pending'}) return Response({'status': 'not_found'})
def give_slack_access(self, request, pk): user = self.get_object() s = SlackBot() response = s.find_by_email(user.email) if response: user.slack_user_id = response['user']['id'] user.save() translation.activate(user.language) blocks = [{ "type": "section", "text": { "type": "mrkdwn", "text": _("Click on the button to see all the categories that are available to you!" ) } }, { "type": "actions", "elements": [{ "type": "button", "text": { "type": "plain_text", "text": _("resources") }, "style": "primary", "value": "show:resources" }] }] s.set_user(user) res = s.send_message(blocks=blocks) user.slack_channel_id = res['channel'] user.save() return Response() return Response( { "error": _('We couldn\'t find anyone in Slack with the same email address.' ) }, status=status.HTTP_400_BAD_REQUEST)
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
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