functions.py 21.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
# Standard import
import ast
import datetime
import os, yaml
import pickle
# Django imports
from django.forms.models import model_to_dict
from user_manager.models import PyrosUser, ScientificProgram, Period
from routine_manager.models import Sequence, Album, Plan
from .forms import SequenceForm, PlanForm   #, AlbumForm
from django.db import IntegrityError
# Project imports
from src.core.pyros_django.obsconfig.obsconfig_class import OBSConfig
from django.http import HttpRequest

def check_sequence_file_validity_and_save(yaml_content: dict, request: HttpRequest):
    ''' Create a sequence in DB from the uploaded sequence (yaml_content) '''

    # Get boolean to simplified to know if the file is written in simplified mode (i.e. : Each field of the form is directly associated to its value)
    is_simplified = yaml_content.get("simplified", False)
    # Get scientific programs for the user who is submitting the sequence file
    user_sp = request.user.get_scientific_programs()
    # From user sp, get all SP that can observe / submit sequence for the current period
    sp_list = ScientificProgram.objects.observable_programs().filter(id__in=user_sp)
    
    # Create a sequence seq object (from yaml_content) to be saved in DB
    seq = Sequence.objects.create()
    seq.pyros_user = PyrosUser.objects.get(id=request.user.id)
    
    # Get the unit config
    unit_name = os.environ["unit_name"]
    config = OBSConfig(os.environ["PATH_TO_OBSCONF_FILE"], unit_name)
    
    # Create a Sequence form
    sequence_form = SequenceForm(instance=seq, data_from_config=config.getEditableMountAttributes(config.unit_name), layouts = config.get_layouts(config.unit_name), sp_list=sp_list)
    result = {
        "succeed": True,
        "errors": [],
    }

    if is_simplified:
        seq.scientific_program = sp_list[yaml_content["sequence"]["scientific_program"]]
    
    else:
        # pour la lisibilité du code (et éviter la redondance)
        # get scientific program field's attributes 
        yaml_seq_sp = yaml_content["sequence"]["scientific_program"]
        sp_index_value = yaml_seq_sp["value"]
        values =  yaml_seq_sp["values"]
        # Check if index of sp is valid (in range of possible index from values)
        if sp_index_value < 0 or sp_index_value > len(values):
            result["errors"].append(f"SP value isn't valid, index out of bounds ({sp_index_value} > {len(values)})")
            sp_index_value = 0
        chosen_sp = ScientificProgram.objects.get(name=values[sp_index_value])
        # If the sp is associated to that user, associate the sp to the sequence
        if chosen_sp in sp_list:
            #seq.scientific_program = ScientificProgram.objects.get(name=yaml_content["sequence"]["scientific_program"]["values"][sp_index_value])
            seq.scientific_program = chosen_sp
        else:
            result["errors"].append(f"SP {chosen_sp.name} is not assigned to that user ")
    
    seq.config_attributes = {}

    # Fill all Sequence form fields
    # keys() inutile ? => for field in sequence_form.fields :
    # Sinon, y a aussi => for key,val in sequence_form.fields.items():
    # => Ca éviterait de faire => sequence_form.fields[field] pour recuperer "val"
    for field, field_attribute in sequence_form.fields.items():
        
        #if sequence_form.fields[field].required == False or field == "scientific_program":
        if not field_attribute.required or field=="scientific_program":
            continue
        # pour lisibilité, simplicité et éviter redondance
        yaml_field = yaml_content["sequence"][field]
        value = yaml_field if is_simplified else yaml_field["value"]
        ''' (orig)
        if is_simplified:
            value = yaml_content["sequence"][field]
        else:
            value = yaml_content["sequence"][field]["value"]
        '''
        # If the current field of the sequence isn't found in the file, add an error message to inform the user the missing field
        if field not in yaml_content["sequence"]:
            result["errors"].append(f"{field} not in yaml file")
        else:
            if is_simplified:
                # If the field is a choicefield, get choices and associate the index to the real value
                if sequence_form.fields[field].__dict__.get("_choices"):
                    # y a pas conflit ici avec la variable "value" définie au-dessus ? -> Non car on transforme l'ancien value qui est un index en une vraie valeur
                    values = [value[0] for value in sequence_form.fields[field].__dict__.get("_choices")]
                    value = values[value]
            else:
                if yaml_field.get("values"):
                    # Transform the original value which is an index to a "real" value from the "values" attributes
                    index_value = yaml_field["value"]
                    values = yaml_field["values"]
                    if index_value < 0 or index_value > len(yaml_field["values"]):
                        result["errors"].append(f"Value of {field} isn't valid, index out of bounds ({index_value} > {len(values)})")
                        index_value = 0
                    value = yaml_field["values"][index_value]
                else:
                    # Transform the string value to a datetime value
                    if field == "start_date":
                        if type(value) != datetime.datetime:
                            #value = datetime.datetime.strptime(yaml_content["sequence"][field]["value"],'%d/%m/%Y %H:%M:%S')
                            # ISO format
                            value = datetime.datetime.strptime(value, '%Y-%m-%dT%H:%M:%S.%f')
                            seq.__dict__[field] = value
        ''' (orig)
        else:
            if not is_simplified:
                if yaml_content["sequence"][field].get("values"):
                    index_value = yaml_content["sequence"][field]["value"]
                    values =  yaml_content["sequence"][field]["values"]
                    if index_value < 0 or index_value > len(yaml_content["sequence"][field]["values"]):
                        result["errors"].append(f"Value of {field} isn't valid, index out of bounds ({index_value} > {len(values)})")
                        index_value = 0
                    value = yaml_content["sequence"][field]["values"][index_value]
                else:
                    if field == "start_date":
                        if type(value) != datetime.datetime:
                            #value = datetime.datetime.strptime(yaml_content["sequence"][field]["value"],'%d/%m/%Y %H:%M:%S')
                            # ISO format
                            value = datetime.datetime.strptime(value, '%Y-%m-%dT%H:%M:%S.%f')
                            seq.__dict__[field] = value
            else:
                if sequence_form.fields[field].__dict__.get("_choices"):
                    values = [value[0] for value in sequence_form.fields[field].__dict__.get("_choices")]
                    value = values[value]
        '''

        # suffisant ? => if field in seq.__dict__
        # If field is an attribute of the sequence, associate the field to the value
        if field in seq.__dict__:
            seq.__dict__[field] = value
        else:
            # else associate field & value in config_attributes sequence's field (JsonField) = variable fields of an sequence
            seq.config_attributes[field] = value

    # Create ALBUMS
    albums_from_file = yaml_content["sequence"]["ALBUMS"] 
    chosen_layout = seq.config_attributes["layout"]
    if type(chosen_layout) == int:
        layouts = config.get_layouts(config.unit_name)["layouts"]
        chosen_layout = list(layouts.keys())[chosen_layout]
    # Get album of the selected layout
    layout_albums = config.getLayoutByName(unit_name=config.unit_name, name_of_layout=chosen_layout)["ALBUMS"]
    
    # check if we have all the albums of that layout described in the sequence file
    if len(layout_albums) == len(albums_from_file):
        for album in albums_from_file:
            album = album["Album"]
            if album["name"] not in layout_albums:
                result["errors"].append(f"Album {album['name']} is not the chosen layout")
            else:
                # Create album 
                Album.objects.create(name=album["name"], sequence=seq, complete=True)
        # Create plan for that album
        plans = [a["Album"].get("Plans") for a in albums_from_file]
        process_plans(plans, result, is_simplified, config, album, seq)
    else:
        result["errors"].append(f"The number of albums doesn't correspond to the chosen layout")
    
    # 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"]:
    """
    # Old


    for album in albums_from_file:

        album = album["Album"]
        plans = album.get("Plans")
        # If no plan has been defined
        if plans == None:
            result["errors"].append(f"Album {album['name']} has no plans. Please add at least one plan")
            # pour eviter le else (plus lisible)
            continue
        plans = [a["Album"].get("Plans") for a in albums_from_file]
        process_plans(plans, result, is_simplified, config, album, seq)
        # 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.getEditableChannelAttributes(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]
                '''
                min_value = None
                max_value = None
                value_type = None
                '''
                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)}")
                    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 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    
                    else:
                        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 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
    """

    seq.status = Sequence.TOBEPLANNED
    seq.complete = True
    period = Period.objects.exploitation_period()
    if Period.objects.next_period() != None and Period.objects.next_period().start_date < seq.start_date.date():
        period = Period.objects.next_period()
    seq.period = period
    try:
        seq.save()
    except IntegrityError as e:
        result["errors"].append(str(e))
    if len(result["errors"]) != 0:
        result["succeed"] = False
        seq.delete()
    else:
        result["sequence_id"] = seq.id
    return result


def process_plans(plans: dict, result: dict, is_simplified: bool, config: OBSConfig, album: dict, seq: dict):
    if plans == None:
        result["errors"].append(f"Album {album['name']} has no plans. Please add at least one plan")
        # exit function
        return None
    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.getEditableChannelAttributes(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]
            '''
            min_value = None
            max_value = None
            value_type = None
            '''
            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)}")
                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 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    
                else:
                    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 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()

def create_sequence_pickle(sequence):
    seq_dict = model_to_dict(sequence)
    fullseq_dict = {
        "sequence":seq_dict,
        "albums": {}
    }
    for album in sequence.albums.all():
        fullseq_dict["albums"][album.name] = {"plans" : []}
        for plan in album.plans.all():
            fullseq_dict["albums"][f"{album.name}"]["plans"].append(model_to_dict(instance=plan))
    period = sequence.period
    root_project_path = os.environ.get("PROJECT_ROOT_PATH")
    data_path = root_project_path + "/data/"
    if not os.path.exists(data_path + "sequences_pickle"):
        os.mkdir(data_path +"sequences_pickle")
    if not os.path.exists(data_path + f"sequences_pickle/P{period.id}"):
        os.mkdir(data_path + f"sequences_pickle/P{period.id}")
    if not os.path.exists(data_path +f"sequences_pickle/P{period.id}/{sequence.night_id}"):
        os.mkdir(data_path +f"sequences_pickle/P{period.id}/{sequence.night_id}")
    seq_pickle_file_name = data_path +f"./sequences_pickle/P{period.id}/{sequence.night_id}/{sequence.id}.p"
    pickle.dump(fullseq_dict,open(seq_pickle_file_name,"wb"))