Parcourir la source

Page tagging implemented

master
Dirk Alders il y a 3 mois
Parent
révision
d81c2df678
4 fichiers modifiés avec 35 ajouts et 17 suppressions
  1. 4
    2
      pages/forms.py
  2. 25
    12
      pages/page.py
  3. 3
    1
      pages/search.py
  4. 3
    2
      pages/views.py

+ 4
- 2
pages/forms.py Voir le fichier

@@ -6,9 +6,11 @@ from django.forms.utils import ErrorList
6 6
 
7 7
 class EditForm(forms.Form):  # Note that it is not inheriting from forms.ModelForm
8 8
     page_txt = forms.CharField(max_length=20000, label="Page source text", widget=forms.Textarea(attrs={"rows": "20"}))
9
+    page_tags = forms.CharField(max_length=20000, label="Tags (words separated by spaces)")
9 10
 
10 11
     def __init__(self, *args, **kwargs) -> None:
11 12
         page_data = kwargs.pop("page_data")
13
+        page_tags = kwargs.pop("page_tags")
12 14
         super().__init__(*args, **kwargs)
13
-        page_txt = self.fields['page_txt']
14
-        page_txt.initial = page_data
15
+        self.fields['page_txt'].initial = page_data
16
+        self.fields['page_tags'].initial = page_tags

+ 25
- 12
pages/page.py Voir le fichier

@@ -17,6 +17,7 @@ class meta_data(dict):
17 17
     KEY_CREATION_TIME = "creation_time"
18 18
     KEY_MODIFIED_TIME = "modified_time"
19 19
     KEY_MODIFIED_USER = "modified_user"
20
+    KEY_TAGS = "tags"
20 21
 
21 22
     def __init__(self, meta_filename, page_exists):
22 23
         self._meta_filename = meta_filename
@@ -38,11 +39,15 @@ class meta_data(dict):
38 39
         if missing_keys and page_exists:
39 40
             self.save()
40 41
 
41
-    def update(self, username):
42
-        self[self.KEY_MODIFIED_TIME] = int(time.time())
43
-        self[self.KEY_MODIFIED_USER] = username
42
+    def update(self, username, tags):
43
+        if username:
44
+            self[self.KEY_MODIFIED_TIME] = int(time.time())
45
+            self[self.KEY_MODIFIED_USER] = username
46
+        if tags:
47
+            self[self.KEY_TAGS] = tags
44 48
         #
45
-        self.save()
49
+        if username or tags:
50
+            self.save()
46 51
 
47 52
     def save(self):
48 53
         with open(self._meta_filename, 'w') as fh:
@@ -87,7 +92,7 @@ class base_page(object):
87 92
         shutil.copy(self.filename, os.path.join(history_folder, "%05d_%s" % (hist_number, self.PAGE_FILE_NAME)))
88 93
         shutil.copy(self._meta_filename, os.path.join(history_folder, "%05d_%s" % (hist_number, self.META_FILE_NAME)))
89 94
 
90
-    def update_page(self, page_txt):
95
+    def update_page(self, page_txt, tags):
91 96
         from .search import update_item
92 97
         if page_txt.replace("\r\n", "\n") != self.raw_page_src:
93 98
             # Store page history
@@ -97,12 +102,14 @@ class base_page(object):
97 102
             fstools.mkdir(os.path.dirname(self.filename))
98 103
             with open(self.filename, 'w') as fh:
99 104
                 fh.write(page_txt)
100
-            # update search index
101
-            update_item(self)
102 105
             # update metadata
103
-            self._update_metadata()
104
-            return True
105
-        return False
106
+            page_changed = True
107
+        else:
108
+            page_changed = False
109
+        self._update_metadata(tags)
110
+        # update search index
111
+        update_item(self)
112
+        return page_changed
106 113
 
107 114
     @property
108 115
     def filename(self):
@@ -134,7 +141,7 @@ class base_page(object):
134 141
         self._load_page_src()
135 142
         return self._raw_page_src
136 143
 
137
-    def _update_metadata(self):
144
+    def _update_metadata(self, tags):
138 145
         username = None
139 146
         try:
140 147
             if self._request.user.is_authenticated:
@@ -143,7 +150,11 @@ class base_page(object):
143 150
                 logger.warning("Page edit without having a logged in user. This is not recommended. Check your access definitions!")
144 151
         except AttributeError:
145 152
             logger.exception("Page edit without having a request object. Check programming!")
146
-        self._meta_data.update(username)
153
+        self._meta_data.update(username, tags)
154
+
155
+    @property
156
+    def page_tags(self):
157
+        return self._meta_data.get(self._meta_data.KEY_TAGS)
147 158
 
148 159
 
149 160
 class creole_page(base_page):
@@ -168,10 +179,12 @@ class creole_page(base_page):
168 179
         ctime = timestamp_to_datetime(self._request, self._meta_data.get(self._meta_data.KEY_CREATION_TIME)).strftime('%Y-%m-%d %H:%M')
169 180
         mtime = timestamp_to_datetime(self._request, self._meta_data.get(self._meta_data.KEY_MODIFIED_TIME)).strftime('%Y-%m-%d %H:%M')
170 181
         user = self._meta_data.get(self._meta_data.KEY_MODIFIED_USER)
182
+        tags = self._meta_data.get(self._meta_data.KEY_TAGS, "-")
171 183
         #
172 184
         meta = f'|{_("Created")}:|{ctime}|\n'
173 185
         meta += f'|{_("Modified")}:|{mtime}|\n'
174 186
         meta += f'|{_("Editor")}|{user}|\n'
187
+        meta += f'|{_("Tags")}|{tags}|\n'
175 188
         meta += f'=== {_("Page content")}\n'
176 189
         meta += '{{{\n%s\n}}}\n' % self.raw_page_src
177 190
         #

+ 3
- 1
pages/search.py Voir le fichier

@@ -18,6 +18,7 @@ SCHEMA = Schema(
18 18
     # Page
19 19
     title=TEXT,
20 20
     page_src=TEXT,
21
+    page_tags=TEXT,
21 22
     # metadata
22 23
     creation_time=DATETIME,
23 24
     modified_time=DATETIME,
@@ -63,6 +64,7 @@ def add_item(ix, bp: base_page):
63 64
         #
64 65
         title=bp.title,
65 66
         page_src=bp.raw_page_src,
67
+        page_tags=bp.page_tags,
66 68
         #
67 69
         creation_time=datetime.fromtimestamp(bp._meta_data.get(bp._meta_data.KEY_CREATION_TIME)),
68 70
         modified_time=datetime.fromtimestamp(bp._meta_data.get(bp._meta_data.KEY_MODIFIED_TIME)),
@@ -77,7 +79,7 @@ def add_item(ix, bp: base_page):
77 79
 
78 80
 def whoosh_search(search_txt):
79 81
     ix = load_index()
80
-    qp = qparser.MultifieldParser(['title', 'page_src'], ix.schema)
82
+    qp = qparser.MultifieldParser(['title', 'page_src', 'page_tags'], ix.schema)
81 83
     qp.add_plugin(DateParserPlugin(free=True))
82 84
     try:
83 85
         q = qp.parse(search_txt)

+ 3
- 2
pages/views.py Voir le fichier

@@ -59,7 +59,7 @@ def edit(request, rel_path):
59 59
         p = creole_page(request, rel_path)
60 60
         #
61 61
         if not request.POST:
62
-            form = EditForm(page_data=p.raw_page_src)
62
+            form = EditForm(page_data=p.raw_page_src, page_tags=p.page_tags)
63 63
             #
64 64
             context_adaption(
65 65
                 context,
@@ -73,10 +73,11 @@ def edit(request, rel_path):
73 73
         else:
74 74
             save = request.POST.get("save")
75 75
             page_txt = request.POST.get("page_txt")
76
+            tags = request.POST.get("page_tags")
76 77
             preview = request.POST.get("preview")
77 78
             #
78 79
             if save is not None:
79
-                if p.update_page(page_txt):
80
+                if p.update_page(page_txt, tags):
80 81
                     messages.edit_success(request)
81 82
                 else:
82 83
                     messages.edit_no_change(request)

Chargement…
Annuler
Enregistrer