Spaces:
Sleeping
Sleeping
File size: 15,475 Bytes
18c9405 87c01a0 aa62fa3 18c9405 c31b74d 18c9405 f3a4783 18c9405 050fe0d 18c9405 050fe0d 18c9405 050fe0d aa62fa3 8beb233 050fe0d aa62fa3 8beb233 050fe0d 18c9405 050fe0d 18c9405 050fe0d 18c9405 050fe0d 18c9405 050fe0d 18c9405 8beb233 18c9405 8beb233 18c9405 8beb233 18c9405 8beb233 18c9405 8beb233 18c9405 8beb233 18c9405 | 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 | from flask import jsonify, request
from models.workflow import Workflow
from models.department import Department
import logging
import os
from db import get_gridfs
from bson.objectid import ObjectId
from datetime import datetime
# Configure logging
logger = logging.getLogger(__name__)
def create_workflow(current_user):
"""Create a new workflow for a department"""
# Check if user has admin permissions
if current_user.permissions != 'Admin':
return jsonify({'message': 'Admin permissions required'}), 403
data = request.get_json()
# Check if required fields are present
required_fields = ['title', 'description']
for field in required_fields:
if field not in data:
return jsonify({'message': f'Missing required field: {field}'}), 400
# Get user's department
department = Department.find_by_id(current_user.department_id)
if not department:
return jsonify({'message': 'Department not found'}), 404
# Check if workflow with this title already exists in this department
existing_workflow = Workflow.find_by_title(data['title'], current_user.department_id)
if existing_workflow:
return jsonify({'message': 'Workflow with this title already exists in your department'}), 400
try:
# Create new workflow
workflow = Workflow(
title=data['title'],
description=data['description'],
department_id=current_user.department_id,
data_requirements=data.get('data_requirements', []),
form_fields=data.get('form_fields', [])
)
if workflow.save():
# Add workflow to department
department.add_workflow(workflow._id)
return jsonify({
'message': 'Workflow created successfully',
'workflow': workflow.to_dict()
}), 201
else:
return jsonify({'message': 'Failed to save workflow'}), 500
except Exception as e:
logger.error(f"Error creating workflow: {str(e)}")
return jsonify({'message': f'Error creating workflow: {str(e)}'}), 500
def get_workflow(current_user, workflow_id):
"""Get workflow by ID"""
workflow = Workflow.find_by_id(workflow_id)
if not workflow:
return jsonify({'message': 'Workflow not found'}), 404
# Check if user belongs to the same department as the workflow
if str(workflow.department_id) != str(current_user.department_id):
return jsonify({'message': 'Access denied to workflows from other departments'}), 403
return jsonify({'workflow': workflow.to_dict()}), 200
def update_workflow(current_user, workflow_id):
"""Update workflow details"""
# Check if user has admin permissions
if current_user.permissions != 'Admin':
return jsonify({'message': 'Admin permissions required'}), 403
workflow = Workflow.find_by_id(workflow_id)
if not workflow:
return jsonify({'message': 'Workflow not found'}), 404
# Check if user belongs to the same department as the workflow
if str(workflow.department_id) != str(current_user.department_id):
return jsonify({'message': 'Access denied to workflows from other departments'}), 403
data = request.get_json()
logger.info(f"Update workflow request data keys: {data.keys() if data else 'None'}")
if 'markdown_template' in data:
template_length = len(data['markdown_template']) if data['markdown_template'] else 0
logger.info(f"Received markdown_template with length: {template_length}")
if template_length > 0:
logger.info(f"First 100 chars: {data['markdown_template'][:100]}")
# Update fields if provided
if 'title' in data:
# Check if new title already exists in this department
if data['title'] != workflow.title:
existing_workflow = Workflow.find_by_title(data['title'], current_user.department_id)
if existing_workflow:
return jsonify({'message': 'Workflow with this title already exists in your department'}), 400
workflow.title = data['title']
if 'description' in data:
workflow.description = data['description']
if 'data_requirements' in data:
workflow.data_requirements = data['data_requirements']
if 'form_fields' in data:
workflow.form_fields = data['form_fields']
if 'markdown_template' in data:
workflow.markdown_template = data['markdown_template']
if 'template_name' in data:
workflow.template_name = data['template_name']
# Save changes
if workflow.save():
return jsonify({
'message': 'Workflow updated successfully',
'workflow': workflow.to_dict()
}), 200
else:
return jsonify({'message': 'Failed to update workflow'}), 500
def delete_workflow(current_user, workflow_id):
"""Delete a workflow"""
# Check if user has admin permissions
if current_user.permissions != 'Admin':
return jsonify({'message': 'Admin permissions required'}), 403
workflow = Workflow.find_by_id(workflow_id)
if not workflow:
return jsonify({'message': 'Workflow not found'}), 404
# Check if user belongs to the same department as the workflow
if str(workflow.department_id) != str(current_user.department_id):
return jsonify({'message': 'Access denied to workflows from other departments'}), 403
# Get department to remove workflow reference
department = Department.find_by_id(current_user.department_id)
if department:
department.remove_workflow(workflow_id)
# Delete the workflow
if workflow.delete():
return jsonify({'message': 'Workflow deleted successfully'}), 200
else:
return jsonify({'message': 'Failed to delete workflow'}), 500
def get_department_workflows(current_user):
"""Get all workflows for the user's department"""
workflows = Workflow.find_by_department(current_user.department_id)
return jsonify({'workflows': [workflow.to_dict() for workflow in workflows]}), 200
def add_data_requirement(current_user, workflow_id):
"""Add a data requirement to a workflow"""
# Check if user has admin permissions
if current_user.permissions != 'Admin':
return jsonify({'message': 'Admin permissions required'}), 403
workflow = Workflow.find_by_id(workflow_id)
if not workflow:
return jsonify({'message': 'Workflow not found'}), 404
# Check if user belongs to the same department as the workflow
if str(workflow.department_id) != str(current_user.department_id):
return jsonify({'message': 'Access denied to workflows from other departments'}), 403
data = request.get_json()
# Check if required fields are present
if 'field' not in data or 'description' not in data:
return jsonify({'message': 'Field and description are required'}), 400
# Add data requirement
if workflow.add_data_requirement(data['field'], data['description']):
return jsonify({
'message': 'Data requirement added successfully',
'workflow': workflow.to_dict()
}), 200
else:
return jsonify({'message': 'Failed to add data requirement'}), 500
def remove_data_requirement(current_user, workflow_id):
"""Remove a data requirement from a workflow"""
# Check if user has admin permissions
if current_user.permissions != 'Admin':
return jsonify({'message': 'Admin permissions required'}), 403
workflow = Workflow.find_by_id(workflow_id)
if not workflow:
return jsonify({'message': 'Workflow not found'}), 404
# Check if user belongs to the same department as the workflow
if str(workflow.department_id) != str(current_user.department_id):
return jsonify({'message': 'Access denied to workflows from other departments'}), 403
data = request.get_json()
# Check if required fields are present
if 'index' not in data:
return jsonify({'message': 'Index of data requirement to remove is required'}), 400
try:
index = int(data['index'])
if index < 0 or index >= len(workflow.data_requirements):
return jsonify({'message': 'Invalid index'}), 400
# Remove the data requirement at the specified index
workflow.data_requirements.pop(index)
if workflow.save():
return jsonify({
'message': 'Data requirement removed successfully',
'workflow': workflow.to_dict()
}), 200
else:
return jsonify({'message': 'Failed to remove data requirement'}), 500
except ValueError:
return jsonify({'message': 'Index must be a valid integer'}), 400
except Exception as e:
logger.error(f"Error removing data requirement: {str(e)}")
return jsonify({'message': f'Error removing data requirement: {str(e)}'}), 500
def upload_form(current_user, workflow_id):
"""Upload a markdown form template to a workflow"""
# Check if user has admin permissions
if current_user.permissions != 'Admin':
return jsonify({'message': 'Admin permissions required'}), 403
workflow = Workflow.find_by_id(workflow_id)
if not workflow:
return jsonify({'message': 'Workflow not found'}), 404
# Check if user belongs to the same department as the workflow
if str(workflow.department_id) != str(current_user.department_id):
return jsonify({'message': 'Access denied to workflows from other departments'}), 403
if 'file' not in request.files:
return jsonify({'message': 'No file part'}), 400
file = request.files['file']
if file.filename == '':
return jsonify({'message': 'No selected file'}), 400
if file and file.filename.endswith('.md'):
try:
# Read the markdown content
markdown_content = file.read().decode('utf-8')
# Store directly in the workflow model
workflow.markdown_template = markdown_content
workflow.template_name = file.filename
# Save the workflow
if workflow.save():
return jsonify({
'message': 'Form template uploaded successfully',
'workflow': workflow.to_dict()
}), 200
else:
return jsonify({'message': 'Failed to save workflow with markdown template'}), 500
except Exception as e:
logger.error(f"Error uploading markdown template: {str(e)}")
return jsonify({'message': f'Error uploading template: {str(e)}'}), 500
else:
return jsonify({'message': 'File must be a markdown (.md) file'}), 400
def remove_form(current_user, workflow_id):
"""Remove the form template from a workflow"""
# Check if user has admin permissions
if current_user.permissions != 'Admin':
return jsonify({'message': 'Admin permissions required'}), 403
workflow = Workflow.find_by_id(workflow_id)
if not workflow:
return jsonify({'message': 'Workflow not found'}), 404
# Check if user belongs to the same department as the workflow
if str(workflow.department_id) != str(current_user.department_id):
return jsonify({'message': 'Access denied to workflows from other departments'}), 403
# Check if there's a template to remove
if not workflow.markdown_template:
return jsonify({'message': 'No form template found to remove'}), 404
try:
# Clear the template using the model method
if workflow.clear_template():
return jsonify({
'message': 'Form template removed successfully',
'workflow': workflow.to_dict()
}), 200
else:
return jsonify({'message': 'Failed to remove form template from workflow'}), 500
except Exception as e:
logger.error(f"Error removing form template: {str(e)}")
return jsonify({'message': f'Error removing form template: {str(e)}'}), 500
def add_form_field(current_user, workflow_id):
"""Add a form field to a workflow"""
# Check if user has admin permissions
if current_user.permissions != 'Admin':
return jsonify({'message': 'Admin permissions required'}), 403
workflow = Workflow.find_by_id(workflow_id)
if not workflow:
return jsonify({'message': 'Workflow not found'}), 404
# Check if user belongs to the same department as the workflow
if str(workflow.department_id) != str(current_user.department_id):
return jsonify({'message': 'Access denied to workflows from other departments'}), 403
data = request.get_json()
# Check if required fields are present
if 'position' not in data or 'field_name' not in data:
return jsonify({'message': 'Position and field_name are required'}), 400
try:
position = data['position']
field_name = data['field_name']
# Add form field
if workflow.add_form_field(position, field_name):
return jsonify({
'message': 'Form field added successfully',
'workflow': workflow.to_dict()
}), 200
else:
return jsonify({'message': 'Failed to add form field'}), 500
except Exception as e:
logger.error(f"Error adding form field: {str(e)}")
return jsonify({'message': f'Error adding form field: {str(e)}'}), 500
def remove_form_field(current_user, workflow_id):
"""Remove a form field from a workflow"""
# Check if user has admin permissions
if current_user.permissions != 'Admin':
return jsonify({'message': 'Admin permissions required'}), 403
workflow = Workflow.find_by_id(workflow_id)
if not workflow:
return jsonify({'message': 'Workflow not found'}), 404
# Check if user belongs to the same department as the workflow
if str(workflow.department_id) != str(current_user.department_id):
return jsonify({'message': 'Access denied to workflows from other departments'}), 403
data = request.get_json()
# Check if required fields are present
if 'index' not in data:
return jsonify({'message': 'Index of form field to remove is required'}), 400
try:
index = int(data['index'])
if index < 0 or index >= len(workflow.form_fields):
return jsonify({'message': 'Invalid index'}), 400
# Remove the form field at the specified index
workflow.form_fields.pop(index)
if workflow.save():
return jsonify({
'message': 'Form field removed successfully',
'workflow': workflow.to_dict()
}), 200
else:
return jsonify({'message': 'Failed to remove form field'}), 500
except ValueError:
return jsonify({'message': 'Index must be a valid integer'}), 400
except Exception as e:
logger.error(f"Error removing form field: {str(e)}")
return jsonify({'message': f'Error removing form field: {str(e)}'}), 500 |