Esempio n. 1
0
    def resolve_collections(
        self,
        info,
        id: str = None,
        game: str = None,
        query: str = None,
        created_by: str = None,
    ):
        qs = Collection.objects.all().distinct()

        if not (id or created_by):
            return qs.none()

        if id:
            qs = qs.filter(id=id)

        if game:
            qs = qs.filter(games=game)

        if query:
            qs = qs.filter(name__istartswith=query)

        if created_by:
            qs = qs.filter(created_by=created_by)

        qs = qs.order_by("is_default", "name")

        qs = optimize_queryset(qs, info, "collections", fix_collections_query)

        return qs
Esempio n. 2
0
 def resolve_games(
     self,
     info,
     id: str = None,
     query: str = None,
     parent: str = None,
     players: int = None,
     entity: str = None,
     max_duration: int = None,
 ):
     # TODO(dcramer): fix optimize_queryset so it handles the OneToOne join automatically
     qs = Game.objects.select_related("image").distinct()
     if id:
         qs = qs.filter(id=id)
     if parent:
         qs = qs.filter(parent=parent)
     if query:
         qs = qs.filter(name__istartswith=query)
     if entity:
         qs = qs.filter(entities=entity)
     if players:
         qs = qs.filter(min_players__lte=players, max_players__gte=players)
     if max_duration:
         qs = qs.filter(
             Q(duration__lte=max_duration, duration_type=DurationType.total)
             | Q(
                 duration__lte=max_duration /
                 (players if players else F("max_players")),
                 duration_type=DurationType.per_player,
             ))
     qs = optimize_queryset(qs, info, "games", GameNode.fix_queryset)
     return qs.order_by("name")
Esempio n. 3
0
 def resolve_tags(self, info, id: str = None, query: str = None):
     qs = Tag.objects.all()
     if id:
         qs = qs.filter(id=id)
     if query:
         qs = qs.filter(name__istartswith=query)
     qs = optimize_queryset(qs, info, "tags")
     return qs.order_by("name")
Esempio n. 4
0
    def resolve_checkins(
        self,
        info,
        id: str = None,
        scope: str = None,
        game: str = None,
        created_by: str = None,
    ):
        current_user = info.context.user

        qs = Checkin.objects.all()

        if id:
            qs = qs.filter(id=id)

        if game:
            qs = qs.filter(game=game)

        if scope == "friends":
            if not current_user.is_authenticated:
                return qs.none()
            qs = qs.filter(
                Q(players__in=Follower.objects.filter(
                    from_user=current_user.id).values("to_user"))
                | Q(players=current_user)).distinct()
        # there's not yet privacy scope
        elif scope == "public":
            pass
        elif scope:
            raise NotImplementedError

        if created_by:
            qs = qs.filter(created_by=created_by)

        qs = qs.annotate(total_likes=Count("likes"),
                         total_comments=Count("comments"))
        if current_user.is_authenticated:
            qs = qs.extra(
                select={
                    "is_liked":
                    "select exists(select 1 from tabletop_like where created_by_id = %s and checkin_id = tabletop_checkin.id)"
                },
                select_params=[current_user.id],
            )

        qs = qs.order_by("-created_at")

        qs = optimize_queryset(qs, info, "checkins")

        return qs
Esempio n. 5
0
    def resolve_users(
        self,
        info,
        id: str = None,
        query: str = None,
        scope: str = None,
        include_self: bool = False,
        **kwargs
    ):
        user = info.context.user
        qs = User.objects.all()

        if id:
            qs = qs.filter(id=id)

        if query:
            qs = qs.filter(name__istartswith=query)

        if not include_self:
            qs = qs.exclude(id=user.id)

        if scope == "followers":
            if not user.is_authenticated:
                qs = qs.none()
            else:
                qs = qs.filter(
                    id__in=Follower.objects.filter(to_user_id=user.id).values_list(
                        "from_user_id"
                    )
                )
        elif scope == "following":
            if not user.is_authenticated:
                qs = qs.none()
            else:
                qs = qs.filter(
                    id__in=Follower.objects.filter(from_user_id=user.id).values_list(
                        "to_user_id"
                    )
                )
        elif scope:
            qs = qs.none()

        qs = optimize_queryset(qs, info, "users")

        return qs
Esempio n. 6
0
    def resolve_likes(self,
                      info,
                      id: str = None,
                      checkin: str = None,
                      created_by: str = None):
        qs = Like.objects.all()

        if not (id or checkin or created_by):
            return qs.none()

        if id:
            qs = qs.filter(id=id)

        if checkin:
            qs = qs.filter(checkin=checkin)

        if created_by:
            qs = qs.filter(created_by=created_by)

        qs = qs.order_by("-created_at")

        qs = optimize_queryset(qs, info, "likes")

        return qs