Skip to content

Commit

Permalink
Merge pull request #8 from AAFC-BICoE/Support-34788-Add-form-template…
Browse files Browse the repository at this point in the history
…-and-split-configuration-support

Support #34788 - Add form template and split configuration support
  • Loading branch information
hdosprazeres authored Aug 28, 2024
2 parents c9e2bf3 + 15661b7 commit 5748c78
Show file tree
Hide file tree
Showing 14 changed files with 791 additions and 6 deletions.
19 changes: 19 additions & 0 deletions dinapy/apis/collectionapi/formtemplateapi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# This file contains the FormTemplateAPI class used for interacting with the Form Template endpoint of the Collection API.
from .collectionapi import CollectionModuleApi

class FormTemplateAPI(CollectionModuleApi):
"""
Class for handling form template related DINA API requests.
"""

def __init__(self, config_path: str = None, base_url: str = None) -> None:
"""
Parameters:
config_path (str, optional): Path to a config file (default: None).
base_url (str, optional): URL to the URL to perform the API requests against. If not
provided then local deployment URL is used. Should end with a forward slash.
"""
super().__init__(config_path, base_url)
self.base_url += "form-template"


19 changes: 19 additions & 0 deletions dinapy/apis/collectionapi/splitconfigurationapi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# This file contains the SplitConfigurationAPI class used for interacting with the Split Configuration endpoint of the Collection API.
from .collectionapi import CollectionModuleApi

class SplitConfigurationAPI(CollectionModuleApi):
"""
Class for handling split configuration related DINA API requests.
"""

def __init__(self, config_path: str = None, base_url: str = None) -> None:
"""
Parameters:
config_path (str, optional): Path to a config file (default: None).
base_url (str, optional): URL to the URL to perform the API requests against. If not
provided then local deployment URL is used. Should end with a forward slash.
"""
super().__init__(config_path, base_url)
self.base_url += "split-configuration"


70 changes: 66 additions & 4 deletions dinapy/client/dina_api_client.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
import argparse
import yaml
import os,sys
import json

project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, project_root)

from dinapy.apis.objectstoreapi.metadata_api import MetadataAPI
from dinapy.apis.objectstoreapi.uploadfileapi import UploadFileAPI
from dinapy.apis.collectionapi.formtemplateapi import FormTemplateAPI
from dinapy.apis.collectionapi.splitconfigurationapi import SplitConfigurationAPI
from pathlib import Path

from dinapy.client.utils import get_field_from_config
from dinapy.entities.Metadata import MetadataAttributesDTOBuilder, MetadataDTOBuilder
from dinapy.entities.Relationships import RelationshipDTO
from dinapy.schemas.metadata_schema import MetadataSchema
from dinapy.entities.FormTemplate import FormTemplateAttributesDTOBuilder,FormTemplateDTOBuilder
from dinapy.entities.SplitConfiguration import SplitConfigurationAttributesDTOBuilder,SplitConfigurationDTOBuilder
from dinapy.schemas.splitconfigurationschema import SplitConfigurationSchema
from dinapy.schemas.formtemplateschema import FormTemplateSchema

DINA_API_CONFIG_PATH = "./dina-api-config.yml"
GROUP = ""
Expand All @@ -21,8 +33,8 @@ class DinaApiClient:
def __init__(self, config_path: str = None, base_url: str = None) -> None:
self.upload_file_api = UploadFileAPI(config_path, base_url)
self.metadata_api = MetadataAPI(config_path, base_url)


self.form_template_api = FormTemplateAPI(config_path,base_url)
self.split_configuration_api = SplitConfigurationAPI(config_path,base_url)
def create_parser():
"""
Create command line parser.
Expand Down Expand Up @@ -51,6 +63,16 @@ def create_parser():
metavar="<dir_path> : (str) = Path to the directory to be uploaded.",
help="Upload all files in a directory to Object Store and create metadatas according to constants defined in ./dina-api-config.yml",
)
parser.add_argument(
"-create_form_template",
metavar="<file_path> : (str) = Path to the file to be parsed and created.",
help="Create a form template according to specs defined in a yaml file such as ./form-template-sample.yml",
)
parser.add_argument(
"-create_split_configuration",
metavar="<file_path> : (str) = Path to the file to be parsed and created.",
help="Create a form template according to specs defined in a yaml file such as ./form-template-sample.yml",
)
return parser


Expand All @@ -65,7 +87,41 @@ def upload_file(args: argparse.Namespace, dina_api_client: DinaApiClient, path:

if args.verbose:
print(response_json)

def create_split_configuration(dina_api_client: DinaApiClient, path: Path):
split_configuration_config_path = path.as_posix()
with open(split_configuration_config_path, "r", encoding="utf-8") as split_configuration_config_file:
split_configuration_config = yaml.safe_load(split_configuration_config_file)
split_configuration_attributes = split_configuration_config['attributes']
split_configuration_attributes_dto = SplitConfigurationAttributesDTOBuilder().set_strategy(split_configuration_attributes['strategy'])\
.set_conditionalOnMaterialSampleTypes(split_configuration_attributes['conditionalOnMaterialSampleTypes'])\
.set_characterType(split_configuration_attributes['characterType'])\
.set_group(split_configuration_attributes['group'])\
.set_separator(split_configuration_attributes['separator']).set_name(split_configuration_attributes['name'])\
.set_materialSampleTypeCreatedBySplit(split_configuration_attributes['materialSampleTypeCreatedBySplit']).build()
split_configuration_dto = SplitConfigurationDTOBuilder().set_attributes(split_configuration_attributes_dto).build()

schema = SplitConfigurationSchema()

serialized_split_configuration = schema.dump(split_configuration_dto)
response = dina_api_client.split_configuration_api.create_entity(serialized_split_configuration)
print(response.json())

def create_form_template(dina_api_client: DinaApiClient, path : Path):
form_template_config_path = path.as_posix()
with open(form_template_config_path, "r", encoding="utf-8") as form_template_config_file:
form_template_config = yaml.safe_load(form_template_config_file)
form_template_attributes = form_template_config['attributes']
form_template_attributes_dto = FormTemplateAttributesDTOBuilder().set_components(form_template_attributes['components'])\
.set_viewConfiguration(form_template_attributes['viewConfiguration'])\
.set_restrictToCreatedBy(form_template_attributes['restrictToCreatedBy'])\
.set_group(form_template_attributes['group']).set_name(form_template_attributes['name']).build()
form_template_dto = FormTemplateDTOBuilder().set_attributes(form_template_attributes_dto).build()

schema = FormTemplateSchema()

serialized_form_template = schema.dump(form_template_dto)
response = dina_api_client.form_template_api.create_entity(serialized_form_template)
print(response.json())

def create_metadatas(args: argparse.Namespace, dina_api_client: DinaApiClient):
pathlist = Path(args.create_metadatas).rglob("*.*")
Expand Down Expand Up @@ -128,7 +184,7 @@ def main():
# Initialize argparse
parser = create_parser()
args = parser.parse_args()

print(args)
dina_api_client = DinaApiClient()
with open(DINA_API_CONFIG_PATH, "r", encoding="utf-8") as dina_api_config_file:
dina_api_config = yaml.safe_load(dina_api_config_file)
Expand All @@ -145,6 +201,12 @@ def main():
upload_file(args, dina_api_client, path)
elif args.create_metadatas:
create_metadatas(args, dina_api_client)
elif args.create_form_template:
path = Path(args.create_form_template)
create_form_template(dina_api_client,path)
elif args.create_split_configuration:
path = Path(args.create_split_configuration)
create_split_configuration(dina_api_client,path)
else:
raise Exception("Incorrect arguments.")

Expand Down
106 changes: 106 additions & 0 deletions dinapy/entities/FormTemplate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
class FormTemplateDTO:
def __init__(self, id=None, type=None, attributes=None, relationships=None):
self.id = id
self.type = type
self.attributes = attributes
self.relationships = relationships

def get_id(self):
return self.id

def get_type(self):
return self.type

class FormTemplateDTOBuilder:
def __init__(self):
self._id = None
self._type = 'form-template'
self._attributes = None
self._relationships = None

def set_id(self, id):
self._id = id
return self

def set_type(self, type):
self._type = type
return self

def set_attributes(self, attributes):
self._attributes = attributes
return self

def set_relationships(self, relationships):
self._relationships = relationships
return self

def build(self):
return FormTemplateDTO(self._id, self._type, self._attributes, self._relationships)

class FormTemplateAttributesDTO:
def __init__(
self,
createdOn=None,
createdBy=None,
group=None,
name=None,
restrictToCreatedBy=None,
viewConfiguration=None,
components=None
):
self.createdOn = createdOn
self.createdBy = createdBy
self.group = group
self.name = name
self.restrictToCreatedBy = restrictToCreatedBy
self.viewConfiguration = viewConfiguration
self.components = components

class FormTemplateAttributesDTOBuilder:
def __init__(self):
self._createdOn = 'undefined'
self._createdBy = 'undefined'
self._group = 'undefined'
self._name = 'undefined'
self._restrictToCreatedBy = 'undefined'
self._viewConfiguration = 'undefined'
self._components = 'undefined'

def set_createdOn(self, createdOn):
self._createdOn = createdOn
return self

def set_createdBy(self, createdBy):
self._createdBy = createdBy
return self

def set_group(self, group):
self._group = group
return self

def set_name(self, name):
self._name = name
return self

def set_restrictToCreatedBy(self, restrictToCreatedBy):
self._restrictToCreatedBy = restrictToCreatedBy
return self

def set_viewConfiguration(self, viewConfiguration):
self._viewConfiguration = viewConfiguration
return self

def set_components(self, components):
self._components = components
return self

def build(self):
return FormTemplateAttributesDTO(
self._createdOn,
self._createdBy,
self._group,
self._name,
self._restrictToCreatedBy,
self._viewConfiguration,
self._components
)
117 changes: 117 additions & 0 deletions dinapy/entities/SplitConfiguration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
class SplitConfigurationDTO:
def __init__(self, id=None, type=None, attributes=None, relationships=None):
self.id = id
self.type = type
self.attributes = attributes
self.relationships = relationships

def get_id(self):
return self.id

def get_type(self):
return self.type


class SplitConfigurationDTOBuilder:
def __init__(self):
self._id = None
self._type = 'split-configuration'
self._attributes = None
self._relationships = None

def set_attributes(self, attributes):
self._attributes = attributes
return self

def set_relationships(self, relationships):
self._relationships = relationships
return self

def build(self):
return SplitConfigurationDTO(self._id, self._type, self._attributes, self._relationships)


class SplitConfigurationAttributesDTO:
def __init__(
self,
createdOn=None,
createdBy=None,
group=None,
name=None,
strategy=None,
conditionalOnMaterialSampleTypes=None,
characterType=None,
separator=None,
materialSampleTypeCreatedBySplit=None
):
self.createdOn = createdOn
self.createdBy = createdBy
self.group = group
self.name = name
self.strategy = strategy
self.conditionalOnMaterialSampleTypes = conditionalOnMaterialSampleTypes
self.characterType = characterType
self.separator = separator
self.materialSampleTypeCreatedBySplit = materialSampleTypeCreatedBySplit


class SplitConfigurationAttributesDTOBuilder:
def __init__(self):
self._createdOn = 'undefined'
self._createdBy = 'undefined'
self._group = 'undefined'
self._name = 'undefined'
self._strategy = 'undefined'
self._conditionalOnMaterialSampleTypes = 'undefined'
self._characterType = 'undefined'
self._separator = 'undefined'
self._materialSampleTypeCreatedBySplit = 'undefined'

def set_createdOn(self, createdOn):
self._createdOn = createdOn
return self

def set_createdBy(self, createdBy):
self._createdBy = createdBy
return self

def set_group(self, group):
self._group = group
return self

def set_name(self, name):
self._name = name
return self

def set_strategy(self, strategy):
self._strategy = strategy
return self

def set_conditionalOnMaterialSampleTypes(self, conditionalOnMaterialSampleTypes):
self._conditionalOnMaterialSampleTypes = conditionalOnMaterialSampleTypes
return self

def set_characterType(self, characterType):
self._characterType = characterType
return self

def set_separator(self, separator):
self._separator = separator
return self

def set_materialSampleTypeCreatedBySplit(self, materialSampleTypeCreatedBySplit):
self._materialSampleTypeCreatedBySplit = materialSampleTypeCreatedBySplit
return self

def build(self):
return SplitConfigurationAttributesDTO(
self._createdOn,
self._createdBy,
self._group,
self._name,
self._strategy,
self._conditionalOnMaterialSampleTypes,
self._characterType,
self._separator,
self._materialSampleTypeCreatedBySplit
)
2 changes: 1 addition & 1 deletion dinapy/schemas/customFields.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
def SkipUndefinedField(base_field_class, *args, **kwargs):
class CustomField(base_field_class):
SKIP_MARKER = False
SKIP_MARKER = "//SKIP_FIELD_MARKER//"

def __init__(self, *args, **kwargs):
super(CustomField, self).__init__(*args, **kwargs)
Expand Down
Loading

0 comments on commit 5748c78

Please sign in to comment.