跳至內容
選單
此問題已被標幟
2 回覆
2228 瀏覽次數

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.

頭像
捨棄
最佳答案

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.
頭像
捨棄
最佳答案

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!

頭像
捨棄
作者

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.

作者

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!

相關帖文 回覆 瀏覽次數 活動
2
6月 25
2419
1
11月 24
1298
2
9月 24
2638
3
7月 25
1796
1
6月 25
1842