@@ -107,6 +107,7 @@ def get_datetime(datetime_string):
107107 return None
108108 return date_parser .parse (datetime_string )
109109
110+
110111def get_status_message (positions_messages , deployments_messages , positions_warnings , deployments_warnings ):
111112 msg = ''
112113 msg += 'Summary of Open Positions Import:\n \n '
@@ -125,26 +126,21 @@ def get_status_message(positions_messages, deployments_messages, positions_warni
125126 msg += '\n \n '
126127 return msg
127128
128- def add_tags_to_obj (obj , tags ):
129- # We clear all tags first, and then re-add them
130- tag_molnix_ids = [t ['id' ] for t in tags ]
131- obj .molnix_tags .clear ()
132- for molnix_id in tag_molnix_ids :
133- try :
134- t = MolnixTag .objects .get (molnix_id = molnix_id )
135- except :
136- logger .error ('ERROR - tag not found: %d' % molnix_id )
137- continue
138- obj .molnix_tags .add (t )
139- obj .save ()
140129
130+ def add_tags_to_obj (obj , tags ):
131+ _ids = [int (t ['id' ]) for t in tags ]
132+ tags = list ( # Fetch all at once
133+ MolnixTag .objects .filter (molnix_id__in = _ids )
134+ )
135+ if len (tags ) != len (_ids ): # Show warning if all tags are not available
136+ missing_tag_ids = list (set (_ids ) - set ([tag .id for tag in tags ]))
137+ logger .warning (f'Missing _ids: { missing_tag_ids } ' )
138+ # or ^^^^^^^ logger.error if we need to add molnix tags manually.
139+ obj .molnix_tags .set (tags ) # Add new ones, remove old ones
141140
142141
143142def sync_deployments (molnix_deployments , molnix_api , countries ):
144- #import json
145- #print(json.dumps(molnix_deployments, indent=2))
146143 molnix_ids = [d ['id' ] for d in molnix_deployments ]
147-
148144 warnings = []
149145 messages = []
150146 successful_creates = 0
0 commit comments