Save and restore scripts with saved requests to projects
This commit is contained in:
parent
59dcb78789
commit
f39acaf02e
@ -98,6 +98,7 @@ class SavedRequest:
|
|||||||
request: HttpRequest
|
request: HttpRequest
|
||||||
created_at: str # ISO format
|
created_at: str # ISO format
|
||||||
modified_at: str # ISO format
|
modified_at: str # ISO format
|
||||||
|
scripts: Optional['Scripts'] = None # Scripts for preprocessing and postprocessing
|
||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
"""Convert to dictionary for JSON serialization."""
|
"""Convert to dictionary for JSON serialization."""
|
||||||
@ -106,7 +107,8 @@ class SavedRequest:
|
|||||||
'name': self.name,
|
'name': self.name,
|
||||||
'request': self.request.to_dict(),
|
'request': self.request.to_dict(),
|
||||||
'created_at': self.created_at,
|
'created_at': self.created_at,
|
||||||
'modified_at': self.modified_at
|
'modified_at': self.modified_at,
|
||||||
|
'scripts': self.scripts.to_dict() if self.scripts else None
|
||||||
}
|
}
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@ -117,7 +119,8 @@ class SavedRequest:
|
|||||||
name=data['name'],
|
name=data['name'],
|
||||||
request=HttpRequest.from_dict(data['request']),
|
request=HttpRequest.from_dict(data['request']),
|
||||||
created_at=data['created_at'],
|
created_at=data['created_at'],
|
||||||
modified_at=data['modified_at']
|
modified_at=data['modified_at'],
|
||||||
|
scripts=Scripts.from_dict(data['scripts']) if data.get('scripts') else None
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -111,7 +111,7 @@ class ProjectManager:
|
|||||||
projects = [p for p in projects if p.id != project_id]
|
projects = [p for p in projects if p.id != project_id]
|
||||||
self.save_projects(projects)
|
self.save_projects(projects)
|
||||||
|
|
||||||
def add_request(self, project_id: str, name: str, request: HttpRequest) -> SavedRequest:
|
def add_request(self, project_id: str, name: str, request: HttpRequest, scripts=None) -> SavedRequest:
|
||||||
"""Add request to a project."""
|
"""Add request to a project."""
|
||||||
projects = self.load_projects()
|
projects = self.load_projects()
|
||||||
now = datetime.now(timezone.utc).isoformat()
|
now = datetime.now(timezone.utc).isoformat()
|
||||||
@ -120,7 +120,8 @@ class ProjectManager:
|
|||||||
name=name,
|
name=name,
|
||||||
request=request,
|
request=request,
|
||||||
created_at=now,
|
created_at=now,
|
||||||
modified_at=now
|
modified_at=now,
|
||||||
|
scripts=scripts
|
||||||
)
|
)
|
||||||
for p in projects:
|
for p in projects:
|
||||||
if p.id == project_id:
|
if p.id == project_id:
|
||||||
@ -140,7 +141,7 @@ class ProjectManager:
|
|||||||
break
|
break
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def update_request(self, project_id: str, request_id: str, name: str, request: HttpRequest) -> SavedRequest:
|
def update_request(self, project_id: str, request_id: str, name: str, request: HttpRequest, scripts=None) -> SavedRequest:
|
||||||
"""Update an existing request."""
|
"""Update an existing request."""
|
||||||
projects = self.load_projects()
|
projects = self.load_projects()
|
||||||
updated_request = None
|
updated_request = None
|
||||||
@ -150,6 +151,7 @@ class ProjectManager:
|
|||||||
if req.id == request_id:
|
if req.id == request_id:
|
||||||
req.name = name
|
req.name = name
|
||||||
req.request = request
|
req.request = request
|
||||||
|
req.scripts = scripts
|
||||||
req.modified_at = datetime.now(timezone.utc).isoformat()
|
req.modified_at = datetime.now(timezone.utc).isoformat()
|
||||||
updated_request = req
|
updated_request = req
|
||||||
break
|
break
|
||||||
|
|||||||
@ -705,6 +705,7 @@ class RosterWindow(Adw.ApplicationWindow):
|
|||||||
return
|
return
|
||||||
|
|
||||||
request = widget.get_request()
|
request = widget.get_request()
|
||||||
|
scripts = widget.get_scripts()
|
||||||
|
|
||||||
if not request.url.strip():
|
if not request.url.strip():
|
||||||
self._show_toast("Cannot save: URL is empty")
|
self._show_toast("Cannot save: URL is empty")
|
||||||
@ -779,10 +780,10 @@ class RosterWindow(Adw.ApplicationWindow):
|
|||||||
existing = self.project_manager.find_request_by_name(project.id, name)
|
existing = self.project_manager.find_request_by_name(project.id, name)
|
||||||
if existing:
|
if existing:
|
||||||
# Show overwrite confirmation
|
# Show overwrite confirmation
|
||||||
self._show_overwrite_dialog(project, name, existing.id, request)
|
self._show_overwrite_dialog(project, name, existing.id, request, scripts)
|
||||||
else:
|
else:
|
||||||
# No duplicate, save normally
|
# No duplicate, save normally
|
||||||
saved_request = self.project_manager.add_request(project.id, name, request)
|
saved_request = self.project_manager.add_request(project.id, name, request, scripts)
|
||||||
self._load_projects()
|
self._load_projects()
|
||||||
self._show_toast(f"Saved as '{name}'")
|
self._show_toast(f"Saved as '{name}'")
|
||||||
|
|
||||||
@ -824,7 +825,7 @@ class RosterWindow(Adw.ApplicationWindow):
|
|||||||
# Update tab page title (widget.modified is now False, so no star)
|
# Update tab page title (widget.modified is now False, so no star)
|
||||||
page.set_title(name)
|
page.set_title(name)
|
||||||
|
|
||||||
def _show_overwrite_dialog(self, project, name, existing_request_id, request):
|
def _show_overwrite_dialog(self, project, name, existing_request_id, request, scripts=None):
|
||||||
"""Show dialog asking if user wants to overwrite existing request."""
|
"""Show dialog asking if user wants to overwrite existing request."""
|
||||||
dialog = Adw.AlertDialog()
|
dialog = Adw.AlertDialog()
|
||||||
dialog.set_heading("Request Already Exists")
|
dialog.set_heading("Request Already Exists")
|
||||||
@ -839,7 +840,7 @@ class RosterWindow(Adw.ApplicationWindow):
|
|||||||
def on_overwrite_response(dlg, response):
|
def on_overwrite_response(dlg, response):
|
||||||
if response == "overwrite":
|
if response == "overwrite":
|
||||||
# Update the existing request
|
# Update the existing request
|
||||||
self.project_manager.update_request(project.id, existing_request_id, name, request)
|
self.project_manager.update_request(project.id, existing_request_id, name, request, scripts)
|
||||||
self._load_projects()
|
self._load_projects()
|
||||||
self._show_toast(f"Updated '{name}'")
|
self._show_toast(f"Updated '{name}'")
|
||||||
|
|
||||||
@ -893,6 +894,7 @@ class RosterWindow(Adw.ApplicationWindow):
|
|||||||
def _on_load_request(self, widget, saved_request):
|
def _on_load_request(self, widget, saved_request):
|
||||||
"""Load saved request - smart loading based on current tab state."""
|
"""Load saved request - smart loading based on current tab state."""
|
||||||
req = saved_request.request
|
req = saved_request.request
|
||||||
|
scripts = saved_request.scripts
|
||||||
|
|
||||||
# First, check if this request is already open in an unmodified tab
|
# First, check if this request is already open in an unmodified tab
|
||||||
existing_tab = self._find_tab_by_saved_request_id(saved_request.id)
|
existing_tab = self._find_tab_by_saved_request_id(saved_request.id)
|
||||||
@ -932,6 +934,7 @@ class RosterWindow(Adw.ApplicationWindow):
|
|||||||
current_tab.saved_request_id = link_to_saved
|
current_tab.saved_request_id = link_to_saved
|
||||||
current_tab.project_id = project_id
|
current_tab.project_id = project_id
|
||||||
current_tab.selected_environment_id = default_env_id
|
current_tab.selected_environment_id = default_env_id
|
||||||
|
current_tab.scripts = scripts
|
||||||
|
|
||||||
# Update widget with project context
|
# Update widget with project context
|
||||||
widget.project_id = project_id
|
widget.project_id = project_id
|
||||||
@ -945,6 +948,10 @@ class RosterWindow(Adw.ApplicationWindow):
|
|||||||
# Load request into widget
|
# Load request into widget
|
||||||
widget._load_request(req)
|
widget._load_request(req)
|
||||||
|
|
||||||
|
# Load scripts into widget
|
||||||
|
if scripts:
|
||||||
|
widget.load_scripts(scripts)
|
||||||
|
|
||||||
if is_copy:
|
if is_copy:
|
||||||
# This is a copy - mark as unsaved
|
# This is a copy - mark as unsaved
|
||||||
current_tab.original_request = None
|
current_tab.original_request = None
|
||||||
@ -971,7 +978,8 @@ class RosterWindow(Adw.ApplicationWindow):
|
|||||||
request=req,
|
request=req,
|
||||||
saved_request_id=link_to_saved,
|
saved_request_id=link_to_saved,
|
||||||
project_id=project_id,
|
project_id=project_id,
|
||||||
selected_environment_id=default_env_id
|
selected_environment_id=default_env_id,
|
||||||
|
scripts=scripts
|
||||||
)
|
)
|
||||||
|
|
||||||
# If it's a copy, clear the original_request to mark as unsaved
|
# If it's a copy, clear the original_request to mark as unsaved
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user