130 lines
4.7 KiB
Python
130 lines
4.7 KiB
Python
from datetime import datetime
|
|
import json
|
|
from app.config.database import db
|
|
|
|
|
|
class ScriptGroup(db.Model):
|
|
"""Script group model for organizing scripts."""
|
|
|
|
__tablename__ = "script_groups"
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
name = db.Column(db.String(100), nullable=False)
|
|
directory_path = db.Column(db.String(255), nullable=False)
|
|
description = db.Column(db.Text)
|
|
required_level = db.Column(db.String(20), nullable=False)
|
|
conda_environment = db.Column(db.String(100))
|
|
is_active = db.Column(db.Boolean, default=True)
|
|
discovered_at = db.Column(db.DateTime, default=datetime.utcnow)
|
|
|
|
# Relationships
|
|
scripts = db.relationship("Script", backref="script_group", lazy=True)
|
|
user_projects = db.relationship("UserProject", backref="script_group", lazy=True)
|
|
|
|
@property
|
|
def display_name(self):
|
|
"""Get display name for the script group."""
|
|
return self.name
|
|
|
|
@property
|
|
def conda_env(self):
|
|
"""Get conda environment."""
|
|
return self.conda_environment
|
|
|
|
def get_description(self, language="en"):
|
|
"""Get description in specified language."""
|
|
if not self.description:
|
|
return None
|
|
|
|
try:
|
|
# Try to parse as JSON for multi-language descriptions
|
|
descriptions = json.loads(self.description)
|
|
if isinstance(descriptions, dict):
|
|
# Return the requested language or fallback to English
|
|
return descriptions.get(language, descriptions.get("en", ""))
|
|
else:
|
|
# If it's not a dict, return as-is
|
|
return self.description
|
|
except (json.JSONDecodeError, TypeError):
|
|
# If it's not valid JSON, return as plain text
|
|
return self.description
|
|
|
|
def to_dict(self):
|
|
"""Convert script group to dictionary."""
|
|
return {
|
|
"id": self.id,
|
|
"name": self.name,
|
|
"directory_path": self.directory_path,
|
|
"description": self.description,
|
|
"required_level": self.required_level,
|
|
"conda_environment": self.conda_environment,
|
|
"is_active": self.is_active,
|
|
"discovered_at": (
|
|
self.discovered_at.isoformat() if self.discovered_at else None
|
|
),
|
|
"scripts_count": len(self.scripts),
|
|
}
|
|
|
|
|
|
class Script(db.Model):
|
|
"""Script model for individual scripts."""
|
|
|
|
__tablename__ = "scripts"
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
group_id = db.Column(db.Integer, db.ForeignKey("script_groups.id"), nullable=False)
|
|
filename = db.Column(db.String(100), nullable=False)
|
|
display_name = db.Column(db.String(100))
|
|
description = db.Column(db.Text)
|
|
description_long_path = db.Column(db.String(255))
|
|
tags = db.Column(db.Text) # Comma-separated tags
|
|
required_level = db.Column(db.String(20), nullable=False)
|
|
parameters = db.Column(db.Text) # JSON string
|
|
is_active = db.Column(db.Boolean, default=True)
|
|
last_modified = db.Column(db.DateTime)
|
|
|
|
# Relationships
|
|
user_script_tags = db.relationship("UserScriptTag", backref="script", lazy=True)
|
|
execution_logs = db.relationship("ExecutionLog", backref="script", lazy=True)
|
|
|
|
def get_parameters(self):
|
|
"""Get parameters as Python object."""
|
|
if self.parameters:
|
|
try:
|
|
return json.loads(self.parameters)
|
|
except json.JSONDecodeError:
|
|
return []
|
|
return []
|
|
|
|
def set_parameters(self, params):
|
|
"""Set parameters from Python object."""
|
|
self.parameters = json.dumps(params) if params else None
|
|
|
|
def get_tags_list(self):
|
|
"""Get tags as list."""
|
|
if self.tags:
|
|
return [tag.strip() for tag in self.tags.split(",") if tag.strip()]
|
|
return []
|
|
|
|
def set_tags_list(self, tags_list):
|
|
"""Set tags from list."""
|
|
self.tags = ",".join(tags_list) if tags_list else None
|
|
|
|
def to_dict(self):
|
|
"""Convert script to dictionary."""
|
|
return {
|
|
"id": self.id,
|
|
"group_id": self.group_id,
|
|
"filename": self.filename,
|
|
"display_name": self.display_name,
|
|
"description": self.description,
|
|
"description_long_path": self.description_long_path,
|
|
"tags": self.get_tags_list(),
|
|
"required_level": self.required_level,
|
|
"parameters": self.get_parameters(),
|
|
"is_active": self.is_active,
|
|
"last_modified": (
|
|
self.last_modified.isoformat() if self.last_modified else None
|
|
),
|
|
}
|