Преглед изворни кода

Metadata added to search index and search help page added

master
Dirk Alders пре 2 месеци
родитељ
комит
b7c6374707
5 измењених фајлова са 70 додато и 18 уклоњено
  1. 11
    0
      pages/__init__.py
  2. 41
    2
      pages/help.py
  3. 3
    11
      pages/page.py
  4. 13
    4
      pages/search.py
  5. 2
    1
      pages/views.py

+ 11
- 0
pages/__init__.py Прегледај датотеку

@@ -1,4 +1,6 @@
1
+from datetime import datetime
1 2
 from django.urls.base import reverse
3
+import zoneinfo
2 4
 
3 5
 
4 6
 def url_page(request, rel_path, **kwargs):
@@ -18,3 +20,12 @@ def url_edit(request, rel_path):
18 20
 
19 21
 def get_search_query(request):
20 22
     return request.GET.get('q')
23
+
24
+
25
+def timestamp_to_datetime(request, tm):
26
+    from users.models import get_userprofile
27
+    #
28
+    up = get_userprofile(request.user)
29
+    tz = zoneinfo.ZoneInfo(up.timezone)
30
+    #
31
+    return datetime.fromtimestamp(tm, tz)

+ 41
- 2
pages/help.py Прегледај датотеку

@@ -34,8 +34,47 @@ ACCESS = mycreole.render_simple(_("""
34 34
 = TBD
35 35
 """))
36 36
 
37
-SEARCH = mycreole.render_simple(_("""
38
-= TBD
37
+SEARCH = mycreole.render_simple(_(
38
+    """
39
+= Search
40
+The search looks up full words in //title (page basename)// and //page_src (the creole source)// without giving \
41
+special search commands in the search string.
42
+
43
+=== Search fields
44
+* title (TEXT)
45
+* page_src (TEXT)
46
+* creation_time (DATETIME)
47
+* modified_time (DATETIME)
48
+* modified_user (TEXT)
49
+
50
+== Search syntax (Whoosh)
51
+=== Logic operators
52
+* AND
53
+** **Example:** "foo AND bar" - Search will find all items with foo and bar.
54
+* OR
55
+** **Example:** "foo OR bar" - Search will find all items with foo, bar or with foo and bar.
56
+* NOT
57
+** **Example:** "foo NOT bar" - Search will find all items with foo and no bar.
58
+=== Search in specific fields
59
+A search pattern like //foo:bar// does look for //bar// in the field named //foo//.
60
+
61
+This search pattern can also be combined with other search text via logical operators.
62
+=== Search for specific content
63
+* **Wildcards:**
64
+* **Range:**
65
+** From To:
66
+** Above:
67
+** Below:
68
+* **Named constants:**
69
+** //now//: Current date
70
+** //-[num]y//: Current date minus [num] years
71
+** //+[num]mo//: Current date plus [num] months
72
+** //-[num]d//: Current date minus [num] days
73
+** ...
74
+
75
+== Examples
76
+* [[/search/?q=modified_user:system-page|modified_user:system-page]] results in a list of all system pages.
77
+* [[/search/?q=modified_time%3A%5B-5d+to+now%5D| modified_time:[-5d to now] ]] results in a list of all pages which have been modified within the last 5 days.
39 78
 """))
40 79
 
41 80
 help_pages = {

+ 3
- 11
pages/page.py Прегледај датотеку

@@ -1,4 +1,3 @@
1
-from datetime import datetime
2 1
 from django.conf import settings
3 2
 from django.utils.translation import gettext as _
4 3
 import fstools
@@ -9,8 +8,7 @@ import mycreole
9 8
 import os
10 9
 import shutil
11 10
 import time
12
-from users.models import get_userprofile
13
-import zoneinfo
11
+from . import timestamp_to_datetime
14 12
 
15 13
 logger = logging.getLogger(settings.ROOT_LOGGER_NAME).getChild(__name__)
16 14
 
@@ -167,14 +165,8 @@ class creole_page(base_page):
167 165
             return ""
168 166
 
169 167
     def render_meta(self):
170
-        def str_date(tm):
171
-            up = get_userprofile(self._request.user)
172
-            tz = zoneinfo.ZoneInfo(up.timezone)
173
-            #
174
-            return datetime.fromtimestamp(tm, tz).strftime('%Y-%m-%d %H:%M')
175
-        #
176
-        ctime = str_date(self._meta_data.get(self._meta_data.KEY_CREATION_TIME))
177
-        mtime = str_date(self._meta_data.get(self._meta_data.KEY_MODIFIED_TIME))
168
+        ctime = timestamp_to_datetime(self._meta_data.get(self._meta_data.KEY_CREATION_TIME)).strftime('%Y-%m-%d %H:%M')
169
+        mtime = timestamp_to_datetime(self._meta_data.get(self._meta_data.KEY_MODIFIED_TIME)).strftime('%Y-%m-%d %H:%M')
178 170
         user = self._meta_data.get(self._meta_data.KEY_MODIFIED_USER)
179 171
         #
180 172
         meta = f'|{_("Created")}:|{ctime}|\n'

+ 13
- 4
pages/search.py Прегледај датотеку

@@ -1,9 +1,10 @@
1
+from datetime import datetime
1 2
 from django.conf import settings
2 3
 
3 4
 import fstools
4 5
 import logging
5 6
 import os
6
-from whoosh.fields import Schema, ID, TEXT
7
+from whoosh.fields import Schema, ID, TEXT, DATETIME
7 8
 from whoosh.qparser.dateparse import DateParserPlugin
8 9
 from whoosh import index, qparser
9 10
 
@@ -16,7 +17,11 @@ SCHEMA = Schema(
16 17
     id=ID(unique=True, stored=True),
17 18
     # Page
18 19
     title=TEXT,
19
-    page_src=TEXT
20
+    page_src=TEXT,
21
+    # metadata
22
+    creation_time=DATETIME,
23
+    modified_time=DATETIME,
24
+    modified_user=TEXT
20 25
 )
21 26
 
22 27
 
@@ -55,8 +60,13 @@ def add_item(ix, bp: base_page):
55 60
     #
56 61
     data = dict(
57 62
         id=bp.rel_path,
63
+        #
58 64
         title=bp.title,
59
-        page_src=bp.raw_page_src
65
+        page_src=bp.raw_page_src,
66
+        #
67
+        creation_time=datetime.fromtimestamp(bp._meta_data.get(bp._meta_data.KEY_CREATION_TIME)),
68
+        modified_time=datetime.fromtimestamp(bp._meta_data.get(bp._meta_data.KEY_MODIFIED_TIME)),
69
+        modified_user=bp._meta_data.get(bp._meta_data.KEY_MODIFIED_USER)
60 70
     )
61 71
     with ix.writer() as w:
62 72
         logger.info('Adding document with id=%s to the search index.', data.get('id'))
@@ -93,4 +103,3 @@ def update_item(bp: base_page):
93 103
     ix = load_index()
94 104
     delete_item(ix, bp)
95 105
     add_item(ix, bp)
96
-

+ 2
- 1
pages/views.py Прегледај датотеку

@@ -1,4 +1,5 @@
1 1
 from django.conf import settings
2
+from django.contrib import messages as django_messages
2 3
 from django.shortcuts import render
3 4
 from django.http import HttpResponse, HttpResponseRedirect
4 5
 from django.utils.translation import gettext as _
@@ -107,7 +108,7 @@ def search(request):
107 108
 
108 109
     sr = whoosh_search(search_txt)
109 110
     if sr is None:
110
-        messages.error(request, _('Invalid search pattern: %s') % repr(search_txt))
111
+        django_messages.error(request, _('Invalid search pattern: %s') % repr(search_txt))
111 112
         sr = []
112 113
     page_content = "= Searchresults\n"
113 114
     for rel_path in sr:

Loading…
Откажи
Сачувај