Skip to content

Commit

Permalink
add condition before adding dep fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Hrishabh17 committed Jul 15, 2024
1 parent 7476ba5 commit 1e91694
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions apps/workspaces/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,21 +334,23 @@ def validate(self, data):
import_settings = ImportSetting.objects.filter(workspace_id=workspace_id).first()

if import_settings:
old_code_pref_list = import_settings.import_code_fields
new_code_pref_list = data.get('import_settings').get('import_code_fields', [])
old_code_pref_list = set(import_settings.import_code_fields)
new_code_pref_list = set(data.get('import_settings', {}).get('import_code_fields', []))

""" If the JOB is in the code_fields then we also add Dep fields"""
if 'JOB' in new_code_pref_list and data.get('dependent_field_settings'):
new_code_pref_list += ['COST_CODE', 'COST_CATEGORY']
mapping_settings = data.get('mapping_settings', [])
for setting in mapping_settings:
if setting['destination_field'] == 'JOB':
if setting['source_field'] == 'PROJECT':
new_code_pref_list.update(['COST_CODE', 'COST_CATEGORY'])
else:
old_code_pref_list.difference_update(['COST_CODE', 'COST_CATEGORY'])
break

new_code_pref_list = list(set(new_code_pref_list))
if not old_code_pref_list.issubset(new_code_pref_list):
raise serializers.ValidationError('Cannot remove the attribute from the preference list once imported')

for item in old_code_pref_list:
"""
checking if the old fields are not removed from the import_code_fields
"""
if item not in new_code_pref_list:
raise serializers.ValidationError('Cannot remove the attribute from the preference list once imported')
data.get('import_settings')['import_code_fields'] = list(new_code_pref_list)

return data

Expand Down

0 comments on commit 1e91694

Please sign in to comment.