This question has been flagged
3 Replies
65687 Views

I am creating an addon template functionality to the project module. I would like users to be able to select/change the template from the project form view, but in my onchange function for taskset_template_id it is throwing up an error when I try to delete existing tasks that were created from a previously applied template (only one template at a time should be used). It seems to be a dependency or context issue with the rating mixin, but I've tried adding the 'rating' module to the __manifest__.py file and tasks still aren't successfully unlinked. How can I unlink tasks without running into this rating issue?

The code:

@api.onchange('taskset_template_id')
def onchange_taskset_template_id(self):
if not self.project_deadline:
raise ValidationError('In order to apply a taskset template you must first select a project deadline.')
project = self.env['project.project'].browse(self._origin.id)
tasks = self.env['project.task']

# Check for template tasks delete to avoid task duplication
find_template_tasks = self.env['project.task'].search([('project_id', '=', project.id), ('from_template', '=', True)])
if find_template_tasks:
for task in tasks.browse(find_template_tasks.ids):
super(Task, task).unlink()

if not self.taskset_template_id:
raise ValidationError(
'You have already applied a taskset template to this project. Before removing the template selection, please delete all tasks associated with the current template.'
)

task_ids = self.env['taskset.template.line'].search([('taskset_template_id', '=', self.taskset_template_id.id)]).ids

for task in self.env['taskset.template.line'].browse(task_ids):
data = self._map_template_tasks_default_values(task)

# Calculate deadline
days_from_deadline = task.day_counter
deadline = self._calculate_deadline(self.project_deadline, days_from_deadline, project)
data.update({'project_id': project.id,
'date_deadline': deadline,
'date_assign': fields.Datetime.now(),
'partner_id': self.partner_id.id,
'from_template': True})

new_task = super(Task, tasks).create(data)
tasks += new_task

project.write({'tasks': [(tasks.ids)]})

The error:

Traceback (most recent call last):
  File "/Users/nathancobb/odoo/odoo/odoo/api.py", line 753, in get
    value = self._data[field][record._ids[0]]
KeyError: <NewId origin=4>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/nathancobb/odoo/odoo/odoo/fields.py", line 978, in __get__
    value = env.cache.get(record, self)
  File "/Users/nathancobb/odoo/odoo/odoo/api.py", line 759, in get
    raise CacheMiss(record, field)
odoo.exceptions.CacheMiss: ('project.project(<NewId origin=4>,).rating_percentage_satisfaction', None)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/nathancobb/odoo/odoo/odoo/http.py", line 619, in _handle_exception
    return super(JsonRequest, self)._handle_exception(exception)
  File "/Users/nathancobb/odoo/odoo/odoo/http.py", line 309, in _handle_exception
    raise pycompat.reraise(type(exception), exception, sys.exc_info()[2])
  File "/Users/nathancobb/odoo/odoo/odoo/tools/pycompat.py", line 14, in reraise
    raise value
  File "/Users/nathancobb/odoo/odoo/odoo/http.py", line 664, in dispatch
    result = self._call_function(**self.params)
  File "/Users/nathancobb/odoo/odoo/odoo/http.py", line 345, in _call_function
    return checked_call(self.db, *args, **kwargs)
  File "/Users/nathancobb/odoo/odoo/odoo/service/model.py", line 93, in wrapper
    return f(dbname, *args, **kwargs)
  File "/Users/nathancobb/odoo/odoo/odoo/http.py", line 338, in checked_call
    result = self.endpoint(*a, **kw)
  File "/Users/nathancobb/odoo/odoo/odoo/http.py", line 909, in __call__
    return self.method(*args, **kw)
  File "/Users/nathancobb/odoo/odoo/odoo/http.py", line 510, in response_wrap
    response = f(*args, **kw)
  File "/Users/nathancobb/odoo/odoo/addons/web/controllers/main.py", line 1319, in call_kw
    return self._call_kw(model, method, args, kwargs)
  File "/Users/nathancobb/odoo/odoo/addons/web/controllers/main.py", line 1311, in _call_kw
    return call_kw(request.env[model], method, args, kwargs)
  File "/Users/nathancobb/odoo/odoo/odoo/api.py", line 395, in call_kw
    result = _call_kw_multi(method, model, args, kwargs)
  File "/Users/nathancobb/odoo/odoo/odoo/api.py", line 382, in _call_kw_multi
    result = method(recs, *args, **kwargs)
  File "/Users/nathancobb/odoo/odoo/odoo/models.py", line 6050, in onchange
    todo = [
  File "/Users/nathancobb/odoo/odoo/odoo/models.py", line 6053, in <listcomp>
    if name not in done and snapshot0.has_changed(name)
  File "/Users/nathancobb/odoo/odoo/odoo/models.py", line 5920, in has_changed
    return self[name] != record[name]
  File "/Users/nathancobb/odoo/odoo/odoo/models.py", line 5579, in __getitem__
    return self._fields[key].__get__(self, type(self))
  File "/Users/nathancobb/odoo/odoo/odoo/fields.py", line 1002, in __get__
    self.compute_value(recs)
  File "/Users/nathancobb/odoo/odoo/odoo/fields.py", line 1087, in compute_value
    records._compute_field_value(self)
  File "/Users/nathancobb/odoo/odoo/odoo/models.py", line 3895, in _compute_field_value
    getattr(self, field.compute)()
  File "/Users/nathancobb/odoo/odoo/addons/rating/models/rating_mixin.py", line 42, in _compute_rating_percentage_satisfaction
    record.rating_percentage_satisfaction = repartition['great'] * 100 / sum(repartition.values()) if sum(repartition.values()) else -1
AttributeError: 'NoneType' object has no attribute 'values'
Avatar
Discard

you're trying ot get values of an empty repartition.

make sure you have repartition before retrieving its values.

Author

Right, but I don't understand why repartition is being referenced in the first place. I can't find a function call for _compute_rating_percentage_satisfaction (the only place where repartition is used) anywhere in the project.project or project.task models.

Best Answer

AttributeError means that there was an Error that had to do with an Attribute request. In general, when you write x.y, y is the purported attribute of x. NoneType means that instead of an instance of whatever Class or Object you think you're working with, you've actually got None. That usually means that an assignment or function call up failed or returned an unexpected result.

mylist = mylist.sort()

The sort() method of a list sorts the list in-place, that is, mylist is modified. But the actual return value of the method is None and not the list sorted. So you've just assigned None to mylist. If you next try to do, say, mylist.append(1) Python will give you this error.

http://net-informations.com/python/basics/none.htm


Avatar
Discard