Skip to content

Commit

Permalink
✨ sync: make links dependent on project
Browse files Browse the repository at this point in the history
  • Loading branch information
yelizariev committed May 11, 2024
1 parent 0d9fcb6 commit 1557adc
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 19 deletions.
4 changes: 2 additions & 2 deletions sync/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"name": "Sync 🪬 Studio",
"summary": """Join the Amazing 😍 Community ⤵️""",
"category": "VooDoo ✨ Magic",
"version": "16.0.7.0.0",
"version": "16.0.11.0.0",
"application": True,
"author": "Ivan Kropotkin",
"support": "[email protected]",
Expand All @@ -26,8 +26,8 @@
"views/sync_trigger_webhook_views.xml",
"views/sync_trigger_button_views.xml",
"views/sync_task_views.xml",
"views/sync_project_views.xml",
"views/sync_link_views.xml",
"views/sync_project_views.xml",
"data/queue_job_function_data.xml",
],
"assets": {
Expand Down
6 changes: 6 additions & 0 deletions sync/doc/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
`11.0.0`
-------

- **New:** Use prime numbers for major releases ;-)
- **Improvement:** make links dependent on project

`7.0.0`
-------

Expand Down
11 changes: 8 additions & 3 deletions sync/models/sync_link.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2020 Ivan Yelizariev <https://twitter.com/yelizariev>
# Copyright 2020,2024 Ivan Yelizariev <https://twitter.com/yelizariev>
# License MIT (https://opensource.org/licenses/MIT).

import logging
Expand All @@ -23,6 +23,7 @@ class SyncLink(models.Model):
_description = "Resource Links"
_order = "id desc"

project_id = fields.Char("Project")
relation = fields.Char("Relation Name", required=True)
system1 = fields.Char("System 1", required=True)
# index system2 only to make search "Odoo links"
Expand All @@ -40,7 +41,7 @@ def _auto_init(self):
self._cr,
"sync_link_refs_uniq_index",
self._table,
["relation", "system1", "system2", "ref1", "ref2", "model"],
["project_id", "relation", "system1", "system2", "ref1", "ref2", "model"],
)
return res

Expand Down Expand Up @@ -81,6 +82,7 @@ def _set_link_external(
self, relation, external_refs, sync_date=None, allow_many2many=False, model=None
):
vals = self.refs2vals(external_refs)
vals["project_id"] = self.env.context.get("sync_project_id")
# Check for existing records
if allow_many2many:
existing = self._search_links_external(relation, external_refs)
Expand Down Expand Up @@ -142,7 +144,10 @@ def _search_links_external(
self, relation, external_refs, model=None, make_logs=False
):
vals = self.refs2vals(external_refs)
domain = [("relation", "=", relation)]
domain = [
("relation", "=", relation),
("project_id", "=", self.env.context.get("sync_project_id")),
]
if model:
domain.append(("model", "=", model))
for k, v in vals.items():
Expand Down
9 changes: 8 additions & 1 deletion sync/models/sync_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ class SyncProject(models.Model):
job_count = fields.Integer(compute="_compute_job_count")
log_ids = fields.One2many("ir.logging", "sync_project_id")
log_count = fields.Integer(compute="_compute_log_count")
link_ids = fields.One2many("sync.link", "project_id")
link_count = fields.Integer(compute="_compute_link_count")

def copy(self, default=None):
default = dict(default or {})
Expand Down Expand Up @@ -134,6 +136,11 @@ def _compute_log_count(self):
for r in self:
r.log_count = len(r.log_ids)

@api.depends("link_ids")
def _compute_link_count(self):
for r in self:
r.link_count = len(r.link_ids)

def _compute_triggers(self):
for r in self:
r.trigger_cron_count = len(r.mapped("task_ids.cron_ids"))
Expand Down Expand Up @@ -249,7 +256,7 @@ def record2image(record, fname="image_1920"):
)
)

context = dict(self.env.context, log_function=log)
context = dict(self.env.context, log_function=log, sync_project_id=self.id)
env = self.env(context=context)
link_functions = env["sync.link"]._get_eval_context()
MAGIC = AttrDict(
Expand Down
9 changes: 7 additions & 2 deletions sync/tests/test_links.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2020 Ivan Yelizariev <https://twitter.com/yelizariev>
# Copyright 2020,2024 Ivan Yelizariev <https://twitter.com/yelizariev>
# License MIT (https://opensource.org/licenses/MIT).

import uuid
Expand All @@ -17,7 +17,12 @@ def generate_ref():
class TestLink(TransactionCase):
def setUp(self):
super(TestLink, self).setUp()
funcs = self.env["sync.link"]._get_eval_context()
project = self.env["sync.project"].create({"name": "Test Project"})
funcs = (
self.env["sync.link"]
.with_context(sync_project_id=project.id)
._get_eval_context()
)
self.get_link = funcs["get_link"]
self.set_link = funcs["set_link"]
self.search_links = funcs["search_links"]
Expand Down
13 changes: 12 additions & 1 deletion sync/views/sync_link_views.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!-- Copyright 2020 Ivan Yelizariev <https://twitter.com/yelizariev>
<!-- Copyright 2020,2024 Ivan Yelizariev <https://twitter.com/yelizariev>
License MIT (https://opensource.org/licenses/MIT). -->
<odoo>
<record id="sync_link_view_tree" model="ir.ui.view">
<field name="name">sync.link.tree</field>
<field name="model">sync.link</field>
<field name="arch" type="xml">
<tree>
<field name="project_id" />
<field name="relation" />
<field name="system1" />
<field name="ref1" />
Expand All @@ -25,6 +26,7 @@
<sheet>
<group>
<group>
<field name="project_id" />
<field name="relation" />
<field name="system1" />
<field name="ref1" />
Expand Down Expand Up @@ -79,6 +81,15 @@
<field name="view_mode">tree,form</field>
<field name="search_view_id" ref="sync_link_view_search" />
</record>
<record id="sync_link_action_from_project" model="ir.actions.act_window">
<field name="name">Links</field>
<field name="res_model">sync.link</field>
<field name="view_mode">tree,form</field>
<field name="domain">[('project_id', '=', active_id)]</field>
<field name="context">
{'default_project_id': active_id, 'active_test': False}
</field>
</record>
<menuitem
id="sync_link_menu_action"
name="Links"
Expand Down
14 changes: 4 additions & 10 deletions sync/views/sync_project_views.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version='1.0' encoding='UTF-8' ?>
<!-- Copyright 2020 Ivan Yelizariev <https://twitter.com/yelizariev>
<!-- Copyright 2020,2024 Ivan Yelizariev <https://twitter.com/yelizariev>
Copyright 2021 Denis Mudarisov <https://github.com/trojikman>
License MIT (https://opensource.org/licenses/MIT). -->
<odoo>
Expand Down Expand Up @@ -45,20 +45,14 @@
>
<field string="Logs" name="log_count" widget="statinfo" />
</button>
<!--
<button
type="action"
name="%(sync.sync_task_action_from_project)d"
name="%(sync.sync_link_action_from_project)d"
class="oe_stat_button"
icon="fa-tasks"
icon="fa-link"
>
<field
string="Available Tasks"
name="task_count"
widget="statinfo"
/>
<field string="Links" name="link_count" widget="statinfo" />
</button>
-->
<button
type="action"
name="%(sync.sync_trigger_cron_action_from_project)d"
Expand Down

0 comments on commit 1557adc

Please sign in to comment.