Commit 1e252185f5758186bf6e05ab66364f5e5f22c58a

Authored by Etienne Pallier
1 parent 259a04cb
Exists in dev

petites optimisations et clarifications du code et commentaires

Showing 1 changed file with 114 additions and 34 deletions   Show diff stats
src/core/pyros_django/routine_manager/functions.py
@@ -13,42 +13,91 @@ from src.core.pyros_django.obsconfig.obsconfig_class import OBSConfig @@ -13,42 +13,91 @@ from src.core.pyros_django.obsconfig.obsconfig_class import OBSConfig
13 13
14 14
15 def check_sequence_file_validity_and_save(yaml_content,request): 15 def check_sequence_file_validity_and_save(yaml_content,request):
  16 + ''' Create a sequence in DB from the uploaded sequence (yaml_content) '''
  17 +
16 is_simplified = yaml_content.get("simplified", False) 18 is_simplified = yaml_content.get("simplified", False)
  19 + # renommer : user_sp
17 sp_of_user = request.user.get_scientific_program() 20 sp_of_user = request.user.get_scientific_program()
18 sp_list = ScientificProgram.objects.observable_programs().filter(id__in=sp_of_user) 21 sp_list = ScientificProgram.objects.observable_programs().filter(id__in=sp_of_user)
  22 +
  23 + # Create a sequence seq object (from yaml_content) to be saved in DB
19 seq = Sequence.objects.create() 24 seq = Sequence.objects.create()
20 seq.pyros_user = PyrosUser.objects.get(id=request.user.id) 25 seq.pyros_user = PyrosUser.objects.get(id=request.user.id)
  26 +
  27 + # Get the unit config
21 unit_name = os.environ["unit_name"] 28 unit_name = os.environ["unit_name"]
22 config = OBSConfig(os.environ["PATH_TO_OBSCONF_FILE"], unit_name) 29 config = OBSConfig(os.environ["PATH_TO_OBSCONF_FILE"], unit_name)
  30 +
  31 + # Create a Sequence form
23 sequence_form = SequenceForm(instance=seq, data_from_config=config.getEditableAttributesOfMount(config.unit_name), layouts = config.get_layouts(config.unit_name), sp_list=sp_list) 32 sequence_form = SequenceForm(instance=seq, data_from_config=config.getEditableAttributesOfMount(config.unit_name), layouts = config.get_layouts(config.unit_name), sp_list=sp_list)
24 result = { 33 result = {
25 "succeed": True, 34 "succeed": True,
26 "errors": [], 35 "errors": [],
27 } 36 }
  37 +
28 if is_simplified: 38 if is_simplified:
29 seq.scientific_program = sp_list[yaml_content["sequence"]["scientific_program"]] 39 seq.scientific_program = sp_list[yaml_content["sequence"]["scientific_program"]]
  40 +
30 else: 41 else:
31 - index_value_sp = yaml_content["sequence"]["scientific_program"]["value"]  
32 - values = yaml_content["sequence"]["scientific_program"]["values"]  
33 - if index_value_sp < 0 or index_value_sp > len(yaml_content["sequence"]["scientific_program"]["values"]):  
34 - result["errors"].append(f"Value of scientific program isn't valid, index out of bounds ({index_value_sp} > {len(values)})") 42 + # pour la lisibilité du code (et éviter la redondance)
  43 + yaml_seq_sp = yaml_content["sequence"]["scientific_program"]
  44 + index_value_sp = yaml_seq_sp["value"]
  45 + values = yaml_seq_sp["values"]
  46 + if index_value_sp < 0 or index_value_sp > len(values):
  47 + result["errors"].append(f"SP value isn't valid, index out of bounds ({index_value_sp} > {len(values)})")
35 index_value_sp = 0 48 index_value_sp = 0
36 - chosen_sp = ScientificProgram.objects.get(name=yaml_content["sequence"]["scientific_program"]["values"][index_value_sp]) 49 + chosen_sp = ScientificProgram.objects.get(name=values[index_value_sp])
37 if chosen_sp in sp_list: 50 if chosen_sp in sp_list:
38 - seq.scientific_program = ScientificProgram.objects.get(name=yaml_content["sequence"]["scientific_program"]["values"][index_value_sp]) 51 + #seq.scientific_program = ScientificProgram.objects.get(name=yaml_content["sequence"]["scientific_program"]["values"][index_value_sp])
  52 + seq.scientific_program = chosen_sp
39 else: 53 else:
40 - result["errors"].append(f"Scientific program {chosen_sp.name} is not assigned to that user ") 54 + result["errors"].append(f"SP {chosen_sp.name} is not assigned to that user ")
  55 +
41 seq.config_attributes = {} 56 seq.config_attributes = {}
  57 +
  58 + # Fill all Sequence form fields
  59 + # keys() inutile ? => for field in sequence_form.fields :
  60 + # Sinon, y a aussi => for key,val in sequence_form.fields.items():
  61 + # => Ca éviterait de faire => sequence_form.fields[field] pour recuperer "val"
42 for field in sequence_form.fields.keys(): 62 for field in sequence_form.fields.keys():
43 - if sequence_form.fields[field].required == False or field == "scientific_program": 63 +
  64 + #if sequence_form.fields[field].required == False or field == "scientific_program":
  65 + if not sequence_form.fields[field].required or field=="scientific_program":
44 continue 66 continue
  67 + # pour lisibilité, simplicité et éviter redondance
  68 + yaml_field = yaml_content["sequence"][field]
  69 + value = yaml_field if is_simplified else yaml_field["value"]
  70 + ''' (orig)
45 if is_simplified: 71 if is_simplified:
46 - value = yaml_content["sequence"][field] 72 + value = yaml_content["sequence"][field]
47 else: 73 else:
48 value = yaml_content["sequence"][field]["value"] 74 value = yaml_content["sequence"][field]["value"]
  75 + '''
49 if field not in yaml_content["sequence"]: 76 if field not in yaml_content["sequence"]:
50 result["errors"].append(f"{field} not in yaml file") 77 result["errors"].append(f"{field} not in yaml file")
51 else: 78 else:
  79 + if is_simplified:
  80 + if sequence_form.fields[field].__dict__.get("_choices"):
  81 + # y a pas conflit ici avec la variable "value" définie au-dessus ?
  82 + values = [value[0] for value in sequence_form.fields[field].__dict__.get("_choices")]
  83 + value = values[value]
  84 + else:
  85 + if yaml_field.get("values"):
  86 + index_value = yaml_field["value"]
  87 + values = yaml_field["values"]
  88 + if index_value < 0 or index_value > len(yaml_field["values"]):
  89 + result["errors"].append(f"Value of {field} isn't valid, index out of bounds ({index_value} > {len(values)})")
  90 + index_value = 0
  91 + value = yaml_field["values"][index_value]
  92 + else:
  93 + if field == "start_date":
  94 + if type(value) != datetime.datetime:
  95 + #value = datetime.datetime.strptime(yaml_content["sequence"][field]["value"],'%d/%m/%Y %H:%M:%S')
  96 + # ISO format
  97 + value = datetime.datetime.strptime(value, '%Y-%m-%dT%H:%M:%S.%f')
  98 + seq.__dict__[field] = value
  99 + ''' (orig)
  100 + else:
52 if not is_simplified: 101 if not is_simplified:
53 if yaml_content["sequence"][field].get("values"): 102 if yaml_content["sequence"][field].get("values"):
54 index_value = yaml_content["sequence"][field]["value"] 103 index_value = yaml_content["sequence"][field]["value"]
@@ -68,27 +117,38 @@ def check_sequence_file_validity_and_save(yaml_content,request): @@ -68,27 +117,38 @@ def check_sequence_file_validity_and_save(yaml_content,request):
68 if sequence_form.fields[field].__dict__.get("_choices"): 117 if sequence_form.fields[field].__dict__.get("_choices"):
69 values = [value[0] for value in sequence_form.fields[field].__dict__.get("_choices")] 118 values = [value[0] for value in sequence_form.fields[field].__dict__.get("_choices")]
70 value = values[value] 119 value = values[value]
  120 + '''
  121 +
  122 + # suffisant ? => if field in seq.__dict__
71 if field in seq.__dict__.keys(): 123 if field in seq.__dict__.keys():
72 seq.__dict__[field] = value 124 seq.__dict__[field] = value
73 else: 125 else:
74 seq.config_attributes[field] = value 126 seq.config_attributes[field] = value
75 127
  128 + # Create ALBUMS
76 albums_from_file = yaml_content["sequence"]["ALBUMS"] 129 albums_from_file = yaml_content["sequence"]["ALBUMS"]
77 chosen_layout = seq.config_attributes["layout"] 130 chosen_layout = seq.config_attributes["layout"]
78 if type(chosen_layout) == int: 131 if type(chosen_layout) == int:
79 layouts = config.get_layouts(config.unit_name)["layouts"] 132 layouts = config.get_layouts(config.unit_name)["layouts"]
80 chosen_layout = list(layouts.keys())[chosen_layout] 133 chosen_layout = list(layouts.keys())[chosen_layout]
  134 + # renommer : layout_albums
81 albums_of_layout = config.getLayoutByName(unit_name=config.unit_name,name_of_layout=chosen_layout)["ALBUMS"] 135 albums_of_layout = config.getLayoutByName(unit_name=config.unit_name,name_of_layout=chosen_layout)["ALBUMS"]
  136 +
82 if len(albums_of_layout) == len(albums_from_file): 137 if len(albums_of_layout) == len(albums_from_file):
83 for album in albums_from_file: 138 for album in albums_from_file:
84 album = album["Album"] 139 album = album["Album"]
85 if album["name"] not in albums_of_layout: 140 if album["name"] not in albums_of_layout:
86 result["errors"].append(f"Album {album['name']} is not the chosen layout") 141 result["errors"].append(f"Album {album['name']} is not the chosen layout")
87 else: 142 else:
  143 + # alb n'est pas utilisé
88 alb = Album.objects.create(name=album["name"], sequence=seq, complete=True) 144 alb = Album.objects.create(name=album["name"], sequence=seq, complete=True)
89 else: 145 else:
90 result["errors"].append(f"The number of albums doesn't correspond to the chosen layout") 146 result["errors"].append(f"The number of albums doesn't correspond to the chosen layout")
91 - for album in yaml_content["sequence"]["ALBUMS"]: 147 +
  148 + # Tu itères 2 fois sur albums_from_file, y aurait pas moyen d'iterer 1 seule fois dessus ?
  149 + #for album in yaml_content["sequence"]["ALBUMS"]:
  150 + for album in albums_from_file:
  151 +
92 album = album["Album"] 152 album = album["Album"]
93 plans = album.get("Plans") 153 plans = album.get("Plans")
94 if plans == None: 154 if plans == None:
@@ -101,42 +161,63 @@ def check_sequence_file_validity_and_save(yaml_content,request): @@ -101,42 +161,63 @@ def check_sequence_file_validity_and_save(yaml_content,request):
101 config_attributes = {} 161 config_attributes = {}
102 plan_form = PlanForm(data_from_config=config.getEditableAttributesOfChannel(config.unit_name,list(config.get_channels(config.unit_name).keys())[0]),edited_plan=None) 162 plan_form = PlanForm(data_from_config=config.getEditableAttributesOfChannel(config.unit_name,list(config.get_channels(config.unit_name).keys())[0]),edited_plan=None)
103 for field in plan_form.fields: 163 for field in plan_form.fields:
  164 + plan_field = plan[field]
  165 + '''
104 min_value = None 166 min_value = None
105 max_value = None 167 max_value = None
106 value_type = None 168 value_type = None
  169 + '''
  170 + min_value = max_value = value_type = None
107 if field not in plan.keys(): 171 if field not in plan.keys():
108 result["errors"].append(f"Missing field : '{field}' for plan {plans.index(plan)}") 172 result["errors"].append(f"Missing field : '{field}' for plan {plans.index(plan)}")
109 else: 173 else:
110 # TODO : ajouter max_value, min_value, suppression plan et album si invalides 174 # TODO : ajouter max_value, min_value, suppression plan et album si invalides
111 if not is_simplified: 175 if not is_simplified:
112 - if plan[field].get("value_type"):  
113 - value_type = plan[field]["value_type"]  
114 - if type(plan[field]["value"]) == str and ast.literal_eval(plan[field]["value"]) != value_type: 176 + if plan_field.get("value_type"):
  177 + value_type = plan_field["value_type"]
  178 + if type(plan_field["value"]) == str and ast.literal_eval(plan_field["value"]) != value_type:
115 result["errors"].append(f"Field {field} value doesn't correspond to the assigned type (type required : {value_type})") 179 result["errors"].append(f"Field {field} value doesn't correspond to the assigned type (type required : {value_type})")
116 - if plan[field].get("min_value"):  
117 - if type(plan[field]["min_value"]) == str:  
118 - min_value = ast.literal_eval(plan[field]["min_value"]) 180 + if plan_field.get("min_value"):
  181 + min_value = plan_field["min_value"]
  182 + if type(min_value) == str:
  183 + min_value = ast.literal_eval(min_value)
  184 + '''
  185 + if type(plan_field["min_value"]) == str:
  186 + min_value = ast.literal_eval(plan_field["min_value"])
119 else: 187 else:
120 - min_value = plan[field]["min_value"]  
121 - if plan[field].get("max_value"):  
122 - if type(plan[field].get("max_value")) == str:  
123 - max_value = ast.literal_eval(plan[field]["max_value"]) 188 + min_value = plan_field["min_value"]
  189 + '''
  190 + if plan_field.get("max_value"):
  191 + max_value = plan_field["max_value"]
  192 + if type(max_value) == str:
  193 + max_value = ast.literal_eval(max_value)
  194 + '''
  195 + if type(plan_field.get("max_value")) == str:
  196 + max_value = ast.literal_eval(plan_field["max_value"])
124 else: 197 else:
125 - max_value = plan[field]["max_value"] 198 + max_value = plan_field["max_value"]
  199 + '''
126 if field == "nb_images": 200 if field == "nb_images":
  201 + new_plan_object.__dict__[field] = plan_field if is_simplified else plan_field["value"]
  202 + '''
127 if is_simplified: 203 if is_simplified:
128 - new_plan_object.__dict__[field] = plan[field] 204 + new_plan_object.__dict__[field] = plan_field
129 else: 205 else:
130 - new_plan_object.__dict__[field] = plan[field]["value"] 206 + new_plan_object.__dict__[field] = plan_field["value"]
  207 + '''
131 else: 208 else:
132 - if not is_simplified:  
133 - if plan[field].get("values"):  
134 - index_value = plan[field]["value"]  
135 - values = plan[field]["values"]  
136 - if index_value < 0 or index_value > len(plan[field]["values"]): 209 + # shortcut possible ?
  210 + #new_plan_object_field = new_plan_object.config_attributes[field]
  211 + if is_simplified:
  212 + new_plan_object.config_attributes[field] = plan_field
  213 + else:
  214 + if plan_field.get("values"):
  215 + index_value = plan_field["value"]
  216 + values = plan_field["values"]
  217 + if index_value < 0 or index_value > len(plan_field["values"]):
137 result["errors"].append(f"Value of Plan field '{field}' isn't valid, index out of bounds ({index_value} > {len(values)})") 218 result["errors"].append(f"Value of Plan field '{field}' isn't valid, index out of bounds ({index_value} > {len(values)})")
138 index_value = 0 219 index_value = 0
139 - value = plan[field]["values"][index_value] 220 + value = plan_field["values"][index_value]
140 try: 221 try:
141 # linked values 222 # linked values
142 splitted_values = value.split(";") 223 splitted_values = value.split(";")
@@ -150,13 +231,12 @@ def check_sequence_file_validity_and_save(yaml_content,request): @@ -150,13 +231,12 @@ def check_sequence_file_validity_and_save(yaml_content,request):
150 new_plan_object.config_attributes[field] = config_attributes[field] 231 new_plan_object.config_attributes[field] = config_attributes[field]
151 else: 232 else:
152 if max_value and min_value: 233 if max_value and min_value:
153 - if plan[field]["value"] > max_value: 234 + if plan_field["value"] > max_value:
154 result["errors"].append(f"Plan field {field} doesn't respect max value") 235 result["errors"].append(f"Plan field {field} doesn't respect max value")
155 - if plan[field]["value"] < min_value: 236 + if plan_field["value"] < min_value:
156 result["errors"].append(f"Plan field {field} doesn't respect min value") 237 result["errors"].append(f"Plan field {field} doesn't respect min value")
157 - new_plan_object.config_attributes[field] = plan[field]["value"]  
158 - else:  
159 - new_plan_object.config_attributes[field] = plan[field] 238 + new_plan_object.config_attributes[field] = plan_field["value"]
  239 +
160 new_plan_object.save() 240 new_plan_object.save()
161 241
162 242