~vauxoo/addons-vauxoo/6.0-trunk

« back to all changes in this revision

Viewing changes to bzr_to_task/bzr_to_task.py

  • Committer: nhomar at vauxoo
  • Date: 2011-06-11 22:55:27 UTC
  • Revision ID: nhomar@vauxoo.com-20110611225527-v2mlv4fbx38j0gg3
[SHELDON ] commiting ortografy

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#-*- coding:utf-8 -*-
2
 
#
3
 
#
4
 
#    OpenERP, Open Source Management Solution
5
 
#    Copyright (C)2010-  OpenERP SA (<http://openerp.com>). All Rights Reserved
6
 
#    oszckar@gmail.com
7
 
#
8
 
#    Developed by Oscar Alcala <oszckar@gmail.com>
9
 
#
10
 
#    This program is free software: you can redistribute it and/or modify
11
 
#    it under the terms of the GNU General Public License as published by
12
 
#    the Free Software Foundation, either version 3 of the License, or
13
 
#    (at your option) any later version.
14
 
#
15
 
#    This program is distributed in the hope that it will be useful,
16
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 
#    GNU General Public License for more details.
19
 
#
20
 
#    You should have received a copy of the GNU General Public License
21
 
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 
#
23
 
#
24
 
from openerp import pooler, tools
25
 
from openerp.osv import fields, osv
26
 
from openerp.tools.translate import _
27
 
from bzrlib import branch
28
 
import datetime
29
 
 
30
 
 
31
 
class project_project(osv.Model):
32
 
    _inherit = 'project.project'
33
 
    _columns = {
34
 
        'sprint_id': fields.many2one('sprint.kanban', 'Sprint',
35
 
                                     ondelete="cascade"),
36
 
        'url_branch': fields.char('Url Branch', 264),
37
 
        'merge_proposal': fields.char('Merge Proposal', 264),
38
 
        'blueprint': fields.char('Blueprint', 264),
39
 
        'res_id': fields.char('Revno', 64),
40
 
        'from_revno': fields.integer('From Revno')
41
 
    }
42
 
    _defaults = {
43
 
        'res_id': 0,
44
 
    }
45
 
 
46
 
    def get_works(self, cr, uid, ids, context=None):
47
 
        user_obj = self.pool.get('res.users')
48
 
        task_obj = self.pool.get('project.task')
49
 
        self_obj = self.browse(cr, uid, ids)[0]
50
 
        url = self_obj.url_branch
51
 
        res_id = self_obj.res_id
52
 
        inferior = self_obj.from_revno
53
 
        if url and res_id and inferior and int(res_id) > inferior:
54
 
            project_branch = branch.Branch.open(url)
55
 
            b_revno = project_branch.revno()
56
 
            if res_id:
57
 
                repo = project_branch.repository
58
 
                revision_map = project_branch.get_revision_id_to_revno_map()
59
 
                if revision_map:
60
 
                    for revision_id in revision_map.keys():
61
 
                        if revision_map[revision_id][0] in range(inferior, int(res_id) + 1):
62
 
                            task_data = {}
63
 
                            revision = repo.get_revision(revision_id)
64
 
                            date = datetime.datetime.fromtimestamp(int(
65
 
                                revision.timestamp)).strftime('%Y-%m-%d %H:%M:%S')
66
 
                            splitted_revision_id = revision_id.split('-')
67
 
                            email = revision_id[0]
68
 
                            user_ids = user_obj.search(
69
 
                                cr, uid, [('email', '=', email)])
70
 
                            task_data = {
71
 
                                'name': revision.message,
72
 
                                'date_deadline': date,
73
 
                                'revno': revision_map[revision_id][0],
74
 
                            }
75
 
                            if user_ids:
76
 
                                task_data['user_id'] = user_ids[0]
77
 
                            task_ids = task_obj.search(cr, uid, [('project_id', '=', ids[
78
 
                                                       0]), ('revno', '=', task_data['revno'])])
79
 
                            if not task_ids:
80
 
                                if inferior and inferior <= task_data['revno'] and int(res_id) >= task_data['revno']:
81
 
                                    self.write(cr, uid, ids, {
82
 
                                               'tasks': [(0, 0, task_data)]})
83
 
        else:
84
 
            raise osv.except_osv(('Error'), ('Fields: URL Branch, From Revno and Revno are required to execute this operation, \
85
 
                also From Revno must be minor than Revno'))
86
 
        return True
87
 
 
88
 
 
89
 
class sprint_kanban_tasks(osv.Model):
90
 
    _inherit = 'project.task'
91
 
    _columns = {
92
 
        'revno': fields.integer('Revno'),
93
 
        'from_revno': fields.integer('From Revno')
94
 
    }
95
 
    _defaults = {
96
 
        'res_id': 0,
97
 
    }
98
 
 
99
 
    def set_work_time(self, cr, uid, ids, context=None):
100
 
        rev_date = {}
101
 
        task_work_obj = self.pool.get('project.task.work')
102
 
        for task in self.browse(cr, uid, ids):
103
 
            for tw in task.work_ids:
104
 
                rev_date[tw.revno] = tw.date,
105
 
            for tw in task.work_ids:
106
 
                previous = tw.revno - 1
107
 
                if rev_date.get(previous, False):
108
 
                    p_date = datetime.datetime.strptime(
109
 
                        rev_date[previous][0], '%Y-%m-%d %H:%M:%S')
110
 
                    actual = datetime.datetime.strptime(
111
 
                        tw.date, '%Y-%m-%d %H:%M:%S')
112
 
                    result = actual - p_date
113
 
                    if float(result.seconds) / 60 / 60 > 0.16:
114
 
                        hours = float(result.seconds) / 60 / 60
115
 
                    else:
116
 
                        hours = 0.16
117
 
                    max_date = max(rev_date.itervalues())[0]
118
 
                    deadline = datetime.datetime.strptime(
119
 
                        max_date, '%Y-%m-%d %H:%M:%S')
120
 
                    deadline = deadline + datetime.timedelta(days=1)
121
 
                    task_work_obj.write(cr, uid, [tw.id], {'hours': hours})
122
 
 
123
 
        return deadline
124
 
 
125
 
    def get_works(self, cr, uid, ids, context=None):
126
 
        tw_obj = self.pool.get('project.task.work')
127
 
        obj_self = self.browse(cr, uid, ids)[0]
128
 
        user_obj = self.pool.get('res.users')
129
 
        url = obj_self.url_branch
130
 
        res_id = obj_self.res_id
131
 
        inferior = obj_self.from_revno
132
 
        if url and res_id and inferior and int(res_id) > inferior:
133
 
            task_branch = branch.Branch.open(url)
134
 
            b_revno = task_branch.revno()
135
 
            if res_id:
136
 
                repo = task_branch.repository
137
 
                revision_map = task_branch.get_revision_id_to_revno_map()
138
 
                if revision_map:
139
 
                    for k in revision_map.keys():
140
 
                        if revision_map[k][0] in range(inferior, int(res_id) + 1):
141
 
                            tw_data = {}
142
 
                            revision = repo.get_revision(k)
143
 
                            date = datetime.datetime.fromtimestamp(int(
144
 
                                revision.timestamp)).strftime('%Y-%m-%d %H:%M:%S')
145
 
                            revision_id = k.split('-')
146
 
                            email = revision_id[0]
147
 
                            user_ids = user_obj.search(
148
 
                                cr, uid, [('email', '=', email)])
149
 
                            tw_data = {
150
 
                                'name': revision.message,
151
 
                                'date': date,
152
 
                                'revno': revision_map[k][0],
153
 
                            }
154
 
                            if user_ids:
155
 
                                tw_data['user_id'] = user_ids[0]
156
 
                            tw_ids = tw_obj.search(cr, uid, [('task_id', '=', ids[
157
 
                                                   0]), ('revno', '=', tw_data['revno'])])
158
 
                            if not tw_ids:
159
 
                                if inferior and inferior <= tw_data['revno'] and int(res_id) >= tw_data['revno']:
160
 
                                    self.write(cr, uid, ids, {
161
 
                                               'work_ids': [(0, 0, tw_data), ], })
162
 
                    deadline = self.set_work_time(cr, uid, ids, context)
163
 
                    self.write(cr, uid, ids, {'date_deadline': deadline})
164
 
        else:
165
 
            raise osv.except_osv(('Error'), ('Fields: URL Branch, From Revno and Revno are required to execute this operation, \
166
 
                also From Revno must be minor than Revno'))
167
 
        return True
168
 
 
169
 
 
170
 
class project_task_work(osv.Model):
171
 
    _inherit = 'project.task.work'
172
 
    _columns = {
173
 
        'revno': fields.integer('Revno'),
174
 
    }