+
+class basic_calendar_line(osv.osv):
+ """ Calendar Lines """
+
+ _name = 'basic.calendar.lines'
+ _description = 'Calendar Lines'
+
+ _columns = {
+ 'name': fields.selection([('vevent', 'Event'), ('vtodo', 'TODO'), \
+ ('valarm', 'Alarm'), \
+ ('attendee', 'Attendee')], \
+ string="Type", size=64),
+ 'object_id': fields.many2one('ir.model', 'Object'),
+ 'calendar_id': fields.many2one('basic.calendar', 'Calendar', \
+ required=True, ondelete='cascade'),
+ 'domain': fields.char('Domain', size=124),
+ 'mapping_ids': fields.one2many('basic.calendar.fields', 'type_id', 'Fields Mapping')
+ }
+
+ _defaults = {
+ 'domain': lambda *a: '[]',
+ }
+
+ def create(self, cr, uid, vals, context=None):
+ """ create calendar's line
+ @param self: The object pointer
+ @param cr: the current row, from the database cursor,
+ @param uid: the current user’s ID for security checks,
+ @param vals: Get the Values
+ @param context: A standard dictionary for contextual values
+ """
+
+ cr.execute("SELECT COUNT(id) FROM basic_calendar_lines \
+ WHERE name=%s AND calendar_id=%s",
+ (vals.get('name'), vals.get('calendar_id')))
+ res = cr.fetchone()
+ if res:
+ if res[0] > 0:
+ raise osv.except_osv(_('Warning !'), _('Can not create line "%s" more than once') % (vals.get('name')))
+ return super(basic_calendar_line, self).create(cr, uid, vals, context=context)
+
+basic_calendar_line()
+
+class basic_calendar_alias(osv.osv):
+ """ Mapping of client filenames to ORM ids of calendar records
+
+ Since some clients insist on putting arbitrary filenames on the .ics data
+ they send us, and they won't respect the redirection "Location:" header,
+ we have to store those filenames and allow clients to call our calendar
+ records with them.
+ Note that adding a column to all tables that would possibly hold calendar-
+ mapped data won't work. The user is always allowed to specify more
+ calendars, on any arbitrary ORM object, without need to alter those tables'
+ data or structure
+ """
+ _name = 'basic.calendar.alias'
+ _columns = {
+ 'name': fields.char('Filename', size=512, required=True, select=1),
+ 'cal_line_id': fields.many2one('basic.calendar.lines', 'Calendar', required=True,
+ select=1, help='The calendar/line this mapping applies to'),
+ 'res_id': fields.integer('Res. ID', required=True, select=1),
+ }
+
+ _sql_constraints = [ ('name_cal_uniq', 'UNIQUE(cal_line_id, name)',
+ _('The same filename cannot apply to two records!')), ]
+
+basic_calendar_alias()
+
+class basic_calendar_attribute(osv.osv):
+ _name = 'basic.calendar.attributes'
+ _description = 'Calendar attributes'
+ _columns = {
+ 'name': fields.char("Name", size=64, required=True),
+ 'type': fields.selection([('vevent', 'Event'), ('vtodo', 'TODO'), \
+ ('alarm', 'Alarm'), \
+ ('attendee', 'Attendee')], \
+ string="Type", size=64, required=True),
+ }
+
+basic_calendar_attribute()
+
+
+class basic_calendar_fields(osv.osv):
+ """ Calendar fields """
+
+ _name = 'basic.calendar.fields'
+ _description = 'Calendar fields'
+ _order = 'name'
+
+ _columns = {
+ 'field_id': fields.many2one('ir.model.fields', 'OpenObject Field'),
+ 'name': fields.many2one('basic.calendar.attributes', 'Name', required=True),
+ 'type_id': fields.many2one('basic.calendar.lines', 'Type', \
+ required=True, ondelete='cascade'),
+ 'expr': fields.char("Expression", size=64),
+ 'fn': fields.selection([('field', 'Use the field'),
+ ('const', 'Expression as constant'),
+ ('hours', 'Interval in hours'),
+ ('datetime_utc', 'Datetime In UTC'),
+ ], 'Function'),
+ 'mapping': fields.text('Mapping'),
+ }
+
+ _defaults = {
+ 'fn': 'field',
+ }
+
+ _sql_constraints = [
+ ( 'name_type_uniq', 'UNIQUE(name, type_id)', 'Can not map a field more than once'),
+ ]
+
+ def check_line(self, cr, uid, vals, name, context=None):
+ """ check calendar's line
+ @param self: The object pointer
+ @param cr: the current row, from the database cursor,
+ @param uid: the current user’s ID for security checks,
+ @param vals: Get Values
+ @param context: A standard dictionary for contextual values
+ """
+ f_obj = self.pool.get('ir.model.fields')
+ field = f_obj.browse(cr, uid, vals['field_id'], context=context)
+ relation = field.relation
+ line_obj = self.pool.get('basic.calendar.lines')
+ l_id = line_obj.search(cr, uid, [('name', '=', name)])
+ if l_id:
+ line = line_obj.browse(cr, uid, l_id, context=context)[0]
+ line_rel = line.object_id.model
+ if (relation != 'NULL') and (not relation == line_rel):
+ raise osv.except_osv(_('Warning !'), _('Please provide proper configuration of "%s" in Calendar Lines') % (name))
+ return True
+
+ def create(self, cr, uid, vals, context=None):
+ """ Create Calendar's fields
+ @param self: The object pointer
+ @param cr: the current row, from the database cursor,
+ @param uid: the current user’s ID for security checks,
+ @param vals: Get Values
+ @param context: A standard dictionary for contextual values
+ """
+
+ cr.execute('SELECT name FROM basic_calendar_attributes \
+ WHERE id=%s', (vals.get('name'),))
+ name = cr.fetchone()
+ name = name[0]
+ if name in ('valarm', 'attendee'):
+ self.check_line(cr, uid, vals, name, context=context)
+ return super(basic_calendar_fields, self).create(cr, uid, vals, context=context)
+
+ def write(self, cr, uid, ids, vals, context=None):
+ """ write Calendar's fields
+ @param self: The object pointer
+ @param cr: the current row, from the database cursor,
+ @param uid: the current user’s ID for security checks,
+ @param vals: Get Values
+ @param context: A standard dictionary for contextual values
+ """
+
+ if not vals:
+ return
+ for id in ids:
+ field = self.browse(cr, uid, id, context=context)
+ name = field.name.name
+ if name in ('valarm', 'attendee'):
+ self.check_line(cr, uid, vals, name, context=context)
+ return super(basic_calendar_fields, self).write(cr, uid, ids, vals, context)
+
+basic_calendar_fields()
+
+