Changes to make the module production ready

This commit is contained in:
Jared Esparza 2020-06-17 16:38:56 +02:00
parent a8526ec922
commit abb937b92d
15 changed files with 278 additions and 137 deletions

View File

@ -6,6 +6,7 @@ from . import role
def register():
Pool.register(
role.WorkConfiguration,
role.Role,
role.Allocation,
role.TaskPhase,

Binary file not shown.

Binary file not shown.

137
role.py
View File

@ -1,7 +1,6 @@
from trytond.pool import Pool, PoolMeta
from trytond.model import ModelView, ModelSQL, fields
__all__ = ['Role', 'Allocation', 'TaskPhase', 'Work']
from trytond.pyson import Eval
class Role(ModelSQL, ModelView):
@ -10,9 +9,15 @@ class Role(ModelSQL, ModelView):
name = fields.Char('name', required=True)
class WorkConfiguration(metaclass=PoolMeta):
__name__ = 'work.configuration'
default_allocation_employee = fields.Many2One('company.employee',
'Default Allocation Employee')
class Allocation(metaclass=PoolMeta):
__name__ = 'project.allocation'
role = fields.Many2One('project.role', "Role")
role = fields.Many2One('project.role', "Role", required=True)
class TaskPhase(metaclass=PoolMeta):
@ -25,60 +30,121 @@ class Work(metaclass=PoolMeta):
assignee = fields.Function(fields.Many2One('company.employee',
'Assignee'), 'get_assignee',
searcher='search_assignee')
role_employee = fields.Function(fields.Char('Role Employee'),
'get_role_employee', searcher='search_role_employee')
def get_assignee(self, name):
role_need = self.task_phase.role
for allocation in self.allocations:
if allocation.role == role_need:
return allocation.employee.id
return None
@classmethod
def __setup__(cls):
super().__setup__()
# Enables allocations in Project type task
# allowing children task inheritthe parent allocations
cls.allocations.states['invisible'] &= Eval('type') != 'project'
if not 'type' in cls.allocations.depends:
cls.allocations.depends.append('type')
@fields.depends('parent', 'allocations', '_parent_parent.id')
def on_change_parent(self):
pool = Pool()
Allocation = pool.get('project.allocation')
# try except needed cause super don't have on_change_parent
# but it could have it in the future
try:
super(Work, self).on_change_parent()
except:
pass
if not self.parent or not self.parent.allocations:
return
allocations =[]
for allocation_parent in self.parent.allocations:
for allocation in self.allocations:
if allocation_parent.role == allocation.role:
allocation.employee = allocation_parent.employee
break
else:
new_allocation = Allocation()
new_allocation.role = allocation_parent.role
new_allocation.employee = allocation_parent.employee
new_allocation.percentage = 100.00
allocations.append(new_allocation)
self.allocations += tuple(allocations)
@fields.depends('tracker', 'allocations')
def on_change_tracker(self):
pool = Pool()
Allocation = pool.get('project.allocation')
# try except needed cause super doesn't have on_change_tracker
# but it could have it in the future
try:
super(Work, self).on_change_tracker()
except:
pass
if not self.tracker or not self.tracker.workflow:
return
allocations = []
for line in self.tracker.workflow.lines:
for alocation in self.allocations:
if alocation.role.id == line.phase.role.id:
break
else:
Configuration = Pool().get('work.configuration')
allocation = Allocation()
allocation.role = line.phase.role
allocation.employee = (
Configuration(1).default_allocation_employee)
allocation.percentage = 100.0
allocations.append(allocation)
self.allocations += tuple(allocations)
@classmethod
def search_assignee(cls, name, clause):
def _get_assignee_query(cls):
pool = Pool()
Allocation = pool.get('project.allocation')
Phase = pool.get('project.work.task_phase')
Employee = pool.get('company.employee')
Party = pool.get('party.party')
Role = pool.get('project.role')
work = cls.__table__()
allocation = Allocation.__table__()
phase = Phase.__table__()
employee = Employee.__table__()
party = Party.__table__()
role = Role.__table__()
Operator = fields.SQL_OPERATORS[clause[1]]
join1 = work.join(allocation, condition = work.id == allocation.work)
join2 = join1.join(phase, condition = phase.id == work.task_phase)
join3 = join2.join(employee, condition = allocation.employee == employee.id)
join4 = join3.join(party, condition = party.id == employee.party)
query = join4.select(work.id)
query.where = (phase.role == allocation.role) & Operator(party.name, clause[2])
join5 = join4.join(role, condition = allocation.role == role.id)
query = join5.select(work.id)
query.where = (phase.role == allocation.role)
return query, party, role
def get_assignee(self, name):
if not self.task_phase:
return
role_need = self.task_phase.role
for allocation in self.allocations:
if allocation.role == role_need:
return allocation.employee.id
@classmethod
def search_assignee(cls, name, clause):
query, party, _ = cls._get_assignee_query()
Operator = fields.SQL_OPERATORS[clause[1]]
query.where &= (Operator(party.name, clause[2]))
return [('id', 'in', query)]
def get_role_employee(self, name):
res = []
for allocation in self.allocations:
res.append('%s/%s' % (allocation.employee.rec_name,
allocation.role.rec_name))
allocation.role.rec_name if allocation.role else ''))
return ' '.join(res)
@classmethod
def search_role_employee(cls, name, clause):
pool = Pool()
Allocation = pool.get('project.allocation')
Employee = pool.get('company.employee')
Party = pool.get('party.party')
Role = pool.get('project.role')
work = cls.__table__()
allocation = Allocation.__table__()
employee = Employee.__table__()
party = Party.__table__()
role = Role.__table__()
Operator = fields.SQL_OPERATORS[clause[1]]
value = clause[2]
values = value.split('/')
@ -91,16 +157,15 @@ class Work(metaclass=PoolMeta):
if 'like' in clause[1]:
employee_value = employee_value + '%'
if role:
if role_value:
role_value = '%' + role_value
join = work.join(allocation, condition = work.id == allocation.work)
join = join.join(employee, condition = allocation.employee == employee.id)
join = join.join(party, condition = party.id == employee.party)
if role:
join = join.join(role, condition = allocation.role == role.id)
query = join.select(work.id)
query.where = Operator(party.name, employee_value)
if role:
query.where &= Operator(role.name, role_value)
query, party, role = cls._get_assignee_query()
if role_value:
query.where = (Operator(role.name, role_value)
& Operator(party.name, employee_value))
else:
query.where = Operator(party.name, employee_value)
return [('id', 'in', query)]

128
role.xml
View File

@ -1,69 +1,73 @@
<tryton>
<data>
<data>
<!-- configuration views of roles -->
<record model="ir.ui.view" id="project_role_view_tree">
<field name="model">project.role</field>
<field name="type">tree</field>
<field name="name">project_role_tree</field>
</record>
<record model="ir.ui.view" id="project_role_view_form">
<field name="model">project.role</field>
<field name="type">form</field>
<field name="name">project_role_form</field>
</record>
<record model="ir.action.act_window" id="act_project_role">
<field name="name">Project Roles</field>
<field name="res_model">project.role</field>
</record>
<record model="ir.action.act_window.view" id="act_project_role_tree">
<field name="sequence" eval="10"/>
<field name="view" ref="project_role_view_tree"/>
<field name="act_window" ref="act_project_role"/>
</record>
<record model="ir.action.act_window.view" id="act_project_role_form">
<field name="sequence" eval="20"/>
<field name="view" ref="project_role_view_form"/>
<field name="act_window" ref="act_project_role"/>
</record>
<record model="ir.ui.view" id="project_role_view_tree">
<field name="model">project.role</field>
<field name="type">tree</field>
<field name="name">project_role_tree</field>
</record>
<record model="ir.ui.view" id="project_role_view_form">
<field name="model">project.role</field>
<field name="type">form</field>
<field name="name">project_role_form</field>
</record>
<record model="ir.action.act_window" id="act_project_role">
<field name="name">Project Roles</field>
<field name="res_model">project.role</field>
</record>
<record model="ir.action.act_window.view" id="act_project_role_tree">
<field name="sequence" eval="10"/>
<field name="view" ref="project_role_view_tree"/>
<field name="act_window" ref="act_project_role"/>
</record>
<record model="ir.action.act_window.view" id="act_project_role_form">
<field name="sequence" eval="20"/>
<field name="view" ref="project_role_view_form"/>
<field name="act_window" ref="act_project_role"/>
</record>
<!-- add roles to phase -->
<record model="ir.ui.view" id="project_phase_view_form">
<field name="model">project.work.task_phase</field>
<field name="name">task_phase_form</field>
<field name="inherit" ref="project_phase.project_phase_view_form"/>
</record>
<record model="ir.ui.view" id="project_phase_view_list">
<field name="model">project.work.task_phase</field>
<field name="name">task_phase_list</field>
<field name="inherit" ref="project_phase.project_phase_view_list"/>
</record>
<record model="ir.ui.view" id="project_phase_view_form">
<field name="model">project.work.task_phase</field>
<field name="name">task_phase_form</field>
<field name="inherit" ref="project_phase.project_phase_view_form"/>
</record>
<record model="ir.ui.view" id="project_phase_view_list">
<field name="model">project.work.task_phase</field>
<field name="name">task_phase_list</field>
<field name="inherit" ref="project_phase.project_phase_view_list"/>
</record>
<!-- add roles to allocations -->
<record model="ir.ui.view" id="allocation_view_form">
<field name="model">project.allocation</field>
<field name="name">allocation_form</field>
<field name="inherit" ref="project_plan.allocation_view_form"/>
</record>
<record model="ir.ui.view" id="allocation_view_tree">
<field name="model">project.allocation</field>
<field name="name">allocation_tree</field>
<field name="inherit" ref="project_plan.allocation_view_tree"/>
</record>
<record model="ir.ui.view" id="allocation_view_form">
<field name="model">project.allocation</field>
<field name="name">allocation_form</field>
<field name="inherit" ref="project_plan.allocation_view_form"/>
</record>
<record model="ir.ui.view" id="allocation_view_tree">
<field name="model">project.allocation</field>
<field name="name">allocation_tree</field>
<field name="inherit" ref="project_plan.allocation_view_tree"/>
</record>
<!-- add role to task list view -->
<record model="ir.ui.view" id="work_view_list_simple">
<field name="model">project.work</field>
<field name="inherit" ref="project.work_view_list_simple"/>
<field name="name">work_view_list</field>
</record>
<record model="ir.ui.view" id="work_view_list">
<field name="model">project.work</field>
<field name="inherit" ref="project.work_view_list"/>
<field name="name">work_list</field>
</record>
<!-- add role to task list view -->
<record model="ir.ui.view" id="work_view_list_simple">
<field name="model">project.work</field>
<field name="inherit" ref="project.work_view_list_simple"/>
<field name="name">work_view_list</field>
</record>
<record model="ir.ui.view" id="work_view_list">
<field name="model">project.work</field>
<field name="inherit" ref="project.work_view_list"/>
<field name="name">work_list</field>
</record>
<!-- add default employee to configuration -->
<record model="ir.ui.view" id="work_configuration_view_form">
<field name="model">work.configuration</field>
<field name="inherit" ref="project_sequence.work_configuration_view_form"/>
<field name="name">work_configuration_form</field>
</record>
<!-- roles config menu entry -->
<menuitem parent="project.menu_configuration" name="Roles"
id="menu_project_role_configuration" action="act_project_role"
icon="tryton-list"/>
<menuitem parent="project.menu_configuration" name="Roles"
id="menu_project_role_configuration" action="act_project_role"
icon="tryton-list"/>
</data>
</tryton>
</tryton>

View File

@ -54,6 +54,9 @@ Create Roles::
>>> tester = Role()
>>> tester.name = 'Tester'
>>> tester.save()
>>> reviewer = Role()
>>> reviewer.name = 'Reviewer'
>>> reviewer.save()
Create TaskPhase::
@ -71,7 +74,7 @@ Create Workflow::
>>> Workflow = Model.get('project.work.workflow')
>>> workflow = Workflow()
>>> workflow.name = 'workflowTest'
>>> workflow.name = 'workflow'
>>> workflow.save()
Create Workflow Lines ::
@ -91,64 +94,118 @@ Create Tracker::
>>> Tracker = Model.get('project.work.tracker')
>>> tracker = Tracker()
>>> tracker.name = 'TrackerTest'
>>> tracker.name = 'Tracker'
>>> tracker.workflow = workflow
>>> tracker.save()
Create Configuration::
>>> Configuration = Model.get('work.configuration')
>>> config = Configuration()
>>> config.default_allocation_employee = employee2
>>> config.save()
Create Project::
>>> Work = Model.get('project.work')
>>> project = Work()
>>> project.name = 'Project'
>>> project.company = company
>>> project.type = 'project'
>>> project.state = 'opened'
>>> project.save()
Create Allocation::
>>> Allocation = Model.get('project.allocation')
>>> allocation = Allocation()
>>> allocation.role = dev
>>> allocation.work = project
>>> allocation.employee = employee1
>>> allocation.save()
Create Task::
>>> Task = Model.get('project.work')
>>> task = Task()
>>> task.name = 'testTask'
>>> task = Work()
>>> task.type = 'task'
>>> task.parent = project
>>> task.name = 'Task'
>>> task.company = company
>>> task.tracker = tracker
>>> task.task_phase = workflow_line2.phase
>>> task.state = 'opened'
>>> task.save()
Create Allocation::
>>> Allocation = Model.get('project.allocation')
>>> allocation = Allocation()
>>> allocation.work = task
>>> allocation.role = dev
>>> allocation.employee = employee1
>>> allocation.save()
>>> allocation2 = Allocation()
>>> allocation2.work = task
>>> allocation2.role = tester
>>> allocation2.employee = employee2
>>> allocation2.save()
Searcher ::
>>> result, = Task.find(['name','ilike', '%test%'])
>>> result, = Work.find(['name','ilike', '%Tas%'])
>>> result.id == task.id
True
Searcher Asignee Tests::
>>> result, = Task.find(['assignee', 'ilike', '%emp2%'])
>>> result, = Work.find(['assignee', 'ilike', '%emp2%'])
>>> result.id == task.id
True
>>> result = Task.find(['assignee', 'ilike', '%emp1%'])
>>> result = Work.find(['assignee', 'ilike', '%emp1%'])
>>> result
[]
Searcher employee/role::
>>> result, = Task.find(['role_employee', 'ilike', '%emp1/dev%'])
>>> result, = Work.find(['role_employee', 'ilike', '%emp1/dev%'])
>>> result.id == task.id
True
>>> result, = Task.find(['role_employee', 'ilike', '%emp2/test%'])
>>> result, = Work.find(['role_employee', 'ilike', '%emp2/test%'])
>>> result.id == task.id
True
>>> result = Task.find(['role_employee', 'ilike', '%emp1/test%'])
>>> result = Work.find(['role_employee', 'ilike', '%emp1/test%'])
>>> result
[]
>>> result, = Task.find(['role_employee', 'ilike', '%emp1%'])
>>> result, = Work.find(['role_employee', 'ilike', '%emp1%'])
>>> result.id == task.id
True
>>> result = Task.find(['role_employee', 'ilike', '%test%'])
>>> result = Work.find(['role_employee', 'ilike', '%test%'])
>>> result
[]
On_change_parent test::
>>> task.allocations[0].employee = employee2
>>> task.save()
>>> Work.find(['role_employee', 'ilike', '%emp1/dev%'])
[]
>>> task.parent = None
>>> task.save()
>>> task.parent = project
>>> task.save()
>>> result, = Work.find(['role_employee', 'ilike', '%emp1/dev%'])
>>> result.id == task.id
True
>>> allocation2 = Allocation()
>>> allocation2.role = reviewer
>>> allocation2.employee = employee2
>>> allocation2.work = task
>>> allocation2.save()
>>> result, = Work.find(['role_employee', 'ilike', '%emp2/revi%'])
>>> result.id == task.id
True
>>> task.parent = None
>>> task.save()
>>> task.parent = project
>>> task.save()
>>> result, = Work.find(['role_employee', 'ilike', '%emp2/revi%'])
>>> result.id == task.id
True
>>> task.allocations[0].delete()
>>> task.save()
>>> result = Work.find(['role_employee', 'ilike', '%emp1/dev%'])
>>> result
[]
>>> task.parent = None
>>> task.save()
>>> task.parent = project
>>> task.save()
>>> result, = Work.find(['role_employee', 'ilike', '%emp1/dev%'])
>>> result.id == task.id
True

View File

@ -4,6 +4,8 @@ depends:
ir
project_plan
project_phase
#Required for configuration only
project_sequence
xml:
role.xml
work.xml

View File

@ -1,6 +1,6 @@
<data>
<xpath expr="/form/field[@name='employee']" position="after">
<label name="role"/>
<field name="role"/>
<label name="role"/>
<field name="role"/>
</xpath>
</data>

View File

@ -1,5 +1,8 @@
<data>
<xpath expr="/tree/field[@name='employee']" position="after">
<field name="role"/>
<field name="role"/>
</xpath>
<xpath expr="/tree" position="replace_attributes">
<tree editable="bottom"/>
</xpath>
</data>

View File

@ -1,4 +1,4 @@
<form>
<label name="name"/>
<field name="name"/>
<label name="name"/>
<field name="name"/>
</form>

View File

@ -1,3 +1,3 @@
<tree>
<field name="name"/>
<field name="name"/>
</tree>

View File

@ -1,6 +1,6 @@
<data>
<xpath expr="/form/field[@name='sequence']" position="after">
<label name="role"/>
<field name="role"/>
<label name="role"/>
<field name="role"/>
</xpath>
</data>

View File

@ -1,5 +1,5 @@
<data>
<xpath expr="/tree[@sequence='sequence']/field[@name='name']" position="after">
<field name="role"/>
<field name="role"/>
</xpath>
</data>

View File

@ -0,0 +1,9 @@
<?xml version="1.0"?>
<!-- The COPYRIGHT file at the top level of this repository contains the full
copyright notices and license terms. -->
<data>
<xpath expr="/form/field[@name='work_sequence']" position="after">
<label name="default_allocation_employee"/>
<field name="default_allocation_employee"/>
</xpath>
</data>

View File

@ -3,7 +3,7 @@
this repository contains the full copyright notices and license terms. -->
<data>
<xpath expr="/form/field[@name='sequence']" position="after">
<label name="assignee"/>
<field name="assignee"/>
<label name="assignee"/>
<field name="assignee"/>
</xpath>
</data>