+ if self.completed:
+ raise Task.CompletedTask("Cannot complete a completed task")
+ elif self.deleted:
+ raise Task.DeletedTask("Deleted task cannot be completed")
+
+ self.backend.complete_task(self)
+
+ # Refresh the status again, so that we have updated info stored
+ self.refresh(only_fields=['status', 'start', 'end'])
+
+ def save(self):
+ if self.saved and not self.modified:
+ return
+
+ # All the actual work is done by the backend
+ self.backend.save_task(self)
+
+ def add_annotation(self, annotation):
+ if not self.saved:
+ raise Task.NotSaved("Task needs to be saved to add annotation")
+
+ self.backend.annotate_task(self, annotation)
+ self.refresh(only_fields=['annotations'])
+
+ def remove_annotation(self, annotation):
+ if not self.saved:
+ raise Task.NotSaved("Task needs to be saved to remove annotation")
+
+ if isinstance(annotation, TaskAnnotation):
+ annotation = annotation['description']
+
+ self.backend.denotate_task(self, annotation)
+ self.refresh(only_fields=['annotations'])
+
+ def refresh(self, only_fields=None, after_save=False):
+ # Raise error when trying to refresh a task that has not been saved
+ if not self.saved:
+ raise Task.NotSaved("Task needs to be saved to be refreshed")
+
+ new_data = self.backend.refresh_task(self, after_save=after_save)
+
+ if only_fields:
+ to_update = dict(
+ [(k, new_data.get(k)) for k in only_fields])
+ self._update_data(to_update, update_original=True)
+ else:
+ self._load_data(new_data)
+
+
+class TaskQuerySet(object):
+ """
+ Represents a lazy lookup for a task objects.
+ """
+
+ def __init__(self, backend, filter_obj=None):
+ self.backend = backend
+ self._result_cache = None
+ self.filter_obj = filter_obj or self.backend.filter_class(backend)
+
+ def __deepcopy__(self, memo):
+ """
+ Deep copy of a QuerySet doesn't populate the cache
+ """
+ obj = self.__class__(backend=self.backend)
+ for k, v in self.__dict__.items():
+ if k in ('_iter', '_result_cache'):
+ obj.__dict__[k] = None
+ else:
+ obj.__dict__[k] = copy.deepcopy(v, memo)
+ return obj
+
+ def __repr__(self):
+ data = list(self[:REPR_OUTPUT_SIZE + 1])
+ if len(data) > REPR_OUTPUT_SIZE:
+ data[-1] = "...(remaining elements truncated)..."
+ return repr(data)
+
+ def __len__(self):
+ if self._result_cache is None:
+ self._result_cache = list(self)
+ return len(self._result_cache)
+
+ def __iter__(self):
+ if self._result_cache is None:
+ self._result_cache = self._execute()
+ return iter(self._result_cache)
+
+ def __getitem__(self, k):
+ if self._result_cache is None:
+ self._result_cache = list(self)
+ return self._result_cache.__getitem__(k)
+
+ def __bool__(self):
+ if self._result_cache is not None:
+ return bool(self._result_cache)
+ try:
+ next(iter(self))
+ except StopIteration:
+ return False
+ return True
+
+ def __nonzero__(self):
+ return type(self).__bool__(self)
+
+ def _clone(self, klass=None, **kwargs):
+ if klass is None:
+ klass = self.__class__
+ filter_obj = self.filter_obj.clone()
+ c = klass(backend=self.backend, filter_obj=filter_obj)
+ c.__dict__.update(kwargs)
+ return c
+
+ def _execute(self):
+ """
+ Fetch the tasks which match the current filters.
+ """
+ return self.backend.filter_tasks(self.filter_obj)
+
+ def all(self):
+ """
+ Returns a new TaskQuerySet that is a copy of the current one.
+ """
+ return self._clone()
+
+ def pending(self):
+ return self.filter(status=PENDING)
+
+ def completed(self):
+ return self.filter(status=COMPLETED)