Pārlūkot izejas kodu

Moved page list creation to new class page_list. Usage of this class for search results

master
Dirk Alders 2 mēnešus atpakaļ
vecāks
revīzija
dd21345eb7
5 mainītis faili ar 72 papildinājumiem un 56 dzēšanām
  1. 1
    1
      fstools
  2. 22
    22
      pages/context.py
  3. 43
    25
      pages/page.py
  4. 4
    6
      pages/views.py
  5. 2
    2
      piki/settings.py

+ 1
- 1
fstools

@@ -1 +1 @@
1
-Subproject commit c10e8792abb05671dab6de51cdadda3bf8ead50f
1
+Subproject commit 9237f6f7f77eed79b8163077c1b4d87189193fbe

+ 22
- 22
pages/context.py Parādīt failu

@@ -54,6 +54,28 @@ def navigationbar(context, request, caller_name, **kwargs):
54 54
     finalise_bar(request, bar)
55 55
 
56 56
 
57
+def menubar(context, request, caller_name, **kwargs):
58
+    bar = context[context.MENUBAR]
59
+    menubar_users(bar, request)
60
+    add_help_menu(request, bar, "current_help_page" in kwargs)
61
+    add_index_menu(request, bar, kwargs.get("rel_path", ''))
62
+    finalise_bar(request, bar)
63
+
64
+
65
+def actionbar(context, request, caller_name, **kwargs):
66
+    bar = context[context.ACTIONBAR]
67
+    if caller_name == 'page':
68
+        if access.write_page(request, kwargs["rel_path"]):
69
+            add_edit_menu(request, bar, kwargs["rel_path"])
70
+        if access.modify_attachment(request, kwargs["rel_path"]):
71
+            add_manageupload_menu(request, bar, kwargs['upload_path'])
72
+        if access.read_page(request, kwargs["rel_path"]):
73
+            add_meta_menu(request, bar, kwargs["rel_path"])
74
+    elif caller_name == 'helpview':
75
+        actionbar_add_help(context, request, **kwargs)
76
+    finalise_bar(request, bar)
77
+
78
+
57 79
 def add_back_menu(request, bar):
58 80
     bar.append_entry(
59 81
         BACK_UID,                                   # uid
@@ -76,14 +98,6 @@ def navigation_entry_parameters(request, path):
76 98
     )
77 99
 
78 100
 
79
-def menubar(context, request, caller_name, **kwargs):
80
-    bar = context[context.MENUBAR]
81
-    menubar_users(bar, request)
82
-    add_help_menu(request, bar, "current_help_page" in kwargs)
83
-    add_index_menu(request, bar, kwargs.get("rel_path", ''))
84
-    finalise_bar(request, bar)
85
-
86
-
87 101
 def add_help_menu(request, bar, active):
88 102
     bar.append_entry(
89 103
         HELP_UID,                                   # uid
@@ -106,20 +120,6 @@ def add_index_menu(request, bar, rel_path):
106 120
     )
107 121
 
108 122
 
109
-def actionbar(context, request, caller_name, **kwargs):
110
-    bar = context[context.ACTIONBAR]
111
-    if caller_name == 'page':
112
-        if access.write_page(request, kwargs["rel_path"]):
113
-            add_edit_menu(request, bar, kwargs["rel_path"])
114
-        if access.modify_attachment(request, kwargs["rel_path"]):
115
-            add_manageupload_menu(request, bar, kwargs['upload_path'])
116
-        if access.read_page(request, kwargs["rel_path"]):
117
-            add_meta_menu(request, bar, kwargs["rel_path"])
118
-    elif caller_name == 'helpview':
119
-        actionbar_add_help(context, request, **kwargs)
120
-    finalise_bar(request, bar)
121
-
122
-
123 123
 def add_edit_menu(request, bar, rel_path):
124 124
     bar.append_entry(
125 125
         EDIT_UID,                                   # uid

+ 43
- 25
pages/page.py Parādīt failu

@@ -197,39 +197,57 @@ class creole_page(base_page):
197 197
                 pass
198 198
 
199 199
         params = kwargs.get('', '')
200
-        startname = ''
200
+        filter_str = ''
201 201
         depth = parse_depth(params)
202 202
         if depth is None:
203 203
             params = params.split(",")
204 204
             depth = parse_depth(params[0])
205 205
             if len(params) == 2:
206
-                startname = params[1]
206
+                filter_str = params[1]
207 207
             elif depth is None:
208
-                startname = params[0]
209
-            if depth is None:
210
-                depth = 9999
208
+                filter_str = params[0]
211 209
         #
212 210
         rv = ""
213
-        # create a rel_path list
214
-        pathlist = [base_page(path).rel_path for path in fstools.dirlist(settings.PAGES_ROOT, rekursive=False)]
215
-        # sort basename
216
-        pathlist.sort(key=os.path.basename)
211
+        # create a page_list
212
+        if allpages:
213
+            expression = "*"
214
+            parent_rel_path = ""
215
+        else:
216
+            expression = os.path.basename(self._path) + 2 * self.SPLITCHAR + "*"
217
+            parent_rel_path = self.rel_path
218
+        pl = page_list(
219
+            self._request,
220
+            [creole_page(self._request, path) for path in fstools.dirlist(settings.PAGES_ROOT, expression=expression, rekursive=False)]
221
+        )
222
+        return pl.html_list(depth=depth, filter_str=filter_str, parent_rel_path=parent_rel_path)
223
+
224
+
225
+class page_list(list):
226
+    def __init__(self, request, *args, **kwargs):
227
+        self._request = request
228
+        return super().__init__(*args, **kwargs)
229
+
230
+    def sort_basename(self):
231
+        return list.sort(self, key=lambda x: os.path.basename(x.rel_path))
217 232
 
233
+    def creole_list(self, depth=None, filter_str='', parent_rel_path=''):
234
+        self.sort_basename()
235
+        depth = depth or 9999   # set a random high value if None
236
+        #
237
+        parent_rel_path = parent_rel_path + "/" if len(parent_rel_path) > 0 else ""
238
+        #
239
+        rv = ""
218 240
         last_char = None
219
-        for contentname in pathlist:
220
-            #
221
-            if (contentname.startswith(self.rel_path) or allpages) and contentname != self.rel_path:
222
-                if allpages:
223
-                    name = contentname
224
-                else:
225
-                    name = contentname[len(self.rel_path)+1:]
226
-                if name.count('/') < depth and name.startswith(startname):
227
-                    if last_char != os.path.basename(name)[0].upper():
228
-                        last_char = os.path.basename(name)[0].upper()
229
-                        if last_char is not None:
230
-                            rv += "</ul>\n"
231
-                        rv += f'<h3>{last_char}</h3>\n<ul>\n'
232
-                    rv += f'  <li><a href="{url_page(self._request, contentname)}">{name}</a></li>\n'
233
-        if len(rv) > 0:
234
-            rv += "</ul>\n"
241
+        for page in self:
242
+            name = page.rel_path[len(parent_rel_path):]
243
+            if name.startswith(filter_str) and name != filter_str:
244
+                if name.count('/') < depth:
245
+                    first_char = os.path.basename(name)[0].upper()
246
+                    if last_char != first_char:
247
+                        last_char = first_char
248
+                        rv += f"=== {first_char}\n"
249
+                    rv += f"* [[{url_page(self._request, page.rel_path)} | {name} ]]\n"
235 250
         return rv
251
+
252
+    def html_list(self, depth=9999, filter_str='', parent_rel_path=''):
253
+        return mycreole.render_simple(self.creole_list(depth, filter_str, parent_rel_path))

+ 4
- 6
pages/views.py Parādīt failu

@@ -15,7 +15,7 @@ from .context import context_adaption
15 15
 from .forms import EditForm
16 16
 from .help import help_pages
17 17
 import mycreole
18
-from .page import creole_page
18
+from .page import creole_page, page_list
19 19
 from .search import whoosh_search
20 20
 from themes import Context
21 21
 
@@ -110,15 +110,13 @@ def search(request):
110 110
     if sr is None:
111 111
         django_messages.error(request, _('Invalid search pattern: %s') % repr(search_txt))
112 112
         sr = []
113
-    page_content = "= Searchresults\n"
114
-    for rel_path in sr:
115
-        p = creole_page(request, rel_path)
116
-        page_content += f"[[/page/{rel_path}|{p.title}]]\n"
113
+    pl = page_list(request, [creole_page(request, rel_path) for rel_path in sr])
117 114
     #
118 115
     context_adaption(
119 116
         context,
120 117
         request,
121
-        page_content=mycreole.render_simple(page_content)
118
+        title=_("Searchresults"),
119
+        page_content=mycreole.render_simple(pl.creole_list())
122 120
     )
123 121
     return render(request, 'pages/page.html', context=context)
124 122
 

+ 2
- 2
piki/settings.py Parādīt failu

@@ -176,14 +176,14 @@ for property_name in USER_CONFIG_DEFAULTS:
176 176
 if SECRET_KEY is None:
177 177
     chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)'
178 178
     s_key = ''.join([random.choice(chars) for n in range(50)])
179
-    secret_key_warning = "You need to create a config.py file including at least a SECRET_KEY definition (e.g.: --> %s <--).    " % repr(s_key)
179
+    secret_key_warning = "You need to create a config.py file including at least a SECRET_KEY definition (e.g.: --> %s <--)." % repr(s_key)
180 180
     raise KeyError(secret_key_warning)
181 181
 
182 182
 
183 183
 # Logging Configuration
184 184
 #
185 185
 ROOT_LOGGER_NAME = os.path.basename(os.path.dirname(__file__))
186
-default_handler = ['socket'] if DEBUG else ['console']
186
+default_handler = ['socket', 'console'] if DEBUG else ['console']
187 187
 
188 188
 
189 189
 class DjangoSocketHandler(_SocketHandler):

Notiek ielāde…
Atcelt
Saglabāt