diff --git a/horilla_automations/apps.py b/horilla_automations/apps.py index ab9130e81..ab760462b 100644 --- a/horilla_automations/apps.py +++ b/horilla_automations/apps.py @@ -1,56 +1,50 @@ -from django.apps import AppConfig +""" +App configuration for the Horilla Automations app. +Initializes model choices and starts automation when the server runs. +""" -from horilla_automations.signals import start_automation +import os +import sys + +from django.apps import AppConfig class HorillaAutomationConfig(AppConfig): + """Configuration class for the Horilla Automations Django app.""" + default_auto_field = "django.db.models.BigAutoField" name = "horilla_automations" - def ready(self) -> None: - ready = super().ready() - try: + def ready(self): + """Run initialization tasks when the app is ready.""" + from base.templatetags.horillafilters import app_installed + from employee.models import Employee + from horilla_automations.methods.methods import get_related_models + from horilla_automations.models import MODEL_CHOICES as model_choices - from base.templatetags.horillafilters import app_installed - from employee.models import Employee - from horilla_automations.methods.methods import get_related_models - from horilla_automations.models import MODEL_CHOICES + # Build MODEL_CHOICES + models = [Employee] + if app_installed("recruitment"): + from recruitment.models import Candidate - recruitment_installed = False - if app_installed("recruitment"): - recruitment_installed = True + models.append(Candidate) - models = [Employee] - if recruitment_installed: - from recruitment.models import Candidate + for main_model in models: + for model in get_related_models(main_model): + model_choices.append( + (f"{model.__module__}.{model.__name__}", model.__name__) + ) - models.append(Candidate) + model_choices.append(("employee.models.Employee", "Employee")) + model_choices.append(("pms.models.EmployeeKeyResult", "Employee Key Results")) + model_choices[:] = list(set(model_choices)) # Update in-place - main_models = models - for main_model in main_models: - related_models = get_related_models(main_model) + # Only start automation when running the server + if ( + len(sys.argv) >= 2 + and sys.argv[1] == "runserver" + and os.environ.get("RUN_MAIN") == "true" + ): + from horilla_automations.signals import start_automation - for model in related_models: - path = f"{model.__module__}.{model.__name__}" - MODEL_CHOICES.append((path, model.__name__)) - MODEL_CHOICES.append(("employee.models.Employee", "Employee")) - MODEL_CHOICES.append( - ("pms.models.EmployeeKeyResult", "Employee Key Results"), - ) - MODEL_CHOICES.append( - ("pms.models.Comment", "Key Result Comment"), - ) - - MODEL_CHOICES = list(set(MODEL_CHOICES)) - try: - start_automation() - except Exception as e: - print(e) - """ - Migrations are not affected yet - """ - except: - """ - Models not ready yet - """ - return ready + start_automation() diff --git a/horilla_automations/forms.py b/horilla_automations/forms.py index 60eb90476..f7ed7790c 100644 --- a/horilla_automations/forms.py +++ b/horilla_automations/forms.py @@ -84,13 +84,16 @@ class AutomationForm(ModelForm): # --- Field: mail_template --- self.fields["mail_template"].empty_label = "----------" - # --- Instance condition fields --- - if self.instance.pk: - self.fields["condition"].initial = self.instance.condition_html - self.fields["condition_html"].initial = self.instance.condition_html - self.fields["condition_querystring"].initial = ( - self.instance.condition_querystring - ) + # --- Field: condition fields --- + self.fields["condition"].initial = getattr( + self.instance, "condition", None + ) or self.data.get("condition") + self.fields["condition_html"].initial = getattr( + self.instance, "condition_html", None + ) or self.data.get("condition_html") + self.fields["condition_querystring"].initial = getattr( + self.instance, "condition", None + ) or self.data.get("condition_html") # --- Apply option template name for all select fields --- for field in self.fields.values(): diff --git a/horilla_automations/static/automation/automation.js b/horilla_automations/static/automation/automation.js index 2ca862799..c122883d2 100644 --- a/horilla_automations/static/automation/automation.js +++ b/horilla_automations/static/automation/automation.js @@ -1,48 +1,48 @@ function getToMail(element) { - model = element.val(); - $.ajax({ - type: "get", - url: "/get-to-mail-field", - data: { - model: model, - }, - success: function (response) { - $(".dynamic-condition-row").remove(); - select = $("#id_mail_to"); - select.html(""); - detailSelect = $("#id_mail_details"); - detailSelect.html(""); - mailTo = response.choices; - mailDetail = response.mail_details_choice; + model = element.val(); + $.ajax({ + type: "get", + url: "/get-to-mail-field", + data: { + model: model, + }, + success: function (response) { + $(".dynamic-condition-row").remove(); + select = $("#id_mail_to"); + select.html(""); + detailSelect = $("#id_mail_details"); + detailSelect.html(""); + mailTo = response.choices; + mailDetail = response.mail_details_choice; - for (let option = 0; option < mailTo.length; option++) { - const element = mailTo[option]; + for (let option = 0; option < mailTo.length; option++) { + const element = mailTo[option]; - var selected = option === 0; // Set the first option as selected - var newOption = new Option(element[1], element[0], selected, selected); - select.append(newOption); - } - for (let option = 0; option < mailDetail.length; option++) { - const element = mailDetail[option]; + var selected = option === 0; // Set the first option as selected + var newOption = new Option(element[1], element[0], selected, selected); + select.append(newOption); + } + for (let option = 0; option < mailDetail.length; option++) { + const element = mailDetail[option]; - var selected = option === 0; // Set the first option as selected - var newOption = new Option(element[1], element[0], selected, selected); - detailSelect.append(newOption); - } - select.trigger("change"); - detailSelect.trigger("change"); + var selected = option === 0; // Set the first option as selected + var newOption = new Option(element[1], element[0], selected, selected); + detailSelect.append(newOption); + } + select.trigger("change"); + detailSelect.trigger("change"); - table = $("#multipleConditionTable"); - $("#multipleConditionTable select").select2("destroy"); + table = $("#multipleConditionTable"); + $("#multipleConditionTable select").select2("destroy"); - totalRows = "C" + (table.find(".dynamic-condition-row").length + 1); + totalRows = "C" + (table.find(".dynamic-condition-row").length + 1); - fieldsChoices = []; - $.each(response.serialized_form, function (indexInArray, valueOfElement) { - fieldsChoices.push([valueOfElement["name"], valueOfElement["label"]]); - }); - selectField = populateSelect(fieldsChoices, response); - tr = ` + fieldsChoices = []; + $.each(response.serialized_form, function (indexInArray, valueOfElement) { + fieldsChoices.push([valueOfElement["name"], valueOfElement["label"]]); + }); + selectField = populateSelect(fieldsChoices, response); + tr = `