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,12 +18,12 @@
</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 -->
<div class="col-lg-9 col-sm-12"> <div class="col-lg-9 col-sm-12">
<!-- intro --> <!-- intro -->
<div class="card border-light"> <div class="card border-light">
<div class="card-body"> <div class="card-body">
@ -33,23 +33,23 @@
</div> </div>
<hr /> <hr />
<!-- end intro --> <!-- end intro -->
<!-- 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>
{% endfor %} {% endfor %}
<!-- end index list --> <!-- end index list -->
</div> </div>
</div> </div>
<!-- end left column --> <!-- end left column -->
<!-- right column (index) --> <!-- right column (index) -->
<div class="col-lg-3 col-sm-12"> <div class="col-lg-3 col-sm-12">
{% if user.is_staff %} {% if user.is_staff %}
@ -58,16 +58,16 @@
<!-- end admin button --> <!-- end admin button -->
<hr /> <hr />
{% endif %} {% endif %}
<div class="card mb-3"> <div class="card mb-3">
<div class="card-header">Category Index</div> <div class="card-header">Category Index</div>
<ul class="list-group list-group-flush"> <ul class="list-group list-group-flush">
{% for category in category_list %} {% for category in category_list %}
<a href="#{{ category.grouper }}" class="list-group-item">{{ category.grouper|title }}</a> <a href="#{{ category.grouper }}" class="list-group-item">{{ category.grouper|title }}</a>
{% endfor %} {% endfor %}
</ul> </ul>
</div> </div>
</div> </div>
<!-- end right column --> <!-- end right column -->
@ -81,14 +81,14 @@
<p>You're missing out on an opportunity to attract visitors (and potentially new players) to {{ game_name }}!</p> <p>You're missing out on an opportunity to attract visitors (and potentially new players) to {{ game_name }}!</p>
<p>Use Evennia's <a href="https://github.com/evennia/evennia/wiki/Help-System#database-help-entries" class="alert-link" target="_blank">Help System</a> to tell the world about the universe you've created, its lore and legends, its people and creatures, and their customs and conflicts!</p> <p>Use Evennia's <a href="https://github.com/evennia/evennia/wiki/Help-System#database-help-entries" class="alert-link" target="_blank">Help System</a> to tell the world about the universe you've created, its lore and legends, its people and creatures, and their customs and conflicts!</p>
<p>You don't even need coding skills-- writing Help Entries is no more complicated than writing an email or blog post. Once you publish your first entry, these ugly boxes go away and this page will turn into an index of everything you've written about {{ game_name }}.</p> <p>You don't even need coding skills-- writing Help Entries is no more complicated than writing an email or blog post. Once you publish your first entry, these ugly boxes go away and this page will turn into an index of everything you've written about {{ game_name }}.</p>
<p>The documentation you write is eventually picked up by search engines, so the more you write about how {{ game_name }} works, the larger your web presence will be-- and the more traffic you'll attract. <p>The documentation you write is eventually picked up by search engines, so the more you write about how {{ game_name }} works, the larger your web presence will be-- and the more traffic you'll attract.
<p>Everything you write can be viewed either on this site or within the game itself, using the in-game help commands.</p> <p>Everything you write can be viewed either on this site or within the game itself, using the in-game help commands.</p>
<hr> <hr>
<p class="mb-0"><a href="/admin/help/helpentry/add/" class="alert-link">Click here</a> to start writing about {{ game_name }}!</p> <p class="mb-0"><a href="/admin/help/helpentry/add/" class="alert-link">Click here</a> to start writing about {{ game_name }}!</p>
</div> </div>
</div> </div>
{% endif %} {% endif %}
<div class="col-lg-12 col-sm-12"> <div class="col-lg-12 col-sm-12">
<div class="alert alert-secondary" role="alert"> <div class="alert alert-secondary" role="alert">
<h4 class="alert-heading">Under Construction!</h4> <h4 class="alert-heading">Under Construction!</h4>
@ -99,7 +99,7 @@
</div> </div>
</div> </div>
{% endif %} {% endif %}
</div> </div>
<hr /> <hr />
</div> </div>

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