Update for new Transaction
This commit is contained in:
@@ -4,7 +4,7 @@ Installing trytond_timesheet
|
||||
Prerequisites
|
||||
-------------
|
||||
|
||||
* Python 2.4 or later (http://www.python.org/)
|
||||
* Python 2.5 or later (http://www.python.org/)
|
||||
* trytond (http://www.tryton.org/)
|
||||
* trytond_company (http://www.tryton.org/)
|
||||
* trytond_company_work_time (http://www.tryton.org/)
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
#This file is part of Tryton. The COPYRIGHT file at the top level of
|
||||
#this repository contains the full copyright notices and license terms.
|
||||
"Timesheet Line"
|
||||
from trytond.model import ModelView, ModelSQL, fields
|
||||
from trytond.wizard import Wizard
|
||||
from trytond.backend import FIELDS
|
||||
from trytond.pyson import Eval, PYSONEncoder, Date
|
||||
|
||||
from trytond.transaction import Transaction
|
||||
|
||||
class Line(ModelSQL, ModelView):
|
||||
'Timesheet Line'
|
||||
@@ -29,39 +28,31 @@ class Line(ModelSQL, ModelView):
|
||||
'CHECK(hours >= 0.0)', 'Hours field must be positive'),
|
||||
]
|
||||
|
||||
def default_employee(self, cursor, user_id, context=None):
|
||||
def default_employee(self):
|
||||
user_obj = self.pool.get('res.user')
|
||||
employee_obj = self.pool.get('company.employee')
|
||||
|
||||
if context is None:
|
||||
context = {}
|
||||
employee_id = None
|
||||
if context.get('employee'):
|
||||
employee_id = context['employee']
|
||||
if Transaction().context.get('employee'):
|
||||
employee_id = Transaction().context['employee']
|
||||
else:
|
||||
user = user_obj.browse(cursor, user_id, user_id, context=context)
|
||||
user = user_obj.browse(Transaction().user)
|
||||
if user.employee:
|
||||
employee_id = user.employee.id
|
||||
if employee_id:
|
||||
return employee_id
|
||||
return False
|
||||
|
||||
def default_date(self, cursor, user, context=None):
|
||||
def default_date(self):
|
||||
date_obj = self.pool.get('ir.date')
|
||||
|
||||
if context is None:
|
||||
context = {}
|
||||
if context.get('date'):
|
||||
return context['date']
|
||||
return date_obj.today(cursor, user, context=context)
|
||||
return Transaction().context.get('date') or date_obj.today()
|
||||
|
||||
def view_header_get(self, cursor, user, value, view_type='form',
|
||||
context=None):
|
||||
if not context.get('employee'):
|
||||
def view_header_get(self, value, view_type='form'):
|
||||
if not Transaction().context.get('employee'):
|
||||
return value
|
||||
employee_obj = self.pool.get('company.employee')
|
||||
employee = employee_obj.browse(cursor, user, context['employee'],
|
||||
context=context)
|
||||
employee = employee_obj.browse(Transaction().context['employee'])
|
||||
return value + " (" + employee.name + ")"
|
||||
|
||||
Line()
|
||||
@@ -75,13 +66,13 @@ class EnterLinesInit(ModelView):
|
||||
domain=[('company', '=', Eval('company'))])
|
||||
date = fields.Date('Date', required=True)
|
||||
|
||||
def default_employee(self, cursor, user, context=None):
|
||||
def default_employee(self):
|
||||
line_obj = self.pool.get('timesheet.line')
|
||||
return line_obj.default_employee(cursor, user, context=context)
|
||||
return line_obj.default_employee()
|
||||
|
||||
def default_date(self, cursor, user, context=None):
|
||||
def default_date(self):
|
||||
line_obj = self.pool.get('timesheet.line')
|
||||
return line_obj.default_date(cursor, user, context=context)
|
||||
return line_obj.default_date()
|
||||
|
||||
EnterLinesInit()
|
||||
|
||||
@@ -109,13 +100,12 @@ class EnterLines(Wizard):
|
||||
}
|
||||
}
|
||||
|
||||
def _action_enter_lines(self, cursor, user, data, context=None):
|
||||
def _action_enter_lines(self, data):
|
||||
model_data_obj = self.pool.get('ir.model.data')
|
||||
act_window_obj = self.pool.get('ir.action.act_window')
|
||||
employee_obj = self.pool.get('company.employee')
|
||||
act_window_id = model_data_obj.get_id(cursor, user, 'timesheet',
|
||||
'act_line_form', context=context)
|
||||
res = act_window_obj.read(cursor, user, act_window_id, context=context)
|
||||
act_window_id = model_data_obj.get_id('timesheet', 'act_line_form')
|
||||
res = act_window_obj.read(act_window_id)
|
||||
date = data['form']['date']
|
||||
date = Date(date.year, date.month, date.day)
|
||||
res['pyson_domain'] = PYSONEncoder().encode([
|
||||
@@ -128,8 +118,7 @@ class EnterLines(Wizard):
|
||||
})
|
||||
|
||||
if data['form']['employee']:
|
||||
employee = employee_obj.browse(
|
||||
cursor, user, data['form']['employee'], context=context)
|
||||
employee = employee_obj.browse(data['form']['employee'])
|
||||
res['name'] += " - " + employee.rec_name
|
||||
|
||||
return res
|
||||
@@ -145,17 +134,15 @@ class HoursEmployee(ModelSQL, ModelView):
|
||||
employee = fields.Many2One('company.employee', 'Employee', select=1)
|
||||
hours = fields.Float('Hours', digits=(16, 2))
|
||||
|
||||
def table_query(self, context=None):
|
||||
if context is None:
|
||||
context = {}
|
||||
def table_query(self):
|
||||
clause = ' '
|
||||
args = [True]
|
||||
if context.get('start_date'):
|
||||
if Transaction().context.get('start_date'):
|
||||
clause += 'AND date >= %s '
|
||||
args.append(context['start_date'])
|
||||
if context.get('end_date'):
|
||||
args.append(Transaction().context['start_date'])
|
||||
if Transaction().context.get('end_date'):
|
||||
clause += 'AND date <= %s '
|
||||
args.append(context['end_date'])
|
||||
args.append(Transaction().context['end_date'])
|
||||
return ('SELECT DISTINCT(employee) AS id, ' \
|
||||
'MAX(create_uid) AS create_uid, ' \
|
||||
'MAX(create_date) AS create_date, ' \
|
||||
@@ -204,12 +191,12 @@ class OpenHoursEmployee(Wizard):
|
||||
},
|
||||
}
|
||||
|
||||
def _action_open(self, cursor, user, data, context=None):
|
||||
def _action_open(self, data):
|
||||
model_data_obj = self.pool.get('ir.model.data')
|
||||
act_window_obj = self.pool.get('ir.action.act_window')
|
||||
act_window_id = model_data_obj.get_id(cursor, user, 'timesheet',
|
||||
'act_hours_employee_form', context=context)
|
||||
res = act_window_obj.read(cursor, user, act_window_id, context=context)
|
||||
act_window_id = model_data_obj.get_id('timesheet',
|
||||
'act_hours_employee_form')
|
||||
res = act_window_obj.read(act_window_id)
|
||||
res['pyson_context'] = PYSONEncoder().encode({
|
||||
'start_date': data['form']['start_date'],
|
||||
'end_date': data['form']['end_date'],
|
||||
@@ -235,7 +222,7 @@ class HoursEmployeeWeekly(ModelSQL, ModelView):
|
||||
self._order.insert(1, ('week', 'DESC'))
|
||||
self._order.insert(2, ('employee', 'ASC'))
|
||||
|
||||
def table_query(self, context=None):
|
||||
def table_query(self):
|
||||
type_name = FIELDS[self.year._type].sql_type(self.year)[0]
|
||||
return ('SELECT id, create_uid, create_date, write_uid, write_date, ' \
|
||||
'CAST(year AS ' + type_name + '), week, employee, hours ' \
|
||||
@@ -272,7 +259,7 @@ class HoursEmployeeMonthly(ModelSQL, ModelView):
|
||||
self._order.insert(1, ('month', 'DESC'))
|
||||
self._order.insert(2, ('employee', 'ASC'))
|
||||
|
||||
def table_query(self, context=None):
|
||||
def table_query(self):
|
||||
type_name = FIELDS[self.year._type].sql_type(self.year)[0]
|
||||
return ('SELECT id, create_uid, create_date, write_uid, write_date, ' \
|
||||
'CAST(year AS ' + type_name + '), month, employee, hours ' \
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
#This file is part of Tryton. The COPYRIGHT file at the top level of
|
||||
#this repository contains the full copyright notices and license terms.
|
||||
"Work"
|
||||
from trytond.model import ModelView, ModelSQL, fields
|
||||
from trytond.wizard import Wizard
|
||||
from trytond.pyson import PYSONEncoder
|
||||
|
||||
from trytond.transaction import Transaction
|
||||
|
||||
class Work(ModelSQL, ModelView):
|
||||
'Work'
|
||||
@@ -36,21 +35,17 @@ class Work(ModelSQL, ModelView):
|
||||
'as it\'s parent work!',
|
||||
})
|
||||
|
||||
def default_active(self, cursor, user, context=None):
|
||||
def default_active(self):
|
||||
return True
|
||||
|
||||
def default_timesheet_available(self, cursor, user, context=None):
|
||||
def default_timesheet_available(self):
|
||||
return True
|
||||
|
||||
def default_company(self, cursor, user, context=None):
|
||||
if context is None:
|
||||
context = {}
|
||||
if context.get('company'):
|
||||
return context['company']
|
||||
return False
|
||||
def default_company(self):
|
||||
return Transaction().context.get('company') or False
|
||||
|
||||
def check_parent_company(self, cursor, user, ids):
|
||||
for work in self.browse(cursor, user, ids):
|
||||
def check_parent_company(self, ids):
|
||||
for work in self.browse(ids):
|
||||
if not work.parent:
|
||||
continue
|
||||
if work.parent.company.id != work.company.id:
|
||||
@@ -71,10 +66,10 @@ class Work(ModelSQL, ModelView):
|
||||
to_compute[h] = False
|
||||
return res
|
||||
|
||||
def get_hours(self, cursor, user, ids, name, context=None):
|
||||
all_ids = self.search(cursor, user, [
|
||||
('parent', 'child_of', ids)
|
||||
], context=context)
|
||||
def get_hours(self, name):
|
||||
all_ids = self.search([
|
||||
('parent', 'child_of', ids),
|
||||
])
|
||||
# force inactive ids to be in all_ids
|
||||
all_ids = all_ids + ids
|
||||
clause = "SELECT work, sum(hours) FROM timesheet_line "\
|
||||
@@ -82,19 +77,20 @@ class Work(ModelSQL, ModelView):
|
||||
% ",".join(('%s',) * len(all_ids))
|
||||
date_cond = ""
|
||||
args = []
|
||||
if context.get('from_date'):
|
||||
if Transaction().context.get('from_date'):
|
||||
date_cond = " AND date >= %s"
|
||||
args.append(context['from_date'])
|
||||
if context.get('to_date'):
|
||||
args.append(Transaction().context['from_date'])
|
||||
if Transaction().context.get('to_date'):
|
||||
date_cond += " AND date <= %s"
|
||||
args.append(context['to_date'])
|
||||
args.append(Transaction().context['to_date'])
|
||||
clause += date_cond + " GROUP BY work"
|
||||
|
||||
cursor.execute(clause, all_ids + args)
|
||||
Transaction().cursor.execute(clause, all_ids + args)
|
||||
|
||||
hours_by_wt = dict([(i[0], i[1]) for i in cursor.fetchall()])
|
||||
hours_by_wt = dict((i[0], i[1]) for i in
|
||||
Transaction().cursor.fetchall())
|
||||
to_compute = dict.fromkeys(all_ids, True)
|
||||
works = self.browse(cursor, user, all_ids, context=context)
|
||||
works = self.browse(all_ids)
|
||||
children = {}
|
||||
for work in works:
|
||||
if work.parent:
|
||||
@@ -102,7 +98,7 @@ class Work(ModelSQL, ModelView):
|
||||
self._tree_qty(hours_by_wt, children, ids, to_compute)
|
||||
return hours_by_wt
|
||||
|
||||
def get_rec_name(self, cursor, user, ids, name, context=None):
|
||||
def get_rec_name(self, name):
|
||||
if not ids:
|
||||
return {}
|
||||
res = {}
|
||||
@@ -111,22 +107,21 @@ class Work(ModelSQL, ModelView):
|
||||
return _name(work.parent) + '\\' + work.name
|
||||
else:
|
||||
return work.name
|
||||
for work in self.browse(cursor, user, ids, context=context):
|
||||
for work in self.browse(ids):
|
||||
res[work.id] = _name(work)
|
||||
return res
|
||||
|
||||
def write(self, cursor, user, ids, vals, context=None):
|
||||
def write(self, ids, vals):
|
||||
child_ids = None
|
||||
if not vals.get('active', True):
|
||||
child_ids = self.search(cursor, user, [
|
||||
child_ids = self.search([
|
||||
('parent', 'child_of', ids),
|
||||
], context=context)
|
||||
res = super(Work, self).write(cursor, user, ids, vals,
|
||||
context=context)
|
||||
])
|
||||
res = super(Work, self).write(ids, vals)
|
||||
if child_ids:
|
||||
self.write(cursor, user, child_ids, {
|
||||
self.write(child_ids, {
|
||||
'active': False,
|
||||
}, context=context)
|
||||
})
|
||||
return res
|
||||
|
||||
Work()
|
||||
@@ -164,12 +159,11 @@ class OpenWork(Wizard):
|
||||
},
|
||||
}
|
||||
|
||||
def _action_open_work(self, cursor, user, data, context=None):
|
||||
def _action_open_work(self, data):
|
||||
model_data_obj = self.pool.get('ir.model.data')
|
||||
act_window_obj = self.pool.get('ir.action.act_window')
|
||||
act_window_id = model_data_obj.get_id(cursor, user, 'timesheet',
|
||||
'act_work_tree2', context=context)
|
||||
res = act_window_obj.read(cursor, user, act_window_id, context=context)
|
||||
act_window_id = model_data_obj.get_id('timesheet', 'act_work_tree2')
|
||||
res = act_window_obj.read(act_window_id)
|
||||
res['pyson_context'] = PYSONEncoder().encode({
|
||||
'from_date': data['form']['from_date'],
|
||||
'to_date': data['form']['to_date'],
|
||||
@@ -182,12 +176,11 @@ OpenWork()
|
||||
class OpenWork2(OpenWork):
|
||||
_name = 'timesheet.work.open2'
|
||||
|
||||
def _action_open_work(self, cursor, user, data, context=None):
|
||||
def _action_open_work(self, data):
|
||||
model_data_obj = self.pool.get('ir.model.data')
|
||||
act_window_obj = self.pool.get('ir.action.act_window')
|
||||
act_window_id = model_data_obj.get_id(cursor, user, 'timesheet',
|
||||
'act_work_form2', context=context)
|
||||
res = act_window_obj.read(cursor, user, act_window_id, context=context)
|
||||
act_window_id = model_data_obj.get_id('timesheet', 'act_work_form2')
|
||||
res = act_window_obj.read(act_window_id)
|
||||
res['pyson_context'] = PYSONEncoder().encode({
|
||||
'from_date': data['form']['from_date'],
|
||||
'to_date': data['form']['to_date'],
|
||||
@@ -209,20 +202,15 @@ class OpenWorkGraph(Wizard):
|
||||
},
|
||||
}
|
||||
|
||||
def _action_open_work(self, cursor, user, data, context=None):
|
||||
def _action_open_work(self, data):
|
||||
model_data_obj = self.pool.get('ir.model.data')
|
||||
act_window_obj = self.pool.get('ir.action.act_window')
|
||||
work_obj = self.pool.get('timesheet.work')
|
||||
|
||||
if context is None:
|
||||
context = {}
|
||||
|
||||
act_window_id = model_data_obj.get_id(cursor, user, 'timesheet',
|
||||
'act_work_form3', context=context)
|
||||
res = act_window_obj.read(cursor, user, act_window_id, context=context)
|
||||
if 'active_id' in context:
|
||||
work = work_obj.browse(cursor, user, context['active_id'],
|
||||
context=context)
|
||||
act_window_id = model_data_obj.get_id('timesheet', 'act_work_form3')
|
||||
res = act_window_obj.read(act_window_id)
|
||||
if 'active_id' in Transaction().context:
|
||||
work = work_obj.browse(Transaction().context['active_id'])
|
||||
res['name'] = res['name'] + ' - ' + work.rec_name
|
||||
return res
|
||||
|
||||
|
||||
Reference in New Issue
Block a user