تخطي للذهاب إلى المحتوى
القائمة
لقد تم الإبلاغ عن هذا السؤال
2 الردود
2216 أدوات العرض

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
يونيو 25
2408
1
نوفمبر 24
1292
2
سبتمبر 24
2635
3
يوليو 25
1762
1
يونيو 25
1799