示例#1
0
 async def delete(self, post_id: int, current_user_id: int) -> Optional[PostID]:
     query: Delete = self.table.delete().\
         where(and_(self.table.c.id == post_id, self.table.c.user_id == current_user_id)).\
         returning(self.table.c.id)
     deleted_post_id = await db.execute(query)
     if deleted_post_id is None:
         return
     return PostID(id=deleted_post_id)
示例#2
0
    def test_compile_nested_operators(self):
        """
        Tests SQL compilation of nested operators.
        """
        self.td_engine.execute(
            operators.and_(
                operators.ne(self.table.c.c1, 0),
                operators.mod(self.table.c.c1, 0)))

        assert(self.last_compiled == 't_test.c1 <> ? AND t_test.c1 MOD ?')
示例#3
0
    def get_deed_status(title_number, mdref):

        deeds = Deed.query.filter(
            and_(Deed.deed['title_number'].astext == title_number,
                 Deed.deed['md_ref'].astext == mdref)).all()

        deeds_with_status = list(
            map(lambda deed: {
                "token": deed.token,
                "status": deed.status
            }, deeds))

        return deeds_with_status
示例#4
0
    async def get_lang(self, guild_id, user_id):
        langs = await HighlightLang.query.where(
            and_(
                HighlightLang.guild_id == guild_id,
                or_(HighlightLang.user_id == user_id,
                    HighlightLang.user_id == None))).gino.all()

        selected = None
        for lang in langs:
            if lang.user_id is not None:
                return lang.language
            selected = lang.language

        return selected or self.language
示例#5
0
    async def guildlang(self, ctx, *, language: LangConverter):
        '''Set the default guild highlighting language.'''

        server = await HighlightLang.query.where(
            and_(HighlightLang.guild_id == ctx.guild.id,
                 HighlightLang.user_id == None)).gino.first()

        if server is None:
            await HighlightLang.create(guild_id=ctx.guild.id,
                                       language=language)
        else:
            await server.update(language=language).apply()

        await ctx.send(f'Guild language preference set to `{language}`')
示例#6
0
    def get_deed_status(title_number, mdref):

        deeds = Deed.query.filter(
            and_(
                Deed.deed['title_number'].astext == title_number,
                Deed.deed['md_ref'].astext == mdref
            )
        ).all()

        deeds_with_status = list(
            map(lambda deed: {
                "token": deed.token,
                "status": deed.status
            }, deeds)
        )

        return deeds_with_status
示例#7
0
    def get_next(self, pid, level=0, user=None):
        data = Post.query \
            .filter(sql_ops.and_(Post.post_id > pid, Post.secret <= level))

        if isinstance(user, (str, int)):
            user = UserManager.find_user(user)

        if user:
            data = data.filter(
                Post.owner == User.user_id).filter((~Post.hidden) | (
                    Post.owner == user.user_id) | (User.role < user.role))
        else:
            data = data.filter(~Post.hidden)

        data = data \
            .order_by(Post.post_id.asc()).limit(1).first()
        if not data:
            return -1
        return data.post_id
示例#8
0
 def count_new_labelled_data(self):
     last_training_date = self.session.query(
         func.max(ModelTrainingLog.end_time)).filter(
             ModelTrainingLog.category_group ==
             self.category_group).scalar()
     if not last_training_date:
         last_training_date = datetime(2016, 5, 1)
     self.logger.info(
         'Last model training date: {}'.format(last_training_date))
     query = self.session.query(
         func.count(Interactions.id).label('sample_count')).join(
             InteractionCategory).join(Category).filter(
                 and_(
                     Category.category_group == self.category_group,
                     InteractionCategory.created_date > last_training_date))
     self.logger.info(query)
     new_labelled_sample_count = query.scalar()
     self.logger.info("{} new labelled samples available".format(
         new_labelled_sample_count))
     return new_labelled_sample_count
示例#9
0
    def test_logical_operators(self):
        """
        Tests the functionality of the following logical operators:

            AND, OR
        """
        res = self.engine.execute(
            sql.select([self.table.c.c0]). \
            where(operators.and_(
                operators.gt(self.table.c.c2, 3),
                operators.lt(self.table.c.c2, 7))))

        assert(set([row[0] for row in res]) == {'fox', 'jumps', 'over'})

        res = self.engine.execute(
            sql.select([self.table.c.c0]). \
            where(operators.or_(
                operators.lt(self.table.c.c2, 3),
                operators.gt(self.table.c.c2, 7))))

        assert(set([row[0] for row in res]) == {'the', 'quick', 'lazy',
                                                'dog', 'foobar'})
示例#10
0
    async def lang(self, ctx, *, language: LangConverter = None):
        '''Set your preferred highlighting language.'''

        personal = await HighlightLang.query.where(
            and_(HighlightLang.guild_id == ctx.guild.id,
                 HighlightLang.user_id == ctx.author.id)).gino.first()

        if language is None:
            server = await self.get_lang(ctx.guild.id, None)

            e = discord.Embed(
                description='Do `.lang clear` to clear preference.')

            e.add_field(
                name='Server setting',
                value=f'`{self.language if server is None else server}`')

            e.add_field(
                name='Personal setting',
                value='None' if personal is None else f'`{personal.language}`')

            await ctx.send(embed=e)
            return

        if personal is None:
            await HighlightLang.create(guild_id=ctx.guild.id,
                                       user_id=ctx.author.id,
                                       language=language)
        else:
            if language == 'clear':
                await personal.delete()
                await ctx.send('Language preference cleared.')
                return
            else:
                await personal.update(language=language).apply()

        await ctx.send(f'Language preference set to `{language}`')