~openerp-community/openobject-addons/mgmtsystem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
# -*- encoding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
#    $Id$
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import time
import tools
from osv import fields,osv,orm
import pooler

import mx.DateTime
import base64
from tools.translate import _
from SimpleXMLRPCServer import SimpleXMLRPCServer
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler
import os
import threading
import pickle
import time
import sys
import datetime

try:
    from launchpadlib.launchpad import Launchpad, EDGE_SERVICE_ROOT
    from launchpadlib.credentials import Credentials
except:
    raise osv.except_osv('Warning!','Please install launchpadlib direction package from https://help.launchpad.net/API/launchpadlib#Installation ')

class lpServer(threading.Thread):
    launchpad = False

    def __init__(self):
        super(lpServer, self).__init__()
        self.launchpad = self.get_lp()

    def get_lp(self):
        launchpad = False
        cachedir = os.path.expanduser('~/.launchpadlib/cache')
        if not os.path.exists(cachedir):
                os.makedirs(cachedir,0700)
        credfile = os.path.expanduser('~/.launchpadlib/credentials')
        try:
                credentials = Credentials()
                credentials.load(open(credfile))
                launchpad = Launchpad(credentials, EDGE_SERVICE_ROOT, cachedir)
        except:
                launchpad = Launchpad.get_token_and_login(sys.argv[0], EDGE_SERVICE_ROOT, cachedir)
        return launchpad


    def get_lp_bugs(self, projects):
        launchpad = self.launchpad
        res = {}
        if not launchpad:
            return res
        if not isinstance(projects,list):
            projects = [projects]

        bug_status=[]
        for project in projects:
            result = {}
            r = {}
            lp_project = self.launchpad.projects[str(project)]
            result['non-series'] = lp_project.searchTasks(status=bug_status)
            if 'series' in lp_project.lp_collections:
                for series in lp_project.series:
                    result[series.name] = series.searchTasks()
                bug_list=[]
                for name, bugs in result.items():
                    for bug in bugs:
                        bug_list.append(bug)
                res[project]=bug_list
        return  res

    def getProject(self, project):
        try:
            project = self.launchpad.projects[project]
        except:
            return None
        return project

    def getSeries(self, project):
        lp_project = self.launchpad.projects[project]
        if 'series' in lp_project.lp_collections:
                return lp_project.series.entries
        else:
            return None

    def getMilestones(self, project, ml):
        lp_project =self.launchpad.projects[project]
        if 'all_milestones' in lp_project.lp_collections:
                temp = lp_project.all_milestones.entries
                res = [item for item in temp if item['series_target_link'] == ml]
                return res
        else:
            return None

class project_project(osv.osv):
    _inherit = "project.project"
    _columns = {
                'series_ids' : fields.one2many('project.series', 'project_id', 'LP Series'),
                'milestone_ids' : fields.one2many('project.milestone', 'project_id', 'LP Milestone'),
                'bugs_target': fields.char('Bugs Target', size=300),
                'flag':fields.boolean('Synchronize Lp'),
                }
    def write(self, cr, uid, ids, vals, context=None, check=True, update_check=True):
        if vals.get('name' , False):
            vals['bugs_target'] = "https://api.edge.launchpad.net/beta/" + vals['name']
        return  super(osv.osv, self).write(cr, uid, ids, vals, context)

project_project()

class project_series(osv.osv):
    _name="project.series"
    _description="Project Series"
    _columns={
              'name':fields.char("Series Name",size=200, required=True, help="The name of the series"),
              'status': fields.char("Status", size=100),
              'summary': fields.char("Summary", size=1000, help="The summary should be a single short paragraph."),
              'project_id': fields.many2one('project.project', 'LP Project', ondelete='cascade'),
               'milestone_ids' : fields.one2many('project.milestone', 'series_id', 'LP Milestone'),
              }
project_series()

class project_milestone(osv.osv):
    _name="project.milestone"
    _description= "Project milestone"
    _columns={
        'name':fields.char('Version', size=100,required=True),
        'series_id':fields.many2one('project.series', 'Series', readonly=True,ondelete='cascade'),
        'project_id': fields.many2one('project.project', 'Project', readonly=True),
        'expect_date': fields.datetime('Expected Date', readonly=True),
        }

project_milestone()

class project_issue(osv.osv):
    _inherit = "project.issue"

    _columns = {
                'bug_id': fields.integer('Bug ID',readonly=True),
                'milestone_id': fields.many2one('project.milestone', 'Milestone'),
                'bug_owner_id': fields.many2one('res.users', 'Bug Owner'),                
                }

    def write(self, cr, uid, ids, vals, context=None, check=True, update_check=True):
        attrs = ['stage_id', 'priority']
        for attr in attrs:
            if attr in vals:
                vals['date_action_last'] = time.strftime('%Y-%m-%d %H:%M:%S')
        result = super(osv.osv, self).write(cr, uid, ids, vals, context)
        return result

    def _check_bug(self, cr, uid, ids=False,date_from=False,date_to=False, context={}):
        '''
        Function called by the scheduler to process cases for date actions
        Only works on not done and cancelled cases
        '''
        pool=pooler.get_pool(cr.dbname)
        lp_server = lpServer()
        val={}
        if date_from and date_to:
              self._find_project_bug(cr,uid,lp_server,date_from,date_to)
        else:
              self._create_bug(cr, uid, lp_server, context)
             # self._find_project_bug(cr,uid,lp_server)
      
        return True

    def _create_bug(self, cr, uid, lp_server=None,context={}):
        pool=pooler.get_pool(cr.dbname)
        case_stage= pool.get('crm.case.stage')
        
        crm_case_obj = pool.get('project.issue')
        for sec_id in section_id:
            crm_ids=crm_case_obj.search(cr,uid,[('bug_id','=',False),('project_id','!=',False)])
            launchpad = lp_server.launchpad
            if crm_ids:
                for case in crm_case_obj.browse(cr,uid, crm_ids):
                    title = case.name
                    target = case.project_id.bugs_target
                    if not target:
                        target = "https://api.edge.launchpad.net/beta/" + case.project_id.name
                    description=case.description
                    b=launchpad.bugs.createBug(title=title, target=target, description=description)
                    if b:
                        bool = self.write(cr,uid,case.id,{'bug_id' : b.id},context=None)
                        status='New'
                        imp ='Undecided'
                        if case.stage_id.name == 'Future' and case.priority == '5':
                            imp = 'Wishlist'
                        elif case.priority == '1':
                            imp = 'High'
                        elif case.priority == '3':
                            imp = 'Medium'
                        if case.stage_id.name == 'Fixed':
                            status = 'Fix Released'
                        if case.stage_id.id == 'Invalid':
                            status = 'invalid'
                        t=b.bug_tasks[0]
                        t.status = status
                        t.importance = imp
                        t.lp_save()
            return True
            

    def _find_project_bug(self, cr, uid,lp_server=None,date_from=False,date_to=False,context={}):
        pool=pooler.get_pool(cr.dbname)
        case_stage= pool.get('crm.case.stage')
        categ_id=self.pool.get('crm.case.categ').search(cr, uid, [('object_id.model','=','project.issue'),('name','=','Bugs')])
        categ_fix_id=case_stage.search(cr, uid, [('object_id.model','=','project.issue'),('name','=','Fixed')])
        categ_inv_id=case_stage.search(cr, uid, [('object_id.model','=','project.issue'),('name','=','Invalid')])
        categ_future_id=case_stage.search(cr, uid, [('object_id.model','=','project.issue'), ('name','=','Future')])
        categ_wfix_id=case_stage.search(cr, uid, [('object_id.model','=','project.issue'),('name','=',"Won't fix")])
        val={}
        res={}
        series_ids=[]
        prj = self.pool.get('project.project')
        
        project_id=prj.search(cr,uid,[])
        for prj_id in prj.browse(cr,uid, project_id):
            
            project_name=str(prj_id.name)
            
            lp_project = lp_server.getProject(project_name)
            if lp_project and prj_id.flag:
                prjs=lp_server.get_lp_bugs(project_name)
                self._get_project_series( cr, uid,lp_project,prj_id.id,lp_server)
                for key, bugs in prjs.items():
                        for bug in bugs:
                            lp_date=mx.DateTime.strptime(str(bug.date_created).split('T')[0],"%Y-%m-%d").strftime("%Y-%m-%d")
                            start_date=mx.DateTime.strptime(date_from,"%Y-%m-%d").strftime("%Y-%m-%d")
                            end_date=mx.DateTime.strptime(date_to,"%Y-%m-%d").strftime("%Y-%m-%d")
                            
                            if lp_date >=start_date and end_date<=end_date:
                                b_id = self.search(cr,uid,[('bug_id','=',bug.bug.id)])
                                val['project_id']=prj_id.id
                                val['categ_id']=categ_id and categ_id[0] or False
                                val['description']=bug.bug.description                                
                                val['bug_id']=bug.bug.id
                                val['name']=bug.bug.title
                                owner = bug.owner
                                parnter_rec=self.pool.get('res.partner')
                                user_rec=self.pool.get('res.users')                                  
                                partner_id = parnter_rec.search(cr,uid,[('name', '=',owner.display_name)])
                                user_id = user_rec.search(cr,uid,[('login', '=',owner.name)])
                                if not partner_id and not user_id :
                                    res={} 
                                    res['name']=owner.display_name
                                    res['login']=owner.name
                                    res['password']=owner.name
                                    res['lp_login']=owner.name                                    
                                    user_id = user_rec.create(cr,uid,res,context=context)                                    
                                    partner_id = parnter_rec.create(cr, uid, {'name': owner.display_name,'user_id':user_id})
                                else:
                                    partner_id= partner_id and partner_id[0] or False
                                    user_id=user_id and user_id[0] or False
                                if bug.assignee:
                                    assing_id = user_rec.search(cr,uid,[('login', '=', bug.assignee.name)])
                                    if assing_id:
                                         val['user_id']=assing_id[0]
                                    else: 
                                        res={}
                                        res['name']=bug.assignee.name
                                        res['login']=bug.assignee.name
                                        res['password']=bug.assignee.name
                                        res['lp_login']=bug.assignee.name                                                             
                                        assing_id = user_rec.create(cr,uid,res,context=context)
                                        val['user_id']=assing_id                                          
                                val['partner_id'] =partner_id                                           
                                val['bug_owner_id']= user_id  
                                if bug.importance == 'Wishlist':
                                    val['stage_id']=categ_future_id and categ_future_id[0] or False
                                    val['priority']='5'
                                elif bug.importance == 'Critical':
                                    val['priority']='1'
                                    val['state']='open'                                    
                                elif bug.importance=='High':
                                    val['priority']='2'
                                    val['state']='open'                                    
                                elif bug.importance=='Medium':
                                    val['priority']='3'
                                    val['state']='open'                                    
                                if bug.status =='Fix Released':
                                    val['stage_id']=categ_fix_id[0] or False
                                    val['state']='done'
                                if bug.status =='invaild':
                                    val['stage_id']= val['stage_id']=categ_inv_id[0] or False
                                    val['state']='cancel'     
                                if bug.status=='Confirmed':
                                    val['state']='open'   
                                if bug.milestone_link:
                                    val['milestone_url']=bug.milestone_link
                                    ml = bug.milestone_link.rsplit('/',1)[0]
                                    ml_ids = self.pool.get('project.milestone').search(cr, uid, [('project_id','=',prj_id.id),('name','=', ml)])                          
                                if not b_id:
                                    val['analytic_account_id']=prj_id.category_id.id
                                    bug_id=self.create(cr, uid, val,context=context)
                                    self._check_state(cr, uid,[bug_id],val)     
                                    self._store_bug_history(cr, uid , bug_id,bug,val['bug_owner_id'])                               
                                if b_id:
                                    crm_case = self.browse(cr,uid, b_id[0])
                                    lp_last_up_time = str(bug.bug.date_last_updated).split('.')[0]
                                    lp_last_up_timestamp = time.mktime(time.strptime(lp_last_up_time,"%Y-%m-%dT%H:%M:%S"))+ time.timezone
                                    if not crm_case.date_action_last:
                                        local_last_up_time=0
                                        local_last_up_timestamp = 0
                                        local_last_up_timestamp1=0
                                    else:
                                        local_last_up_time = str(crm_case.date_action_last).split('.')[0]
                                        local_last_up_timestamp = time.mktime(time.strptime(local_last_up_time,"%Y-%m-%dT%H:%M:%S")) + time.timezone
                                        local_last_up_timestamp1 = time.mktime(time.strptime(local_last_up_time,'%Y-%m-%dT%H:%M:%S'))
    
                                    args = (cr, uid, context,crm_case, bug, val)
                                    if lp_last_up_timestamp >= local_last_up_timestamp:
                                        self._update_local_record(*args)
                                    elif lp_last_up_timestamp < local_last_up_timestamp:
                                        self._update_lp_record(*args)
                                        cr.commit()
        return True

    def _get_project_series(self, cr, uid,lp_project,lp_project_id,lp_server=None,context={}):
        pool=pooler.get_pool(cr.dbname)
        all_series = lp_server.getSeries(lp_project)
        if all_series:
            prj_milestone_ids=[]
            lp_series = pool.get('project.series')
            for series in all_series:
                res={}
                ids = self.pool.get('project.series').search(cr, uid, [('project_id','=',lp_project_id),('name','=', series['name'])])
                if not ids:                
                    res['name'] = series['name']
                    res['status'] = series['status']
                    res['summary'] = series['summary']
                    res['project_id'] = lp_project_id
                    lp_series_id=lp_series.create(cr, uid, res,context=context)
                    ml = series['all_milestones_collection_link'].rsplit('/',1)[0]
                    cr.commit()
                    self._get_project_milestone(cr, uid, lp_series_id,ml,lp_project,lp_project_id, lp_server)
        return True

    def _get_project_milestone(self, cr, uid,lp_series_id,milestone,lp_project,lp_project_id,lp_server=None,context={}):
        pool=pooler.get_pool(cr.dbname)
        res={}
        all_milestones = lp_server.getMilestones(lp_project, milestone)
        if all_milestones:
            milestone_ids=[]
            lp_milestones = pool.get('project.milestone')
            for ms in all_milestones:
                ids = self.pool.get('project.milestone').search(cr, uid, [('project_id','=',lp_project_id),('series_id','=',lp_series_id),('name','=', ms['name'])])
                if not ids:
                    res['name'] = ms['name']
                    res['series_id'] = lp_series_id
                    res['project_id'] = lp_project_id
                    if ms['date_targeted']:
                        res['expect_date'] = ms['date_targeted']
                    lp_milestones_id=lp_milestones.create(cr, uid, res,context=context)
                    cr.commit()
        return True

    def _update_local_record(self, cr, uid, context, crm_bug, lp_bug, val):
        pool=pooler.get_pool(cr.dbname)
        case= pool.get('project.issue')
        b=case.write(cr,uid,crm_bug.id,val,context=context)
        if b:
            case._check_state(cr, uid,[crm_bug.id],val)
        return True

    def _update_lp_record(self, cr, uid, context,crm_bug, lp_bug, val):
        pool=pooler.get_pool(cr.dbname)
        case_stage= pool.get('crm.case.stage')

        status='New'
        imp ='Undecided'
        if crm_bug.stage_id.name == 'Future' and crm_bug.priority == '5':
            imp = 'Wishlist'
        elif crm_bug.priority == '1':
            imp = 'Critical'
        elif crm_bug.priority == '2':
            imp = 'High'
        elif crm_bug.priority == '3':
            imp = 'Medium'
        if crm_bug.stage_id.name == 'Fixed':
            status = 'Fix Released'
        if crm_bug.stage_id.name == 'Invalid':
            status = 'Invalid'
        t=lp_bug.bug.bug_tasks[0]
        t.status = status
        t.importance = imp
        t.lp_save()
        return True
    
    def _check_state(self, cr, uid,bug_id,vals={}):
        partner = self.pool.get('res.users').browse(cr, uid, uid).address_id.partner_id.id
        if not vals['partner_id']:
            vals['partner_id']=partner
        if vals['state']=='open': 
            self.case_open(cr,uid,bug_id) 
        if vals['state']=='cancel': 
            self.case_cancel(cr,uid,bug_id)   
        if vals['state']=='done': 
            self.case_close(cr,uid,bug_id)
        return True 
    
    def _store_bug_history(self, cr, uid,bug_id,bug,user_id,context={}):
        model_obj = self.pool.get('ir.model')        
        model_ids = model_obj.search(cr, uid, [('model','=',self._name)])          
        obj = self.pool.get('mailgate.message')
        
        for key in bug.bug.messages.entries:        
            if key['self_link'].rsplit('/',1)[1]!=0:
                attach_id=False
                if bug.bug.attachments:
                    attachment = bug.bug.attachments[0]  
                    attachment_data = attachment.data
                    attachment_fd = attachment_data.open() 
                    ids = self.pool.get('ir.attachment').search(cr, uid, [('name','=',attachment_fd.filename.split('.')[0]),('res_model','=','project.issue'),])
                    if not ids:
                        attach_id=self.pool.get('ir.attachment').create(cr, uid, {
                                            'name': attachment_fd.filename.split('.')[0],
                                            'datas': base64.encodestring(attachment_fd.read()),
                                            'datas_fname':attachment_fd.filename,
                                            'res_model': self._name,
                                            'res_id': bug_id,
                                            }, context=context  )
                        data = {
                        'name': bug.status,
                        'user_id': uid,
                        'model_id': model_ids[0] or False,
                        'res_id':bug_id,
                        'description':key['content'],
                        'filename':attach_id,
                        'bug_owner_id':user_id}   
                        obj.create(cr, uid, data, context)                                   
                else:
                    data={'name': bug.status,
                    'user_id': uid,
                    'model_id': model_ids[0] or False,
                    'res_id':bug_id,
                    'description':key['content'],
                    'bug_owner_id':user_id}                     
#                    
                    obj.create(cr, uid, data, context)
        cr.commit()
        return True      
project_issue()

class res_users(osv.osv):
    _inherit = 'res.users'
    _columns = {
        'lp_login': fields.char('Launchpad Login', size=100),
        }
res_users()


class mailgate_message(osv.osv):
    _inherit = 'mailgate.message'
    _columns = {    
        'bug_owner_id': fields.many2one('res.users', 'Bug Owner'),
        'filename':fields.many2one('ir.attachment','File'),        
    }
mailgate_message()    
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: