Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMP] cetmix_tower_server: implement UI updates #185

Open
wants to merge 2 commits into
base: 14.0-dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions cetmix_tower_server/models/cx_tower_command_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,20 @@ class CxTowerCommandLog(models.Model):
plan_log_id = fields.Many2one(comodel_name="cx.tower.plan.log", ondelete="cascade")
triggered_plan_log_id = fields.Many2one(comodel_name="cx.tower.plan.log")

# -- Related fields for plan_log_id
plan_start_date = fields.Datetime(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to show all these fields. Just show the related flight_plan_line.

<field name="plan_log_id" attrs="{'invisible': [('plan_log_id', '=', False)]}"/>

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still pending.

related="plan_log_id.start_date", store=True, string="Plan Start Date"
)
plan_finish_date = fields.Datetime(
related="plan_log_id.finish_date", store=True, string="Plan Finish Date"
)
plan_status = fields.Integer(
related="plan_log_id.plan_status", store=True, string="Plan Status"
)
plan_is_running = fields.Boolean(
related="plan_log_id.is_running", store=True, string="Plan Running"
)

@api.depends("name", "command_id.name")
def _compute_name(self):
for rec in self:
Expand Down
23 changes: 22 additions & 1 deletion cetmix_tower_server/models/cx_tower_plan_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ class CxTowerPlanLine(models.Model):
help="Actions trigger based on command result."
" If empty next command will be executed",
)
command_code = fields.Text(related="command_id.code", readonly=True)
command_code = fields.Text(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach doesn't allow to check those lines if required.
So we need to add a compute o2m field for lines and hide/show it based on the command action.

string="Code", compute="_compute_command_code", readonly=True
)
action = fields.Selection(related="command_id.action", readonly=True)
tag_ids = fields.Many2many(related="command_id.tag_ids", readonly=True)
access_level = fields.Selection(
Expand Down Expand Up @@ -88,6 +90,25 @@ def _check_command_id(self):
visited_plans = set()
self._check_recursive_plan(line.command_id, visited_plans)

@api.depends("command_id", "action")
def _compute_command_code(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to compute anything. Just hide this field if command action is not SSH or Python.

"""
Compute the preview of the command based on its action.
"""
for line in self:
if line.action == "file_using_template":
line.command_code = line.command_id.code or "No template code available"
elif line.action == "plan":
flight_plan_lines = line.command_id.flight_plan_id.line_ids
preview_lines = [line.name for line in flight_plan_lines]
line.command_code = (
"\n".join(preview_lines)
if preview_lines
else "No related lines available"
)
else:
line.command_code = "No preview available"

Copy link

@coderabbitai coderabbitai bot Jan 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add validation checks for better error handling.

The compute method should validate the existence of required related records to prevent potential errors.

     @api.depends("command_id", "action")
     def _compute_command_code(self):
         """
         Compute the preview of the command based on its action.
         """
         for line in self:
+            if not line.command_id:
+                line.command_code = "No command selected"
+                continue
+
             if line.action == "file_using_template":
                 line.command_code = line.command_id.code or "No template code available"
             elif line.action == "plan":
+                if not line.command_id.flight_plan_id:
+                    line.command_code = "No flight plan selected"
+                    continue
+
                 flight_plan_lines = line.command_id.flight_plan_id.line_ids
                 preview_lines = [line.name for line in flight_plan_lines]
                 line.command_code = (
                     "\n".join(preview_lines)
                     if preview_lines
                     else "No related lines available"
                 )
             else:
                 line.command_code = "No preview available"
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
@api.depends("command_id", "action")
def _compute_command_code(self):
"""
Compute the preview of the command based on its action.
"""
for line in self:
if line.action == "file_using_template":
line.command_code = line.command_id.code or "No template code available"
elif line.action == "plan":
flight_plan_lines = line.command_id.flight_plan_id.line_ids
preview_lines = [line.name for line in flight_plan_lines]
line.command_code = (
"\n".join(preview_lines)
if preview_lines
else "No related lines available"
)
else:
line.command_code = "No preview available"
@api.depends("command_id", "action")
def _compute_command_code(self):
"""
Compute the preview of the command based on its action.
"""
for line in self:
if not line.command_id:
line.command_code = "No command selected"
continue
if line.action == "file_using_template":
line.command_code = line.command_id.code or "No template code available"
elif line.action == "plan":
if not line.command_id.flight_plan_id:
line.command_code = "No flight plan selected"
continue
flight_plan_lines = line.command_id.flight_plan_id.line_ids
preview_lines = [line.name for line in flight_plan_lines]
line.command_code = (
"\n".join(preview_lines)
if preview_lines
else "No related lines available"
)
else:
line.command_code = "No preview available"

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now, don't as part of this task.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tendil Understood, I'll skip the validation checks for now. Would you like me to create a follow-up issue to track this improvement for future implementation?

def _check_recursive_plan(self, command, visited_plans):
"""
Recursively check if the command plan creates a cycle.
Expand Down
9 changes: 8 additions & 1 deletion cetmix_tower_server/views/cx_tower_command_log_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@
<field name="duration_current" />
</group>
</group>
<group>
<field name="plan_log_id" />
<field name="plan_start_date" string="Plan Start Date" />
<field name="plan_finish_date" string="Plan Finish Date" />
<field name="plan_status" string="Plan Status" />
<field name="plan_is_running" string="Plan Running" />
</group>
<notebook attrs="{'invisible': [('command_action', '=', 'plan')]}">
<page name="result" string="Result">
<field
Expand All @@ -99,7 +106,7 @@
<field name="model">cx.tower.command.log</field>
<field name="arch" type="xml">
<tree
default_order="id"
default_order="start_date desc, id desc"
decoration-danger="command_status not in [0, -20]"
decoration-info="is_running == True"
decoration-muted="command_status == -20"
Expand Down
5 changes: 5 additions & 0 deletions cetmix_tower_server/views/cx_tower_command_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,11 @@
name="file_from_template"
domain="[('action', '=', 'file_using_template')]"
/>
<filter
string="Run Flight Plan"
name="filter_flight_plan"
domain="[('action', '=', 'plan')]"
/>
<separator />
<filter
string="Tagged"
Expand Down
1 change: 1 addition & 0 deletions cetmix_tower_server/views/cx_tower_plan_log_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
<field name="model">cx.tower.plan.log</field>
<field name="arch" type="xml">
<tree
default_order="start_date desc, id desc"
decoration-danger="plan_status != 0"
decoration-info="is_running == True"
>
Expand Down
6 changes: 5 additions & 1 deletion cetmix_tower_server/views/cx_tower_server_template_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<field name="tag_ids" />
<field name="os_id" />
<field name="color" />
<field name="server_count" />
<templates>
<t t-name="kanban-box">
<div
Expand Down Expand Up @@ -56,7 +57,10 @@
<div class="oe_kanban_global_click">
<div class="col-12 pt8 o_kanban_primary_right">
<div>
<field name="reference" />
<strong>Servers:</strong>
<span class="badge badge-primary">
<t t-esc="record.server_count.value" />
</span>
</div>
<div attrs="{'invisible': [('os_id', '=', False)]}">
<strong>Operating System:</strong>
Expand Down
Loading