def test_create_help_entry__simple(self): entry = create.create_help_entry("testentry", self.help_entry, category="Testing") self.assertEqual(entry.key, "testentry") self.assertEqual(entry.entrytext, self.help_entry) self.assertEqual(entry.help_category, "Testing") # creating same-named entry should not work (must edit existing) self.assertFalse(create.create_help_entry("testentry", "testtext"))
def get_help_entry(key, text, category="General", locks=None, aliases=None): """Get or create a help entry. Args: key (str): the help entry key. text (str): the content of the help entry that will be updated or created. category (str, optional): the help entry category. locks (str, optional): locks to be used. aliases (list, optional): a list of aliases to associate to this help entry. Note: Lock access type can be: view: who can view this help entry """ try: topic = HelpEntry.objects.get(db_key=key) except HelpEntry.DoesNotExist: topic = create.create_help_entry(key, dedent(text), category) # Update the help entry topic.entrytext = dedent(text) topic.category = category if locks: topic.locks.clear() topic.locks.add(locks) if aliases: topic.aliases.clear() topic.aliases.add(aliases) topic.save() return topic
def test_create_help_entry__complex(self): locks = "foo:false();bar:true()" aliases = ['foo', 'bar', 'tst'] tags = [("tag1", "help"), ("tag2", "help"), ("tag3", "help")] entry = create.create_help_entry("testentry", self.help_entry, category="Testing", locks=locks, aliases=aliases, tags=tags) self.assertTrue(all(lock in entry.locks.all() for lock in locks.split(";"))) self.assertEqual(list(entry.aliases.all()).sort(), aliases.sort()) self.assertEqual(entry.tags.all(return_key_and_category=True), tags)
def func(self): """Implement the function""" switches = self.switches lhslist = self.lhslist if not self.args: self.msg( "Usage: @sethelp[/switches] <topic>[;alias;alias][,category[,locks,..] = <text>" ) return nlist = len(lhslist) topicstr = lhslist[0] if nlist > 0 else "" if not topicstr: self.msg("You have to define a topic!") return topicstrlist = topicstr.split(";") topicstr, aliases = topicstrlist[0], topicstrlist[1:] if len( topicstr) > 1 else [] aliastxt = ("(aliases: %s)" % ", ".join(aliases)) if aliases else "" old_entry = None # check if we have an old entry with the same name try: for querystr in topicstrlist: old_entry = HelpEntry.objects.find_topicmatch( querystr) # also search by alias if old_entry: old_entry = list(old_entry)[0] break category = lhslist[1] if nlist > 1 else old_entry.help_category lockstring = ",".join( lhslist[2:]) if nlist > 2 else old_entry.locks.get() except Exception: old_entry = None category = lhslist[1] if nlist > 1 else "General" lockstring = ",".join(lhslist[2:]) if nlist > 2 else "view:all()" category = category.lower() if 'edit' in switches: # open the line editor to edit the helptext. No = is needed. if old_entry: topicstr = old_entry.key if self.rhs: # we assume append here. old_entry.entrytext += "\n%s" % self.rhs helpentry = old_entry else: helpentry = create.create_help_entry(topicstr, self.rhs, category=category, locks=lockstring, aliases=aliases) self.caller.db._editing_help = helpentry EvEditor(self.caller, loadfunc=_loadhelp, savefunc=_savehelp, quitfunc=_quithelp, key="topic {}".format(topicstr), persistent=True) return if 'append' in switches or "merge" in switches or "extend" in switches: # merge/append operations if not old_entry: self.msg( "Could not find topic '%s'. You must give an exact name." % topicstr) return if not self.rhs: self.msg("You must supply text to append/merge.") return if 'merge' in switches: old_entry.entrytext += " " + self.rhs else: old_entry.entrytext += "\n%s" % self.rhs old_entry.aliases.add(aliases) self.msg("Entry updated:\n%s%s" % (old_entry.entrytext, aliastxt)) return if 'delete' in switches or 'del' in switches: # delete the help entry if not old_entry: self.msg("Could not find topic '%s'%s." % (topicstr, aliastxt)) return old_entry.delete() self.msg("Deleted help entry '%s'%s." % (topicstr, aliastxt)) return # at this point it means we want to add a new help entry. if not self.rhs: self.msg("You must supply a help text to add.") return if old_entry: if 'replace' in switches: # overwrite old entry old_entry.key = topicstr old_entry.entrytext = self.rhs old_entry.help_category = category old_entry.locks.clear() old_entry.locks.add(lockstring) old_entry.aliases.add(aliases) old_entry.save() self.msg("Overwrote the old topic '%s'%s." % (topicstr, aliastxt)) else: self.msg( "Topic '%s'%s already exists. Use /replace to overwrite " "or /append or /merge to add text to it." % (topicstr, aliastxt)) else: # no old entry. Create a new one. new_entry = create.create_help_entry(topicstr, self.rhs, category=category, locks=lockstring, aliases=aliases) if new_entry: self.msg("Topic '%s'%s was successfully created." % (topicstr, aliastxt)) if 'edit' in switches: # open the line editor to edit the helptext self.caller.db._editing_help = new_entry EvEditor(self.caller, loadfunc=_loadhelp, savefunc=_savehelp, quitfunc=_quithelp, key="topic {}".format(new_entry.key), persistent=True) return else: self.msg( "Error when creating topic '%s'%s! Contact an admin." % (topicstr, aliastxt))
def func(self): """Implement the function""" switches = self.switches lhslist = self.lhslist if not self.args: self.msg( "Usage: @sethelp/[add|del|append|merge] <topic>[,category[,locks,..] = <text>" ) return topicstr = "" category = "General" lockstring = "view:all()" try: topicstr = lhslist[0] category = lhslist[1] lockstring = ",".join(lhslist[2:]) except Exception: pass if not topicstr: self.msg("You have to define a topic!") return # check if we have an old entry with the same name try: old_entry = HelpEntry.objects.get(db_key__iexact=topicstr) except Exception: old_entry = None if 'append' in switches or "merge" in switches: # merge/append operations if not old_entry: self.msg( "Could not find topic '%s'. You must give an exact name." % topicstr) return if not self.rhs: self.msg("You must supply text to append/merge.") return if 'merge' in switches: old_entry.entrytext += " " + self.rhs else: old_entry.entrytext += "\n\n%s" % self.rhs self.msg("Entry updated:\n%s" % old_entry.entrytext) return if 'delete' in switches or 'del' in switches: # delete the help entry if not old_entry: self.msg("Could not find topic '%s'" % topicstr) return old_entry.delete() self.msg("Deleted help entry '%s'." % topicstr) return # at this point it means we want to add a new help entry. if not self.rhs: self.msg("You must supply a help text to add.") return if old_entry: if 'for' in switches or 'force' in switches: # overwrite old entry old_entry.key = topicstr old_entry.entrytext = self.rhs old_entry.help_category = category.lower() old_entry.locks.clear() old_entry.locks.add(lockstring) old_entry.save() self.msg("Overwrote the old topic '%s' with a new one." % topicstr) else: msg = "Topic '%s' already exists." % topicstr msg += " Use /force to overwrite or /append or /merge to add text to it." self.msg(msg) else: # no old entry. Create a new one. new_entry = create.create_help_entry(topicstr, self.rhs, category.lower(), lockstring) if new_entry: self.msg("Topic '%s' was successfully created." % topicstr) else: self.msg( "Error when creating topic '%s'! Maybe it already exists?" % topicstr)
def func(self): "Implement the function" switches = self.switches lhslist = self.lhslist if not self.args: self.msg("Usage: @sethelp[/switches] <topic>[;alias;alias][,category[,locks,..] = <text>") return nlist = len(lhslist) topicstr = lhslist[0] if nlist > 0 else "" if not topicstr: self.msg("You have to define a topic!") return topicstrlist = topicstr.split(";") topicstr, aliases = topicstrlist[0], topicstrlist[1:] if len(topicstr) > 1 else [] aliastxt = ("(aliases: %s)" % ", ".join(aliases)) if aliases else "" old_entry = None # check if we have an old entry with the same name try: for querystr in topicstrlist: old_entry = HelpEntry.objects.find_topicmatch(querystr) # also search by alias if old_entry: old_entry = list(old_entry)[0] break category = lhslist[1] if nlist > 1 else old_entry.help_category lockstring = ",".join(lhslist[2:]) if nlist > 2 else old_entry.locks.get() except Exception: old_entry = None category = lhslist[1] if nlist > 1 else "General" lockstring = ",".join(lhslist[2:]) if nlist > 2 else "view:all()" category = category.lower() if 'edit' in switches: # open the line editor to edit the helptext. No = is needed. if old_entry: topicstr = old_entry.key if self.rhs: # we assume append here. old_entry.entrytext += "\n%s" % self.rhs helpentry = old_entry else: helpentry = create.create_help_entry(topicstr, self.rhs, category=category, locks=lockstring,aliases=aliases) self.caller.db._editing_help = helpentry EvEditor(self.caller, loadfunc=_loadhelp, savefunc=_savehelp, quitfunc=_quithelp, key="topic {}".format(topicstr), persistent=True) return if 'append' in switches or "merge" in switches or "extend" in switches: # merge/append operations if not old_entry: self.msg("Could not find topic '%s'. You must give an exact name." % topicstr) return if not self.rhs: self.msg("You must supply text to append/merge.") return if 'merge' in switches: old_entry.entrytext += " " + self.rhs else: old_entry.entrytext += "\n%s" % self.rhs old_entry.aliases.add(aliases) self.msg("Entry updated:\n%s%s" % (old_entry.entrytext, aliastxt)) return if 'delete' in switches or 'del' in switches: # delete the help entry if not old_entry: self.msg("Could not find topic '%s'%s." % (topicstr, aliastxt)) return old_entry.delete() self.msg("Deleted help entry '%s'%s." % (topicstr, aliastxt)) return # at this point it means we want to add a new help entry. if not self.rhs: self.msg("You must supply a help text to add.") return if old_entry: if 'replace' in switches: # overwrite old entry old_entry.key = topicstr old_entry.entrytext = self.rhs old_entry.help_category = category old_entry.locks.clear() old_entry.locks.add(lockstring) old_entry.aliases.add(aliases) old_entry.save() self.msg("Overwrote the old topic '%s'%s." % (topicstr, aliastxt)) else: self.msg("Topic '%s'%s already exists. Use /replace to overwrite " "or /append or /merge to add text to it." % (topicstr, aliastxt)) else: # no old entry. Create a new one. new_entry = create.create_help_entry(topicstr, self.rhs, category=category, locks=lockstring,aliases=aliases) if new_entry: self.msg("Topic '%s'%s was successfully created." % (topicstr, aliastxt)) if 'edit' in switches: # open the line editor to edit the helptext self.caller.db._editing_help = new_entry EvEditor(self.caller, loadfunc=_loadhelp, savefunc=_savehelp, quitfunc=_quithelp, key="topic {}".format(new_entry.key), persistent=True) return else: self.msg("Error when creating topic '%s'%s! Contact an admin." % (topicstr, aliastxt))
def func(self): "Implement the function" switches = self.switches lhslist = self.lhslist if not self.args: self.msg( "Использование: @sethelp/[add|del|append|merge] <заголвок>[,категория[,ограничения,..] = <текст>" ) return topicstr = "" category = "General" lockstring = "view:all()" try: topicstr = lhslist[0] category = lhslist[1] lockstring = ",".join(lhslist[2:]) except Exception: pass if not topicstr: self.msg("Ты должен определить тему!") return # check if we have an old entry with the same name try: old_entry = HelpEntry.objects.get(db_key__iexact=topicstr) except Exception: old_entry = None if 'append' in switches or "merge" in switches: # merge/append operations if not old_entry: self.msg( "Не могу найти тему '%s'. Ты должен дать точное название." % topicstr) return if not self.rhs: self.msg("Ты должен указать текст append/merge.") return if 'merge' in switches: old_entry.entrytext += " " + self.rhs else: old_entry.entrytext += "\n%s" % self.rhs self.msg("Всутпление обновлено:\n%s" % old_entry.entrytext) return if 'delete' in switches or 'del' in switches: # delete the help entry if not old_entry: self.msg("Не могу найти тему '%s'" % topicstr) return old_entry.delete() self.msg("Удалено '%s'." % topicstr) return # at this point it means we want to add a new help entry. if not self.rhs: self.msg("Ты должен указать текст.") return if old_entry: if 'for' in switches or 'force' in switches: # overwrite old entry old_entry.key = topicstr old_entry.entrytext = self.rhs old_entry.help_category = category old_entry.locks.clear() old_entry.locks.add(lockstring) old_entry.save() self.msg("Переписана тема '%s' на новую." % topicstr) else: self.msg( "Топик '%s' уже существует. Используй /force для перезаписи или /append или /merge чтобы добавить текст." % topicstr) else: # no old entry. Create a new one. new_entry = create.create_help_entry(topicstr, self.rhs, category, lockstring) if new_entry: self.msg("Топик '%s' был успешно создан." % topicstr) else: self.msg( "Ошибка при создании темы '%s'! Свяжитесь с админом." % topicstr)
def func(self): "Implement the function" switches = self.switches lhslist = self.lhslist if not self.args: self.msg("Использование: @sethelp/[add|del|append|merge] <заголвок>[,категория[,ограничения,..] = <текст>") return topicstr = "" category = "General" lockstring = "view:all()" try: topicstr = lhslist[0] category = lhslist[1] lockstring = ",".join(lhslist[2:]) except Exception: pass if not topicstr: self.msg("Ты должен определить тему!") return # check if we have an old entry with the same name try: old_entry = HelpEntry.objects.get(db_key__iexact=topicstr) except Exception: old_entry = None if 'append' in switches or "merge" in switches: # merge/append operations if not old_entry: self.msg("Не могу найти тему '%s'. Ты должен дать точное название." % topicstr) return if not self.rhs: self.msg("Ты должен указать текст append/merge.") return if 'merge' in switches: old_entry.entrytext += " " + self.rhs else: old_entry.entrytext += "\n%s" % self.rhs self.msg("Всутпление обновлено:\n%s" % old_entry.entrytext) return if 'delete' in switches or 'del' in switches: # delete the help entry if not old_entry: self.msg("Не могу найти тему '%s'" % topicstr) return old_entry.delete() self.msg("Удалено '%s'." % topicstr) return # at this point it means we want to add a new help entry. if not self.rhs: self.msg("Ты должен указать текст.") return if old_entry: if 'for' in switches or 'force' in switches: # overwrite old entry old_entry.key = topicstr old_entry.entrytext = self.rhs old_entry.help_category = category old_entry.locks.clear() old_entry.locks.add(lockstring) old_entry.save() self.msg("Переписана тема '%s' на новую." % topicstr) else: self.msg("Топик '%s' уже существует. Используй /force для перезаписи или /append или /merge чтобы добавить текст." % topicstr) else: # no old entry. Create a new one. new_entry = create.create_help_entry(topicstr, self.rhs, category, lockstring) if new_entry: self.msg("Топик '%s' был успешно создан." % topicstr) else: self.msg("Ошибка при создании темы '%s'! Свяжитесь с админом." % topicstr)
def func(self): "Implement the function" switches = self.switches lhslist = self.lhslist if not self.args: self.msg("Usage: @sethelp/[add|del|append|merge] <topic>[,category[,locks,..] = <text>") return topicstr = "" category = "General" lockstring = "view:all()" try: topicstr = lhslist[0] category = lhslist[1] lockstring = ",".join(lhslist[2:]) except Exception: pass if not topicstr: self.msg("You have to define a topic!") return # check if we have an old entry with the same name try: old_entry = HelpEntry.objects.get(db_key__iexact=topicstr) except Exception: old_entry = None if 'append' in switches or "merge" in switches: # merge/append operations if not old_entry: self.msg("Could not find topic '%s'. You must give an exact name." % topicstr) return if not self.rhs: self.msg("You must supply text to append/merge.") return if 'merge' in switches: old_entry.entrytext += " " + self.rhs else: old_entry.entrytext += "\n%s" % self.rhs self.msg("Entry updated:\n%s" % old_entry.entrytext) return if 'delete' in switches or 'del' in switches: # delete the help entry if not old_entry: self.msg("Could not find topic '%s'" % topicstr) return old_entry.delete() self.msg("Deleted help entry '%s'." % topicstr) return # at this point it means we want to add a new help entry. if not self.rhs: self.msg("You must supply a help text to add.") return if old_entry: if 'for' in switches or 'force' in switches: # overwrite old entry old_entry.key = topicstr old_entry.entrytext = self.rhs old_entry.help_category = category old_entry.locks.clear() old_entry.locks.add(lockstring) old_entry.save() self.msg("Overwrote the old topic '%s' with a new one." % topicstr) else: self.msg("Topic '%s' already exists. Use /force to overwrite or /append or /merge to add text to it." % topicstr) else: # no old entry. Create a new one. new_entry = create.create_help_entry(topicstr, self.rhs, category, lockstring) if new_entry: self.msg("Topic '%s' was successfully created." % topicstr) else: self.msg("Error when creating topic '%s'! Contact an admin." % topicstr)