1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ##############################################################################
23 from osv import osv, fields
24 from osv.orm import except_orm
32 from tools.translate import _
41 'last-modified':'normal' ,
43 'attendee':'multiple',
45 'categories': 'normal',
46 'description':'normal',
48 # TODO: handle the 'duration' property
51 class document_directory_ics_fields(osv.osv):
52 _name = 'document.directory.ics.fields'
54 'field_id': fields.many2one('ir.model.fields', 'Open ERP Field', required=True),
55 'name': fields.selection(map(lambda x: (x, x), ICS_TAGS.keys()), 'ICS Value', required=True),
56 'content_id': fields.many2one('document.directory.content', 'Content', required=True, ondelete='cascade')
58 document_directory_ics_fields()
60 class document_directory_content(osv.osv):
61 _inherit = 'document.directory.content'
63 'ics_object_id': fields.many2one('ir.model', 'Object'),
64 'ics_domain': fields.char('Domain', size=64),
65 'ics_field_ids': fields.one2many('document.directory.ics.fields', 'content_id', 'Fields Mapping')
68 'ics_domain': lambda *args: '[]'
70 def process_write_ics(self, cr, uid, node, data, context={}):
72 parsedCal = vobject.readOne(data)
74 fobj = self.pool.get('document.directory.content')
75 content = fobj.browse(cr, uid, node.content.id, context)
78 for d in eval(content.ics_domain):
80 for n in content.ics_field_ids:
81 fields[n.name] = n.field_id.name
82 if 'uid' not in fields:
84 for child in parsedCal.getChildren():
87 for event in child.getChildren():
88 if event.name.lower()=='uid':
90 if event.name.lower() in fields:
91 if ICS_TAGS[event.name.lower()]=='normal':
92 result[fields[event.name.lower()]] = event.value.encode('utf8')
93 elif ICS_TAGS[event.name.lower()]=='date':
94 result[fields[event.name.lower()]] = event.value.strftime('%Y-%m-%d %H:%M:%S')
98 fobj = self.pool.get(content.ics_object_id.model)
99 id = fobj.search(cr, uid, [(fields['uid'], '=', uuid.encode('utf8'))], context=context)
101 fobj.write(cr, uid, id, result, context=context)
105 fobj.create(cr, uid, r, context=context)
109 def process_read_ics(self, cr, uid, node, context={}):
110 def ics_datetime(idate, short=False):
112 return datetime.date.fromtimestamp(time.mktime(time.strptime(idate, '%Y-%m-%d')))
114 return datetime.datetime.strptime(idate, '%Y-%m-%d %H:%M:%S')
117 obj_class = self.pool.get(node.content.ics_object_id.model)
118 # Can be improved to use context and active_id !
119 domain = eval(node.content.ics_domain)
120 ids = obj_class.search(cr, uid, domain, context)
121 cal = vobject.iCalendar()
122 for obj in obj_class.browse(cr, uid, ids, context):
123 event = cal.add('vevent')
124 # Fix dtstamp et last-modified with create and write date on the object line
125 perm = obj_class.perm_read(cr, uid, [obj.id], context)
126 event.add('created').value = ics_datetime(time.strftime('%Y-%m-%d %H:%M:%S'))
127 event.add('dtstamp').value = ics_datetime(perm[0]['create_date'][:19])
128 if perm[0]['write_date']:
129 event.add('last-modified').value = ics_datetime(perm[0]['write_date'][:19])
130 for field in node.content.ics_field_ids:
131 value = getattr(obj, field.field_id.name)
132 value = value and tools.ustr(value)
133 if (not value) and field.name=='uid':
134 value = 'OpenERP-%s_%s@%s' % (node.content.ics_object_id.model, str(obj.id), cr.dbname,)
135 obj_class.write(cr, uid, [obj.id], {field.field_id.name: value})
136 if ICS_TAGS[field.name]=='normal':
137 if type(value)==type(obj):
140 event.add(field.name).value = value or ''
141 elif ICS_TAGS[field.name]=='date' and value:
142 if field.name == 'dtstart':
143 date_start = start_date = datetime.datetime.fromtimestamp(time.mktime(time.strptime(value , "%Y-%m-%d %H:%M:%S")))
144 if field.name == 'dtend' and isinstance(value, float):
145 value = (start_date + datetime.timedelta(hours=value)).strftime('%Y-%m-%d %H:%M:%S')
147 value = ics_datetime(value, True)
149 value = ics_datetime(value)
150 event.add(field.name).value = value
151 s= StringIO.StringIO(cal.serialize())
155 document_directory_content()
157 class crm_case(osv.osv):
158 _inherit = 'crm.case'
160 'code': fields.char('Calendar Code', size=64),
161 'date_deadline': fields.datetime('Deadline', help="Deadline Date is automatically computed from Start Date + Duration"),
165 'code': lambda obj, cr, uid, context: obj.pool.get('ir.sequence').get(cr, uid, 'crm.case'),
168 def copy(self, cr, uid, id, default=None, context=None):
170 code field must be unique in ICS file
172 if not default: default = {}
173 if not context: context = {}
174 default.update({'code': self.pool.get('ir.sequence').get(cr, uid, 'crm.case'), 'id': False})
175 return super(crm_case, self).copy(cr, uid, id, default, context)
177 def on_change_duration(self, cr, uid, id, date, duration):
180 start_date = datetime.datetime.fromtimestamp(time.mktime(time.strptime(date, "%Y-%m-%d %H:%M:%S")))
182 end = start_date + datetime.timedelta(hours=duration)
184 raise osv.except_osv(_('Warning !'),
185 _('You can not set negative Duration.'))
187 res = {'value' : {'date_deadline' : end.strftime('%Y-%m-%d %H:%M:%S')}}
192 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: