Skip to Content
Menu
This question has been flagged
2 Replies
1154 Views

I want to do readonly all fields on project.task except description, What is best practice to do,

This is my code,

def write(self, vals):
fields_to_check_access = ['name', 'project_id', 'planned_date_begin', 'planned_date_end', 'recurrence_id',
'recurring_task', 'recurring_count', 'repeat_interval', 'repeat_unit', 'repeat_type',
'repeat_until', 'child_ids', 'parent_id', 'sequence', 'email_cc',
'displayed_image_id', 'date_deadline', 'tag_ids']

if self.project_id and self.project_id.is_office_project:
try:
if all(e in fields_to_check_access for e in vals):
self.check_access_rule('unlink')
except AccessError:
user_description = f"{self.env.user.name} (id={self.env.user.id})"
operation_error = _("Uh-oh! Looks like you have stumbled upon some top-secret records.\n\n" \
"Sorry, %s doesn't have write access to Tasks:", user_description)
failing_model = _("- %s (%s)", 'Task', 'project.task')
resolution_info = _(
"If you really, really need access, perhaps you can win over your friendly administrator with a batch of freshly baked cookies.")
raise AccessError(f"{operation_error}\n{failing_model}\n\n{resolution_info}")
return super(Task, self).write(vals)

I made a record rule for a specific group for unlink, and I am using that for write aswell.
In write function I am checking and I initialized some fields to check access, if changed field lies in those fields then I am just promting out the warning.

I want best practise to do this.
Thanks in advance.

Avatar
Discard
Best Answer

Hello Maqsood, 

There are following way according to me :

  1. Using fields_view_get(v16) or _get_view (V17) method.
  2. You can add one checkbox and based on your conditions using compute method you can mark True or False and based on checkbox field you make field readonly 
  3. If you want to do for all users in case you can manage via access rights

    Thanks.
Avatar
Discard
Best Answer

You can override the view and add an xpath for all the fields that you want to make readonly with the attribute name readonly

<xpath expr="//field[@name='project_id']" position="attributes">
<attribute name="readonly">1</attribute>
</xpath>

If you want this to apply for only a specific group of users, just add another attribute called groups

<attribute name="groups">stock.group_stock_manager</attribute>


Hope this helps!

Avatar
Discard
Author

so, Still I need to justify the all fields, right?

Yes. All the fields need to be specified. You can also use the fields_view_get method but you will still have to specify the fields in a list.

Author

Which option is best,
The code I shared or that one you have given.
because my project manager will review my code so I don't want any complains,
Please give me suggestion.

xpath is the best option. It will save you a lot of code!

Related Posts Replies Views Activity
1
Nov 24
720
1
Sep 24
1153
2
Sep 24
1673
1
Feb 25
37
1
Feb 25
502