-
-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathchapters.py
109 lines (93 loc) · 3.33 KB
/
chapters.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
"""Handler for OWASP Chapters Slack functionality."""
from __future__ import annotations
from django.conf import settings
from django.utils.text import Truncator
from apps.common.constants import NL
from apps.common.utils import get_absolute_url
from apps.slack.blocks import get_pagination_buttons, markdown
from apps.slack.common.constants import TRUNCATION_INDICATOR
from apps.slack.common.presentation import EntityPresentation
from apps.slack.constants import FEEDBACK_CHANNEL_MESSAGE
from apps.slack.utils import escape
def get_blocks(
page=1, search_query: str = "", limit: int = 10, presentation: EntityPresentation | None = None
):
"""Get chapters blocks."""
from apps.owasp.api.search.chapter import get_chapters
from apps.owasp.models.chapter import Chapter
presentation = presentation or EntityPresentation()
search_query_escaped = escape(search_query)
attributes = [
"idx_country",
"idx_leaders",
"idx_name",
"idx_region",
"idx_suggested_location",
"idx_summary",
"idx_updated_at",
"idx_url",
]
offset = (page - 1) * limit
chapters_data = get_chapters(search_query, attributes=attributes, limit=limit, page=page)
chapters = chapters_data["hits"]
total_pages = chapters_data["nbPages"]
if not chapters:
return [
markdown(
f"*No chapters found for `{search_query_escaped}`*{NL}"
if search_query
else "*No chapters found*{NL}"
)
]
blocks = [
markdown(
f"{NL}*OWASP chapters that I found for* `{search_query_escaped}`:{NL}"
if search_query_escaped
else f"{NL}*OWASP chapters:*{NL}"
),
]
for idx, chapter in enumerate(chapters):
location = chapter["idx_suggested_location"] or chapter["idx_country"]
leaders = chapter.get("idx_leaders", [])
leaders_text = (
f"_Leader{'' if len(leaders) == 1 else 's'}: {', '.join(leaders)}_{NL}"
if leaders and presentation.include_metadata
else ""
)
name = Truncator(escape(chapter["idx_name"])).chars(
presentation.name_truncation, truncate=TRUNCATION_INDICATOR
)
summary = Truncator(chapter["idx_summary"]).chars(
presentation.summary_truncation, truncate=TRUNCATION_INDICATOR
)
blocks.append(
markdown(
f"{offset + idx + 1}. <{chapter['idx_url']}|*{name}*>{NL}"
f"_{location}_{NL}"
f"{leaders_text}"
f"{escape(summary)}{NL}"
)
)
if presentation.include_feedback:
blocks.append(
markdown(
f"⚠️ *Extended search over {Chapter.active_chapters_count()} OWASP chapters "
f"is available at <{get_absolute_url('chapters')}"
f"?q={search_query}|{settings.SITE_NAME}>*{NL}"
f"{FEEDBACK_CHANNEL_MESSAGE}"
)
)
if presentation.include_pagination and (
pagination_block := get_pagination_buttons(
"chapters",
page,
total_pages - 1,
)
):
blocks.append(
{
"type": "actions",
"elements": pagination_block,
}
)
return blocks