def get_pad(self, pad_id): if not isinstance(pad_id, ObjectId): pad_id = Pad.make_id(pad_id) if pad_id == self._current_pad_id: return self.get_current_pad() pad = Pad.lookup(pad_id, self) return pad
def get_current_pad(self): """ The current pad always exists; A new one is created if necessary. """ if self._current_pad_id is None: cursor = Pad.iterate(self, 1) try: self._current_pad = cursor.next() except StopIteration: self._current_pad = Pad.make(self.get_id()) self._current_pad_id = self._current_pad.get_id() self.save() if self._current_pad is None: try: pad = Pad.lookup(self._current_pad_id, self) except PadInvalidId: pad = Pad.make(self) self._current_pad = pad self._current_pad_id = self._current_pad.get_id() self.save() return self._current_pad
def set_current_pad(self, pad): """ Set the currently active pad INPUT: - ``pad`` -- a :class:`pad.Pad` or a pad id (:class:`ObjectId` or string). Throws `PadInvalidId` if ``pad`` does not exists. """ if isinstance(pad, basestring): pad = Pad.make_id(pad) if isinstance(pad, ObjectId): if pad == self._current_pad_id: return pad = Pad.lookup(pad, self) if pad.get_id() == self._current_pad_id: return self._current_pad = pad self._current_pad_id = pad.get_id() self.save()