def sync(self, commands=None, **kwargs): start_time = time.time() new_state = super(UserTodoistAPI, self).sync(commands, **kwargs) self._save_statsd(start_time) return_or_raise(new_state) self.user_obj.api_state = self.serialize() self.user_obj.api_last_sync = now() self.user_obj.save(update_fields=['api_state', 'api_last_sync']) return new_state
def sync(self, commands=None, **kwargs): current_state = deepcopy(self.state) start_time = time.time() new_state = super(StatefulTodoistAPI, self).sync(commands, **kwargs) _save_integration_statsd(self.integration, start_time) return_or_raise(new_state) if kwargs.pop('save_state', True): self.integration.api_state = self.serialize() self.integration.save(update_fields=['api_state']) self.emit_sync_signals(current_state, new_state) return new_state
def push_task(self, task_id, task, extra): todoist_task_fields = ['checked', 'in_history', 'indent', 'item_order', 'priority', 'due_date', 'date_string', 'content'] task_dict = task._asdict() kwargs = {} for field_name in todoist_task_fields: value = task_dict[field_name] if value is not undefined: kwargs[field_name] = value if task_id: self.api.item_update(task_id, **kwargs) return_or_raise(self.api.commit()) return task_id, {} # return task_id and extra else: content = defined(kwargs.pop('content', 'New Task'), 'New Task') obj = self.api.items.add(content, self.project_id, **kwargs) return_or_raise(self.api.commit()) return obj['id'], {} # return task_id and extra
def get_personal_project(integration, project_name, pid_field='project_id'): """ It's a common thing for integrations to have personal projects, which they operate in. This helper function searches for a "personal project id" saved in the integration. If project is found and exist, it returns it. Otherwise it creates a new project with a given name, saves its id in the integration settings, and returns the object """ # we're using the "shared API", because in a stateless mode we don't # have access to all user projects api = integration.user.api assert isinstance(api, UserTodoistAPI) # IDE hint settings = integration.settings or {} # Try to find a project by id project_id = settings.get(pid_field) # it's a broken project, ignore it if str(project_id).startswith('$'): project_id = None if project_id: project = api.projects.get_by_id(project_id) if project: return project # Project is not found. Search by project name, or create a new one projects = api.projects.all(filt=lambda p: p['name'] == project_name and not str(p['id']).startswith('$')) project = projects[0] if projects else api.projects.add(project_name) # commit all changes and update settings return_or_raise(api.commit()) integration.update_settings(**{pid_field: project['id']}) return project
def complete_task(self, task_id, extra): self.api.item_update(task_id, checked=True, in_history=True) return_or_raise(self.api.commit())
def autocommit(self): yield return_or_raise(self.commit())