ListView category grouping fix

This commit is contained in:
davewiththenicehat 2021-06-05 10:06:33 -04:00
parent 6d7755976e
commit 2e351b7fab
2 changed files with 53 additions and 39 deletions

View file

@ -18,7 +18,7 @@
</ol> </ol>
<hr /> <hr />
<div class="row"> <div class="row">
{% regroup object_list by help_category as category_list %} {% regroup object_list by web_help_category as category_list %}
{% if category_list %} {% if category_list %}
<!-- left column --> <!-- left column -->
@ -36,10 +36,10 @@
<!-- index list --> <!-- index list -->
<div class="mx-3"> <div class="mx-3">
{% for help_category in category_list %} {% for web_help_category in category_list %}
<h5><a id="{{ help_category.grouper }}"></a>{{ help_category.grouper|title }}</h5> <h5><a id="{{ web_help_category.grouper }}"></a>{{ web_help_category.grouper|title }}</h5>
<ul> <ul>
{% for object in help_category.list %} {% for object in web_help_category.list %}
<li><a href="{{ object.web_get_detail_url }}">{{ object|title }}</a></li> <li><a href="{{ object.web_get_detail_url }}">{{ object|title }}</a></li>
{% endfor %} {% endfor %}
</ul> </ul>

View file

@ -2,30 +2,38 @@
Views to manipulate help entries. Views to manipulate help entries.
""" """
from dataclasses import dataclass
from django.utils.text import slugify from django.utils.text import slugify
from django.conf import settings from django.conf import settings
from evennia.utils.utils import inherits_from from evennia.utils.utils import inherits_from
from django.views.generic import ListView from django.views.generic import ListView, DetailView
from django.http import HttpResponseBadRequest from django.http import HttpResponseBadRequest
from django.db.models.functions import Lower
from evennia.help.models import HelpEntry from evennia.help.models import HelpEntry
from evennia.help.filehelp import FILE_HELP_ENTRIES from evennia.help.filehelp import FILE_HELP_ENTRIES
from .mixins import TypeclassMixin, EvenniaDetailView from .mixins import TypeclassMixin
from django.views.generic import DetailView
from evennia.utils.logger import log_info from evennia.utils.logger import log_info
DEFAULT_HELP_CATEGORY = settings.DEFAULT_HELP_CATEGORY DEFAULT_HELP_CATEGORY = settings.DEFAULT_HELP_CATEGORY
def get_help_category(help_entry):
if hasattr(help_entry, 'help_category'): def get_help_category(help_entry, slugify_cat=True):
return help_entry.help_category """Returns help category.
elif hasattr(help_entry, 'category'):
return help_entry.category Args:
elif hasattr(help_entry, 'db_help_category'): help_entry (HelpEntry, FileHelpEntry or Command): Help entry instance.
return help_entry.db_help_category slugify_cat (bool): If true the return string is slugified. Default is True.
else:
return 'unsorted' Notes:
If the entry does not have attribute 'web_help_entries'. One is created with
a slugified copy of the attribute help_category.
This attribute is used for sorting the entries for the help index (ListView) page.
Returns:
help_category (str): The category for the help entry.
"""
if not hasattr(help_entry, 'web_help_category'):
setattr(help_entry, 'web_help_category', slugify(help_entry.help_category))
return slugify(help_entry.help_category) if slugify_cat else help_entry.help_category
def get_help_topic(help_entry): def get_help_topic(help_entry):
topic = getattr(help_entry, 'key', False) topic = getattr(help_entry, 'key', False)
@ -34,6 +42,7 @@ def get_help_topic(help_entry):
# log_info(f'get_help_topic returning: {topic}') # log_info(f'get_help_topic returning: {topic}')
return topic return topic
def can_read_topic(cmd_or_topic, caller): def can_read_topic(cmd_or_topic, caller):
""" """
Helper method. If this return True, the given help topic Helper method. If this return True, the given help topic
@ -54,6 +63,7 @@ def can_read_topic(cmd_or_topic, caller):
else: else:
return cmd_or_topic.access(caller, 'read', default=True) return cmd_or_topic.access(caller, 'read', default=True)
def can_list_topic(cmd_or_topic, caller): def can_list_topic(cmd_or_topic, caller):
""" """
Should the specified command appear in the help table? Should the specified command appear in the help table?
@ -85,6 +95,7 @@ def can_list_topic(cmd_or_topic, caller):
# no explicit 'view' lock - use the 'read' lock # no explicit 'view' lock - use the 'read' lock
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(caller, mode='list'):
""" """
Collect help topics from all sources (cmd/db/file). Collect help topics from all sources (cmd/db/file).
@ -149,6 +160,7 @@ def collect_topics(caller, mode='list'):
return cmd_help_topics, db_help_topics, file_help_topics return cmd_help_topics, db_help_topics, file_help_topics
class HelpMixin(TypeclassMixin): class HelpMixin(TypeclassMixin):
""" """
This is a "mixin", a modifier of sorts. This is a "mixin", a modifier of sorts.
@ -180,11 +192,13 @@ class HelpMixin(TypeclassMixin):
# collect all help entries # collect all help entries
cmd_help_topics, db_help_topics, file_help_topics = \ cmd_help_topics, db_help_topics, file_help_topics = \
collect_topics(account.db._playable_characters[0], mode='query') collect_topics(account.db._playable_characters[0], mode='query')
# combine and sort all the help entries # merge the entries
file_db_help_topics = {**file_help_topics, **db_help_topics} file_db_help_topics = {**file_help_topics, **db_help_topics}
all_topics = {**file_db_help_topics, **cmd_help_topics} all_topics = {**file_db_help_topics, **cmd_help_topics}
all_entries = list(all_topics.values()) all_entries = list(all_topics.values())
all_entries.sort(key=get_help_category) # 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(f'{all_entries}')
log_info('get_queryset success') log_info('get_queryset success')
return all_entries return all_entries
@ -254,18 +268,18 @@ class HelpDetailView(HelpMixin, DetailView):
# Get queryset and filter out non-related categories # Get queryset and filter out non-related categories
full_set = self.get_queryset() full_set = self.get_queryset()
obj_topic = get_help_category(obj) obj_category = get_help_category(obj)
topic_set = [] category_set = []
for entry in full_set: for entry in full_set:
entry_topic = get_help_category(entry) entry_category = get_help_category(entry)
if entry_topic.lower() == obj_topic.lower(): if entry_category.lower() == obj_category.lower():
topic_set.append(entry) category_set.append(entry)
context["topic_list"] = topic_set context["topic_list"] = category_set
# log_info(f'topic_set: {topic_set}') # log_info(f'category_set: {category_set}')
# Find the index position of the given obj in the queryset # Find the index position of the given obj in the queryset
objs = list(topic_set) objs = list(category_set)
for i, x in enumerate(objs): for i, x in enumerate(objs):
if obj is x: if obj is x:
break break