~ubuntu-branches/ubuntu/precise/tryton-modules-project/precise

« back to all changes in this revision

Viewing changes to work.py

  • Committer: Bazaar Package Importer
  • Author(s): Mathias Behrle
  • Date: 2011-02-15 13:13:51 UTC
  • mfrom: (4.1.1 experimental)
  • Revision ID: james.westby@ubuntu.com-20110215131351-a8hd8upkwagc90fd
Tags: 1.8.0-2
* Changing my email address.
* Setting minimal Python version to 2.5.
* Updating Copyright.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#This file is part of Tryton.  The COPYRIGHT file at the top level of
2
2
#this repository contains the full copyright notices and license terms.
3
 
"Project"
 
3
from __future__ import with_statement
 
4
import copy
4
5
from trytond.model import ModelView, ModelSQL, fields
5
6
from trytond.model.modelstorage import OPERATORS
6
7
from trytond.pyson import Not, Bool, Eval, Get, Equal
7
 
import copy
 
8
from trytond.transaction import Transaction
8
9
 
9
10
 
10
11
class TimesheetWork(ModelSQL, ModelView):
76
77
                'required': Equal(Eval('type'), 'task'),
77
78
            }, select=1, depends=['type'])
78
79
 
79
 
    def default_type(self, cursor, user, context=None):
80
 
        if context.get('type') == 'project':
 
80
    def default_type(self):
 
81
        if Transaction().context.get('type') == 'project':
81
82
            return 'project'
82
83
        return 'task'
83
84
 
84
 
    def default_state(self, cursor, user, context=None):
 
85
    def default_state(self):
85
86
        return 'opened'
86
87
 
87
88
    def __init__(self):
102
103
 
103
104
            self._inherit_fields['company'] = company_field
104
105
 
105
 
    def get_parent(self, cursor, user, ids, name, context=None):
 
106
    def get_parent(self, ids, name):
106
107
        res = dict.fromkeys(ids, None)
107
 
        project_works = self.browse(cursor, user, ids, context=context)
 
108
        project_works = self.browse(ids)
108
109
 
109
110
        # ptw2pw is "parent timesheet work to project works":
110
111
        ptw2pw = {}
114
115
            else:
115
116
                ptw2pw[project_work.work.parent.id] = [project_work.id]
116
117
 
117
 
        ctx = context and context.copy() or {}
118
 
        ctx['active_test'] = False
119
 
        parent_project_ids = self.search(cursor, user, [
120
 
                ('work', 'in', ptw2pw.keys()),
121
 
                ], context=ctx)
122
 
        parent_projects = self.browse(cursor, user, parent_project_ids,
123
 
                context=context)
 
118
        with Transaction().set_context(active_test=False):
 
119
            parent_project_ids = self.search([
 
120
                    ('work', 'in', ptw2pw.keys()),
 
121
                    ])
 
122
        parent_projects = self.browse(parent_project_ids)
124
123
        for parent_project in parent_projects:
125
124
            if parent_project.work.id in ptw2pw:
126
125
                child_projects = ptw2pw[parent_project.work.id]
129
128
 
130
129
        return res
131
130
 
132
 
    def set_parent(self, cursor, user, ids, name, value, context=None):
 
131
    def set_parent(self, ids, name, value):
133
132
        timesheet_work_obj = self.pool.get('timesheet.work')
134
133
        if value:
135
 
            project_works = self.browse( cursor, user, ids + [value],
136
 
                    context=context)
 
134
            project_works = self.browse(ids + [value])
137
135
            child_timesheet_work_ids = [x.work.id for x in project_works[:-1]]
138
136
            parent_timesheet_work_id = project_works[-1].work.id
139
137
        else:
140
 
            child_project_works = self.browse( cursor, user, ids, context=context)
 
138
            child_project_works = self.browse(ids)
141
139
            child_timesheet_work_ids = [x.work.id for x in child_project_works]
142
140
            parent_timesheet_work_id = False
143
141
 
144
 
        timesheet_work_obj.write(cursor, user, child_timesheet_work_ids, {
 
142
        timesheet_work_obj.write(child_timesheet_work_ids, {
145
143
                'parent': parent_timesheet_work_id
146
 
                },
147
 
                context=context)
 
144
                })
148
145
 
149
 
    def search_parent(self, cursor, user, name, domain=None, context=None):
 
146
    def search_parent(self, name, domain=None):
150
147
        timesheet_work_obj = self.pool.get('timesheet.work')
151
148
 
152
149
        project_work_domain = []
172
169
        if operands:
173
170
            operands = list(operands)
174
171
            # filter out non-existing ids:
175
 
            operands = self.search(cursor, user, [
 
172
            operands = self.search([
176
173
                    ('id', 'in', operands)
177
 
                    ], context=context)
 
174
                    ])
178
175
            # create project_work > timesheet_work mapping
179
 
            for pw in self.browse(cursor, user, operands, context=context):
 
176
            for pw in self.browse(operands):
180
177
                pw2tw[pw.id] = pw.work.id
181
178
 
182
179
            for i, d in enumerate(timesheet_work_domain):
190
187
                timesheet_work_domain[i] = (d[0], d[1], new_d2)
191
188
 
192
189
        if project_work_domain:
193
 
            pw_ids = self.search(
194
 
                cursor, user, project_work_domain, context=context)
195
 
            project_works = self.browse(cursor, user, pw_ids, context=context)
 
190
            pw_ids = self.search(project_work_domain)
 
191
            project_works = self.browse(pw_ids)
196
192
            timesheet_work_domain.append(
197
193
                ('id', 'in', [pw.work.id for pw in project_works]))
198
194
 
199
 
        tw_ids = timesheet_work_obj.search(
200
 
            cursor, user, timesheet_work_domain, context=context)
 
195
        tw_ids = timesheet_work_obj.search(timesheet_work_domain)
201
196
 
202
197
        return [('work', 'in', tw_ids)]
203
198
 
204
 
    def get_total_effort(self, cursor, user, ids, name, context=None):
 
199
    def get_total_effort(self, ids, name):
205
200
 
206
 
        all_ids = self.search(cursor, user, [
 
201
        all_ids = self.search([
207
202
                ('parent', 'child_of', ids),
208
 
                ('active', '=', True)], context=context) + ids
 
203
                ('active', '=', True),
 
204
                ]) + ids
209
205
        all_ids = list(set(all_ids))
210
206
 
211
 
        works = self.browse(cursor, user, all_ids, context=context)
 
207
        works = self.browse(all_ids)
212
208
 
213
209
        res = {}
214
210
        id2work = {}
233
229
        return res
234
230
 
235
231
 
236
 
    def delete(self, cursor, user, ids, context=None):
 
232
    def delete(self, ids):
237
233
        timesheet_work_obj = self.pool.get('timesheet.work')
238
234
 
239
235
        if isinstance(ids, (int, long)):
240
236
            ids = [ids]
241
237
 
242
238
        # Get the timesheet works linked to the project works
243
 
        project_works = self.browse(cursor, user, ids, context=context)
 
239
        project_works = self.browse(ids)
244
240
        timesheet_work_ids = [pw.work.id for pw in project_works]
245
241
 
246
 
        res = super(Work, self).delete(cursor, user, ids, context=context)
 
242
        res = super(Work, self).delete(ids)
247
243
 
248
 
        timesheet_work_obj.delete(
249
 
            cursor, user, timesheet_work_ids, context=context)
 
244
        timesheet_work_obj.delete(timesheet_work_ids)
250
245
        return res
251
246
 
252
247