From 0937b4c1223b73e8d7186cb50d54e27db579e62f Mon Sep 17 00:00:00 2001 From: Horilla Date: Mon, 12 May 2025 17:01:23 +0530 Subject: [PATCH] [ADD] HORILLA AUTOMATIONS: Refresh manual automation action --- horilla_automations/apps.py | 3 ++- horilla_automations/signals.py | 10 ++++++++-- horilla_automations/urls.py | 5 +++++ horilla_automations/views/cbvs.py | 21 +++++++++++---------- horilla_automations/views/views.py | 16 +++++++++++++++- 5 files changed, 41 insertions(+), 14 deletions(-) diff --git a/horilla_automations/apps.py b/horilla_automations/apps.py index 08c2b551c..86c627862 100644 --- a/horilla_automations/apps.py +++ b/horilla_automations/apps.py @@ -41,7 +41,8 @@ class HorillaAutomationConfig(AppConfig): MODEL_CHOICES = list(set(MODEL_CHOICES)) try: start_automation() - except: + except Exception as e: + print(e) """ Migrations are not affected yet """ diff --git a/horilla_automations/signals.py b/horilla_automations/signals.py index 39044fa8a..d0f98953c 100644 --- a/horilla_automations/signals.py +++ b/horilla_automations/signals.py @@ -37,6 +37,7 @@ setattr(QuerySet, "from_list", from_list) SIGNAL_HANDLERS = [] INSTANCE_HANDLERS = [] +REFRESH_METHODS = {} def start_automation(): @@ -74,6 +75,8 @@ def start_automation(): post_bulk_update.disconnect(handler, sender=handler.model_class) SIGNAL_HANDLERS.clear() + REFRESH_METHODS["clear_connection"] = clear_connection + def create_post_bulk_update_handler(automation, model_class, query_strings): def post_bulk_update_handler(sender, queryset, *args, **kwargs): def _bulk_update_thread_handler( @@ -185,6 +188,8 @@ def start_automation(): dynamic_signal_handler, sender=dynamic_signal_handler.model_class ) + REFRESH_METHODS["start_connection"] = start_connection + def create_pre_bulk_update_handler(automation, model_class): def pre_bulk_update_handler(sender, queryset, *args, **kwargs): request = getattr(_thread_locals, "request", None) @@ -479,11 +484,9 @@ def send_mail(request, automation, instance): f"Automation {automation.title} is triggered by {request.user.employee_get}" ) except Exception as e: - print("ERRRRRR") logger.error(e) def _send_notification(text): - print(text) notify.send( sender, recipient=user_ids, @@ -506,3 +509,6 @@ def send_mail(request, automation, instance): target=lambda: _send_notification(plain_text), ) thread.start() + logger.info( + f"Automation Triggered | {automation.get_delivary_channel_display()} | {automation}" + ) diff --git a/horilla_automations/urls.py b/horilla_automations/urls.py index 4f4c8a97b..66c9aa700 100644 --- a/horilla_automations/urls.py +++ b/horilla_automations/urls.py @@ -52,4 +52,9 @@ urlpatterns = [ cbvs.LoadAutomationsView.as_view(), name="load-automations", ), + path( + "refresh-automations", + views.refresh_automations, + name="refresh-automations", + ), ] diff --git a/horilla_automations/views/cbvs.py b/horilla_automations/views/cbvs.py index 4d233d6f5..bbaa5ac1c 100644 --- a/horilla_automations/views/cbvs.py +++ b/horilla_automations/views/cbvs.py @@ -76,16 +76,17 @@ class AutomationNavView(views.HorillaNavView): } ) - self.actions.append( - { - "action": "Refresh Automations", - "attrs": f""" - hx-get="{reverse_lazy('mail-automations-list-view')}" - hx-target="#listContainer" - class="oh-btn oh-btn--light-bkg" - """, - } - ) + if self.request.user.has_perm("horilla_automation.add_mailautomation"): + self.actions.append( + { + "action": "Refresh Automations", + "attrs": f""" + hx-get="{reverse_lazy('refresh-automations')}" + hx-target="#reloadMessages" + class="oh-btn oh-btn--light-bkg" + """, + } + ) nav_title = _trans("Automations") search_url = reverse_lazy("mail-automations-list-view") diff --git a/horilla_automations/views/views.py b/horilla_automations/views/views.py index 3bb4c5fd8..6e7ec3384 100644 --- a/horilla_automations/views/views.py +++ b/horilla_automations/views/views.py @@ -12,6 +12,8 @@ from horilla.decorators import login_required, permission_required from horilla_automations.methods.methods import generate_choices from horilla_automations.methods.serialize import serialize_form from horilla_automations.models import MailAutomation +from horilla_automations.signals import REFRESH_METHODS +from horilla_views.generic.cbv.views import HorillaFormView @login_required @@ -43,7 +45,7 @@ def get_to_field(request): @login_required -@permission_required("horilla_automation") +@permission_required("horilla_automation.delete_mailautomation") def delete_automation(request, pk): """ Automation delete view @@ -55,3 +57,15 @@ def delete_automation(request, pk): print(e) messages.error(request, "Something went wrong") return redirect(reverse("mail-automations")) + + +@login_required +@permission_required("horilla_automation.add_mailautomation") +def refresh_automations(request): + """ + Method to refresh automation signals + """ + REFRESH_METHODS["clear_connection"]() + REFRESH_METHODS["start_connection"]() + messages.success(request, "Automations refreshed") + return HorillaFormView.HttpResponse()