def remove_member(team, user_id, session=None): sess_flag = False if session is None: session = session_creator() sess_flag = True session.add(team) session.query(Members).filter(Members.member_id == user_id, Members.team_id == team.id).delete() if sess_flag: session.commit() session.close()
def get_team_by_name(name, session=None) -> Optional[Team]: """Returns the team with the given name, or none if it doesn't exist""" sess_flag = False if session is None: session = session_creator() sess_flag = True team = session.query(Team).filter(Team.team_name == name).first() if sess_flag: session.commit() session.close() return team
def get_all_teams(session=None) -> list: """Returns a list of team objects""" sess_flag = False if session is None: session = session_creator() sess_flag = True teams = session.query(Team).all() if sess_flag: session.commit() session.close() return teams
def get_team_by_join_message_id(id, session=None) -> Optional[Team]: """Returns the team with the given join message id, or none if it doesn't exist""" sess_flag = False if session is None: session = session_creator() sess_flag = True team = session.query(Team).filter(Team.join_message_id == id).first() if sess_flag: session.commit() session.close() return team
def get_groups(): if requires_scope('sms:read'): session = session_creator() groups = [g.to_dict() for g in session.query(Group).all()] session.close() return json.dumps(groups) raise AuthError( { "code": "Unauthorized", "description": "You don't have access to this resource" }, 403)
def get_all_pods(session=None) -> list: """Returns a list of pod objects""" sess_flag = False if session is None: session = session_creator() sess_flag = True pods = session.query(Pod).all() if sess_flag: session.commit() session.close() return pods
def get_pod_by_id(id, session=None) -> Optional[Pod]: """Returns the pod with the given id, or none if it doesn't exist""" sess_flag = False if session is None: session = session_creator() sess_flag = True pod = session.query(Pod).filter(Pod.id == id).first() if sess_flag: session.commit() session.close() return pod
def add_team_to_pod(pod, team_showcase_id, session=None): sess_flag = False if session is None: session = session_creator() sess_flag = True session.add(pod) team = PodService.get_team_by_showcase_id(team_showcase_id, session) team.pod = pod if sess_flag: session.commit() session.close()
def get_teams_by_name(name, session=None) -> list: """Returns a list of teams that match the given name""" sess_flag = False if session is None: session = session_creator() sess_flag = True teams = session.query(Team).filter(Team.team_name.contains(name)).all() if sess_flag: session.commit() session.close() return teams
def set_notified(self, channel, user): session = session_creator() session.add( ReadGuide( channel_id=str(channel.id), user_id=str(user.id), ) ) session.commit() session.close() self.cache_notified(channel, user) pass
def edit_team(name, project) -> bool: """Takes a name, and a project description""" # TODO: does this need a try/catch?? session = session_creator() team = session.query(Team).filter(Team.team_name == name).first() if team is not None: team.project = project session.commit() session.close() return True else: session.commit() session.close() return False
def create_group(): if requires_scope('sms:write'): body = request.get_json() assert 'group_name' in body session = session_creator() session.add(Group(group_name=body['group_name'])) session.commit() session.close() return 'ok' raise AuthError( { "code": "Unauthorized", "description": "You don't have access to this resource" }, 403)
def create_team(showcase_id) -> bool: """Create a new team""" try: session = session_creator() session.add( Team( showcase_id=showcase_id ) ) session.commit() session.close() return True except IntegrityError: return False
async def checkin_all(self, ctx: commands.Context): """checks in on all teams""" session = session_creator() guild: discord.Guild = ctx.guild for pod in PodService.get_all_pods(session): channel: discord.DMChannel = guild.get_channel(int(pod.tc_id)) message = await channel.send( "Hello! This is your friendly Showcase bot! Please react " "to this message with one of the emojis below with " "how you are feeling about your project so far!") await message.add_reaction("😀") await message.add_reaction("😐") await message.add_reaction("☹") session.commit() session.close()
def get_smallest_pod(session=None, team_size=5): """returns the smallest pod under given team size""" sess_flag = False if session is None: session = session_creator() sess_flag = True pods = session.query(Pod).all() pods.sort(key=lambda x: len(x.teams)) if sess_flag: session.commit() session.close() if len(pods[0].teams) <= team_size: return pods[0] else: return None
def complete_challenge(member_id): """adds one completed challenge to a user""" session = session_creator() user = session.query(ChallengeUser).filter( ChallengeUser.member_id == str(member_id)).first() if not user: user = ChallengeUser(member_id=str(member_id), challenges_completed=0) session.add(user) session.commit() session.close() else: user.challenges_completed += 1 session.commit() session.close()
async def teams(self, ctx): """Prints out the current teams.""" # TODO: Check on how this is parsed, might need to write something to clean up the team data async with ctx.typing(): session = session_creator() teams = self.team_service.get_all_teams(session=session) out = "Team List:\n" for team in teams: out += str(team) + "\n" session.commit() session.close() await paginated_send(ctx, out, allowed_mentions=discord.AllowedMentions( everyone=False, users=False, roles=False))
def was_notified(self, channel, user): if channel.id in self.already_notified_cache and user.id in self.already_notified_cache[channel.id]: return True session = session_creator() query = session.query(ReadGuide).filter_by( channel_id=str(channel.id), user_id=str(user.id), ) resp = query.all() if len(resp) > 0: self.cache_notified(channel, user) return True return False
def get_team_by_showcase_id(showcase_id, session=None) -> Team: """returns a Team with the given showcase id, freshly created if it doesn't exist""" sess_flag = False if session is None: session = session_creator() sess_flag = True team = session.query(Team).filter( Team.showcase_id == showcase_id).first() if not team: PodService.create_team(showcase_id) team = session.query(Team).filter( Team.showcase_id == showcase_id).first() if sess_flag: session.commit() session.close() return team
async def on_raw_reaction_remove(self, payload: discord.RawReactionActionEvent): if (payload.event_type == "REACTION_REMOVE" and payload.emoji.name == "CODEDAY" and payload.channel_id == self.channel_gallery): session = session_creator() team = self.team_service.get_team_by_join_message_id( str(payload.message_id), session) guild = self.bot.get_guild(payload.guild_id) member = guild.get_member(payload.user_id) await guild.get_channel(int(team.tc_id) ).set_permissions(member, overwrite=None) self.team_service.remove_member(team, str(payload.user_id), session) session.commit() session.close()
def create_pod(name, tc_id, mentor) -> bool: """Create a new pod""" try: session = session_creator() session.add( Pod( name=name, tc_id=tc_id, mentor=mentor, ) ) session.commit() session.close() return True except IntegrityError: return False
def get_teams_by_member(member, session=None) -> Optional[list]: """Returns a list of all teams the member is a part of""" sess_flag = False if session is None: session = session_creator() sess_flag = True team_ids = session.query(Members).filter( Members.member_id == str(member)).all() teams = [] for t in team_ids: teams.append( session.query(Team).filter(Team.id == t.team_id).first()) if sess_flag: session.commit() session.close() return teams
def add_team(name, tc, join_message, project=None) -> bool: """Add a new team""" try: session = session_creator() session.add( Team( team_name=name, tc_id=tc, join_message_id=join_message, project=project, )) session.commit() session.close() return True except IntegrityError: return False
async def on_raw_reaction_add(self, payload: discord.RawReactionActionEvent): if (payload.event_type == "REACTION_ADD" and payload.emoji.name == "CODEDAY" and payload.channel_id == self.channel_gallery and payload.member.id != self.bot.user.id): session = session_creator() team = self.team_service.get_team_by_join_message_id( str(payload.message_id), session) await payload.member.guild.get_channel( int(team.tc_id) ).set_permissions(payload.member, overwrite=discord.PermissionOverwrite( **dict(discord.Permissions.text()))) self.team_service.add_member(team, str(payload.user_id), session) session.commit() session.close()
async def assign_pods_helper(bot: discord.ext.commands.Bot): session = session_creator() all_teams_without_pods = await GQLService.get_all_showcase_teams_without_pods( ) for team in all_teams_without_pods: smallest_pod = PodService.get_smallest_pod(session) print(smallest_pod) if smallest_pod: await Pods.assign_pod_helper(bot, team["id"], smallest_pod.name, session) else: await Pods.assign_pod_helper(bot, team["id"], "overflow", session) session.commit() session.close()
async def checkin(self, ctx: commands.Context, pod_name): """Checks in on a specific pod""" session = session_creator() guild: discord.Guild = ctx.guild pod = PodService.get_pod_by_name(pod_name, session) if pod is not None: channel: discord.DMChannel = guild.get_channel(int(pod.tc_id)) await channel.send("Hello! This is your friendly Showcase bot!") message = await channel.send( "Please react " + "to this message with one of the emojis below with " + "how you are feeling about your project so far!") await message.add_reaction("😀") await message.add_reaction("😐") await message.add_reaction("☹") session.commit() session.close()
async def search_teams(self, ctx, *, name): """Returns teams that match the search string""" async with ctx.typing(): name = re.sub(r'^"|"$', '', name) session = session_creator() teams = self.team_service.get_teams_by_name(name, session) if teams: out = f'{len(teams)} result{"s" if len(teams) > 1 else ""}:\n' for team in teams: out += str(team) + "\n" else: out = 'No teams found' session.commit() session.close() await paginated_send(ctx, out, allowed_mentions=discord.AllowedMentions( everyone=False, users=False, roles=False))
async def team_broadcast_message(self, ctx: commands.context.Context, *message): message = " ".join(message) if await confirm( f'Are you sure you would like to send the message "{message}" to all teams?', bot=self.bot, ctx=ctx, success_msg="Ok, I will send the message", abort_msg="Ok, I will not send the message", ): session = session_creator() for team in self.team_service.get_all_teams(session): try: await ctx.guild.get_channel(int(team.tc_id)).send(message) except Exception as ex: raise ex session.commit() session.close()
def delete_team_by_name(name) -> bool: # TODO: Confirm that member references are deleted as well """Deletes team with given id""" session = session_creator() team = session.query(Team).filter(Team.team_name == name).first() if team is not None: # if team.members is not None: # members = team.members # for member in members: # session.delete(member) session.delete(team) session.commit() session.close() return True else: session.commit() session.close() return False
async def team_broadcast_form_individual(self, ctx, name=None, *form): if not name: await ctx.send("No team name provided!") return if not form: await ctx.send("No form provided!") return session = session_creator() team = self.team_service.get_team_by_name(name, session) if not team: await ctx.send("Could not find a team with that name!") return form = " ".join(form) if form in self.forms: await self.forms[form]["func_i"](self, ctx, team) await ctx.send("Form is away!") else: await ctx.send( "I'm sorry, but I do not know the form you are talking about")