Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 5 additions & 1 deletion sign_oca/models/sign_oca_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ class SignOcaField(models.Model):
required=True,
default="text",
)
default_value = fields.Char()
default_value = fields.Char(
help="If filled with the technical name of a field of type char or text "
"linked to the template model when it is res.partner, the value will be "
"precompiled."
)
30 changes: 24 additions & 6 deletions sign_oca/models/sign_oca_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from odoo.exceptions import UserError, ValidationError
from odoo.http import request
from odoo.tools import float_repr
from odoo.tools.misc import get_lang


class SignOcaRequest(models.Model):
Expand Down Expand Up @@ -413,18 +414,35 @@ def _onchange_role_id(self):
def get_info(self, access_token=False):
self.ensure_one()
self._set_action_log("view", access_token=access_token)
partner_fields_dict = {"id": self.partner_id.id}
Copy link
Member

Choose a reason for hiding this comment

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

I don't like this approach as we are getting all the data of the partner....

My suggestion would be to match with the possible field names. WDYT?

Copy link
Author

Choose a reason for hiding this comment

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

A better approach could be to make only visible the fields availables to group base_user? If there were some.
A mere list seems a hard approach, or not?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, but it could be actually a portal user 🤔

The thing is that it is more limited to attach to the fields that we have defined

Copy link
Author

Choose a reason for hiding this comment

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

I added a with_user to ensure only fields accessible to the user opening the form are added.
Do you think this is enough?

for field_name, field_info in (
self.env["res.partner"]
.with_user(self.partner_id.user_id)
Copy link
Contributor

Choose a reason for hiding this comment

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

This line was suspecious to me, I logged and saw that all user_id are just empty recordset:

res.partner(7,)
res.users()
{'id': 7, 'message_main_attachment_id': False, 'email_normalized': '[email protected]', 'activity_user_id': False, 'activity_type_id': False, 'activity_type_icon': False, 'activity_summary': False, 'activity_exception_icon': False, 'name': 'Marc Demo', 'display_name': 'YourCompany, Marc Demo', 'translated_display_name': 'YourCompany, Marc Demo', 'title': False, 'parent_id': False, 'parent_name': False, 'ref': False, 'tz_offset': '+0300', 'user_id': False, 'vat': '123456789', 'same_vat_partner_id': False, 'same_company_registry_partner_id': False, 'company_registry': False, 'website': False, 'function': False, 'street': '3575 Buena Vista Avenue', 'street2': False, 'zip': '97401', 'city': 'Eugene', 'state_id': 'Córdoba', 'country_id': 'United States', 'country_code': 'US', 'email': '[email protected]', 'email_formatted': '"Marc Demo" [email protected]', 'phone': '(441)-695-2334', 'mobile': False, 'industry_id': False, 'company_id': 'YourCompany', 'contact_address': 'YourCompany\n3575 Buena Vista Avenue\n\nEugene COR 97401\nUnited States', 'commercial_partner_id': 'Marc Demo', 'commercial_company_name': 'YourCompany', 'company_name': 'YourCompany', 'barcode': False, 'self': 'Marc Demo', 'create_uid': 'OdooBot', 'write_uid': 'Mitchell Admin', 'im_status': 'offline', 'signup_token': False, 'signup_type': False, 'signup_url': 'http://localhost:8069/web/login?db=sign_survey_16&login=demo', 'additional_info': False, 'phone_sanitized': '(441)-695-2334', 'phone_mobile_search': False, 'current_date': '04/02/2025'}

Copy link
Contributor

Choose a reason for hiding this comment

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

I suggest to add this line above the for loop:
user_id = self.env['res.users'].search([("partner_id", "=", self.partner_id.id)]) for field_name, field_info in ( self.env["res.partner"] .with_user(user_id) .fields_get() .items() ):
as the field user_id is not the partner user account, but the sales person if present!

image

In contrast, passing with_user() function did not make change to the returned dictionary, I suggest keeping the former code as it was.

.fields_get()
.items()
):
if field_info["type"] in ["char", "text"]:
partner_fields_dict.update({field_name: self.partner_id[field_name]})
if field_info["type"] == "many2one":
partner_fields_dict.update(
{field_name: self.partner_id[field_name].name}
)
Comment on lines +427 to +429
Copy link
Contributor

Choose a reason for hiding this comment

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

please, don't merge before fixing this line, some models don't have name field and this should be in account as I catch these errors:

File "/opt/odoo/auto/addons/sign_oca/controllers/main.py", line 87, in get_sign_oca_info_access
return signer_sudo.get_info(access_token=access_token)
File "/opt/odoo/auto/addons/survey_sign_oca/models/sign_oca_request.py", line 88, in get_info
vals = super().get_info(access_token)
File "/opt/odoo/auto/addons/sign_oca/models/sign_oca_request.py", line 435, in get_info
{field_name: self.partner_id[field_name].name}
AttributeError: 'serial.shipping.container.code' object has no attribute 'name'

Copy link
Contributor

Choose a reason for hiding this comment

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

Manage KeyError if user add a wrong technical field name.

            try:
                # name_get method returns a list, if name exists the list looks like:
                # [(123, 'Foo')]
                name_get = self.partner_id[field_name].name_get()
                if name_get and name_get[0]:
                    partner_fields_dict.update(
                        {field_name: name_get[0][1]}
                    )
            except KeyError:
                pass

# add current_date formatted string too
lang_id = get_lang(self.env, self.env.user.lang)
partner_fields_dict.update(
{
"current_date": fields.Date.context_today(self).strftime(
lang_id.date_format
)
}
)
return {
"role_id": self.role_id.id if not self.signed_on else False,
"name": self.request_id.template_id.name,
"items": self.request_id.signatory_data,
"to_sign": self.request_id.to_sign,
"ask_location": self.request_id.ask_location,
"partner": {
"id": self.partner_id.id,
"name": self.partner_id.name,
"email": self.partner_id.email,
"phone": self.partner_id.phone,
},
"partner": partner_fields_dict,
}

def sign(self):
Expand Down
1 change: 1 addition & 0 deletions sign_oca/views/sign_oca_field.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<group>
<field name="name" />
<field name="field_type" />
<field name="default_value" />
</group>
</sheet>
</form>
Expand Down