spiffworkflow-backend/bin/import_tickets_for_command_...

107 lines
3.8 KiB
Python
Raw Normal View History

2022-06-19 23:02:42 +00:00
"""Grabs tickets from csv and makes process instances."""
2022-06-20 14:56:32 +00:00
import csv
2022-10-12 16:36:53 +00:00
from spiffworkflow_backend import get_hacked_up_app_for_script
from spiffworkflow_backend.models.db import db
2022-06-20 14:56:32 +00:00
from spiffworkflow_backend.models.process_instance import ProcessInstanceModel
2022-06-19 23:02:42 +00:00
from spiffworkflow_backend.models.user import UserModel
2022-06-20 14:56:32 +00:00
from spiffworkflow_backend.services.process_instance_processor import (
ProcessInstanceProcessor,
)
from spiffworkflow_backend.services.process_instance_service import (
ProcessInstanceService,
)
2022-06-19 23:02:42 +00:00
2022-06-19 23:19:42 +00:00
def print_process_instance_count(process_model_identifier_ticket: str) -> None:
2022-06-19 23:02:42 +00:00
"""Print process instance count."""
2022-06-20 14:56:32 +00:00
process_instances = ProcessInstanceModel.query.filter_by(
process_model_identifier=process_model_identifier_ticket
).all()
2022-06-19 23:02:42 +00:00
process_instance_count = len(process_instances)
print(f"process_instance_count: {process_instance_count}")
def main():
"""Main."""
2022-10-12 16:34:24 +00:00
app = get_hacked_up_app_for_script()
2022-06-19 23:02:42 +00:00
with app.app_context():
2022-06-19 23:19:42 +00:00
process_model_identifier_ticket = "ticket"
2022-06-20 14:56:32 +00:00
db.session.query(ProcessInstanceModel).filter(
ProcessInstanceModel.process_model_identifier
== process_model_identifier_ticket
).delete()
2022-06-19 23:02:42 +00:00
db.session.commit()
2022-06-20 02:57:57 +00:00
"""Print process instance count."""
2022-06-20 14:56:32 +00:00
process_instances = ProcessInstanceModel.query.filter_by(
process_model_identifier=process_model_identifier_ticket
).all()
2022-06-20 02:57:57 +00:00
process_instance_count = len(process_instances)
print(f"process_instance_count: {process_instance_count}")
2022-06-20 14:56:32 +00:00
columns_to_data_key_mappings = {
"Month": "month",
"MS": "milestone",
"ID": "req_id",
"Dev Days": "dev_days",
"Feature": "feature",
"Priority": "priority",
}
2022-06-19 23:02:42 +00:00
columns_to_header_index_mappings = {}
2022-09-29 04:03:36 +00:00
user = UserModel.query.first()
2022-06-19 23:02:42 +00:00
2022-06-19 23:19:42 +00:00
with open("tests/files/tickets.csv") as infile:
2022-06-19 23:02:42 +00:00
reader = csv.reader(infile, delimiter=",")
2022-06-20 02:57:57 +00:00
# first row is garbage
next(reader)
2022-06-19 23:02:42 +00:00
header = next(reader)
for column_name in columns_to_data_key_mappings:
2022-06-20 14:56:32 +00:00
columns_to_header_index_mappings[column_name] = header.index(
column_name
)
2022-06-19 23:02:42 +00:00
id_index = header.index("ID")
priority_index = header.index("Priority")
print(f"header: {header}")
for row in reader:
ticket_identifier = row[id_index]
priority = row[priority_index]
print(f"ticket_identifier: {ticket_identifier}")
print(f"priority: {priority}")
2022-11-24 17:00:26 +00:00
process_instance = ProcessInstanceService.create_process_instance_from_process_model_identifier(
2022-06-20 21:17:51 +00:00
process_model_identifier_ticket,
user,
process_group_identifier="sartography-admin",
2022-06-19 23:02:42 +00:00
)
processor = ProcessInstanceProcessor(process_instance)
processor.do_engine_steps()
2022-06-20 02:57:57 +00:00
# processor.save()
2022-06-19 23:02:42 +00:00
2022-06-20 14:56:32 +00:00
for (
column_name,
desired_data_key,
) in columns_to_data_key_mappings.items():
2022-06-19 23:02:42 +00:00
appropriate_index = columns_to_header_index_mappings[column_name]
2022-06-20 14:56:32 +00:00
processor.bpmn_process_instance.data[desired_data_key] = row[
appropriate_index
]
2022-06-19 23:02:42 +00:00
2022-06-20 02:57:57 +00:00
print(f"datas: {processor.bpmn_process_instance.data}")
if processor.bpmn_process_instance.data["month"] == "":
continue
# you at least need a month, or else this row in the csv is considered garbage
# if processor.bpmn_process_instance.data["month"] is None:
# continue
processor.save()
2022-06-19 23:02:42 +00:00
# if __name__ == "__main__":
main()