Guénaël Muller 6 anni fa
parent
commit
4d3884952a

+ 17 - 0
tracim/tracim/lib/content.py Vedi File

@@ -29,6 +29,7 @@ from sqlalchemy.sql.elements import and_
29 29
 from tracim.lib import cmp_to_key
30 30
 from tracim.lib.notifications import NotifierFactory
31 31
 from tracim.lib.utils import SameValueError
32
+from tracim.lib.utils import current_date_for_filename
32 33
 from tracim.model import DBSession
33 34
 from tracim.model import new_revision
34 35
 from tracim.model.auth import User
@@ -894,6 +895,14 @@ class ContentApi(object):
894 895
     def archive(self, content: Content):
895 896
         content.owner = self._user
896 897
         content.is_archived = True
898
+        # TODO - G.M - 12-03-2018 - Inspect possible label conflict problem
899
+        # INFO - G.M - 12-03-2018 - Set label name to avoid trouble when
900
+        # un-archiving file.
901
+        content.label = '{label}-{action}-{date}'.format(
902
+            label=content.label,
903
+            action='archived',
904
+            date=current_date_for_filename()
905
+        )
897 906
         content.revision_type = ActionDescription.ARCHIVING
898 907
 
899 908
     def unarchive(self, content: Content):
@@ -904,6 +913,14 @@ class ContentApi(object):
904 913
     def delete(self, content: Content):
905 914
         content.owner = self._user
906 915
         content.is_deleted = True
916
+        # TODO - G.M - 12-03-2018 - Inspect possible label conflict problem
917
+        # INFO - G.M - 12-03-2018 - Set label name to avoid trouble when
918
+        # un-deleting file.
919
+        content.label = '{label}-{action}-{date}'.format(
920
+            label=content.label,
921
+            action='deleted',
922
+            date=current_date_for_filename()
923
+        )
907 924
         content.revision_type = ActionDescription.DELETION
908 925
 
909 926
     def undelete(self, content: Content):

+ 10 - 0
tracim/tracim/lib/utils.py Vedi File

@@ -1,4 +1,5 @@
1 1
 # -*- coding: utf-8 -*-
2
+import datetime
2 3
 import os
3 4
 import time
4 5
 import signal
@@ -180,3 +181,12 @@ def get_rq_queue(queue_name: str= 'default') -> Queue:
180 181
         port=cfg.EMAIL_SENDER_REDIS_PORT,
181 182
         db=cfg.EMAIL_SENDER_REDIS_DB,
182 183
     ))
184
+
185
+
186
+def current_date_for_filename() -> str:
187
+    """
188
+    ISO8601 current date, adapted to be used in filename (for
189
+    webdav feature for example), with trouble-free characters.
190
+    :return: current date as string
191
+    """
192
+    return datetime.datetime.now().isoformat().replace(':', '.')

+ 6 - 48
tracim/tracim/lib/webdav/sql_resources.py Vedi File

@@ -36,7 +36,8 @@ logger = logging.getLogger()
36 36
 
37 37
 class ManageActions(object):
38 38
     """
39
-    This object is used to encapsulate all Deletion/Archiving related method as to not duplicate too much code
39
+    This object is used to encapsulate all Deletion/Archiving related
40
+    method as to not duplicate too much code
40 41
     """
41 42
     def __init__(self, action_type: str, api: ContentApi, content: Content):
42 43
         self.content_api = api
@@ -49,57 +50,14 @@ class ManageActions(object):
49 50
             ActionDescription.UNDELETION: self.content_api.undelete
50 51
         }
51 52
 
52
-        self._to_name = {
53
-            ActionDescription.ARCHIVING: 'archived',
54
-            ActionDescription.DELETION: 'deleted'
55
-        }
56
-
57 53
         self._type = action_type
58
-        self._new_name = self.make_name()
59 54
 
60 55
     def action(self):
61
-        try:
62
-            # When undeleting/unarchiving we except a content with the new name to not exist, thus if we
63
-            # don't get an error and the database request send back a result, we stop the action
64
-            self.content_api.get_one_by_label_and_parent(self._new_name, self.content.parent)
65
-            raise DAVError(HTTP_FORBIDDEN)
66
-        except NoResultFound:
67
-            with new_revision(self.content):
68
-                self.content_api.update_content(self.content, self._new_name)
69
-                self._actions[self._type](self.content)
70
-                self.content_api.save(self.content, self._type)
71
-
72
-            transaction.commit()
73
-
74
-    def make_name(self) -> str:
75
-        """
76
-        Will create the new name, either by adding '- deleted the [date]' after the name when archiving/deleting or
77
-        removing this string when undeleting/unarchiving
78
-        """
79
-        new_name = self.content.get_label_as_file()
80
-        extension = ''
81
-
82
-        # if the content has no label, the last .ext is important
83
-        # thus we want to rename a file from 'file.txt' to 'file - deleted... .txt' and not 'file.txt - deleted...'
84
-        is_file_name = self.content.label == ''
85
-        if is_file_name:
86
-            search = re.search(r'(\.[^.]+)$', new_name)
87
-            if search:
88
-                extension = search.group(0)
89
-            new_name = re.sub(r'(\.[^.]+)$', '', new_name)
90
-
91
-        if self._type in [ActionDescription.ARCHIVING, ActionDescription.DELETION]:
92
-            new_name += ' - %s the %s' % (self._to_name[self._type], datetime.now().strftime('%d-%m-%Y at %H_%M'))
93
-        else:
94
-            new_name = re.sub(
95
-                r'( - (%s|%s) the .*)$' % (self._to_name[ActionDescription.DELETION], self._to_name[ActionDescription.ARCHIVING]),
96
-                '',
97
-                new_name
98
-            )
99
-
100
-        new_name += extension
56
+        with new_revision(self.content):
57
+            self._actions[self._type](self.content)
58
+            self.content_api.save(self.content, self._type)
101 59
 
102
-        return new_name
60
+        transaction.commit()
103 61
 
104 62
 
105 63
 class Root(DAVCollection):