From ea36dc1d7c473b36b995a578e2e56b3011d11202 Mon Sep 17 00:00:00 2001 From: Etienne Pallier Date: Wed, 8 Mar 2023 12:09:00 +0100 Subject: [PATCH] 2e petit changement pour lisibilité (continue au lieu de else) --- src/core/pyros_django/routine_manager/functions.py | 133 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------------------------- 1 file changed, 69 insertions(+), 64 deletions(-) diff --git a/src/core/pyros_django/routine_manager/functions.py b/src/core/pyros_django/routine_manager/functions.py index 7922212..7cc6caf 100644 --- a/src/core/pyros_django/routine_manager/functions.py +++ b/src/core/pyros_django/routine_manager/functions.py @@ -145,7 +145,11 @@ def check_sequence_file_validity_and_save(yaml_content,request): else: result["errors"].append(f"The number of albums doesn't correspond to the chosen layout") - # For each Album a + # optim possible ? + #[ process_plans(a["Album"].get("Plans")) for a in albums_from_file ] + # Puis écrire la fonction process_plans() + + # For each Album a (in file) # Tu itères 2 fois sur albums_from_file, y aurait pas moyen d'iterer 1 seule fois dessus ? #for album in yaml_content["sequence"]["ALBUMS"]: for album in albums_from_file: @@ -157,13 +161,14 @@ def check_sequence_file_validity_and_save(yaml_content,request): # pour eviter le else (plus lisible) continue - # For each plan p in album a + # For each plan p (in album a) for plan in plans: new_plan_object = Plan.objects.create(album=Album.objects.get(name=album["name"],sequence=seq),complete=True) new_plan_object.config_attributes = {} plan = plan["Plan"] config_attributes = {} plan_form = PlanForm(data_from_config=config.getEditableAttributesOfChannel(config.unit_name,list(config.get_channels(config.unit_name).keys())[0]),edited_plan=None) + # Process each plan field for field in plan_form.fields: plan_field = plan[field] ''' @@ -174,74 +179,74 @@ def check_sequence_file_validity_and_save(yaml_content,request): min_value = max_value = value_type = None if field not in plan.keys(): result["errors"].append(f"Missing field : '{field}' for plan {plans.index(plan)}") - else: - # TODO : ajouter max_value, min_value, suppression plan et album si invalides - if not is_simplified: - if plan_field.get("value_type"): - value_type = plan_field["value_type"] - if type(plan_field["value"]) == str and ast.literal_eval(plan_field["value"]) != value_type: - result["errors"].append(f"Field {field} value doesn't correspond to the assigned type (type required : {value_type})") - if plan_field.get("min_value"): + continue + # TODO : ajouter max_value, min_value, suppression plan et album si invalides + if not is_simplified: + if plan_field.get("value_type"): + value_type = plan_field["value_type"] + if type(plan_field["value"]) == str and ast.literal_eval(plan_field["value"]) != value_type: + result["errors"].append(f"Field {field} value doesn't correspond to the assigned type (type required : {value_type})") + if plan_field.get("min_value"): + min_value = plan_field["min_value"] + if type(min_value) == str: + min_value = ast.literal_eval(min_value) + ''' + if type(plan_field["min_value"]) == str: + min_value = ast.literal_eval(plan_field["min_value"]) + else: min_value = plan_field["min_value"] - if type(min_value) == str: - min_value = ast.literal_eval(min_value) - ''' - if type(plan_field["min_value"]) == str: - min_value = ast.literal_eval(plan_field["min_value"]) - else: - min_value = plan_field["min_value"] - ''' - if plan_field.get("max_value"): - max_value = plan_field["max_value"] - if type(max_value) == str: - max_value = ast.literal_eval(max_value) - ''' - if type(plan_field.get("max_value")) == str: - max_value = ast.literal_eval(plan_field["max_value"]) - else: - max_value = plan_field["max_value"] - ''' - if field == "nb_images": - new_plan_object.__dict__[field] = plan_field if is_simplified else plan_field["value"] ''' - if is_simplified: - new_plan_object.__dict__[field] = plan_field + if plan_field.get("max_value"): + max_value = plan_field["max_value"] + if type(max_value) == str: + max_value = ast.literal_eval(max_value) + ''' + if type(plan_field.get("max_value")) == str: + max_value = ast.literal_eval(plan_field["max_value"]) else: - new_plan_object.__dict__[field] = plan_field["value"] + max_value = plan_field["max_value"] ''' + if field == "nb_images": + new_plan_object.__dict__[field] = plan_field if is_simplified else plan_field["value"] + ''' + if is_simplified: + new_plan_object.__dict__[field] = plan_field else: - # shortcut possible ? - #new_plan_object_field = new_plan_object.config_attributes[field] - if is_simplified: - new_plan_object.config_attributes[field] = plan_field + new_plan_object.__dict__[field] = plan_field["value"] + ''' + else: + # shortcut possible ? + #new_plan_object_field = new_plan_object.config_attributes[field] + if is_simplified: + new_plan_object.config_attributes[field] = plan_field + else: + if plan_field.get("values"): + index_value = plan_field["value"] + values = plan_field["values"] + if index_value < 0 or index_value > len(plan_field["values"]): + result["errors"].append(f"Value of Plan field '{field}' isn't valid, index out of bounds ({index_value} > {len(values)})") + index_value = 0 + value = plan_field["values"][index_value] + try: + # linked values + splitted_values = value.split(";") + config_attributes[field] = {} + for splitted_value in splitted_values: + subkey,subvalue = splitted_value.split(":") + config_attributes[field][subkey] = ast.literal_eval(subvalue) + # vaudrait mieux préciser l'exception ici + except: + # Do nothing, normal string + config_attributes[field] = ast.literal_eval(value) + new_plan_object.config_attributes[field] = config_attributes[field] else: - if plan_field.get("values"): - index_value = plan_field["value"] - values = plan_field["values"] - if index_value < 0 or index_value > len(plan_field["values"]): - result["errors"].append(f"Value of Plan field '{field}' isn't valid, index out of bounds ({index_value} > {len(values)})") - index_value = 0 - value = plan_field["values"][index_value] - try: - # linked values - splitted_values = value.split(";") - config_attributes[field] = {} - for splitted_value in splitted_values: - subkey,subvalue = splitted_value.split(":") - config_attributes[field][subkey] = ast.literal_eval(subvalue) - # vaudrait mieux préciser l'exception ici - except: - # Do nothing, normal string - config_attributes[field] = ast.literal_eval(value) - new_plan_object.config_attributes[field] = config_attributes[field] - else: - if max_value and min_value: - if plan_field["value"] > max_value: - result["errors"].append(f"Plan field {field} doesn't respect max value") - if plan_field["value"] < min_value: - result["errors"].append(f"Plan field {field} doesn't respect min value") - new_plan_object.config_attributes[field] = plan_field["value"] - + if max_value and min_value: + if plan_field["value"] > max_value: + result["errors"].append(f"Plan field {field} doesn't respect max value") + if plan_field["value"] < min_value: + result["errors"].append(f"Plan field {field} doesn't respect min value") + new_plan_object.config_attributes[field] = plan_field["value"] + # end foreach plan field new_plan_object.save() # end foreach plan # end foreach album -- libgit2 0.21.2