def testGetOrCreateKeyword(self): # interactive tui.addInputAnswers("y") dbutils.getOrCreateKeyword("k1") self._assertOneObject(Keyword.selectBy(name="k1")) # !interactive dbutils.getOrCreateKeyword("k2", interactive=False) self._assertOneObject(Keyword.selectBy(name="k2"))
def getKeywordFromName(name): """Returns a keyword from its name, which may start with "@" raises a YokadiException if not found @param name: the keyword name @return: The keyword""" if not name: raise YokadiException("No keyword supplied") if name.startswith("@"): name = name[1:] lst = list(Keyword.selectBy(name=name)) if len(lst) == 0: raise YokadiException("No keyword named '%s' found" % name) return lst[0]
def getOrCreateKeyword(keywordName, interactive=True): """Get a keyword by its name. Create it if needed @param keywordName: keyword name as a string @param interactive: Ask user before creating keyword (this is the default) @type interactive: Bool @return: Keyword instance or None if user cancel creation""" result = Keyword.selectBy(name=keywordName) result = list(result) if len(result): return result[0] if interactive and not tui.confirm("Keyword '%s' does not exist, create it" % keywordName): return None keyword = Keyword(name=keywordName) print "Added keyword '%s'" % keywordName return keyword
def do_k_edit(self, line): """Edit a keyword k_edit @<keyword>""" keyword = dbutils.getKeywordFromName(line) oldName = keyword.name newName = tui.editLine(oldName) if newName == "": print "Cancelled" return lst = list(Keyword.selectBy(name=newName)) if len(lst) == 0: # Simple case: newName does not exist, just rename the existing keyword keyword.name = newName print "Keyword %s has been renamed to %s" % (oldName, newName) return # We already have a keyword with this name, we need to merge print "Keyword %s already exists" % newName if not tui.confirm("Do you want to merge %s and %s" % (oldName, newName)): return # Check we can merge conflictingTasks = [] for task in keyword.tasks: kwDict = task.getKeywordDict() if oldName in kwDict and newName in kwDict and kwDict[oldName] != kwDict[newName]: conflictingTasks.append(task) if len(conflictingTasks) > 0: # We cannot merge tui.error("Cannot merge keywords %s and %s because they are both used with different values in these tasks:" % (oldName, newName)) for task in conflictingTasks: print "- %d, %s" % (task.id, task.title) print "Edit these tasks and try again" return # Merge for task in keyword.tasks: kwDict = task.getKeywordDict() if not newName in kwDict: kwDict[newName] = kwDict[oldName] del kwDict[oldName] task.setKeywordDict(kwDict) keyword.destroySelf() print "Keyword %s has been merged with %s" % (oldName, newName)