account plus all puppets commands help entries

This commit is contained in:
davewiththenicehat 2021-06-05 19:24:27 -04:00
parent 4c0fcfbaf6
commit 5caaf149f4

View file

@ -99,11 +99,11 @@ def can_list_topic(cmd_or_topic, caller):
return cmd_or_topic.access(caller, 'read', default=True) return cmd_or_topic.access(caller, 'read', default=True)
def collect_topics(caller, mode='list'): def collect_topics(account, mode='list'):
""" """
Collect help topics from all sources (cmd/db/file). Collect help topics from all sources (cmd/db/file).
Args: Args:
caller (Object or Account): The user of the Command. account (Object or Account): The user of the Command.
mode (str): One of 'list' or 'query', where the first means we are collecting to view mode (str): One of 'list' or 'query', where the first means we are collecting to view
the help index and the second because of wanting to search for a specific help the help index and the second because of wanting to search for a specific help
entry/cmd to read. This determines which access should be checked. entry/cmd to read. This determines which access should be checked.
@ -113,17 +113,34 @@ def collect_topics(caller, mode='list'):
`({key: cmd,...}, {key: dbentry,...}, {key: fileentry,...}` `({key: cmd,...}, {key: dbentry,...}, {key: fileentry,...}`
""" """
# start with cmd-help # start with cmd-help
cmd_help_topics = []
# get Character's primary command set. if not str(account) == 'AnonymousUser':
cmdset = caller.cmdset.get()[0] # create list of account and account's puppets
puppets = account.db._playable_characters + [account]
# removing doublets in cmdset, caused by cmdhandler # add the account's and puppets' commands to cmd_help_topics list
# having to allow doublet commands to manage exits etc. for puppet in puppets:
cmdset.make_unique(caller) for cmdset in puppet.cmdset.get():
# removing doublets in cmdset, caused by cmdhandler
# retrieve all available commands and database / file-help topics. # having to allow doublet commands to manage exits etc.
# also check the 'cmd:' lock here cmdset.make_unique(puppet)
cmd_help_topics = [cmd for cmd in cmdset if cmd and cmd.access(caller, 'cmd')] # retrieve all available commands and database / file-help topics.
# also check the 'cmd:' lock here
for cmd in cmdset:
# skip the command if the puppet does not have access
if not cmd.access(puppet, 'cmd'):
continue
# skip the command if it's help entry already exists in the topic list
entry_exists = False
for verify_cmd in cmd_help_topics:
if verify_cmd.key and cmd.key and \
verify_cmd.help_category == cmd.help_category and \
verify_cmd.__doc__ == cmd.__doc__:
entry_exists = True
break
if entry_exists:
continue
# add this command to the list
cmd_help_topics.append(cmd)
# get all file-based help entries, checking perms # get all file-based help entries, checking perms
file_help_topics = { file_help_topics = {
topic.key.lower().strip(): topic topic.key.lower().strip(): topic
@ -139,27 +156,27 @@ def collect_topics(caller, mode='list'):
cmd_help_topics = { cmd_help_topics = {
cmd.auto_help_display_key cmd.auto_help_display_key
if hasattr(cmd, "auto_help_display_key") else cmd.key: cmd if hasattr(cmd, "auto_help_display_key") else cmd.key: cmd
for cmd in cmd_help_topics if can_list_topic(cmd, caller)} for cmd in cmd_help_topics if can_list_topic(cmd, account)}
db_help_topics = { db_help_topics = {
key: entry for key, entry in db_help_topics.items() key: entry for key, entry in db_help_topics.items()
if can_list_topic(entry, caller) if can_list_topic(entry, account)
} }
file_help_topics = { file_help_topics = {
key: entry for key, entry in file_help_topics.items() key: entry for key, entry in file_help_topics.items()
if can_list_topic(entry, caller)} if can_list_topic(entry, account)}
else: else:
# query # query
cmd_help_topics = { cmd_help_topics = {
cmd.auto_help_display_key cmd.auto_help_display_key
if hasattr(cmd, "auto_help_display_key") else cmd.key: cmd if hasattr(cmd, "auto_help_display_key") else cmd.key: cmd
for cmd in cmd_help_topics if can_read_topic(cmd, caller)} for cmd in cmd_help_topics if can_read_topic(cmd, account)}
db_help_topics = { db_help_topics = {
key: entry for key, entry in db_help_topics.items() key: entry for key, entry in db_help_topics.items()
if can_read_topic(entry, caller) if can_read_topic(entry, account)
} }
file_help_topics = { file_help_topics = {
key: entry for key, entry in file_help_topics.items() key: entry for key, entry in file_help_topics.items()
if can_read_topic(entry, caller)} if can_read_topic(entry, account)}
return cmd_help_topics, db_help_topics, file_help_topics return cmd_help_topics, db_help_topics, file_help_topics
@ -190,37 +207,18 @@ class HelpMixin(TypeclassMixin):
""" """
log_info('get_queryset') log_info('get_queryset')
account = self.request.user account = self.request.user
all_entries = [] # collect all help entries
if not str(account) == 'AnonymousUser': cmd_help_topics, db_help_topics, file_help_topics = collect_topics(account, mode='query')
# collect all help entries # merge the entries
cmd_help_topics, db_help_topics, file_help_topics = \ file_db_help_topics = {**file_help_topics, **db_help_topics}
collect_topics(account.db._playable_characters[0], mode='query') all_topics = {**file_db_help_topics, **cmd_help_topics}
# merge the entries all_entries = list(all_topics.values())
file_db_help_topics = {**file_help_topics, **db_help_topics} # sort the entries
all_topics = {**file_db_help_topics, **cmd_help_topics} all_entries = sorted(all_entries, key=get_help_topic) # sort alphabetically
all_entries = list(all_topics.values()) all_entries.sort(key=get_help_category) # group by categories
# sort the entries
all_entries = sorted(all_entries, key=get_help_topic) # sort alphabetically
all_entries.sort(key=get_help_category) # group by categories
# log_info(f'{all_entries}')
log_info('get_queryset success') log_info('get_queryset success')
return all_entries return all_entries
def get_entries(self):
account = self.request.user
all_entries = []
if not str(account) == 'AnonymousUser':
# collect all help entries
cmd_help_topics, db_help_topics, file_help_topics = \
collect_topics(account.db._playable_characters[0], mode='query')
# combine and sort all the help entries
file_db_help_topics = {**file_help_topics, **db_help_topics}
all_topics = {**file_db_help_topics, **cmd_help_topics}
all_entries = list(all_topics.values())
all_entries.sort(key=get_help_category)
# log_info(f'{all_entries}')
return all_entries
class HelpListView(HelpMixin, ListView): class HelpListView(HelpMixin, ListView):
""" """
@ -249,7 +247,7 @@ class HelpDetailView(HelpMixin, DetailView):
@property @property
def page_title(self): def page_title(self):
# Makes sure the page has a sensible title. # Makes sure the page has a sensible title.
#return "%s Detail" % self.typeclass._meta.verbose_name.title() # return "%s Detail" % self.typeclass._meta.verbose_name.title()
obj = self.get_object() obj = self.get_object()
topic = get_help_topic(obj) topic = get_help_topic(obj)
return f'{topic} detail' return f'{topic} detail'