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 _
42 'last-modified':'normal' ,
44 'attendee':'multiple',
46 'categories': 'normal',
47 'description':'normal',
49 # TODO: handle the 'duration' property
52 class document_directory_ics_fields(osv.osv):
53 _name = 'document.directory.ics.fields'
55 'field_id': fields.many2one('ir.model.fields', 'Open ERP Field', required=True),
56 'name': fields.selection(map(lambda x: (x, x), ICS_TAGS.keys()), 'ICS Value', required=True),
57 'content_id': fields.many2one('document.directory.content', 'Content', required=True, ondelete='cascade')
59 document_directory_ics_fields()
61 class document_directory_content(osv.osv):
62 _inherit = 'document.directory.content'
64 'ics_object_id': fields.many2one('ir.model', 'Object'),
65 'ics_domain': fields.char('Domain', size=64),
66 'ics_field_ids': fields.one2many('document.directory.ics.fields', 'content_id', 'Fields Mapping')
69 'ics_domain': lambda *args: '[]'
71 def process_write_ics(self, cr, uid, node, data, context={}):
73 parsedCal = vobject.readOne(data)
75 fobj = self.pool.get('document.directory.content')
76 content = fobj.browse(cr, uid, node.content.id, context)
79 for d in eval(content.ics_domain):
81 for n in content.ics_field_ids:
82 fields[n.name] = n.field_id.name
83 if 'uid' not in fields:
85 for child in parsedCal.getChildren():
88 for event in child.getChildren():
89 if event.name.lower()=='uid':
91 if event.name.lower() in fields:
92 if ICS_TAGS[event.name.lower()]=='normal':
93 result[fields[event.name.lower()]] = event.value.encode('utf8')
94 elif ICS_TAGS[event.name.lower()]=='date':
95 result[fields[event.name.lower()]] = event.value.strftime('%Y-%m-%d %H:%M:%S')
99 fobj = self.pool.get(content.ics_object_id.model)
100 id = fobj.search(cr, uid, [(fields['uid'], '=', uuid.encode('utf8'))], context=context)
102 fobj.write(cr, uid, id, result, context=context)
106 fobj.create(cr, uid, r, context=context)
110 def process_read_ics(self, cr, uid, node, context={}):
111 def ics_datetime(idate, short=False):
113 return datetime.date.fromtimestamp(time.mktime(time.strptime(idate, '%Y-%m-%d')))
115 return mx.DateTime.strptime(idate, '%Y-%m-%d %H:%M:%S')
118 obj_class = self.pool.get(node.content.ics_object_id.model)
119 # Can be improved to use context and active_id !
120 domain = eval(node.content.ics_domain)
121 ids = obj_class.search(cr, uid, domain, context)
122 cal = vobject.iCalendar()
123 for obj in obj_class.browse(cr, uid, ids, context):
124 event = cal.add('vevent')
125 # Fix dtstamp et last-modified with create and write date on the object line
126 perm = obj_class.perm_read(cr, uid, [obj.id], context)
127 event.add('created').value = ics_datetime(time.strftime('%Y-%m-%d %H:%M:%S'))
128 event.add('dtstamp').value = ics_datetime(perm[0]['create_date'][:19])
129 if perm[0]['write_date']:
130 event.add('last-modified').value = ics_datetime(perm[0]['write_date'][:19])
131 for field in node.content.ics_field_ids:
132 value = getattr(obj, field.field_id.name)
133 value = value and tools.ustr(value)
134 if (not value) and field.name=='uid':
135 value = 'OpenERP-%s_%s@%s' % (node.content.ics_object_id.model, str(obj.id), cr.dbname,)
136 obj_class.write(cr, uid, [obj.id], {field.field_id.name: value})
137 if ICS_TAGS[field.name]=='normal':
138 if type(value)==type(obj):
141 event.add(field.name).value = value or ''
142 elif ICS_TAGS[field.name]=='date' and value:
143 if field.name == 'dtstart':
144 date_start = start_date = datetime.datetime.fromtimestamp(time.mktime(time.strptime(value , "%Y-%m-%d %H:%M:%S")))
145 if field.name == 'dtend' and isinstance(value, float):
146 value = (start_date + datetime.timedelta(hours=value)).strftime('%Y-%m-%d %H:%M:%S')
148 value = ics_datetime(value, True)
150 value = ics_datetime(value)
151 event.add(field.name).value = value
152 s= StringIO.StringIO(cal.serialize())
156 document_directory_content()
158 class crm_case(osv.osv):
159 _inherit = 'crm.case'
161 'code': fields.char('Calendar Code', size=64),
162 'date_deadline': fields.datetime('Deadline', help="Deadline Date is automatically computed from Start Date + Duration"),
166 'code': lambda obj, cr, uid, context: obj.pool.get('ir.sequence').get(cr, uid, 'crm.case'),
169 def copy(self, cr, uid, id, default=None, context=None):
171 code field must be unique in ICS file
173 if not default: default = {}
174 if not context: context = {}
175 default.update({'code': self.pool.get('ir.sequence').get(cr, uid, 'crm.case'), 'id': False})
176 return super(crm_case, self).copy(cr, uid, id, default, context)
178 def on_change_duration(self, cr, uid, id, date, duration):
181 start_date = datetime.datetime.fromtimestamp(time.mktime(time.strptime(date, "%Y-%m-%d %H:%M:%S")))
183 end = start_date + datetime.timedelta(hours=duration)
185 raise osv.except_osv(_('Warning !'),
186 _('You can not set negative Duration.'))
188 res = {'value' : {'date_deadline' : end.strftime('%Y-%m-%d %H:%M:%S')}}
193 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: