fix: send_new_post script

This commit is contained in:
Vasily Zubarev 2023-01-30 12:58:21 +01:00
parent 24c5af2a81
commit d8b0b2f081
4 changed files with 5 additions and 14 deletions

View File

@ -24,7 +24,7 @@
</p>
<a href="https://vas3k.blog/{{ post.type }}/{{ post.slug }}/">
<img src="{% if post.preview_image %}{{ post.preview_image }}{% else %}{{ post.image }}{% endif %}" alt="{{ post.title }}">
<img src="{% if post.og_image %}{{ post.og_image }}{% else %}{{ post.image }}{% endif %}" alt="{{ post.title }}">
</a>
{% if post.announce_text %}

View File

@ -1,5 +1,4 @@
import logging
from datetime import datetime, timedelta
from django.core.management import BaseCommand
from django.template import loader
@ -16,21 +15,13 @@ class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument("--production", type=bool, required=False, default=False)
parser.add_argument("--hours", type=int, required=False, default=24)
parser.add_argument("--auto-confirm", type=bool, required=False, default=False)
def handle(self, *args, **options):
production = options.get("production")
hours = options.get("hours") or 24
if not production:
hours = 99999
# Step 1. Check for a new post
post = Post.objects.filter(
is_visible=True,
created_at__lte=datetime.utcnow(),
created_at__gte=datetime.utcnow() - timedelta(hours=hours),
).order_by("-created_at").first()
post = Post.visible_objects().order_by("-published_at").first()
if not post:
self.stdout.write(f"No new posts. Exiting...")

View File

@ -1,4 +1,5 @@
import logging
import re
from django.conf import settings
from django.core.mail import EmailMultiAlternatives
@ -31,7 +32,7 @@ def send_vas3k_email(subscriber: Subscriber, subject: str, html: str, force: boo
email = EmailMultiAlternatives(
subject=subject,
body=prepared_html,
body=re.sub(r"<[^>]+>", "", html),
from_email=settings.DEFAULT_FROM_EMAIL,
to=[subscriber.email],
headers={
@ -40,5 +41,4 @@ def send_vas3k_email(subscriber: Subscriber, subject: str, html: str, force: boo
**kwargs
)
email.attach_alternative(prepared_html, "text/html")
email.content_subtype = "html"
return email.send(fail_silently=True)

View File

@ -34,7 +34,7 @@ def index(request):
*[post.id for post in latest_posts],
*[post.id for post in latest_world_posts],
])\
.order_by("-view_count")[:6]
.order_by("-view_count")[:5]
world_posts = list(latest_world_posts) + list(top_world_posts)
# featured posts