Commit ea36dc1d7c473b36b995a578e2e56b3011d11202

Authored by Etienne Pallier
1 parent 059447d3
Exists in dev

2e petit changement pour lisibilité (continue au lieu de else)

Showing 1 changed file with 69 additions and 64 deletions   Show diff stats
src/core/pyros_django/routine_manager/functions.py
... ... @@ -145,7 +145,11 @@ def check_sequence_file_validity_and_save(yaml_content,request):
145 145 else:
146 146 result["errors"].append(f"The number of albums doesn't correspond to the chosen layout")
147 147  
148   - # For each Album a
  148 + # optim possible ?
  149 + #[ process_plans(a["Album"].get("Plans")) for a in albums_from_file ]
  150 + # Puis écrire la fonction process_plans()
  151 +
  152 + # For each Album a (in file)
149 153 # Tu itères 2 fois sur albums_from_file, y aurait pas moyen d'iterer 1 seule fois dessus ?
150 154 #for album in yaml_content["sequence"]["ALBUMS"]:
151 155 for album in albums_from_file:
... ... @@ -157,13 +161,14 @@ def check_sequence_file_validity_and_save(yaml_content,request):
157 161 # pour eviter le else (plus lisible)
158 162 continue
159 163  
160   - # For each plan p in album a
  164 + # For each plan p (in album a)
161 165 for plan in plans:
162 166 new_plan_object = Plan.objects.create(album=Album.objects.get(name=album["name"],sequence=seq),complete=True)
163 167 new_plan_object.config_attributes = {}
164 168 plan = plan["Plan"]
165 169 config_attributes = {}
166 170 plan_form = PlanForm(data_from_config=config.getEditableAttributesOfChannel(config.unit_name,list(config.get_channels(config.unit_name).keys())[0]),edited_plan=None)
  171 + # Process each plan field
167 172 for field in plan_form.fields:
168 173 plan_field = plan[field]
169 174 '''
... ... @@ -174,74 +179,74 @@ def check_sequence_file_validity_and_save(yaml_content,request):
174 179 min_value = max_value = value_type = None
175 180 if field not in plan.keys():
176 181 result["errors"].append(f"Missing field : '{field}' for plan {plans.index(plan)}")
177   - else:
178   - # TODO : ajouter max_value, min_value, suppression plan et album si invalides
179   - if not is_simplified:
180   - if plan_field.get("value_type"):
181   - value_type = plan_field["value_type"]
182   - if type(plan_field["value"]) == str and ast.literal_eval(plan_field["value"]) != value_type:
183   - result["errors"].append(f"Field {field} value doesn't correspond to the assigned type (type required : {value_type})")
184   - if plan_field.get("min_value"):
  182 + continue
  183 + # TODO : ajouter max_value, min_value, suppression plan et album si invalides
  184 + if not is_simplified:
  185 + if plan_field.get("value_type"):
  186 + value_type = plan_field["value_type"]
  187 + if type(plan_field["value"]) == str and ast.literal_eval(plan_field["value"]) != value_type:
  188 + result["errors"].append(f"Field {field} value doesn't correspond to the assigned type (type required : {value_type})")
  189 + if plan_field.get("min_value"):
  190 + min_value = plan_field["min_value"]
  191 + if type(min_value) == str:
  192 + min_value = ast.literal_eval(min_value)
  193 + '''
  194 + if type(plan_field["min_value"]) == str:
  195 + min_value = ast.literal_eval(plan_field["min_value"])
  196 + else:
185 197 min_value = plan_field["min_value"]
186   - if type(min_value) == str:
187   - min_value = ast.literal_eval(min_value)
188   - '''
189   - if type(plan_field["min_value"]) == str:
190   - min_value = ast.literal_eval(plan_field["min_value"])
191   - else:
192   - min_value = plan_field["min_value"]
193   - '''
194   - if plan_field.get("max_value"):
195   - max_value = plan_field["max_value"]
196   - if type(max_value) == str:
197   - max_value = ast.literal_eval(max_value)
198   - '''
199   - if type(plan_field.get("max_value")) == str:
200   - max_value = ast.literal_eval(plan_field["max_value"])
201   - else:
202   - max_value = plan_field["max_value"]
203   - '''
204   - if field == "nb_images":
205   - new_plan_object.__dict__[field] = plan_field if is_simplified else plan_field["value"]
206 198 '''
207   - if is_simplified:
208   - new_plan_object.__dict__[field] = plan_field
  199 + if plan_field.get("max_value"):
  200 + max_value = plan_field["max_value"]
  201 + if type(max_value) == str:
  202 + max_value = ast.literal_eval(max_value)
  203 + '''
  204 + if type(plan_field.get("max_value")) == str:
  205 + max_value = ast.literal_eval(plan_field["max_value"])
209 206 else:
210   - new_plan_object.__dict__[field] = plan_field["value"]
  207 + max_value = plan_field["max_value"]
211 208 '''
  209 + if field == "nb_images":
  210 + new_plan_object.__dict__[field] = plan_field if is_simplified else plan_field["value"]
  211 + '''
  212 + if is_simplified:
  213 + new_plan_object.__dict__[field] = plan_field
212 214 else:
213   - # shortcut possible ?
214   - #new_plan_object_field = new_plan_object.config_attributes[field]
215   - if is_simplified:
216   - new_plan_object.config_attributes[field] = plan_field
  215 + new_plan_object.__dict__[field] = plan_field["value"]
  216 + '''
  217 + else:
  218 + # shortcut possible ?
  219 + #new_plan_object_field = new_plan_object.config_attributes[field]
  220 + if is_simplified:
  221 + new_plan_object.config_attributes[field] = plan_field
  222 + else:
  223 + if plan_field.get("values"):
  224 + index_value = plan_field["value"]
  225 + values = plan_field["values"]
  226 + if index_value < 0 or index_value > len(plan_field["values"]):
  227 + result["errors"].append(f"Value of Plan field '{field}' isn't valid, index out of bounds ({index_value} > {len(values)})")
  228 + index_value = 0
  229 + value = plan_field["values"][index_value]
  230 + try:
  231 + # linked values
  232 + splitted_values = value.split(";")
  233 + config_attributes[field] = {}
  234 + for splitted_value in splitted_values:
  235 + subkey,subvalue = splitted_value.split(":")
  236 + config_attributes[field][subkey] = ast.literal_eval(subvalue)
  237 + # vaudrait mieux préciser l'exception ici
  238 + except:
  239 + # Do nothing, normal string
  240 + config_attributes[field] = ast.literal_eval(value)
  241 + new_plan_object.config_attributes[field] = config_attributes[field]
217 242 else:
218   - if plan_field.get("values"):
219   - index_value = plan_field["value"]
220   - values = plan_field["values"]
221   - if index_value < 0 or index_value > len(plan_field["values"]):
222   - result["errors"].append(f"Value of Plan field '{field}' isn't valid, index out of bounds ({index_value} > {len(values)})")
223   - index_value = 0
224   - value = plan_field["values"][index_value]
225   - try:
226   - # linked values
227   - splitted_values = value.split(";")
228   - config_attributes[field] = {}
229   - for splitted_value in splitted_values:
230   - subkey,subvalue = splitted_value.split(":")
231   - config_attributes[field][subkey] = ast.literal_eval(subvalue)
232   - # vaudrait mieux préciser l'exception ici
233   - except:
234   - # Do nothing, normal string
235   - config_attributes[field] = ast.literal_eval(value)
236   - new_plan_object.config_attributes[field] = config_attributes[field]
237   - else:
238   - if max_value and min_value:
239   - if plan_field["value"] > max_value:
240   - result["errors"].append(f"Plan field {field} doesn't respect max value")
241   - if plan_field["value"] < min_value:
242   - result["errors"].append(f"Plan field {field} doesn't respect min value")
243   - new_plan_object.config_attributes[field] = plan_field["value"]
244   -
  243 + if max_value and min_value:
  244 + if plan_field["value"] > max_value:
  245 + result["errors"].append(f"Plan field {field} doesn't respect max value")
  246 + if plan_field["value"] < min_value:
  247 + result["errors"].append(f"Plan field {field} doesn't respect min value")
  248 + new_plan_object.config_attributes[field] = plan_field["value"]
  249 + # end foreach plan field
245 250 new_plan_object.save()
246 251 # end foreach plan
247 252 # end foreach album
... ...