def _set_task(self, task: Task) -> None: logger.debug('set_task todo for %r', task.get_uuid()) with DisabledSyncCtx(task, sync_on_exit=False): seq_value = SEQUENCE.get_gtg(task, self.namespace) SEQUENCE.write_gtg(task, seq_value + 1, self.namespace) todo, calendar = self._get_todo_and_calendar(task) if not calendar: logger.info("%r has no calendar to be synced with", task) return if todo and todo.parent.url != calendar.url: # switch calendar self._remove_todo(UID_FIELD.get_dav(todo), todo) self._create_todo(task, calendar) elif todo: # found one, saving it if not Translator.should_sync(task, self.namespace, todo): logger.debug('insufficient change, ignoring set_task call') return # updating vtodo content Translator.fill_vtodo(task, calendar.name, self.namespace, todo.instance.vtodo) logger.info('SYNCING updating todo %r', todo) try: todo.save() except caldav.lib.error.DAVError: logger.exception( 'Something went wrong while updating ' '%r => %r', task, todo) else: # creating from task self._create_todo(task, calendar)
def _import_calendar_todos(self, calendar: iCalendar, import_started_on: datetime, counts: dict): todos = calendar.todos(include_completed=not self._cache.initialized) todo_uids = {UID_FIELD.get_dav(todo) for todo in todos} # browsing all task linked to current calendar, # removing missed ones we don't see in fetched todos calendar_tasks = dict(self._get_calendar_tasks(calendar)) for uid in set(calendar_tasks).difference(todo_uids): self._clean_task_missing_from_backend(uid, calendar_tasks, counts, import_started_on) self._denorm_children_on_vtodos(todos) for todo in self.__sort_todos(todos): uid = UID_FIELD.get_dav(todo) self._cache.set_todo(todo, uid) # Updating and creating task according to todos task = self.datastore.get_task(uid) if not task: # not found, creating it task = self.datastore.task_factory(uid) with DisabledSyncCtx(task): Translator.fill_task(todo, task, self.namespace) self.datastore.push_task(task) counts['created'] += 1 else: result = self._update_task(task, todo) counts[result] += 1 if logger.isEnabledFor(logging.DEBUG): if Translator.should_sync(task, self.namespace, todo): logger.warning("Shouldn't be diff for %r", uid)
def _update_task(self, task: Task, todo: iCalendar, force: bool = False): with DisabledSyncCtx(task): if not force: task_seq = SEQUENCE.get_gtg(task, self.namespace) todo_seq = SEQUENCE.get_dav(todo) if task_seq >= todo_seq: return 'unchanged' Translator.fill_task(todo, task, self.namespace) return 'updated'
def fill_task(cls, todo: iCalendar, task: Task, namespace: str): nmspc = {'namespace': namespace} with DisabledSyncCtx(task): for field in cls.fields: field.set_gtg(todo, task, **nmspc) task.set_attribute("url", str(todo.url), **nmspc) task.set_attribute("calendar_url", str(todo.parent.url), **nmspc) task.set_attribute("calendar_name", todo.parent.name, **nmspc) if not CATEGORIES.has_calendar_tag(task, todo.parent): task.add_tag(CATEGORIES.get_calendar_tag(todo.parent)) return task