예제 #1
0
    async def test_filter_by_aggregation_field_lower(self):
        await self.model.create(chars="AaA")
        obj = await self.model.annotate(chars_lower=Lower("chars")
                                        ).filter(chars_lower="aaa")

        self.assertEqual(len(obj), 1)
        self.assertEqual(obj[0].chars_lower, "aaa")
예제 #2
0
 async def test_nested_functions(self):
     author = await Author.create(name="Some One")
     await Book.create(name="First!", author=author, rating=4)
     await Book.create(name="Second!", author=author, rating=3)
     await Book.create(name="Third!", author=author, rating=3)
     ret = await Book.all().annotate(max_name=Lower(Max("name"))
                                     ).values("max_name")
     self.assertEqual(ret, [{"max_name": "third!"}])
예제 #3
0
 async def test_filter_by_annotation_lower(self):
     await Tournament.create(name="Tournament")
     await Tournament.create(name="NEW Tournament")
     tournaments = await Tournament.annotate(name_lower=Lower("name"))
     self.assertEqual(len(tournaments), 2)
     self.assertSetEqual({t.name_lower
                          for t in tournaments},
                         {"tournament", "new tournament"})
예제 #4
0
async def run():
    await Tortoise.init(db_url="mysql://*****:*****@localhost:55555/test_demo", modules={"models": ["__main__"]})
    await Tortoise.generate_schemas()
    tournament = await Tournament.create(name="New Tournament", desc="great")
    await tournament.save()
    await Tournament.create(name="Second tournament")
    await Tournament.create(name=" final tournament ")
    await Event(name="Without participants", tournament_id=tournament.id).save()
    event = Event(name="Test", tournament_id=tournament.id)
    await event.save()
    participants = []
    for i in range(10):
        team = Team(name=f"Team {(i + 1)}")
        await team.save()
        participants.append(team)

    await event.participants.add(*participants)

    # await event.participants.add(participants[0], participants[1])

    print(await Tournament.all().annotate(events_count=Count("events")).filter(events_count__lte=3).values())
    print(
        await Tournament
        .annotate(events_count_with_filter=Count("events", _filter=Q(name="New Tournament")))
        .filter(events_count_with_filter__gte=0).values()
    )

    print(await Event.annotate(lowest_team_id=Count("participants__id")).values())

    print(await Tournament.annotate(events_count=Count("events")).order_by("events_count"))

    print(await Event.annotate(tournament_test_id=Sum("tournament__id")).first())

    print(
        await Tournament.annotate(clean_desciption=Coalesce("desc", "hehe")).values()
    )

    print(
        await Tournament.annotate(trimmed_name=Trim("name")).values()
    )

    print(
        await Tournament.annotate(name_len=Length("name")).filter(
            name_len__gt=len("New Tournament")
        )
    )

    print(await Tournament.annotate(name_lo=Lower("name")).filter(name_lo="new tournament").values())
    print(await Tournament.annotate(name_lo=Upper("name")).filter(name_lo="NEW TOURNAMENT").values())

    print()
예제 #5
0
async def run():
    await Tortoise.init(db_url="sqlite://:memory:",
                        modules={"models": ["__main__"]})
    await Tortoise.generate_schemas()
    tournament = await Tournament.create(name="New Tournament", desc="great")
    await tournament.save()
    await Tournament.create(name="Second tournament")
    await Tournament.create(name=" final tournament ")
    await Event(name="Without participants",
                tournament_id=tournament.id).save()
    event = Event(name="Test", tournament_id=tournament.id)
    await event.save()
    participants = []
    for i in range(2):
        team = Team(name=f"Team {(i + 1)}")
        await team.save()
        participants.append(team)
    await event.participants.add(participants[0], participants[1])
    await event.participants.add(participants[0], participants[1])

    print(await Tournament.all().annotate(events_count=Count("events")
                                          ).filter(events_count__gte=1))

    print(await Event.filter(id=event.id).first().annotate(
        lowest_team_id=Min("participants__id")))

    print(await Tournament.all().annotate(events_count=Count("events")
                                          ).order_by("events_count"))

    print(await Event.all().annotate(tournament_test_id=Sum("tournament__id")
                                     ).first())

    print(await Tournament.annotate(clean_desciption=Coalesce("desc")
                                    ).filter(clean_desciption=""))

    print(await Tournament.annotate(trimmed_name=Trim("name")
                                    ).filter(trimmed_name="final tournament"))

    print(await
          Tournament.annotate(name_len=Length("name")
                              ).filter(name_len__gt=len("New Tournament")))

    print(await Tournament.annotate(name_lo=Lower("name")
                                    ).filter(name_lo="new tournament"))
    print(await Tournament.annotate(name_lo=Upper("name")
                                    ).filter(name_lo="NEW TOURNAMENT"))
예제 #6
0
    async def get_member_by_platform_username(self, username, platform_id):
        username = username.lower()

        if platform_id == constants.PLATFORM_BUNGIE:
            username_field = "bungie_username"
        elif platform_id == constants.PLATFORM_PSN:
            username_field = "psn_username"
        elif platform_id == constants.PLATFORM_XBOX:
            username_field = "xbox_username"
        elif platform_id == constants.PLATFORM_BLIZZARD:
            username_field = "blizzard_username"
        elif platform_id == constants.PLATFORM_STEAM:
            username_field = "steam_username"
        elif platform_id == constants.PLATFORM_STADIA:
            username_field = "stadia_username"

        query = (
            await Member.annotate(name_lo=Lower(username_field))
            .get_or_none(name_lo=username)
            .prefetch_related("clans")
        )
        return query
예제 #7
0
    async def get_member_by_naive_username(self, username, include_clan=True):
        username = username.lower()

        if include_clan:
            member_db = (
                await ClanMember.annotate(
                    bungie_username_lo=Lower("member__bungie_username"),
                    xbox_username_lo=Lower("member__xbox_username"),
                    psn_username_lo=Lower("member__psn_username"),
                    blizzard_username_lo=Lower("member__blizzard_username"),
                    steam_username_lo=Lower("member__steam_username"),
                    stadia_username_lo=Lower("member__stadia_username"),
                )
                .get_or_none(
                    Q(bungie_username_lo=username)
                    | Q(xbox_username_lo=username)
                    | Q(psn_username_lo=username)
                    | Q(blizzard_username_lo=username)
                    | Q(steam_username_lo=username)
                    | Q(stadia_username_lo=username)
                )
                .prefetch_related("clan", "member")
            )
        else:
            member_db = await Member.annotate(
                bungie_username_lo=Lower("bungie_username"),
                xbox_username_lo=Lower("xbox_username"),
                psn_username_lo=Lower("psn_username"),
                blizzard_username_lo=Lower("blizzard_username"),
                steam_username_lo=Lower("steam_username"),
                stadia_username_lo=Lower("stadia_username"),
            ).get_or_none(
                Q(bungie_username_lo=username)
                | Q(xbox_username_lo=username)
                | Q(psn_username_lo=username)
                | Q(blizzard_username_lo=username)
                | Q(steam_username_lo=username)
                | Q(stadia_username_lo=username)
            )

        return member_db