Example #1
0
    def refresh_concept_table(self):
        """
        copy contents of the concept tables in reference schema
        to the concept table in main db
        """

        LOGGER.info('loading reference concept tables into main db')

        e = get_engine(local_config.NCM_DB_CONN_STR)
        s = make_session()

        concept_tables = sql.get_reference_concept_manifest(e)

        # iterate through each of the tables in reference schema
        for i, r in concept_tables.iterrows():
            d = sql.get_reference_concept_table(r.codesystem, r.table_name, e)
            # add each of the table's rows to the concept table in main db
            for x, y in d.iterrows():
                s.add(
                    ncm.Concept(uid=y.uid,
                                local_concept_code=y.local_concept_code,
                                nhs_concept_code=y.nhs_concept_code,
                                codesystem=r.codesystem))

        s.commit()
Example #2
0
    async def on_message(self, message):
        """Saves a message sent on a server the bot is in.

        Will not save if it is a command for this bot or if the message is from this bot
        """
        skip_saving = False

        if message.author == self.bot.user:
            skip_saving = True
        if 'r!' in message.content:
            skip_saving = True
        if message.content.startswith("〰"):
            await self.reacted_message_response(message)
            skip_saving = True
        if not skip_saving:
            session = make_session()
            if message.clean_content != '':
                current_message = Message(
                    message_content=message.clean_content,
                    message_sender=message.author.id,
                    message_channel=message.channel.id,
                    message_server=message.guild.id,
                    message_id=message.id)
                session.add(current_message)
            session.commit()
            session.close()
            Message.prune_db(50000)
    def setUp(self):

        self.c = config.TestConfig()

        create_database(self.c, 'db_a')

        self.s = make_session(self.c)
Example #4
0
 async def on_message_delete(self, message):
     session = make_session()
     new_message = session.query(Message).filter(
         Message.message_id == message.id).first()
     if new_message is not None:
         session.delete(new_message)
         session.commit()
     session.close()
Example #5
0
    def refresh_metric_table(self):
        """
        gather all metrics required and add/update table in main db
        """

        LOGGER.info('fetching and refreshing all metrics')

        s = make_session()
        refresh_metric_table.run_etl(s)
        s.commit()
Example #6
0
    def refresh_identifier_relationship_table(self):
        """
        gather all identifier relationships required and add/update table
        in main db
        """

        LOGGER.info('fetching and refreshing all identifier relationships')

        s = make_session()
        refresh_identifier_relationship_table.run_etl(s)
        s.commit()
Example #7
0
 async def on_raw_reaction_remove(self, payload):
     """Checks to see if the reaction removed is the '〰' emoji. If so, remove the message from the db"""
     if payload.emoji.name == '〰':
         channel = self.bot.get_channel(payload.channel_id)
         message = await channel.fetch_message(payload.message_id)
         session = make_session()
         new_temp_message = session.query(TempMessage).filter_by(
             message_id=message.id,
             message_server=message.guild.id,
             message_reactor_id=payload.user_id).first()
         if new_temp_message is not None:
             session.delete(new_temp_message)
         session.commit()
         session.close()
Example #8
0
 async def on_raw_reaction_add(self,
                               payload: discord.RawReactionActionEvent):
     """Checks to see if the reaction added is the '〰' emoji. If so, save the message from the db"""
     if payload.emoji.name == '〰':
         channel: discord.TextChannel = self.bot.get_channel(
             payload.channel_id)
         message: discord.message = await channel.fetch_message(
             payload.message_id)
         session = make_session()
         new_temp_message = TempMessage(message_id=message.id,
                                        message_sender=message.author.id,
                                        message_channel=message.channel.id,
                                        message_server=message.guild.id,
                                        message_reactor_id=payload.user_id)
         session.add(new_temp_message)
         session.commit()
         session.close()
         TempMessage.prune_db(1)
Example #9
0
 async def reacted_message_response(self, message: discord.Message):
     session = make_session()
     original_message: TempMessage = session.query(TempMessage).filter(
         TempMessage.message_reactor_id == message.author.id).order_by(
             TempMessage.message_sent_time.desc()).first()
     if original_message is None:
         return
     elif original_message.message_sent_time >= datetime.datetime.now(
     ) - datetime.timedelta(minutes=5):
         request_ctx = await self.bot.get_context(message)
         original_message_data: discord.Message = await get_message(
             request_ctx, original_message.message_id)
         # channel_object = await self.bot.get_channel()
         junk, response_content = split_message(message.clean_content)
         await self.send_response(request_ctx, response_content,
                                  message.channel,
                                  original_message_data.clean_content,
                                  original_message_data.author.id,
                                  original_message_data.channel.id,
                                  original_message_data.created_at,
                                  original_message_data.id)
     TempMessage.prune_db(2)
Example #10
0
def database_search(ctx: Context,
                    channel: typing.Optional[discord.TextChannel],
                    target_user: typing.Optional[discord.Member],
                    search_terms: str) -> discord.message.Message:
    """Searches through the database for the search for the requested message.

    :param ctx: The Context of the message
    :param channel: The channel that the desired message is in. Is optional
    :param target_user: The user that sent the desired message. Is optional
    :param search_terms: The main text that the user is searching for
    :return message: The message found. Returns None if no message is found
    """
    session = make_session()
    new_message = None

    if channel is not None and target_user is not None:
        if search_terms != "":
            new_message = session.query(
                Message
                # func.lower() insures that case isn't an issue
            ).filter(
                func.lower(Message.message_content).contains(
                    func.lower(search_terms)),
                Message.message_channel == channel.id,
                Message.message_sender == target_user.id,
                Message.message_server == ctx.guild.id).order_by(
                    Message.message_sent_time.desc()).first()
        else:
            new_message = session.query(Message).filter(
                Message.message_channel == channel.id,
                Message.message_sender == target_user.id,
                Message.message_server == ctx.guild.id).order_by(
                    Message.message_sent_time.desc()).first()
    elif channel is not None:
        if search_terms != "":
            new_message = session.query(Message).filter(
                func.lower(Message.message_content).contains(
                    func.lower(search_terms)),
                Message.message_channel == channel.id,
                Message.message_server == ctx.guild.id).order_by(
                    Message.message_sent_time.desc()).first()
        else:
            new_message = session.query(Message).filter(
                Message.message_channel == channel.id,
                Message.message_server == ctx.guild.id).order_by(
                    Message.message_sent_time.desc()).first()
    elif target_user is not None:
        if search_terms != "":
            new_message = session.query(Message).filter(
                func.lower(Message.message_content).contains(
                    func.lower(search_terms)),
                Message.message_sender == target_user.id,
                Message.message_server == ctx.guild.id).order_by(
                    Message.message_sent_time.desc()).first()
        else:
            new_message = session.query(Message).filter(
                Message.message_sender == target_user.id,
                Message.message_server == ctx.guild.id).order_by(
                    Message.message_sent_time.desc()).first()
    else:
        if search_terms != '':
            new_message = session.query(Message).filter(
                func.lower(Message.message_content).contains(
                    func.lower(search_terms)),
                Message.message_server == ctx.guild.id).order_by(
                    Message.message_sent_time.desc()).first()
        else:
            new_message = session.query(Message).filter(
                Message.message_server == ctx.guild.id).order_by(
                    Message.message_sent_time.desc()).first()

    session.close()
    return new_message