Pārlūkot izejas kodu

Storage of page history implemented

master
Dirk Alders 3 mēnešus atpakaļ
vecāks
revīzija
dee48093be
3 mainītis faili ar 45 papildinājumiem un 10 dzēšanām
  1. 10
    0
      pages/messages.py
  2. 31
    9
      pages/page.py
  3. 4
    1
      pages/views.py

+ 10
- 0
pages/messages.py Parādīt failu

@@ -10,3 +10,13 @@ def permission_denied_msg_page(request, rel_path):
10 10
 def unavailable_msg_page(request, rel_path):
11 11
     # TODO: Add translation for this message
12 12
     messages.info(request, _("Unavailable: The Page '%s' is not available. Create it or follow a valid link, please.") % rel_path)
13
+
14
+
15
+def edit_success(request):
16
+    # TODO: Add translation for this message
17
+    messages.success(request, _('Thanks for editing, page stored.'))
18
+
19
+
20
+def edit_no_change(request):
21
+    # TODO: Add translation for this message
22
+    messages.info(request, _("Nothing changed, no storage needed."))

+ 31
- 9
pages/page.py Parādīt failu

@@ -7,6 +7,7 @@ import logging
7 7
 from pages import messages, url_page
8 8
 import mycreole
9 9
 import os
10
+import shutil
10 11
 import time
11 12
 from users.models import get_userprofile
12 13
 import zoneinfo
@@ -53,6 +54,7 @@ class meta_data(dict):
53 54
 class base_page(object):
54 55
     PAGE_FILE_NAME = 'page'
55 56
     META_FILE_NAME = 'meta.json'
57
+    HISTORY_FOLDER_NAME = 'history'
56 58
     SPLITCHAR = ":"
57 59
 
58 60
     def __init__(self, path):
@@ -72,17 +74,37 @@ class base_page(object):
72 74
             except FileNotFoundError:
73 75
                 self._raw_page_src = ""
74 76
 
77
+    def _store_history(self):
78
+        history_folder = os.path.join(self._path, self.HISTORY_FOLDER_NAME)
79
+        # create folder if needed
80
+        fstools.mkdir(history_folder)
81
+        # identify last_history number
82
+        flist = fstools.filelist(history_folder)
83
+        flist.sort()
84
+        if flist:
85
+            hist_number = int(os.path.basename(flist[-1])[:5]) + 1
86
+        else:
87
+            hist_number = 1
88
+        # copy file to history folder
89
+        shutil.copy(self.filename, os.path.join(history_folder, "%05d_%s" % (hist_number, self.PAGE_FILE_NAME)))
90
+        shutil.copy(self._meta_filename, os.path.join(history_folder, "%05d_%s" % (hist_number, self.META_FILE_NAME)))
91
+
75 92
     def update_page(self, page_txt):
76 93
         from .search import update_item
77
-        #
78
-        folder = os.path.dirname(self.filename)
79
-        if not os.path.exists(folder):
80
-            fstools.mkdir(folder)
81
-        with open(self.filename, 'w') as fh:
82
-            fh.write(page_txt)
83
-        update_item(self)
84
-        #
85
-        self._update_metadata()
94
+        if page_txt.replace("\r\n", "\n") != self.raw_page_src:
95
+            # Store page history
96
+            if self.raw_page_src:
97
+                self._store_history()
98
+            # save the new page content
99
+            fstools.mkdir(os.path.dirname(self.filename))
100
+            with open(self.filename, 'w') as fh:
101
+                fh.write(page_txt)
102
+            # update search index
103
+            update_item(self)
104
+            # update metadata
105
+            self._update_metadata()
106
+            return True
107
+        return False
86 108
 
87 109
     @property
88 110
     def filename(self):

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

@@ -75,7 +75,10 @@ def edit(request, rel_path):
75 75
             preview = request.POST.get("preview")
76 76
             #
77 77
             if save is not None:
78
-                p.update_page(page_txt)
78
+                if p.update_page(page_txt):
79
+                    messages.edit_success(request)
80
+                else:
81
+                    messages.edit_no_change(request)
79 82
                 return HttpResponseRedirect(url_page(request, rel_path))
80 83
             elif preview is not None:
81 84
                 form = EditForm(page_data=page_txt)

Notiek ielāde…
Atcelt
Saglabāt