~jamesj/openobject-addons/c2c_budget_fixes

« back to all changes in this revision

Viewing changes to project_event/project_event.py

[IMP] Adding multi_company_stock_module, nto finished

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# -*- encoding: utf-8 -*-
2
2
##############################################################################
3
3
#
4
 
#    OpenERP, Open Source Management Solution    
 
4
#    OpenERP, Open Source Management Solution
5
5
#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6
6
#    $Id$
7
7
#
19
19
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
20
#
21
21
##############################################################################
 
22
import time
22
23
 
23
24
from osv import fields, osv
24
25
import pooler
26
27
from tools.config import config
27
28
from tools.translate import _
28
29
import netsvc
29
 
import time
 
30
 
30
31
 
31
32
class project_project(osv.osv):
32
33
    _inherit = "project.project"
33
 
    _columns = {                
 
34
    _columns = {
34
35
        "event_ids": fields.one2many('project.event', 'project_id', 'Events', readonly=True),
35
36
        "event_configuration_ids": fields.one2many('project.event.configuration', 'project_id', 'Event Configuration'),
36
37
    }
37
 
    
 
38
 
38
39
    def _log_event(self, cr, uid, project_id, values={}, context={}):
39
 
        obj_project_event = self.pool.get('project.event') 
40
 
        values['project_id'] = project_id      
41
 
        obj_project_event.create(cr, uid, values)
42
 
        
 
40
        values['project_id'] = project_id
 
41
        if type(project_id) == type([]):
 
42
            values['project_id'] = project_id[0]
 
43
        self.pool.get('project.event').create(cr, uid, values, context)
 
44
 
43
45
project_project()
44
46
 
45
47
class project_event_type(osv.osv):
46
48
    _name = "project.event.type"
47
49
    _columns = {
48
 
        'name': fields.char('Name',size=64, required=True),        
49
 
        'code': fields.char('Code',size=64, required=True)       
 
50
        'name': fields.char('Name',size=64, required=True),
 
51
        'code': fields.char('Code',size=64, required=True)
50
52
    }
51
53
 
52
54
project_event_type()
60
62
class project_event(osv.osv):
61
63
    _name = "project.event"
62
64
    _columns = {
63
 
        'name': fields.char('Events', size=64, required=True),        
64
 
        'description': fields.text('Description'), 
65
 
        'res_id': fields.integer('Resource Id'),       
 
65
        'name': fields.char('Events', size=64, required=True),
 
66
        'description': fields.text('Description'),
 
67
        'res_id': fields.integer('Resource Id'),
66
68
        'project_id': fields.many2one('project.project', 'Project', select=True, required=True),
67
69
        'date': fields.datetime('Date', size=16),
68
70
        'user_id': fields.many2one('res.users', 'User', required=True),
69
 
        'action' : fields.selection([('create','Create'),('write','Modify'),('unlink','Remove')], 'Action', required=True),            
70
 
        'type': fields.selection(_links_get, 'Type of Event', required=True),        
 
71
        'action' : fields.selection([('create','Create'),('write','Modify'),('unlink','Remove')], 'Action', required=True),
 
72
        'type': fields.selection(_links_get, 'Type of Event', required=True),
71
73
    }
72
74
    _order = 'date desc'
73
75
    _defaults = {
74
76
        'date': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'),
75
77
    }
76
 
    
 
78
 
77
79
    def create(self, cr, uid, values, *args, **kwargs):
78
 
        attach = False 
 
80
        attach = False
79
81
        new_values = {}
80
82
        for key in values:
81
83
            if key != 'attach':
82
 
                new_values[key] = values[key]        
 
84
                new_values[key] = values[key]
83
85
        event_id = super(project_event, self).create(cr, uid, new_values, *args, **kwargs)
84
86
        cr.commit()
85
87
        project_id = values.get('project_id',False)
86
 
        if project_id:            
 
88
        if project_id:
87
89
            obj_configuration = self.pool.get('project.event.configuration')
88
90
            config_ids = obj_configuration.search(cr, uid, [('project_id','=',project_id)])
89
91
            obj_configuration.run(cr, uid, config_ids, values)
90
 
            
 
92
 
91
93
project_event()
92
94
 
93
95
class project_event_configuration(osv.osv):
94
96
    _name = "project.event.configuration"
95
 
    _columns = {        
 
97
    _columns = {
96
98
        'name': fields.char('Name',size=64, required=True),
97
 
        'project_id': fields.many2one('project.project', 'Project', select=True, required=True),   
98
 
        'create' :fields.boolean('On Create'), 
99
 
        'write' :fields.boolean('On Write'),  
 
99
        'project_id': fields.many2one('project.project', 'Project', select=True, required=True),
 
100
        'create' :fields.boolean('On Create'),
 
101
        'write' :fields.boolean('On Write'),
100
102
        'unlink' :fields.boolean('On Delete'),
101
 
        'type': fields.selection(_links_get, 'Type of Event', required=True),        
 
103
        'type': fields.selection(_links_get, 'Type of Event', required=True),
102
104
        'action_type': fields.selection([('email','Email'),('sms','SMS'),('request','Request')], 'Action Type', required=True),
103
 
    }  
 
105
    }
104
106
    _defaults = {
105
107
        'action_type': lambda *a: 'email',
106
 
    }  
 
108
    }
107
109
 
108
 
    def send_mail(self, to_address, subject, body, attach=None):        
109
 
        sent = tools.email_send(config['email_from'], to_address, subject, body, attach=attach)
 
110
    def send_mail(self, to_address, subject, body, attach=None):
 
111
        sent = tools.email_send(config['email_from'], to_address, subject, body, attach=attach, ssl=True)
110
112
        logger = netsvc.Logger()
111
 
        if not sent:                
 
113
        if not sent:
112
114
            logger.notifyChannel('email', netsvc.LOG_ERROR, 'Failed to send email to : %s' % (to_address))
113
 
        else:                        
 
115
        else:
114
116
            logger.notifyChannel('email', netsvc.LOG_INFO, 'Email successfully send to : %s' % (to_address))
115
117
 
116
118
    def run(self, cr, uid, ids, values, context={}):
117
 
        for config in self.browse(cr, uid, ids, context=context):            
 
119
        for config in self.browse(cr, uid, ids, context=context):
118
120
            if values.get('action',False) and config[values['action']] and config.type == values.get('type',False):
119
121
                action_to = []
120
122
                for member in config.project_id.members:
128
130
                        action = 'Modified'
129
131
                    elif config.unlink:
130
132
                        action = 'Deleted'
131
 
                    subject = '[%s] %s - %d : %s (%s)' %(config.project_id.name, config.type, values.get('res_id',False), values.get('name',False),action)
 
133
                    subject = '[%s] %s - %d : %s ' %(config.project_id.name, config.type, values.get('res_id',False), values.get('name',False))
132
134
                    body = values.get('description',False)
133
 
                    self.send_mail(action_to, subject, body, attach=values.get('attach',False)) 
 
135
                    self.send_mail(action_to, subject, body, attach=values.get('attach',False))
134
136
                elif config.action_type == 'sms':
135
137
                    #TODO
136
 
                    pass 
 
138
                    pass
137
139
                elif config.action_type == 'request':
138
140
                    #TODO
139
 
                    pass 
140
 
    
 
141
                    pass
 
142
 
141
143
project_event_configuration()
142
144
 
143
145
class project_task(osv.osv):
144
146
    _inherit = 'project.task'
145
 
    
 
147
 
146
148
    def create(self, cr, uid, values, context={}):
147
149
        res = super(project_task, self).create(cr, uid, values, context=context)
148
 
        cr.commit()        
 
150
        cr.commit()
149
151
        task = self.browse(cr, uid, res, context=context)
150
 
        if task.project_id:         
 
152
        if task.project_id:
 
153
            desc = ''' Hello, \n \t The new task is created for the Project: %s \n\n And its Details are: \n \n Task: %s \n Created on: %s \n Assigned to: %s \n Deadline: %s \n Planned hours: %s \n Remaining hours: %s \n Total Hours: %s \n For Partner: %s \n Task Summary: \n ====== \n %s \n \n ======= \n \nThanks,\nProject Manager \n%s''' \
 
154
                       %(task.project_id.name,\
 
155
                         task.name, task.date_start, task.user_id.name, \
 
156
                         task.date_deadline or '', task.planned_hours or 0, \
 
157
                         task.remaining_hours or 0, task.total_hours or 0,\
 
158
                         task.partner_id and task.partner_id.name or '',\
 
159
                         task.description or '',task.project_id.manager and task.project_id.manager.name or '')
151
160
            self.pool.get('project.project')._log_event(cr, uid, task.project_id.id, {
152
161
                                'res_id' : task.id,
153
 
                                'name' : task.name, 
154
 
                                'description' : task.description, 
155
 
                                'user_id': uid, 
 
162
                                'name' : task.name,
 
163
                                'description' : desc,
 
164
                                'user_id': uid,
156
165
                                'action' : 'create',
157
166
                                'type' : 'task'})
158
167
        return res
159
 
    
 
168
 
160
169
    def write(self, cr, uid, ids, vals, context={}):
 
170
        task = self.browse(cr, uid, ids)[0]
161
171
        res = super(project_task, self).write(cr, uid, ids, vals, context={})
162
172
        cr.commit()
163
 
        for task in self.browse(cr, uid, ids, context=context):
164
 
            if task.project_id:         
165
 
                self.pool.get('project.project')._log_event(cr, uid, task.project_id.id, {
166
 
                                    'res_id' : task.id,
167
 
                                    'name' : task.name, 
168
 
                                    'description' : task.description, 
169
 
                                    'user_id': uid, 
170
 
                                    'action' : 'write',
171
 
                                    'type' : 'task'})
 
173
        task_data = self.browse(cr, uid, ids[0], context)
 
174
        desc = '''Hello ,\n\n  The task is updated for the project: %s\n\nModified Datas are:\n''' %(str(task.project_id.name),)
 
175
        for val in vals:
 
176
            if val.endswith('id') or val.endswith('ids'):
 
177
                continue
 
178
            desc += val + ':' + str(vals[val]) + "\n"
 
179
        desc += '\nThanks,\n' + 'Project Manager\n' + (task_data.project_id.manager and task_data.project_id.manager.name) or ''
 
180
        self.pool.get('project.project')._log_event(cr, uid, task.project_id.id, {
 
181
                                                                'res_id' : ids[0],
 
182
                                                                'name' : task.name or '',
 
183
                                                                'description' : desc,
 
184
                                                                'user_id': uid,
 
185
                                                                'action' : 'write',
 
186
                                                                'type' : 'task'})
172
187
        return res
173
 
    
 
188
 
174
189
project_task()
175
190
 
176
191
class document_file(osv.osv):
177
192
    _inherit = 'ir.attachment'
178
 
    
 
193
 
179
194
    def create(self, cr, uid, values, *args, **kwargs):
180
195
        res = super(document_file, self).create(cr, uid, values, *args, **kwargs)
181
196
        cr.commit()
182
197
        document = self.browse(cr, uid, res)
183
 
        if document.res_model == 'project.project' and document.res_id:         
 
198
        if document.file_size >= 1073741824:
 
199
                size = str((document.file_size) / 1024 / 1024 / 1024) + ' GB'
 
200
        elif document.file_size >= 1048576:
 
201
            size = str((document.file_size) / 1024 / 1024) + ' MB'
 
202
        elif document.file_size >= 1024:
 
203
            size = str((document.file_size) / 1024) + ' KB'
 
204
        elif document.file_size < 1024:
 
205
            size = str(document.file_size) + ' bytes'
 
206
 
 
207
        if document.res_model == 'project.project' and document.res_id:
 
208
            desc = ''' Hello, \n \n \t The new document is uploaded on the Project: %s \n\n Document attached: %s \n Attachment name: %s \n Owner: %s \n Size: %s \n Creator: %s \n Date Created: %s \n Document Summary: %s \n \n Thanks,\n Project Manager\n''' \
 
209
                       %(document.title, document.datas_fname, document.name, \
 
210
                         document.user_id.name, size, document.create_uid.name, document.create_date, document.description or '')
184
211
            self.pool.get('project.project')._log_event(cr, uid, document.res_id, {
185
212
                                'res_id' : document.id,
186
213
                                'name' : document.name,
187
 
                                'description' : document.description,                                 
188
 
                                'user_id': uid, 
 
214
                                'description' :desc,#document description,
 
215
                                'user_id': uid,
189
216
                                'attach' : [(document.datas_fname, document.datas)],
190
217
                                'action' : 'create',
191
 
                                'type' : 'document'})        
192
 
        return res  
193
 
    
 
218
                                'type' : 'document'})
 
219
        return res
 
220
 
194
221
document_file()
195
222
 
196
223
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: