diff --git a/src/core/pyros_django/api/views.py b/src/core/pyros_django/api/views.py index 5183255..01ae9f8 100644 --- a/src/core/pyros_django/api/views.py +++ b/src/core/pyros_django/api/views.py @@ -1,6 +1,6 @@ from django.shortcuts import render from rest_framework.views import APIView -from rest_framework.response import Response +from rest_framework.response import Response from rest_framework import viewsets, renderers from rest_framework.permissions import IsAuthenticated, AllowAny from rest_framework.decorators import api_view, permission_classes, action @@ -11,7 +11,7 @@ from api.serializers import AgentSurveySerializer, AlbumSerializer, FullSequence from common.models import PyrosUser, SP_Period, ScientificProgram, UserLevel, SP_Period_User, AgentSurvey, AgentCmd from routine_manager.models import Sequence, Album, Plan -from routine_manager.functions import check_sequence_file_validity +from routine_manager.functions import check_sequence_file_validity_and_save from rest_framework.request import Request from django.db.models import Q from datetime import datetime, timezone, timedelta @@ -92,7 +92,8 @@ class SequenceViewSet(viewsets.ModelViewSet): if user_role in ("Unit-PI", "Admin"): return Sequence.objects.all().order_by("-updated") else: - return Sequence.objects.filter(pyros_user=user).order_by("-updated") + sp_of_user = user.get_scientific_program() + return Sequence.objects.filter(scientific_program__in=sp_of_user).order_by("-updated") class ScientificProgramViewSet(viewsets.ModelViewSet): @@ -235,7 +236,7 @@ class PlanViewSet(viewsets.ModelViewSet): @api_view(["PUT"]) def submit_sequence_with_json(request): sequence_json = request.data - response = check_sequence_file_validity(sequence_json, request) + response = check_sequence_file_validity_and_save(sequence_json, request) if response.get("succeed") == True: seq = Sequence.objects.get(id=response.get("sequence_id")) log.info( diff --git a/src/core/pyros_django/routine_manager/functions.py b/src/core/pyros_django/routine_manager/functions.py index 5910005..b10a568 100644 --- a/src/core/pyros_django/routine_manager/functions.py +++ b/src/core/pyros_django/routine_manager/functions.py @@ -7,7 +7,8 @@ import datetime import os, yaml -def check_sequence_file_validity(yaml_content,request): +def check_sequence_file_validity_and_save(yaml_content,request): + is_simplified = yaml_content.get("simplified", False) sp_of_user = request.user.get_scientific_program() sp_list = ScientificProgram.objects.observable_programs().filter(id__in=sp_of_user) seq = Sequence.objects.create() @@ -19,39 +20,49 @@ def check_sequence_file_validity(yaml_content,request): "succeed": True, "errors": [], } - index_value_sp = yaml_content["sequence"]["scientific_program"]["value"] - values = yaml_content["sequence"]["scientific_program"]["values"] - if index_value_sp < 0 or index_value_sp > len(yaml_content["sequence"]["scientific_program"]["values"]): - result["errors"].append(f"Value of scientific program isn't valid, index out of bounds ({index_value_sp} > {len(values)})") - index_value_sp = 0 - chosen_sp = ScientificProgram.objects.get(name=yaml_content["sequence"]["scientific_program"]["values"][index_value_sp]) - if chosen_sp in sp_list: - seq.scientific_program = ScientificProgram.objects.get(name=yaml_content["sequence"]["scientific_program"]["values"][index_value_sp]) + if is_simplified: + seq.scientific_program = sp_list[yaml_content["sequence"]["scientific_program"]] else: - result["errors"].append(f"Scientific program {chosen_sp.name} is not assigned to that user ") + index_value_sp = yaml_content["sequence"]["scientific_program"]["value"] + values = yaml_content["sequence"]["scientific_program"]["values"] + if index_value_sp < 0 or index_value_sp > len(yaml_content["sequence"]["scientific_program"]["values"]): + result["errors"].append(f"Value of scientific program isn't valid, index out of bounds ({index_value_sp} > {len(values)})") + index_value_sp = 0 + chosen_sp = ScientificProgram.objects.get(name=yaml_content["sequence"]["scientific_program"]["values"][index_value_sp]) + if chosen_sp in sp_list: + seq.scientific_program = ScientificProgram.objects.get(name=yaml_content["sequence"]["scientific_program"]["values"][index_value_sp]) + else: + result["errors"].append(f"Scientific program {chosen_sp.name} is not assigned to that user ") seq.config_attributes = {} for field in sequence_form.fields.keys(): if sequence_form.fields[field].required == False or field == "scientific_program": continue - value = yaml_content["sequence"][field]["value"] + if is_simplified: + value = yaml_content["sequence"][field] + else: + value = yaml_content["sequence"][field]["value"] if field not in yaml_content["sequence"]: result["errors"].append(f"{field} not in yaml file") else: - 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] + 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 field == "start_date": - if type(yaml_content["sequence"][field]["value"]) != datetime.datetime: - #value = datetime.datetime.strptime(yaml_content["sequence"][field]["value"],'%d/%m/%Y %H:%M:%S') - # ISO format - value = datetime.datetime.strptime(yaml_content["sequence"][field]["value"],'%Y-%m/%dT%H:%M:%S.%f') - seq.__dict__[field] = value - continue + if sequence_form.fields[field].__dict__.get("_choices"): + values = [value[0] for value in sequence_form.fields[field].__dict__.get("_choices")] + value = values[value] if field in seq.__dict__.keys(): seq.__dict__[field] = value else: @@ -92,48 +103,55 @@ def check_sequence_file_validity(yaml_content,request): 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 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"): - 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"): - 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 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"): + 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"): + 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]["value"] + if is_simplified: + new_plan_object.__dict__[field] = plan[field] + else: + new_plan_object.__dict__[field] = plan[field]["value"] 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) - except: - # Do nothing, normal string - config_attributes[field] = ast.literal_eval(value) - new_plan_object.config_attributes[field] = config_attributes[field] + if not is_simplified: + 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) + 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"] 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"] + new_plan_object.config_attributes[field] = plan[field] new_plan_object.save() diff --git a/src/core/pyros_django/routine_manager/views.py b/src/core/pyros_django/routine_manager/views.py index c415542..98f31de 100644 --- a/src/core/pyros_django/routine_manager/views.py +++ b/src/core/pyros_django/routine_manager/views.py @@ -28,7 +28,7 @@ from .forms import SequenceForm, AlbumForm, PlanForm, uneditablePlanForm #from .forms import RequestForm, SequenceForm, AlbumForm, PlanForm, uneditablePlanForm from .validators import check_plan_validity, check_album_validity, check_sequence_validity, check_request_validity -from .functions import check_sequence_file_validity +from .functions import check_sequence_file_validity_and_save from .RequestSerializer import RequestSerializer import scheduler from django.contrib import messages @@ -854,7 +854,7 @@ def import_sequence(request): message = "File is too big (more than 1 000 000 bytes)" else: yaml_content = yaml.safe_load(file.read()) - import_response = check_sequence_file_validity( + import_response = check_sequence_file_validity_and_save( yaml_content, request) if import_response["succeed"]: message = "Request imported successfully. Please check it before submitting for scheduling." @@ -896,10 +896,13 @@ def export_sequence(request, seq_id: int, type: str): elif field == "start_date": #field_dict["value"] = seq.start_date.strftime("%d/%m/%Y %H:%M:%S") # ISO format - field_dict["value"] = seq.start_date.strftime("Y-%m/%dT%H:%M:%S.%f") + field_dict["value"] = seq.start_date.strftime("%Y-%m-%dT%H:%M:%S.%f") field_dict["value_type"] = "str" else: - field_dict["value_type"] = sequence_form.fields[field].widget.input_type + if hasattr(sequence_form.fields[field].widget,"input_type"): + field_dict["value_type"] = sequence_form.fields[field].widget.input_type + else: + field_dict["value_type"] = "str" if seq.__dict__.get(field) == None: value = seq.config_attributes.get(field) else: -- libgit2 0.21.2