Passa al contenuto
Menu
È necessario essere registrati per interagire con la community.
La domanda è stata contrassegnata
2 Risposte
2220 Visualizzazioni

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
Abbandona
Risposta migliore

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
Abbandona
Risposta migliore

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
Abbandona
Autore

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.

Autore

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!

Post correlati Risposte Visualizzazioni Attività
2
giu 25
2411
1
nov 24
1296
2
set 24
2638
3
lug 25
1791
1
giu 25
1824