From ab6584d889b92c5aa707f8b395faa93b49407e05 Mon Sep 17 00:00:00 2001 From: "Dr. Shubham Dipt" Date: Thu, 20 Mar 2025 12:49:20 +0100 Subject: [PATCH] Loading of Test data fails due to unique primary key constraint in Allowance (#613) * Ignore Pycharm IDE directory * init will get executed even if there are no tables, hence migrations cannot be applies at the first step * Separate loanaccount test data load because primary key conflicts due to post_save signal * Avoid multiple DB save calls --- .gitignore | 3 +++ base/views.py | 23 ++++++++++---------- horilla_backup/migrations/__init__.py | 19 +++++++++------- load_data/payroll_data.json | 27 ----------------------- load_data/payroll_loanaccount_data.json | 29 +++++++++++++++++++++++++ payroll/signals.py | 3 +-- 6 files changed, 56 insertions(+), 48 deletions(-) create mode 100644 load_data/payroll_loanaccount_data.json diff --git a/.gitignore b/.gitignore index ea03e0209..928641fa2 100755 --- a/.gitignore +++ b/.gitignore @@ -148,3 +148,6 @@ whoosh_index/ #VSCode .vscode/ + +#PyCharm +.idea/ diff --git a/base/views.py b/base/views.py index 1a8d36c84..c873739e7 100644 --- a/base/views.py +++ b/base/views.py @@ -236,21 +236,22 @@ def load_demo_database(request): "base_data.json", "work_info_data.json", ] - optional_apps = { - "attendance": "attendance_data.json", - "leave": "leave_data.json", - "asset": "asset_data.json", - "recruitment": "recruitment_data.json", - "onboarding": "onboarding_data.json", - "offboarding": "offboarding_data.json", - "pms": "pms_data.json", - "payroll": "payroll_data.json", - } + optional_apps = [ + ("attendance", "attendance_data.json"), + ("leave", "leave_data.json"), + ("asset", "asset_data.json"), + ("recruitment", "recruitment_data.json"), + ("onboarding", "onboarding_data.json"), + ("offboarding", "offboarding_data.json"), + ("pms", "pms_data.json"), + ("payroll", "payroll_data.json"), + ("payroll", "payroll_loanaccount_data.json"), + ] # Add data files for installed apps data_files += [ file - for app, file in optional_apps.items() + for app, file in optional_apps if apps.is_installed(app) ] diff --git a/horilla_backup/migrations/__init__.py b/horilla_backup/migrations/__init__.py index 4898c42f8..1a186e937 100644 --- a/horilla_backup/migrations/__init__.py +++ b/horilla_backup/migrations/__init__.py @@ -4,14 +4,17 @@ import atexit def shutdown_function(): from horilla_backup.models import GoogleDriveBackup, LocalBackup - if GoogleDriveBackup.objects.exists(): - google_drive_backup = GoogleDriveBackup.objects.first() - google_drive_backup.active = False - google_drive_backup.save() - if LocalBackup.objects.exists(): - local_backup = LocalBackup.objects.first() - local_backup.active = False - local_backup.save() + try: + if GoogleDriveBackup.objects.exists(): + google_drive_backup = GoogleDriveBackup.objects.first() + google_drive_backup.active = False + google_drive_backup.save() + if LocalBackup.objects.exists(): + local_backup = LocalBackup.objects.first() + local_backup.active = False + local_backup.save() + except: + pass try: diff --git a/load_data/payroll_data.json b/load_data/payroll_data.json index a5f7a4bdb..3c5e64b9a 100644 --- a/load_data/payroll_data.json +++ b/load_data/payroll_data.json @@ -1592,32 +1592,5 @@ "status": "requested", "description": "Software license for Sublime Text (1-year subscription for team development)." } - }, - { - "model": "payroll.loanaccount", - "pk": 1, - "fields": { - "created_at": "2024-12-12T09:45:26.097Z", - "created_by": 1, - "modified_by": 1, - "is_active": true, - "type": "loan", - "title": "Medical Loan", - "employee_id": 18, - "loan_amount": 60000.0, - "provided_date": "2024-12-01", - "allowance_id": 5, - "description": "Loan for parents medical case", - "is_fixed": true, - "rate": 0.0, - "installment_amount": 5000.0, - "installments": 12, - "installment_start_date": "2025-01-01", - "apply_on": "end_of_month", - "settled": false, - "settled_date": null, - "asset_id": null, - "deduction_ids": [22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33] - } } ] diff --git a/load_data/payroll_loanaccount_data.json b/load_data/payroll_loanaccount_data.json new file mode 100644 index 000000000..a5eeda152 --- /dev/null +++ b/load_data/payroll_loanaccount_data.json @@ -0,0 +1,29 @@ +[ + { + "model": "payroll.loanaccount", + "pk": 1, + "fields": { + "created_at": "2024-12-12T09:45:26.097Z", + "created_by": 1, + "modified_by": 1, + "is_active": true, + "type": "loan", + "title": "Medical Loan", + "employee_id": 18, + "loan_amount": 60000.0, + "provided_date": "2024-12-01", + "allowance_id": 5, + "description": "Loan for parents medical case", + "is_fixed": true, + "rate": 0.0, + "installment_amount": 5000.0, + "installments": 12, + "installment_start_date": "2025-01-01", + "apply_on": "end_of_month", + "settled": false, + "settled_date": null, + "asset_id": null, + "deduction_ids": [1,2,3] + } + } +] diff --git a/payroll/signals.py b/payroll/signals.py index 42b37a95d..a645c8740 100644 --- a/payroll/signals.py +++ b/payroll/signals.py @@ -52,10 +52,9 @@ def create_installments(sender, instance, created, **kwargs): loan.is_fixed = True loan.one_time_date = instance.provided_date loan.is_loan = True - loan.save() loan.include_active_employees = False - loan.specific_employees.add(instance.employee_id) loan.save() + loan.specific_employees.add(instance.employee_id) instance.allowance_id = loan super(LoanAccount, instance).save() else: