コード例 #1
0
def create_tasklist(caldav_conn, args):
    cal_obj = caldav.Principal(caldav_conn).make_calendar(
        cal_id=args.cal_id, supported_calendar_component_set=['VTODO'])
    if cal_obj:
        print("Created a task list with id " + args.tasklist_id)
コード例 #2
0
def create_calendar(caldav_conn, args):
    cal_obj = caldav.Principal(caldav_conn).make_calendar(cal_id=args.cal_id)
    if cal_obj:
        print("Created a calendar with id " + args.cal_id)
コード例 #3
0
 def setUp(self):
     self.client = caldav.DAVClient(URL)
     self.principal = caldav.Principal(self.client, URL)
     self.calendar, = [
         x for x in self.principal.calendars() if x.url.path.endswith(user)
     ]
コード例 #4
0
    def create(self, cr, uid, vals, context=None):
        # Synchornize a meeting with a caldav calendar
        # NICETOHAVE: Should be possible to choose not to sync a meeting
        meeting_id = super(crm_meeting, self).create(cr,
                                                     uid,
                                                     vals,
                                                     context=None)
        meeting = self.browse(cr, uid, meeting_id)

        # To change by meeting.responsible_id => What of meetings without responsible ?
        user = self.pool.get('res.users').browse(cr, uid, uid)
        print "MEETING CREATE CONTEXT:", context

        if user.caldav_state == 'validated' and 'from_caldav' not in context:
            # Could thread all this to speed up meeting creation
            try:
                client = caldav.DAVClient(user.caldav_url)
                principal = caldav.Principal(client, user.caldav_url)
                calendars = principal.calendars()
                if not calendars:
                    raise osv.except_osv(
                        _('CalDav Client Error'),
                        _('No CalDav calendar found for user %s' %
                          (user.name)))

                if len(calendars) > 0:
                    # To improve for multi-calendars
                    calendar = calendars[0]

                    # cannot use meeting.class so use vals['class']
                    if vals['class'] in ['private', 'confidential']:
                        accessrule = 'CONFIDENTIAL'
                        cdclass = 'PRIVATE'
                    else:
                        accessrule = 'PUBLIC'
                        cdclass = 'PUBLIC'

                    uniqueid = 'OPENERP-' + str(time.time())
                    dtstamp = time.strftime('%Y%m%dT%H%M%SZ')

                    # Convert from UTC to user timezone
                    tzone = timezone(
                        self.pool.get('res.users').read(cr, uid, uid,
                                                        ['tz'])['tz'])
                    dtstart_utc = pytz.utc.localize(
                        datetime.strptime(meeting.date, '%Y-%m-%d %H:%M:%S'))
                    dtstart_loc = dtstart_utc.astimezone(tzone)
                    dtstart = dtstart_loc.strftime('%Y%m%dT%H%M%S')
                    dtend_utc = pytz.utc.localize(
                        datetime.strptime(meeting.date_deadline,
                                          '%Y-%m-%d %H:%M:%S'))
                    dtend_loc = dtend_utc.astimezone(tzone)
                    dtend = dtend_loc.strftime('%Y%m%dT%H%M%S')

                    # To avoid 'False' in meeting description
                    if not meeting.description:
                        description = ""
                    else:
                        # TOFIX: bug with \n in description
                        description = repr(meeting.description)[2:-1]

                    if meeting.show_as == 'free':
                        dispo = 'FREE'
                    elif meeting.show_as == 'busy':
                        dispo = 'BUSY'

                    vcal = """
BEGIN:VCALENDAR
VERSION:2.0
X-CALENDARSERVER-ACCESS:%s
PRODID:-//SmartSolution//CalDAV Client//EN
BEGIN:VEVENT
UID:%s
DTSTAMP:%s
DTSTART:%s
DTEND:%s
SUMMARY:%s
DESCRIPTION:%s
CLASS:%s
X-MICROSOFT-CDO-BUSYSTATUS:%s
END:VEVENT
END:VCALENDAR
""" % (accessrule, uniqueid, dtstamp, dtstart, dtend, meeting.name,
                    description, cdclass, dispo)

                    # Create event in calendar
                    event = caldav.Event(client, data=vcal,
                                         parent=calendar).save()
                    print "CREATED EVENT:", event

                    if event:
                        self.write(cr,
                                   uid, [meeting_id], {'caldav_uid': uniqueid},
                                   context=context)

            except Exception, exception:
                print exception
                log = '%s ERROR Unable to create event %s\n' % (
                    time.strftime('%Y-%m-%d %H:%M:%S'), meeting.name) + (
                        user.caldav_log or "")
                self.pool.get('res.users').write(cr, uid, [user.id],
                                                 {'caldav_log': log})
コード例 #5
0
    def caldav_client_sync(self, cr, uid, ids, context=None):
        """ Sync CalDAV Server -> OpenERP user calendar """
        for user in self.browse(cr, uid, ids):
            client = caldav.DAVClient(user.caldav_url)
            print "CLIENT:", client
            principal = caldav.Principal(client, user.caldav_url)
            print "PRINCIPAL:", principal
            calendars = principal.calendars()
            print "CALENDARS:", calendars

            if len(calendars) > 0:
                # To improve for multi-calendars
                calendar = calendars[0]

                # Check if past event should be ignored
                # For performance issue in case LAST-MODIFIED is not send by the CalDAV server
                if user.caldav_ignore_past:
                    events = calendar.date_search(datetime.now())
                else:
                    events = calendar.events()
                print "events to import:", len(events)
                # Get events from calendar
                i = 0
                for event in events:
                    i += 1
                    print "Importing event : ", i
                    e = event.load()
                    #print "Event:",e.data
                    #print "Event:",e.instance

                    lst_data = list(i.split(':') for i in e.data.split('\n'))

                    # Construct a hierarchical dict from the event
                    data = {}
                    parent = []
                    for item in lst_data:
                        #print 'ITEM %s'%(item)
                        if item[0] == 'BEGIN':
                            element = item[1].strip('\r')
                            if data == {}:
                                parent.append(data)
                                data[element] = {}
                                current = data[element]
                            else:
                                current[element] = {}
                                parent.append(current)
                                current = current[element]
                        elif item[0] == 'END':
                            if len(parent):
                                current = parent[-1]
                            parent.pop()
                        else:
                            if item[0] == "":
                                # Bypass last empty element
                                continue
                            elif len(item) == 1:
                                continue
                            elif item[0][:7] == 'DTSTART':
                                element = 'DTSTART'
                                current[element] = item[1].strip('\r')
                            elif item[0][:5] == 'DTEND':
                                element = 'DTEND'
                                current[element] = item[1].strip('\r')
                            else:
                                element = item[0].strip('\r')
                                current[element] = item[1].strip('\r')
                    print "DATA:", data

                    meeting_id = self._meeting_create(cr,
                                                      uid,
                                                      data,
                                                      context=context)

                log = '%s INFO %s event successfully imported\n' % (
                    time.strftime('%Y-%m-%d %H:%M:%S'), len(events)) + (
                        user.caldav_log or "")
                self.pool.get('res.users').write(cr, uid, [user.id],
                                                 {'caldav_log': log})

        return True
コード例 #6
0
 def principal(self):
     if not hasattr(self, '_principal'):
         self._principal = caldav.Principal(self.client, self.url)
     return self._principal