Skip to content

Commit

Permalink
Allow sorting menus and submenus
Browse files Browse the repository at this point in the history
The order of menu elements is builtins, followed by
submenus, followed by html and direct link files.

For submenus, you can name them using numerical prefixes.
For example, if you have in /opt/quads/web:

   1_Tickets
   2_Docs
   Foo
   Bar

The submenus will be named:

   Tickets
   Docs
   Bar
   Foo

with the numbered elements listed first, sorted and numbers
stripped off, followed by the unnumbered elements also sorted
but being listed after the numbered submenus

Within each menu, the same logic is used to sort the html
and direct links.

Fixes: #525
Change-Id: I1d9c22969531ad44b68f796852c6d313a4ab6c4f
  • Loading branch information
kambiz-aghaiepour committed Sep 25, 2024
1 parent c12131a commit 28613db
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 8 deletions.
25 changes: 21 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ QUADS automates the future scheduling, end-to-end provisioning and delivery of b
* [Using SSL with Flask API and QUADS](#using-ssl-with-flask-api-and-quads)
* [QUADS Wiki](#quads-wiki)
* [Dynamic Wiki Content](#dynamic-wiki-content)
* [Ordering Dynamic Wiki Content](#ordering-elements-in-the-dynamic-wiki-content)
* [Installing other QUADS Components](#installing-other-quads-components)
* [QUADS Move Command](#quads-move-command)
* [Making QUADS Run](#making-quads-run)
Expand Down Expand Up @@ -234,18 +235,18 @@ systemctl restart nginx
- Any files without extensions will be considered direct links with the content of it being only the hyperlink in plain text.
- The html files should be structured for the correct jinja templating that is expected like this:

```
```
{% extends "base.html" %}
{% block title %} INSERT TITLE HERE {% endblock %}
{% block page_content %}
INSERT HTML CONTENT HERE
{% endblock %}
```
```

- For static files such as images and css, all files go on the root `/static` directory and the src href has to be passed via `url_for` like this:

```
```
<img
loading="lazy"
decoding="async"
Expand All @@ -255,7 +256,23 @@ systemctl restart nginx
width="1030"
height="542"
/>
```
```

##### Ordering Elements in the Dynamic Wiki Content
- The type of content in the `/opt/quads/web` directory are either files or directories.
- Flat files are either html files which follow the jinja templating, or direct links files which contain a link to an external resource.
- Directories are translated into sub-menus and in turn can contain flat files as described above.
- In order to control the ordering of various elements, they can be named with numeric prefixes.
- Custom flat file elements are listed first, followed by sub-menu (Directory) elements.
- Example of unnumbered content is as follows. In this example, you will have a menu that contains `Chat`, `Contact`, `FAQ`, and `Usage`, followed by the Submenus `Docs`, `Resources` and `Tickets`

![Unordered Content](/image/Menus-Unnumbered.png)

- If you wish to order them, rename your files and directories and add integer prefixes. For example, the following will list the numbered elements followed by the un-numbered elements and would yield: `FAQ`, `Usage`, `Chat`, `Contact`, followed by the Submenus `Tickets`, `Resources` and `Docs` in that order.

![Ordered Content](/image/Menus-Numbered.png)



### Installing other QUADS Components

Expand Down
Binary file added image/Menus-Numbered.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/Menus-Unnumbered.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
76 changes: 72 additions & 4 deletions src/quads/web/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,64 @@ def get_dynamic_navigation():
link["href"] = f.readline().strip()
link["text"] = file.replace("_", " ")
links.append(link)

numbered_links = []
unnumbered_links = []
for link in links:
ln = link["text"]
try:
lnum = int(ln.split()[0])
numbered_links.append(link)
except:
unnumbered_links.append(link)

sorted_numbered_links = sorted(numbered_links, key=lambda x: x["text"])
sorted_unnumbered_links = sorted(unnumbered_links, key=lambda x: x["text"])
stripped_numbered_links = []

for link in sorted_numbered_links:
link["text"] = " ".join(link["text"].split()[1:])
stripped_numbered_links.append(link)

links = stripped_numbered_links + sorted_unnumbered_links

dynamic_navigation["links"] = links

submenus = [d.name for d in os.scandir(WEB_CONTENT_PATH) if d.is_dir() and d.name not in EXCLUDE_DIRS]
numbered_submenus = []
unnumbered_submenus = []
for sm in submenus:
try:
sm_num = int(sm.split("_")[0])
numbered_submenus.append(sm)
except:
unnumbered_submenus.append(sm)

sorted_numbered_submenus = sorted(numbered_submenus, key=lambda x: x)
sorted_unnumbered_submenus = sorted(unnumbered_submenus, key=lambda x: x)
stripped_numbered_submenus = []
stripped_unnumbered_submenus = []

for sm in sorted_numbered_submenus:
sm_dir = sm
sm = "_".join(sm.split("_")[1:])
stripped_numbered_submenus.append({"dir": sm_dir, "name": sm})

for sm in sorted_unnumbered_submenus:
sm_dir = sm
stripped_unnumbered_submenus.append({"dir": sm_dir, "name": sm})

submenus = stripped_numbered_submenus + stripped_unnumbered_submenus

for sub in submenus:
sub_links = []
sub_path = os.path.join(WEB_CONTENT_PATH, sub)
sub_path = os.path.join(WEB_CONTENT_PATH, sub["dir"])
sub_files = [d.name for d in os.scandir(sub_path) if not d.is_dir() and d.name not in EXCLUDE_DIRS]
html_links = [file for file in sub_files if file.endswith(".html")]
for hl in html_links:
href = url_for(
"content.dynamic_content_sub",
directory=sub,
directory=sub["dir"],
page=hl.replace(".html", ""),
)
link = {"href": href, "text": hl.replace(".html", "").replace("_", " ")}
Expand All @@ -72,12 +118,33 @@ def get_dynamic_navigation():
direct_links = [file for file in sub_files if not file.endswith(".html") and not file in EXCLUDE_DIRS]
for dl in direct_links:
link = {}
with open(os.path.join(WEB_CONTENT_PATH, sub, dl)) as f:
with open(os.path.join(WEB_CONTENT_PATH, sub["dir"], dl)) as f:
link["href"] = f.readline().strip()
link["text"] = dl.replace("_", " ")
sub_links.append(link)

menus[sub] = sub_links
numbered_sub_links = []
unnumbered_sub_links = []
stripped_numbered_sub_links = []

for sl in sub_links:
sl_name = sl["text"]
try:
sl_num = int(sl_name.split()[0])
numbered_sub_links.append(sl)
except:
unnumbered_sub_links.append(sl)

sorted_numbered_sub_links = sorted(numbered_sub_links, key=lambda x: x["text"])
sorted_unnumbered_sub_links = sorted(unnumbered_sub_links, key=lambda x: x["text"])
stripped_numbered_sub_links = []

for sl in sorted_numbered_sub_links:
sl["text"] = " ".join(sl["text"].split()[1:])
stripped_numbered_sub_links.append(sl)

sub_links = stripped_numbered_sub_links + sorted_unnumbered_sub_links
menus[sub["name"]] = sub_links
dynamic_navigation["menus"] = menus

return dynamic_navigation
Expand Down Expand Up @@ -240,6 +307,7 @@ def create_vlans():
def visuals(when):
path = os.path.join(WEB_CONTENT_PATH, "visual")
file_paths = get_file_paths(path)
print(file_paths)
for file in file_paths:
if when in file:
return render_template(file)
Expand Down

0 comments on commit 28613db

Please sign in to comment.