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
23 changes: 15 additions & 8 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ jobs:
runs-on: ubuntu-latest
timeout-minutes: 10

services:
redis:
image: redis:7
ports:
- 6379:6379


steps:
- name: Checkout repo
uses: actions/checkout@v3
Expand All @@ -33,11 +40,11 @@ jobs:
- name: make
run: poetry run ruff check --output-format=github .

# - name: Run Tests
# env:
# SECRET_KEY: "j+(#!sag4%^ay+oanu&t-&3x@2$!+s%x4u!4%pser4o9)2!ua1"
# ENVIRONMENT: "local"
# STRIPE_SECRET_KEY: "fail"
# STRIPE_PUBLIC_KEY: "fail"
# timeout-minutes: 5
# run: poetry run python manage.py test
- name: Run Tests
env:
SECRET_KEY: "j+(#!sag4%^ay+oanu&t-&3x@2$!+s%x4u!4%pser4o9)2!ua1"
ENVIRONMENT: "local"
STRIPE_SECRET_KEY: "fail"
STRIPE_PUBLIC_KEY: "fail"
timeout-minutes: 5
run: poetry run python manage.py test
5 changes: 5 additions & 0 deletions book/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@


class IsAdminOrReadOnly(BasePermission):
"""
Custom permission to allow only admin users to modify data,
while read-only access is granted to all users.
"""

def has_permission(self, request, view):
if request.method in SAFE_METHODS:
return True
Expand Down
13 changes: 13 additions & 0 deletions borrowing/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@

@receiver(post_save, sender=Borrowing)
def handle_borrowing_creation(instance, created, **kwargs):
"""
Signal handler to send a notification when a new Borrowing instance is created.

This function listens for the post_save signal of the Borrowing model. If a new
borrowing record is created, it sends a Telegram notification with the book title
and expected return date.

Args:
instance (Borrowing): The instance of Borrowing that was saved.
created (bool): Indicates if the instance was created (True) or updated (False).
**kwargs: Additional keyword arguments.

"""
if created:
message = (
f"New borrowing for book: {instance.book.title}, "
Expand Down
17 changes: 17 additions & 0 deletions borrowing/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,23 @@

@shared_task
def send_message():
"""
Celery task to send reminders for overdue borrowings.

This task checks for borrowings that are overdue (expected return date has passed)
and have not yet been returned. For each overdue borrowing, it sends a Telegram
notification to remind the user.

The notification includes details such as the user's name, book title, borrow date,
and the number of days the borrowing is overdue.

Args:
None

Returns:
None
"""

today = datetime.date.today()
overdue_borrowings = Borrowing.objects.filter(
expected_return_date__lte=today,
Expand Down
4 changes: 1 addition & 3 deletions borrowing/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,7 @@ def return_book(self, request, pk=None):
"/api/borrowings/", status=status.HTTP_302_FOUND
)

payment = Payment.objects.filter(type="FINE", borrowing=borrowing)[
0
]
payment = Payment.objects.filter(type="FINE", borrowing=borrowing)[0]

return HttpResponseRedirect(
payment.session_url, status=status.HTTP_302_FOUND
Expand Down
33 changes: 33 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ services:
- ENVIRONMENT=docker
depends_on:
- db
- redis
command: >
sh -c "python manage.py wait_for_db &&
python manage.py migrate &&
Expand All @@ -25,5 +26,37 @@ services:
volumes:
- my_db:/var/lib/postgresql/data

redis:
image: redis:7
restart: always
ports:
- "6379:6379"

celery-worker:
build:
context: .
depends_on:
- redis
- library
env_file:
- .env
environment:
- ENVIRONMENT=docker
command: >
sh -c "celery -A core worker --loglevel=info --pool=solo"

celery-beat:
build:
context: .
depends_on:
- redis
- library
env_file:
- .env
environment:
- ENVIRONMENT=docker
command: >
sh -c "python manage.py migrate django_celery_beat && celery -A core beat --loglevel=info"

volumes:
my_db:
10 changes: 8 additions & 2 deletions core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,17 @@

# Celery settings

CELERY_BROKER_URL = "redis://localhost:6379/0"
if os.environ.get("ENVIRONMENT") == "local":
CELERY_BROKER_URL = "redis://localhost:6379/0"
CELERY_RESULT_BACKEND = "redis://localhost:6379/0"
else:
CELERY_BROKER_URL = "redis://redis:6379/0"
CELERY_RESULT_BACKEND = "redis://redis:6379/0"

CELERY_ACCEPT_CONTENT = ["json"]
CELERY_TASK_SERIALIZER = "json"
CELERY_RESULT_BACKEND = "redis://localhost:6379/0"
CELERY_TIMEZONE = "Europe/Kiev"
CELERY_BEAT_SCHEDULER = "django_celery_beat.schedulers:DatabaseScheduler"

CELERY_BEAT_SCHEDULE = {
"send_message_daily": {
Expand Down
2 changes: 1 addition & 1 deletion core/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

urlpatterns = [
path("admin/", admin.site.urls),
path("api/user/", include("user.urls", namespace="user")),
path("api/users/", include("user.urls", namespace="user")),
path("api/borrowings/", include("borrowing.urls", namespace="borrowing")),
path("api/payments/", include("payment.urls", namespace="payment")),
path("api/schema/", SpectacularAPIView.as_view(), name="schema"),
Expand Down
Loading