Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions borrowing/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import datetime

from celery import shared_task
from tg_bot.utils import send_telegram_notification

from borrowing.models import Borrowing


@shared_task
def send_message():
today = datetime.date.today()
overdue_borrowings = Borrowing.objects.filter(
expected_return_date__lte=today,
actual_return_date__isnull=True
)
for borrowing in overdue_borrowings:
message = (
f"📚 Borrowing Overdue Reminder ‼️\n"
f"User: {borrowing.user.username}\n"
f"Book: {borrowing.book.title}\n"
f"Borrow Date: {borrowing.borrow_date}\n"
f"Expected Return Date: {borrowing.expected_return_date}\n"
f"Overdue by: {today - borrowing.expected_return_date} days\n"
)
send_telegram_notification.delay(message)
8 changes: 8 additions & 0 deletions core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from datetime import timedelta
from pathlib import Path

from celery.schedules import crontab
from dotenv import load_dotenv

load_dotenv()
Expand Down Expand Up @@ -49,6 +50,7 @@
"drf_spectacular",
"django_filters",
"debug_toolbar",
"django_celery_beat",
# custom apps
"book",
"user",
Expand Down Expand Up @@ -189,3 +191,9 @@
CELERY_RESULT_BACKEND = "redis://localhost:6379/0"
CELERY_TIMEZONE = "Europe/Kiev"

CELERY_BEAT_SCHEDULE = {
"send_message_daily": {
"task": "borrowing.tasks.send_message",
"schedule": crontab(minute="*"),
},
}
68 changes: 67 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ django-debug-toolbar = "^4.4.6"
stripe = "^11.4.1"
celery = {extras = ["redis"], version = "^5.4.0"}
python-telegram-bot = "^21.10"
django-celery-beat = "^2.7.0"

[build-system]
requires = ["poetry-core"]
Expand Down
Loading