Skip to content

Commit

Permalink
🚸 Run fximport as task
Browse files Browse the repository at this point in the history
  • Loading branch information
pajowu committed Nov 20, 2024
1 parent acd0d73 commit fb2e428
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 24 deletions.
2 changes: 1 addition & 1 deletion fragdenstaat_de/fds_fximport/askfx.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ def _post(self, *args, **kwargs) -> requests.Response:
req.raise_for_status()
return req

def _login(self, retries: int = 100) -> str:
def _login(self, retries: int = 500) -> str:
"""Load the login form and return a path to the captcha iamge
Returns:
Expand Down
65 changes: 59 additions & 6 deletions fragdenstaat_de/fds_fximport/tasks.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,68 @@
from django.db import transaction
from django.utils.translation import gettext_lazy as _

from sentry_sdk import capture_exception

from froide.celery import app as celery_app
from froide.foirequest.models import FoiMessage
from froide.foirequest.models.message import MessageKind
from froide.foirequest.utils import send_request_user_email
from froide.helper.email_sending import mail_registry

from . import helper

fximport_success_mail = mail_registry.register(
"fds_fximport/emails/success",
("action_url", "foirequest", "message", "user"),
)

fximport_failed_mail = mail_registry.register(
"fds_fximport/emails/failed",
("action_url", "foirequest", "message", "user"),
)


@celery_app.task(name="fragdenstaat_de.fds_fximport.import_case")
def import_case(message_id):
from froide.foirequest.models import FoiMessage
with transaction.atomic():
try:
message = FoiMessage.objects.get(id=message_id)
except FoiMessage.DoesNotExist:
return
try:
helper.import_frontex_case(message)
newest_fx_message = (
FoiMessage.objects.filter(request=message.request)
.filter(kind=MessageKind.IMPORT)
.last()
)

try:
message = FoiMessage.objects.get(id=message_id)
except FoiMessage.DoesNotExist:
return
send_request_user_email(
fximport_success_mail,
message.request,
subject=_("New Message imported"),
context={
"message": message,
"foirequest": message.request,
"user": message.request.user,
"action_url": message.request.user.get_autologin_url(
newest_fx_message.get_absolute_short_url()
),
},
)

helper.import_frontex_case(message)
except Exception as e:
capture_exception(e)
send_request_user_email(
fximport_failed_mail,
message.request,
subject=_("Message import failed"),
context={
"message": message,
"foirequest": message.request,
"user": message.request.user,
"action_url": message.request.user.get_autologin_url(
message.get_absolute_short_url()
),
},
)
25 changes: 8 additions & 17 deletions fragdenstaat_de/fds_fximport/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
from django.utils.translation import gettext_lazy as _
from django.views.decorators.http import require_POST

from sentry_sdk import capture_exception

from froide.foirequest.models import FoiMessage
from froide.foirequest.views.request_actions import allow_write_foirequest

from .helper import import_frontex_case, is_frontex_msg
from .helper import is_frontex_msg
from .tasks import import_case


@require_POST
Expand All @@ -22,19 +21,11 @@ def frontex_pad_import(request, foirequest, message_id):
)
return redirect(foirequest)

try:
import_frontex_case(message)
messages.add_message(
request,
messages.SUCCESS,
_("Import successful."),
)
except Exception as e:
messages.add_message(
request,
messages.ERROR,
_("Something went wrong. Please try again later."),
)
capture_exception(e)
import_case.delay(message.pk)
messages.add_message(
request,
messages.SUCCESS,
_("Import stated. You will receive an email once it finishes."),
)

return redirect(foirequest)

0 comments on commit fb2e428

Please sign in to comment.