Beispiel #1
0
def create_team_channel(self, team_ids):
    intro_message = textwrap.dedent("""
            Welcome! This is your private team channel. Feel free to chat, study, discuss strategy, or whatever you like!
            You need to pick a team captain and a team name by {season_start}.
            Once you've chosen (or if you need help with anything), contact one of the moderators:
            {mods}

            Here are some useful links for your team:
            - <{pairings_url}|View your team pairings>
            - <{calendar_url}|Import your team pairings to your calendar>""")

    for team in Team.objects.filter(id__in=team_ids).select_related('season__league').nocache():
        pairings_url = abs_url(reverse('by_league:by_season:pairings_by_team',
                                       args=[team.season.league.tag, team.season.tag, team.number]))
        calendar_url = abs_url(reverse('by_league:by_season:pairings_by_team_icalendar',
                                       args=[team.season.league.tag, team.season.tag,
                                             team.number])).replace('https:', 'webcal:')
        mods = team.season.league.leaguemoderator_set.filter(is_active=True)
        mods_str = ' '.join(('<@%s>' % lm.player.lichess_username.lower() for lm in mods))
        season_start = '?' if team.season.start_date is None else team.season.start_date.strftime(
            '%b %-d')
        intro_message_formatted = intro_message.format(mods=mods_str, season_start=season_start,
                                                       pairings_url=pairings_url,
                                                       calendar_url=calendar_url)
        team_members = team.teammember_set.select_related('player').nocache()
        user_ids = [tm.player.slack_user_id for tm in team_members]
        channel_name = 'team-%d-s%s' % (team.number, team.season.tag)
        topic = "Team Pairings: %s | Team Calendar: %s" % (pairings_url, calendar_url)

        try:
            group = slackapi.create_group(channel_name)
            time.sleep(1)
        except slackapi.NameTaken:
            logger.error('Could not create slack team, name taken: %s' % channel_name)
            continue
        channel_ref = '#%s' % group.name
        for user_id in user_ids:
            if user_id:
                try:
                    slackapi.invite_to_group(group.id, user_id)
                except slackapi.SlackError:
                    logger.exception('Could not invite %s to slack' % user_id)
                time.sleep(1)
        slackapi.invite_to_group(group.id, settings.CHESSTER_USER_ID)
        time.sleep(1)
        with reversion.create_revision():
            reversion.set_comment('Creating slack channel')
            team.slack_channel = group.id
            team.save()

        slackapi.set_group_topic(group.id, topic)
        time.sleep(1)
        slackapi.leave_group(group.id)
        time.sleep(1)
        slackapi.send_message(channel_ref, intro_message_formatted)
        time.sleep(1)
Beispiel #2
0
def create_team_channel(self, team_ids):
    username_to_id = {u.name: u.id for u in slackapi.get_user_list()}
    intro_message = 'Welcome! This is your private team channel. Feel free to chat, study, discuss strategy, or whatever you like!\n' \
                      + 'You need to pick a team captain and a team name by {season_start}.\n' \
                      + 'Once you\'ve chosen (or if you need help with anything), contact one of the moderators:\n' \
                      + '{mods}'

    for team in Team.objects.filter(id__in=team_ids).select_related('season__league').nocache():
        pairings_url = abs_url(reverse('by_league:by_season:pairings_by_team', args=[team.season.league.tag, team.season.tag, team.number]))
        mods = team.season.league.leaguemoderator_set.filter(is_active=True)
        mods_str = ' '.join(('<@%s>' % lm.player.lichess_username.lower() for lm in mods))
        season_start = '?' if team.season.start_date is None else team.season.start_date.strftime('%b %-d')
        intro_message_formatted = intro_message.format(mods=mods_str, season_start=season_start)
        team_members = team.teammember_set.select_related('player').nocache()
        chesster_id = username_to_id['chesster']
        user_ids = [username_to_id.get(tm.player.lichess_username.lower()) for tm in team_members]
        channel_name = 'team-%d-s%s' % (team.number, team.season.tag)

        try:
            group = slackapi.create_group(channel_name)
            time.sleep(1)
        except slackapi.NameTaken:
            logger.error('Could not create slack team, name taken: %s' % channel_name)
            continue
        channel_ref = '#%s' % group.name
        for user_id in user_ids:
            if user_id:
                try:
                    slackapi.invite_to_group(group.id, user_id)
                except slackapi.SlackError:
                    logger.exception('Could not invite %s to slack' % user_id)
                time.sleep(1)
        slackapi.invite_to_group(group.id, chesster_id)
        time.sleep(1)
        with reversion.create_revision():
            reversion.set_comment('Creating slack channel')
            team.slack_channel = group.id
            team.save()

        slackapi.set_group_topic(group.id, pairings_url)
        time.sleep(1)
        slackapi.leave_group(group.id)
        time.sleep(1)
        slackapi.send_message(channel_ref, intro_message_formatted)
        time.sleep(1)