Move business logic to FileService

This commit is contained in:
mike cullerton 2021-09-02 09:52:58 -04:00
parent f49128adf4
commit 4fa68bd4c0
1 changed files with 92 additions and 0 deletions

View File

@ -1,7 +1,10 @@
import hashlib
import io
import json
import os
from datetime import datetime
import random
import string
import pandas as pd
from github import Github, GithubObject, UnknownObjectException
@ -436,3 +439,92 @@ class FileService(object):
branch=target_branch
)
return {'updated': True}
@staticmethod
def dmn_from_spreadsheet(ss_data):
def _get_random_string(length):
return ''.join(
[random.choice(string.ascii_letters + string.digits) for n in range(length)])
def _row_has_value(values):
for value_item in values:
if not pd.isnull(value_item):
return True
return False
df = pd.read_excel(io.BytesIO(ss_data), header=None)
root = etree.Element("definitions",
xmlns="http://www.omg.org/spec/DMN/20151101/dmn.xsd",
id='Definitions',
name="DRD",
namespace="http://camunda.org/schema/1.0/dmn")
decision_name = df.iat[0, 1]
decision_id = df.iat[1, 1]
decision = etree.SubElement(root, "decision",
id=decision_id,
name=decision_name
)
decision_table = etree.SubElement(decision, 'decisionTable', id='decisionTable_1')
input_output = df.iloc[2][1:]
count = 1
input_count = 1
output_count = 1
for item in input_output:
if item == 'Input':
label = df.iloc[3, count]
input_ = etree.SubElement(decision_table, 'input', id=f'input_{input_count}', label=label)
type_ref = df.iloc[5, count]
input_expression = etree.SubElement(input_, 'inputExpression', id=f'inputExpression_{input_count}',
typeRef=type_ref)
expression = df.iloc[4, count]
expression_text = etree.SubElement(input_expression, 'text')
expression_text.text = expression
input_count += 1
elif item == 'Output':
label = df.iloc[3, count]
name = df.iloc[4, count]
type_ref = df.iloc[5, count]
decision_table.append(etree.Element('output', id=f'output_{output_count}',
label=label, name=name, typeRef=type_ref))
output_count += 1
elif item == 'Annotation':
column_count = count
break
count += 1
row = 6
while row < df.shape[0]:
column = 1
row_values = df.iloc[row].values[1:column_count]
if _row_has_value(row_values):
rando = _get_random_string(7).lower()
rule = etree.SubElement(decision_table, 'rule', id=f'DecisionRule_{rando}')
i = 1
while i < input_count:
input_entry = etree.SubElement(rule, 'inputEntry', id=f'UnaryTests_{_get_random_string(7)}')
text_element = etree.SubElement(input_entry, 'text')
text_element.text = str(df.iloc[row, column]) if not pd.isnull(df.iloc[row, column]) else ''
i += 1
column += 1
i = 1
while i < output_count:
output_entry = etree.SubElement(rule, 'outputEntry', id=f'LiteralExpression_{_get_random_string(7)}')
text_element = etree.SubElement(output_entry, 'text')
text_element.text = str(df.iloc[row, column]) if not pd.isnull(df.iloc[row, column]) else ''
i += 1
column += 1
description = etree.SubElement(rule, 'description')
text = df.iloc[row, column] if not pd.isnull(df.iloc[row, column]) else ''
description.text = text
row += 1
prefix = b'<?xml version="1.0" encoding="UTF-8"?>'
dmn_file = prefix + etree.tostring(root)
return dmn_file