Exemple #1
0
def command_horo(update: Update, context: CallbackContext) -> None:
    horoscope = parse_horoscope()
    update.effective_chat.send_message(
        "Сегодня {club_day} день от сотворения Клуба, {phase_sign}\n\n{phase_description}".format(**horoscope)
    )
Exemple #2
0
def generate_daily_digest(user):
    end_date = datetime.utcnow()
    start_date = end_date - timedelta(days=1)
    if end_date.weekday() == 1:
        # we don't have daily on sundays and mondays, we need to include all these posts at tuesday
        start_date = end_date - timedelta(days=3)

    if settings.DEBUG:
        start_date = end_date - timedelta(days=1000)

    created_at_condition = dict(created_at__gte=start_date, created_at__lte=end_date)
    published_at_condition = dict(published_at__gte=start_date, published_at__lte=end_date)

    # Moon
    moon_phase = parse_horoscope()

    # New actions
    subscription_comments = Comment.visible_objects()\
        .filter(
            post__subscriptions__user=user,
            **created_at_condition
        )\
        .values("post__type", "post__slug", "post__title", "post__author_id")\
        .annotate(count=Count("id"))\
        .order_by()

    replies = Comment.visible_objects()\
        .filter(
            reply_to__author=user,
            **created_at_condition
        )\
        .values("post__type", "post__slug", "post__title")\
        .annotate(count=Count("reply_to_id"))\
        .order_by()

    new_events = [
        {
            "type": "my_post_comment",
            "post_url": reverse("show_post", kwargs={"post_type": e["post__type"], "post_slug": e["post__slug"]}),
            "post_title": e["post__title"],
            "count": e["count"],
        } for e in subscription_comments if e["post__author_id"] == user.id
    ] + [
        {
            "type": "subscribed_post_comment",
            "post_url": reverse("show_post", kwargs={"post_type": e["post__type"], "post_slug": e["post__slug"]}),
            "post_title": e["post__title"],
            "count": e["count"],
        } for e in subscription_comments if e["post__author_id"] != user.id
    ] + [
        {
            "type": "reply",
            "post_url": reverse("show_post", kwargs={"post_type": e["post__type"], "post_slug": e["post__slug"]}),
            "post_title": e["post__title"],
            "count": e["count"],
        } for e in replies
    ]

    upvotes = PostVote.objects.filter(post__author=user, **created_at_condition).count() \
        + CommentVote.objects.filter(comment__author=user, **created_at_condition).count()

    if upvotes:
        new_events = [
            {
                "type": "upvotes",
                "count": upvotes,
            }
        ] + new_events

    # Mentions
    mentions = Comment.visible_objects() \
        .filter(**created_at_condition) \
        .filter(text__regex=fr"@\y{user.slug}\y", is_deleted=False)\
        .exclude(reply_to__author=user)\
        .order_by("-upvotes")[:5]

    # Best posts
    posts = Post.visible_objects()\
        .filter(**published_at_condition)\
        .filter(Q(is_approved_by_moderator=True) | Q(upvotes__gte=settings.COMMUNITY_APPROVE_UPVOTES))\
        .filter(is_visible_in_feeds=True)\
        .exclude(type__in=[Post.TYPE_INTRO, Post.TYPE_WEEKLY_DIGEST])\
        .exclude(is_shadow_banned=True)\
        .order_by("-upvotes")[:100]

    # New joiners
    intros = Post.visible_objects()\
        .filter(type=Post.TYPE_INTRO, **published_at_condition)\
        .order_by("-upvotes")

    if not posts and not mentions and not intros:
        raise NotFound()

    og_params = urlencode({
        **settings.OG_IMAGE_GENERATOR_DEFAULTS,
        "title": f"Ежедневный дайджест пользователя {user.slug}",
        "author": "THE MACHINE",
        "ava": settings.OG_MACHINE_AUTHOR_LOGO
    })

    return render_to_string("emails/daily.html", {
        "user": user,
        "events": new_events,
        "intros": intros,
        "posts": posts,
        "mentions": mentions,
        "date": end_date,
        "moon_phase": moon_phase,
        "og_image_url": f"{settings.OG_IMAGE_GENERATOR_URL}?{og_params}"
    })
Exemple #3
0
def daily_digest(request, user_slug):
    user = get_object_or_404(User, slug=user_slug)

    end_date = datetime.utcnow()
    start_date = end_date - timedelta(days=1)
    if end_date.weekday() == 1:
        # we don't have daily on sundays and mondays, we need to include all these posts at tuesday
        start_date = end_date - timedelta(days=3)

    if settings.DEBUG:
        start_date = end_date - timedelta(days=1000)

    created_at_condition = dict(created_at__gte=start_date,
                                created_at__lte=end_date)
    published_at_condition = dict(published_at__gte=start_date,
                                  published_at__lte=end_date)

    # Moon
    moon_phase = parse_horoscope()

    # New actions
    subscription_comments = Comment.visible_objects()\
        .filter(
            post__subscriptions__user=user,
            **created_at_condition
        )\
        .values("post__type", "post__slug", "post__title", "post__author_id")\
        .annotate(count=Count("id"))\
        .order_by()

    replies = Comment.visible_objects()\
        .filter(
            reply_to__author=user,
            **created_at_condition
        )\
        .values("post__type", "post__slug", "post__title")\
        .annotate(count=Count("reply_to_id"))\
        .order_by()

    new_events = [{
        "type":
        "my_post_comment",
        "post_url":
        reverse("show_post",
                kwargs={
                    "post_type": e["post__type"],
                    "post_slug": e["post__slug"]
                }),
        "post_title":
        e["post__title"],
        "count":
        e["count"],
    } for e in subscription_comments if e["post__author_id"] == user.id] + [{
        "type":
        "subscribed_post_comment",
        "post_url":
        reverse("show_post",
                kwargs={
                    "post_type": e["post__type"],
                    "post_slug": e["post__slug"]
                }),
        "post_title":
        e["post__title"],
        "count":
        e["count"],
    } for e in subscription_comments if e["post__author_id"] != user.id] + [{
        "type":
        "reply",
        "post_url":
        reverse("show_post",
                kwargs={
                    "post_type": e["post__type"],
                    "post_slug": e["post__slug"]
                }),
        "post_title":
        e["post__title"],
        "count":
        e["count"],
    } for e in replies]

    upvotes = PostVote.objects.filter(post__author=user, **created_at_condition).count() \
        + CommentVote.objects.filter(comment__author=user, **created_at_condition).count()

    if upvotes:
        new_events = [{
            "type": "upvotes",
            "count": upvotes,
        }] + new_events

    # Mentions
    mentions = Comment.visible_objects() \
        .filter(**created_at_condition) \
        .filter(text__regex=fr"@\y{user.slug}\y", is_deleted=False)\
        .exclude(reply_to__author=user)\
        .order_by("-upvotes")[:5]

    # Best posts
    posts = Post.visible_objects()\
        .filter(is_approved_by_moderator=True, **published_at_condition)\
        .exclude(type__in=[Post.TYPE_INTRO, Post.TYPE_WEEKLY_DIGEST])\
        .exclude(is_shadow_banned=True)\
        .order_by("-upvotes")[:100]

    # New joiners
    intros = Post.visible_objects()\
        .filter(type=Post.TYPE_INTRO, **published_at_condition)\
        .order_by("-upvotes")

    if not posts and not mentions and not intros:
        raise Http404()

    return render(
        request, "emails/daily.html", {
            "user": user,
            "events": new_events,
            "intros": intros,
            "posts": posts,
            "mentions": mentions,
            "date": end_date,
            "moon_phase": moon_phase,
        })
Exemple #4
0
def daily_digest(request, user_slug):
    user = get_object_or_404(User, slug=user_slug)

    end_date = datetime.utcnow()
    start_date = end_date - timedelta(days=1)
    if end_date.weekday() == 1:
        # we don't have daily on mondays and weekends, we need to include all these posts
        start_date = end_date - timedelta(days=4)

    created_at_condition = dict(created_at__gte=start_date,
                                created_at__lte=end_date)
    published_at_condition = dict(published_at__gte=start_date,
                                  published_at__lte=end_date)

    # Moon
    moon_phase = parse_horoscope()

    # New actions
    post_comment_actions = Comment.visible_objects()\
        .filter(
            post__author=user,
            **created_at_condition
        )\
        .values("post__type", "post__slug", "post__title")\
        .annotate(count=Count("id"))\
        .order_by()
    reply_actions = Comment.visible_objects()\
        .filter(
            reply_to__author=user,
            **created_at_condition
        )\
        .values("post__type", "post__slug", "post__title")\
        .annotate(count=Count("reply_to_id"))\
        .order_by()
    upvotes = PostVote.objects.filter(post__author=user, **created_at_condition).count() \
        + CommentVote.objects.filter(comment__author=user, **created_at_condition).count()

    new_events = [{
        "type":
        "post_comment",
        "post_url":
        reverse("show_post",
                kwargs={
                    "post_type": e["post__type"],
                    "post_slug": e["post__slug"]
                }),
        "post_title":
        e["post__title"],
        "count":
        e["count"],
    } for e in post_comment_actions] + [{
        "type":
        "reply",
        "post_url":
        reverse("show_post",
                kwargs={
                    "post_type": e["post__type"],
                    "post_slug": e["post__slug"]
                }),
        "post_title":
        e["post__title"],
        "count":
        e["count"],
    } for e in reply_actions] + [{
        "type": "upvotes",
        "count": upvotes,
    }]

    # Best posts
    posts = Post.visible_objects()\
        .filter(is_approved_by_moderator=True, **published_at_condition)\
        .exclude(type__in=[Post.TYPE_INTRO, Post.TYPE_WEEKLY_DIGEST])\
        .order_by("-upvotes")[:100]

    # Best comments
    comments = Comment.visible_objects() \
        .filter(**created_at_condition) \
        .filter(is_deleted=False)\
        .order_by("-upvotes")[:1]

    # New joiners
    intros = Post.visible_objects()\
        .filter(type=Post.TYPE_INTRO, **published_at_condition)\
        .order_by("-upvotes")

    if not posts and not comments and not intros:
        raise Http404()

    return render(
        request, "emails/daily.html", {
            "user": user,
            "events": new_events,
            "intros": intros,
            "posts": posts,
            "comments": comments,
            "date": end_date,
            "moon_phase": moon_phase,
        })