浏览代码

WebDav: move transform_to_display and transform_to_bdd to independent functions

Bastien Sevajol (Algoo) 8 年前
父节点
当前提交
67d2b66725

+ 4 - 47
tracim/tracim/lib/webdav/sql_dav_provider.py 查看文件

2
 
2
 
3
 import re
3
 import re
4
 from os.path import basename, dirname, normpath
4
 from os.path import basename, dirname, normpath
5
+from tracim.lib.webdav.utils import transform_to_bdd
5
 
6
 
6
 from wsgidav.dav_provider import DAVProvider
7
 from wsgidav.dav_provider import DAVProvider
7
 from wsgidav.lock_manager import LockManager
8
 from wsgidav.lock_manager import LockManager
242
         parents = blbl.split('/')
243
         parents = blbl.split('/')
243
 
244
 
244
         parents.remove('')
245
         parents.remove('')
245
-        parents = [self.transform_to_bdd(x) for x in parents]
246
+        parents = [transform_to_bdd(x) for x in parents]
246
 
247
 
247
         try:
248
         try:
248
             return content_api.get_one_by_label_and_parent_label(
249
             return content_api.get_one_by_label_and_parent_label(
249
-                self.transform_to_bdd(basename(path)),
250
+                transform_to_bdd(basename(path)),
250
                 parents,
251
                 parents,
251
                 workspace
252
                 workspace
252
             )
253
             )
264
 
265
 
265
     def get_workspace_from_path(self, path: str, api: WorkspaceApi) -> Workspace:
266
     def get_workspace_from_path(self, path: str, api: WorkspaceApi) -> Workspace:
266
         try:
267
         try:
267
-            return api.get_one_by_label(self.transform_to_bdd(path.split('/')[1]))
268
+            return api.get_one_by_label(transform_to_bdd(path.split('/')[1]))
268
         except:
269
         except:
269
             return None
270
             return None
270
-
271
-    def transform_to_display(self, string):
272
-        """
273
-        As characters that Windows does not support may have been inserted through Tracim in names, before displaying
274
-        information we update path so that all these forbidden characters are replaced with similar shape character
275
-        that are allowed so that the user isn't trouble and isn't limited in his naming choice
276
-        """
277
-        _TO_DISPLAY = {
278
-            '/':'⧸',
279
-            '\\': '⧹',
280
-            ':': '∶',
281
-            '*': '∗',
282
-            '?': 'ʔ',
283
-            '"': 'ʺ',
284
-            '<': '❮',
285
-            '>': '❯',
286
-            '|': '∣'
287
-        }
288
-
289
-        for key, value in _TO_DISPLAY.items():
290
-            string = string.replace(key, value)
291
-
292
-        return string
293
-
294
-    def transform_to_bdd(self, string):
295
-        """
296
-        Called before sending request to the database to recover the right names
297
-        """
298
-        _TO_BDD = {
299
-            '⧸': '/',
300
-            '⧹': '\\',
301
-            '∶': ':',
302
-            '∗': '*',
303
-            'ʔ': '?',
304
-            'ʺ': '"',
305
-            '❮': '<',
306
-            '❯': '>',
307
-            '∣': '|'
308
-        }
309
-
310
-        for key, value in _TO_BDD.items():
311
-            string = string.replace(key, value)
312
-
313
-        return string

+ 20 - 18
tracim/tracim/lib/webdav/sql_resources.py 查看文件

10
 from tracim.lib.user import UserApi
10
 from tracim.lib.user import UserApi
11
 from tracim.lib.webdav import HistoryType
11
 from tracim.lib.webdav import HistoryType
12
 from tracim.lib.webdav import FakeFileStream
12
 from tracim.lib.webdav import FakeFileStream
13
+from tracim.lib.webdav.utils import transform_to_display
14
+from tracim.lib.webdav.utils import transform_to_bdd
13
 from tracim.lib.workspace import WorkspaceApi
15
 from tracim.lib.workspace import WorkspaceApi
14
 from tracim.model import data, new_revision
16
 from tracim.model import data, new_revision
15
 from tracim.model.data import Content, ActionDescription
17
 from tracim.model.data import Content, ActionDescription
121
         """
123
         """
122
         try:
124
         try:
123
             workspace = self.workspace_api.get_one_by_label(label)
125
             workspace = self.workspace_api.get_one_by_label(label)
124
-            workspace_path = '%s%s%s' % (self.path, '' if self.path == '/' else '/', self.provider.transform_to_display(workspace.label))
126
+            workspace_path = '%s%s%s' % (self.path, '' if self.path == '/' else '/', transform_to_display(workspace.label))
125
 
127
 
126
             return Workspace(workspace_path, self.environ, workspace)
128
             return Workspace(workspace_path, self.environ, workspace)
127
         except AttributeError:
129
         except AttributeError:
152
         self.workspace_api.save(new_workspace)
154
         self.workspace_api.save(new_workspace)
153
 
155
 
154
         workspace_path = '%s%s%s' % (
156
         workspace_path = '%s%s%s' % (
155
-        self.path, '' if self.path == '/' else '/', self.provider.transform_to_display(new_workspace.label))
157
+        self.path, '' if self.path == '/' else '/', transform_to_display(new_workspace.label))
156
 
158
 
157
         transaction.commit()
159
         transaction.commit()
158
         return Workspace(workspace_path, self.environ, new_workspace)
160
         return Workspace(workspace_path, self.environ, new_workspace)
222
     def getMember(self, content_label: str) -> _DAVResource:
224
     def getMember(self, content_label: str) -> _DAVResource:
223
 
225
 
224
         return self.provider.getResourceInst(
226
         return self.provider.getResourceInst(
225
-            '%s/%s' % (self.path, self.provider.transform_to_display(content_label)),
227
+            '%s/%s' % (self.path, transform_to_display(content_label)),
226
             self.environ
228
             self.environ
227
         )
229
         )
228
 
230
 
275
 
277
 
276
         transaction.commit()
278
         transaction.commit()
277
 
279
 
278
-        return Folder('%s/%s' % (self.path, self.provider.transform_to_display(label)),
280
+        return Folder('%s/%s' % (self.path, transform_to_display(label)),
279
                       self.environ, folder,
281
                       self.environ, folder,
280
                       self.workspace)
282
                       self.workspace)
281
 
283
 
299
         children = self.content_api.get_all(False, ContentType.Any, self.workspace)
301
         children = self.content_api.get_all(False, ContentType.Any, self.workspace)
300
 
302
 
301
         for content in children:
303
         for content in children:
302
-            content_path = '%s/%s' % (self.path, self.provider.transform_to_display(content.get_label()))
304
+            content_path = '%s/%s' % (self.path, transform_to_display(content.get_label()))
303
 
305
 
304
             if content.type == ContentType.Folder:
306
             if content.type == ContentType.Folder:
305
                 members.append(Folder(content_path, self.environ, self.workspace, content))
307
                 members.append(Folder(content_path, self.environ, self.workspace, content))
363
         return mktime(self.content.created.timetuple())
365
         return mktime(self.content.created.timetuple())
364
 
366
 
365
     def getDisplayName(self) -> str:
367
     def getDisplayName(self) -> str:
366
-        return self.provider.transform_to_display(self.content.get_label())
368
+        return transform_to_display(self.content.get_label())
367
 
369
 
368
     def getLastModified(self) -> float:
370
     def getLastModified(self) -> float:
369
         return mktime(self.content.updated.timetuple())
371
         return mktime(self.content.updated.timetuple())
441
 
443
 
442
         with new_revision(self.content):
444
         with new_revision(self.content):
443
             if basename(destpath) != self.getDisplayName():
445
             if basename(destpath) != self.getDisplayName():
444
-                self.content_api.update_content(self.content, self.provider.transform_to_bdd(basename(destpath)))
446
+                self.content_api.update_content(self.content, transform_to_bdd(basename(destpath)))
445
                 self.content_api.save(self.content)
447
                 self.content_api.save(self.content)
446
             else:
448
             else:
447
                 if workspace.workspace_id == self.content.workspace.workspace_id:
449
                 if workspace.workspace_id == self.content.workspace.workspace_id:
461
         )
463
         )
462
 
464
 
463
         for content in visible_children:
465
         for content in visible_children:
464
-            content_path = '%s/%s' % (self.path, self.provider.transform_to_display(content.get_label()))
466
+            content_path = '%s/%s' % (self.path, transform_to_display(content.get_label()))
465
 
467
 
466
             if content.type == ContentType.Folder:
468
             if content.type == ContentType.Folder:
467
                 members.append(Folder(content_path, self.environ, self.workspace, content))
469
                 members.append(Folder(content_path, self.environ, self.workspace, content))
627
         )
629
         )
628
 
630
 
629
         return self.provider.getResourceInst(
631
         return self.provider.getResourceInst(
630
-            path='%s/%s' % (self.path, self.provider.transform_to_display(content.get_label())),
632
+            path='%s/%s' % (self.path, transform_to_display(content.get_label())),
631
             environ=self.environ
633
             environ=self.environ
632
             )
634
             )
633
 
635
 
658
 
660
 
659
         for content in children:
661
         for content in children:
660
             if content.is_deleted:
662
             if content.is_deleted:
661
-                content_path = '%s/%s' % (self.path, self.provider.transform_to_display(content.get_label()))
663
+                content_path = '%s/%s' % (self.path, transform_to_display(content.get_label()))
662
 
664
 
663
                 if content.type == ContentType.Folder:
665
                 if content.type == ContentType.Folder:
664
                     members.append(Folder(content_path, self.environ, self.workspace, content))
666
                     members.append(Folder(content_path, self.environ, self.workspace, content))
712
         )
714
         )
713
 
715
 
714
         return self.provider.getResourceInst(
716
         return self.provider.getResourceInst(
715
-            path=self.path + '/' + self.provider.transform_to_display(content.get_label()),
717
+            path=self.path + '/' + transform_to_display(content.get_label()),
716
             environ=self.environ
718
             environ=self.environ
717
         )
719
         )
718
 
720
 
738
 
740
 
739
         for content in children:
741
         for content in children:
740
             if content.is_archived:
742
             if content.is_archived:
741
-                content_path = '%s/%s' % (self.path, self.provider.transform_to_display(content.get_label()))
743
+                content_path = '%s/%s' % (self.path, transform_to_display(content.get_label()))
742
 
744
 
743
                 if content.type == ContentType.Folder:
745
                 if content.type == ContentType.Folder:
744
                     members.append(Folder(content_path, self.environ, self.workspace, content))
746
                     members.append(Folder(content_path, self.environ, self.workspace, content))
800
 
802
 
801
         if self.content.type == ContentType.File:
803
         if self.content.type == ContentType.File:
802
             return HistoryFile(
804
             return HistoryFile(
803
-                path='%s%s' % (left_side, self.provider.transform_to_display(revision.file_name)),
805
+                path='%s%s' % (left_side, transform_to_display(revision.file_name)),
804
                 environ=self.environ,
806
                 environ=self.environ,
805
                 content=self.content,
807
                 content=self.content,
806
                 content_revision=revision)
808
                 content_revision=revision)
807
         else:
809
         else:
808
             return HistoryOtherFile(
810
             return HistoryOtherFile(
809
-                path='%s%s' % (left_side, self.provider.transform_to_display(revision.get_label())),
811
+                path='%s%s' % (left_side, transform_to_display(revision.get_label())),
810
                 environ=self.environ,
812
                 environ=self.environ,
811
                 content=self.content,
813
                 content=self.content,
812
                 content_revision=revision)
814
                 content_revision=revision)
820
 
822
 
821
             if self.content.type == ContentType.File:
823
             if self.content.type == ContentType.File:
822
                 members.append(HistoryFile(
824
                 members.append(HistoryFile(
823
-                    path='%s%s' % (left_side, self.provider.transform_to_display(content.file_name)),
825
+                    path='%s%s' % (left_side, transform_to_display(content.file_name)),
824
                     environ=self.environ,
826
                     environ=self.environ,
825
                     content=self.content,
827
                     content=self.content,
826
                     content_revision=content)
828
                     content_revision=content)
827
                 )
829
                 )
828
             else:
830
             else:
829
                 members.append(HistoryOtherFile(
831
                 members.append(HistoryOtherFile(
830
-                    path='%s%s' % (left_side, self.provider.transform_to_display(content.file_name)),
832
+                    path='%s%s' % (left_side, transform_to_display(content.file_name)),
831
                     environ=self.environ,
833
                     environ=self.environ,
832
                     content=self.content,
834
                     content=self.content,
833
                     content_revision=content)
835
                     content_revision=content)
992
 
994
 
993
     def getDisplayName(self) -> str:
995
     def getDisplayName(self) -> str:
994
         left_side = '(%d - %s) ' % (self.content_revision.revision_id, self.content_revision.revision_type)
996
         left_side = '(%d - %s) ' % (self.content_revision.revision_id, self.content_revision.revision_type)
995
-        return '%s%s' % (left_side, self.provider.transform_to_display(self.content_revision.file_name))
997
+        return '%s%s' % (left_side, transform_to_display(self.content_revision.file_name))
996
 
998
 
997
     def getContent(self):
999
     def getContent(self):
998
         filestream = compat.BytesIO()
1000
         filestream = compat.BytesIO()
1081
 
1083
 
1082
     def getDisplayName(self) -> str:
1084
     def getDisplayName(self) -> str:
1083
         left_side = '(%d - %s) ' % (self.content_revision.revision_id, self.content_revision.revision_type)
1085
         left_side = '(%d - %s) ' % (self.content_revision.revision_id, self.content_revision.revision_type)
1084
-        return '%s%s' % (left_side, self.provider.transform_to_display(self.content_revision.get_label()))
1086
+        return '%s%s' % (left_side, transform_to_display(self.content_revision.get_label()))
1085
 
1087
 
1086
     def getContent(self):
1088
     def getContent(self):
1087
         filestream = compat.BytesIO()
1089
         filestream = compat.BytesIO()

+ 49 - 0
tracim/tracim/lib/webdav/utils.py 查看文件

1
+# -*- coding: utf-8 -*-
2
+
3
+
4
+def transform_to_display(string: str) -> str:
5
+    """
6
+    As characters that Windows does not support may have been inserted
7
+    through Tracim in names, before displaying information we update path
8
+    so that all these forbidden characters are replaced with similar
9
+    shape character that are allowed so that the user isn't trouble and
10
+    isn't limited in his naming choice
11
+    """
12
+    _TO_DISPLAY = {
13
+        '/': '⧸',
14
+        '\\': '⧹',
15
+        ':': '∶',
16
+        '*': '∗',
17
+        '?': 'ʔ',
18
+        '"': 'ʺ',
19
+        '<': '❮',
20
+        '>': '❯',
21
+        '|': '∣'
22
+    }
23
+
24
+    for key, value in _TO_DISPLAY.items():
25
+        string = string.replace(key, value)
26
+
27
+    return string
28
+
29
+
30
+def transform_to_bdd(string: str) -> str:
31
+    """
32
+    Called before sending request to the database to recover the right names
33
+    """
34
+    _TO_BDD = {
35
+        '⧸': '/',
36
+        '⧹': '\\',
37
+        '∶': ':',
38
+        '∗': '*',
39
+        'ʔ': '?',
40
+        'ʺ': '"',
41
+        '❮': '<',
42
+        '❯': '>',
43
+        '∣': '|'
44
+    }
45
+
46
+    for key, value in _TO_BDD.items():
47
+        string = string.replace(key, value)
48
+
49
+    return string