separate logic for workare and publishe workfiles

This commit is contained in:
Jakub Trllo 2025-11-07 15:52:47 +01:00
parent ece086c03f
commit 0cc99003f6

View file

@ -1,4 +1,5 @@
import datetime
from typing import Optional
from qtpy import QtCore, QtWidgets
@ -7,7 +8,11 @@ def file_size_to_string(file_size):
if not file_size:
return "N/A"
size = 0
size_ending_mapping = {"KB": 1024**1, "MB": 1024**2, "GB": 1024**3}
size_ending_mapping = {
"KB": 1024**1,
"MB": 1024**2,
"GB": 1024**3,
}
ending = "B"
for _ending, _size in size_ending_mapping.items():
if file_size < _size:
@ -66,7 +71,8 @@ class SidePanelWidget(QtWidgets.QWidget):
btn_description_save.clicked.connect(self._on_save_click)
controller.register_event_callback(
"selection.workarea.changed", self._on_workarea_selection_change
"selection.workarea.changed",
self._on_workarea_selection_change
)
controller.register_event_callback(
"selection.representation.changed",
@ -86,9 +92,9 @@ class SidePanelWidget(QtWidgets.QWidget):
self._orig_description = ""
self._controller = controller
self._set_context(None, None, None, None, None)
self._set_context(False, None, None)
def set_published_mode(self, published_mode):
def set_published_mode(self, published_mode: bool) -> None:
"""Change published mode.
Args:
@ -97,7 +103,19 @@ class SidePanelWidget(QtWidgets.QWidget):
self._description_widget.setVisible(not published_mode)
# Clear the context when switching modes to avoid showing stale data
self._set_context(None, None, None, None, None)
if published_mode:
self._set_publish_context(
self._folder_id,
self._task_id,
self._representation_id,
)
else:
self._set_workarea_context(
self._folder_id,
self._task_id,
self._rootless_path,
self._filepath,
)
def _on_workarea_selection_change(self, event):
folder_id = event["folder_id"]
@ -105,12 +123,16 @@ class SidePanelWidget(QtWidgets.QWidget):
filepath = event["path"]
rootless_path = event["rootless_path"]
self._set_context(folder_id, task_id, rootless_path, filepath, None)
self._set_workarea_context(
folder_id, task_id, rootless_path, filepath
)
def _on_representation_selection_change(self, event):
folder_id = event["folder_id"]
task_id = event["task_id"]
representation_id = event["representation_id"]
self._set_context(None, None, None, None, representation_id)
self._set_publish_context(folder_id, task_id, representation_id)
def _on_description_change(self):
text = self._description_input.toPlainText()
@ -126,130 +148,135 @@ class SidePanelWidget(QtWidgets.QWidget):
self._orig_description = description
self._btn_description_save.setEnabled(False)
def _set_context(
self, folder_id, task_id, rootless_path, filepath, representation_id
):
workfile_info = None
published_workfile_info = None
def _set_workarea_context(
self,
folder_id: Optional[str],
task_id: Optional[str],
rootless_path: Optional[str],
filepath: Optional[str],
) -> None:
self._rootless_path = rootless_path
self._filepath = filepath
# Check if folder, task and file are selected (workarea mode)
workfile_info = None
# Check if folder, task and file are selected
if folder_id and task_id and rootless_path:
workfile_info = self._controller.get_workfile_info(
folder_id, task_id, rootless_path
)
# Check if representation is selected (published mode)
elif representation_id:
published_workfile_info = (
self._controller.get_published_workfile_info(representation_id)
)
# Get version comment for published workfiles
version_comment = None
if representation_id and published_workfile_info:
version_comment = (
self._controller.get_published_workfile_version_comment(
representation_id
)
)
enabled = (
workfile_info is not None or published_workfile_info is not None
)
self._details_input.setEnabled(enabled)
self._description_input.setEnabled(workfile_info is not None)
self._btn_description_save.setEnabled(workfile_info is not None)
self._folder_id = folder_id
self._task_id = task_id
self._filepath = filepath
self._rootless_path = rootless_path
self._representation_id = representation_id
# Disable inputs and remove texts if any required arguments are
# missing
if not enabled:
if workfile_info is None:
self._orig_description = ""
self._details_input.setPlainText("")
self._description_input.setPlainText("")
self._set_context(False, folder_id, task_id)
return
# Use published workfile info if available, otherwise use workarea
# info
info = (
published_workfile_info
if published_workfile_info
else workfile_info
self._set_context(
True,
folder_id,
task_id,
file_created = workfile_info.file_created,
file_modified=workfile_info.file_modified,
size_value=workfile_info.file_size,
created_by=workfile_info.created_by,
updated_by=workfile_info.updated_by,
)
description = info.description if hasattr(info, "description") else ""
size_value = file_size_to_string(info.file_size)
description = workfile_info.description
self._orig_description = description
self._description_input.setPlainText(description)
def _set_publish_context(
self,
folder_id: Optional[str],
task_id: Optional[str],
representation_id: Optional[str],
) -> None:
self._representation_id = representation_id
published_workfile_wrap = self._controller.get_published_workfile_info(
folder_id,
representation_id,
)
info = published_workfile_wrap.info
comment = published_workfile_wrap.comment
if info is None:
self._set_context(False, folder_id, task_id)
return
self._set_context(
True,
folder_id,
task_id,
file_created=info.file_created,
file_modified=info.file_modified,
size_value=info.file_size,
created_by=info.author,
comment=comment,
)
def _set_context(
self,
is_valid: bool,
folder_id: Optional[str],
task_id: Optional[str],
*,
file_created: Optional[int] = None,
file_modified: Optional[int] = None,
size_value: Optional[int] = None,
created_by: Optional[str] = None,
updated_by: Optional[str] = None,
comment: Optional[str] = None,
) -> None:
self._folder_id = folder_id
self._task_id = task_id
self._details_input.setEnabled(is_valid)
self._description_input.setEnabled(is_valid)
self._btn_description_save.setEnabled(is_valid)
if not is_valid:
self._details_input.setPlainText("")
return
# Append html string
datetime_format = "%b %d %Y %H:%M:%S"
file_created = info.file_created
modification_time = info.file_modified
if file_created:
file_created = datetime.datetime.fromtimestamp(file_created)
if modification_time:
modification_time = datetime.datetime.fromtimestamp(
modification_time
if file_modified:
file_modified = datetime.datetime.fromtimestamp(
file_modified
)
user_items_by_name = self._controller.get_user_items_by_name()
def convert_username(username):
user_item = user_items_by_name.get(username)
def convert_username(username_v):
user_item = user_items_by_name.get(username_v)
if user_item is not None and user_item.full_name:
return user_item.full_name
return username
return username_v
created_lines = []
# For published workfiles, use 'author' field
if published_workfile_info:
if published_workfile_info.author:
created_lines.append(
convert_username(published_workfile_info.author)
)
if file_created:
created_lines.append(file_created.strftime(datetime_format))
else:
# For workarea workfiles, use 'created_by' field
if workfile_info.created_by:
created_lines.append(
convert_username(workfile_info.created_by)
)
if file_created:
created_lines.append(file_created.strftime(datetime_format))
lines = []
if size_value is not None:
size_value = file_size_to_string(size_value)
lines.append(f"<b>Size:</b><br/>{size_value}")
if created_lines:
created_lines.insert(0, "<b>Created:</b>")
modified_lines = []
# For workarea workfiles, show 'updated_by'
if workfile_info and workfile_info.updated_by:
modified_lines.append(convert_username(workfile_info.updated_by))
if modification_time:
modified_lines.append(modification_time.strftime(datetime_format))
if modified_lines:
modified_lines.insert(0, "<b>Modified:</b>")
lines = [
"<b>Size:</b>",
size_value,
]
# Add version comment for published workfiles
if version_comment:
lines.append(f"<b>Comment:</b><br/>{version_comment}")
if created_lines:
lines.append("<br/>".join(created_lines))
if modified_lines:
lines.append("<br/>".join(modified_lines))
if comment:
lines.append(f"<b>Comment:</b><br/>{comment}")
self._orig_description = description
self._description_input.setPlainText(description)
if created_by or file_created:
lines.append("<b>Created:</b>")
if created_by:
lines.append(convert_username(created_by))
if file_created:
lines.append(file_created.strftime(datetime_format))
if updated_by or file_modified:
lines.append("<b>Modified:</b>")
if updated_by:
lines.append(convert_username(updated_by))
if file_modified:
lines.append(file_modified.strftime(datetime_format))
# Set as empty string
self._details_input.setPlainText("")
self._details_input.appendHtml("<br/>".join(lines))
self._details_input.appendHtml("<br/>".join(lines))