Skip to content

Commit

Permalink
Django tasks test
Browse files Browse the repository at this point in the history
  • Loading branch information
berk76 committed Dec 9, 2024
1 parent 0152795 commit b2fbbae
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ django==5.1.4
django-tinymce==4.1.0
openpyxl==3.1.5
psycopg==3.2.3
django-tasks==0.6.0
17 changes: 12 additions & 5 deletions svjis/articles/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import secrets
import string
import os.path
import time
from . import (
views,
views_contact,
Expand All @@ -18,6 +19,7 @@
from django.core.mail import EmailMessage
from django.conf import settings
from django.contrib.auth.hashers import make_password
from django_tasks import task
from openpyxl.styles import NamedStyle, Font, Border, Side, PatternFill


Expand Down Expand Up @@ -163,12 +165,17 @@ def send_mails(recipient_list: list, subject: str, html_body: str, immediately:
models.MessageQueue.objects.create(email=r, subject=subject, body=html_body, status=0)


@task()
def send_message_queue():
messages = models.MessageQueue.objects.filter(status=0)
for m in messages:
send_mails([m.email], m.subject, m.body, True)
m.status = 1
m.save()
for i in range(100):
print(i)
time.sleep(1)

# messages = models.MessageQueue.objects.filter(status=0)
# for m in messages:
# send_mails([m.email], m.subject, m.body, True)
# m.status = 1
# m.save()


def get_template(template_key):
Expand Down
3 changes: 2 additions & 1 deletion svjis/articles/views_redaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,8 @@ def redaction_article_notifications_send_view(request):
users = get_users_for_notification(article)

recipients = [u for u in users if request.POST.get(f"u_{u.pk}", False) == 'on']
utils.send_article_notification(recipients, f"{request.scheme}://{request.get_host()}", article)
# utils.send_article_notification(recipients, f"{request.scheme}://{request.get_host()}", article)
utils.send_message_queue.enqueue()
i = len(recipients)

ctx = utils.get_context()
Expand Down
11 changes: 11 additions & 0 deletions svjis/svjis/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
'articles',
'tinymce',
'django.contrib.sitemaps',
'django_tasks',
'django_tasks.backends.database',
]

MIDDLEWARE = [
Expand All @@ -55,6 +57,12 @@
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

TASKS = {
"default": {
"BACKEND": "django_tasks.backends.database.DatabaseBackend",
}
}

ROOT_URLCONF = 'svjis.urls'

TEMPLATES = [
Expand Down Expand Up @@ -83,6 +91,9 @@
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
'OPTIONS': {
'transaction_mode': 'EXCLUSIVE',
},
}
}

Expand Down

0 comments on commit b2fbbae

Please sign in to comment.