Browse Source

resolve merge conflict

Nonolost 8 years ago
parent
commit
97169ee229

+ 35 - 18
tracim/tracim/lib/content.py View File

@@ -364,35 +364,43 @@ class ContentApi(object):
364 364
 
365 365
         return self._base_query(workspace).filter(Content.content_id==content_id).filter(Content.type==content_type).one()
366 366
 
367
-    # TODO : temporary code for webdav support; make it clean !
368 367
     def get_one_revision(self, revision_id: int = None) -> Content:
368
+        """
369
+        This method allow us to get directly any revision with its id
370
+        :param revision_id: The content's revision's id that we want to return
371
+        :return: An item Content linked with the correct revision
372
+        """
373
+        assert revision_id is not None# DYN_REMOVE
369 374
 
370
-        return DBSession.query(ContentRevisionRO).filter(ContentRevisionRO.revision_id == revision_id).one_or_none()
371
-
372
-    def get_one_revision2(self, revision_id: int = None):
373
-        ro = self.get_one_revision(revision_id)
375
+        revision = DBSession.query(ContentRevisionRO).filter(ContentRevisionRO.revision_id == revision_id)
376
+        
377
+        result = self._base_query(None)
374 378
 
375
-        return DBSession.query(Content).filter(Content.id == ro.content_id).one()
379
+        return result.filter(Content.revision_id == revision_id).one()
376 380
 
377 381
     def get_one_by_label_and_parent(self, content_label: str, content_parent: Content = None,
378 382
                                     workspace: Workspace = None) -> Content:
383
+        """
384
+        This method let us request the database to obtain a Content with its name and parent
385
+        :param content_label: Either the content's label or the content's filename if the label is None
386
+        :param content_parent: The parent's content
387
+        :param workspace: The workspace's content
388
+        :return The corresponding Content
389
+        """
390
+        assert content_label is not None# DYN_REMOVE
379 391
 
380
-        if not content_label:
381
-            return None
382
-
383
-        query = self._base_query(workspace)
392
+        resultset = self._base_query(workspace)
384 393
 
385 394
         parent_id = content_parent.content_id if content_parent else None
386 395
 
387
-        query = query.filter(Content.parent_id == parent_id)
388
-
389
-        res = query.filter(Content.label == content_label).one_or_none()
396
+        resultset = resultset.filter(Content.parent_id == parent_id)
390 397
 
391
-        return res if res is not None else query.filter(Content.file_name==content_label).one_or_none()
398
+        try:
399
+            return resultset.filter(Content.label == content_label).one()
400
+        except:
401
+            return resultset.filter(Content.file_name == content_label).one()
392 402
 
393
-    # TODO : end of the webdav's code
394
-
395
-    def get_all(self, parent_id: int, content_type: str, workspace: Workspace=None) -> Content:
403
+    def get_all(self, parent_id: int, content_type: str, workspace: Workspace=None) -> [Content]:
396 404
         assert parent_id is None or isinstance(parent_id, int) # DYN_REMOVE
397 405
         assert content_type is not None# DYN_REMOVE
398 406
         assert isinstance(content_type, str) # DYN_REMOVE
@@ -402,11 +410,20 @@ class ContentApi(object):
402 410
         if content_type!=ContentType.Any:
403 411
             resultset = resultset.filter(Content.type==content_type)
404 412
 
405
-        # todo : check utilité if parent_id:
406 413
         resultset = resultset.filter(Content.parent_id==parent_id)
407 414
 
408 415
         return resultset.all()
409 416
 
417
+    def get_all_without_exception(self, content_type: str) -> [Content]:
418
+        assert content_type is not None# DYN_REMOVE
419
+
420
+        resultset = self._base_query(None)
421
+
422
+        if content_type != ContentType.Any:
423
+            resultset = resultset.filter(Content.type==content_type)
424
+
425
+        return resultset.all()
426
+
410 427
     def get_last_active(self, parent_id: int, content_type: str, workspace: Workspace=None, limit=10) -> [Content]:
411 428
         assert parent_id is None or isinstance(parent_id, int) # DYN_REMOVE
412 429
         assert content_type is not None# DYN_REMOVE

+ 39 - 57
tracim/tracim/lib/webdav/__init__.py View File

@@ -1,12 +1,3 @@
1
-from sqlalchemy import create_engine
2
-from sqlalchemy.ext.declarative import declarative_base
3
-from sqlalchemy.orm import sessionmaker
4
-
5
-Base = declarative_base()
6
-
7
-some_engine = create_engine('postgresql://arnaud:wxc@localhost/template1')
8
-Session = sessionmaker(bind=some_engine)
9
-
10 1
 from wsgidav.compat import to_bytes
11 2
 
12 3
 from tracim.lib.content import ContentApi
@@ -15,15 +6,6 @@ from tracim.model.data import ActionDescription, ContentType, Content
15 6
 from wsgidav import util
16 7
 
17 8
 import transaction
18
-Base.metadata.create_all(some_engine)
19
-
20
-role = {
21
-    'NOT_APPLICABLE': 0,
22
-    'READER': 1,
23
-    'CONTRIBUTOR': 2,
24
-    'CONTENT_MANAGER': 4,
25
-    'WORKSPACE_MANAGER': 8
26
-}
27 9
 
28 10
 class HistoryType(object):
29 11
     Deleted = 'deleted'
@@ -31,52 +13,52 @@ class HistoryType(object):
31 13
     Standard = 'standard'
32 14
     All = 'all'
33 15
 
34
-# not_applicable : nothing
35
-# reader : can only read everything in designed workspace
36
-# contributor : + create / modify files
37
-# content_manager : + delete files / create directory / delete directory
38
-# workspace_manager : + create workspace / delete workspace
39 16
 
40
-class MyFileStream(object):
41
-    def __init__(self, content: Content, content_api: ContentApi, file_name: str=''):
42
-        self.buflist = []
17
+class FileStream(object):
18
+    def __init__(self, file_name: str, content: Content, content_api: ContentApi, new_file: bool):
19
+        self._buffer = []
20
+        self._file_name = file_name if file_name != '' else self._content.file_name
43 21
         self._content = content
44 22
         self._api = content_api
45 23
 
46
-        self._file_name = file_name if file_name != '' else self._content.file_name
47
-
48
-    def write(self, s):
49
-        self.buflist.append(s)
24
+    def beginWrite(self, contentType) -> FileStream:
25
+        return self
50 26
 
51
-    def close(self):
52
-        tot = to_bytes('')
53
-        for buf in self.buflist:
54
-            tot += buf
27
+    def endWrite(self, withErrors: bool):
28
+        pass
55 29
 
56
-        with new_revision(self._content):
57
-            self._api.update_file_data(self._content, self._file_name, util.guessMimeType(self._content.file_name), tot)
58
-            self._api.save(self._content, ActionDescription.EDITION)
59
-
60
-        transaction.commit()
61
-
62
-
63
-class MyFileStream2(object):
64
-    def __init__(self, file_name: str, content: Content, content_api: ContentApi):
65
-        self.buflist = []
66
-        self._file_name = file_name
67
-        self._content = content
68
-        self._api = content_api
69
-
70
-    def write(self, s):
71
-        self.buflist.append(s)
30
+    def write(self, s: str):
31
+        self._buffer.append(s)
72 32
 
73 33
     def close(self):
74
-        tot = to_bytes('')
75
-        for buf in self.buflist:
76
-            tot += buf
77
-
78
-        file = self._api.create(ContentType.File, self._content.workspace, self._content)
79
-        self._api.update_file_data(file, self._file_name, util.guessMimeType(self._file_name), tot)
80
-        self._api.save(file, ActionDescription.CREATION)
34
+        item_content = b''
35
+
36
+        for part in self._buffer:
37
+            item_content += part
38
+
39
+        if new_file:
40
+            file = self._api.create(
41
+                content_type=ContentType.File,
42
+                workspace=self._content.workspace,
43
+                parent=self._content
44
+                )
45
+
46
+            self._api.update_file_data(
47
+                file,
48
+                self._file_name,
49
+                util.guessMimeType(self._file_name),
50
+                item_content
51
+                )
52
+
53
+            self._api.save(file, ActionDescription.CREATION)
54
+
55
+        else:
56
+            with new_revision(self._content):
57
+                self._api.update_file_data(
58
+                    self._content,
59
+                    self._file_name,
60
+                    util.guessMimeType(self._content.file_name),
61
+                    item_content)
62
+                self._api.save(self._content, ActionDescription.EDITION)
81 63
 
82 64
         transaction.commit()

+ 5 - 6
tracim/tracim/lib/webdav/sql_dav_provider.py View File

@@ -56,15 +56,14 @@ class Provider(DAVProvider):
56 56
 
57 57
         norm_path = normpath(working_path)
58 58
 
59
-        user = UserApi(None).get_one_by_email(environ['http_authenticator.username'])
60
-        workspace_api = WorkspaceApi(user)
59
+        workspace_api = WorkspaceApi(environ['user'])
61 60
 
62 61
         if dirname(norm_path) == "/":
63 62
             workspace = self.get_workspace_from_path(norm_path, workspace_api)
64 63
             return sql_resources.Workspace(path, environ, workspace)
65 64
 
66 65
 
67
-        api = ContentApi(user, show_archived=True, show_deleted=True)
66
+        api = ContentApi(environ['user'], show_archived=True, show_deleted=True)
68 67
 
69 68
         working_path = self.reduce_path(path)
70 69
 
@@ -129,15 +128,15 @@ class Provider(DAVProvider):
129 128
         elif dirname(path) == "/":
130 129
             return self.get_workspace_from_path(
131 130
                 path,
132
-                WorkspaceApi(UserApi(None).get_one_by_email(environ['http_authenticator.username']))
131
+                WorkspaceApi(environ['user'])
133 132
             ) is not None
134 133
 
135 134
         api = ContentApi(
136
-            current_user=UserApi(None).get_one_by_email(environ['http_authenticator.username']),
135
+            current_user=environ['user'],
137 136
             show_archived=True,
138 137
             show_deleted=True
139 138
         )
140
-        wapi = WorkspaceApi(UserApi(None).get_one_by_email(environ['http_authenticator.username']))
139
+        wapi = WorkspaceApi(environ['user'])
141 140
 
142 141
         norm_path = normpath(path)
143 142
 

+ 1 - 1
tracim/tracim/lib/webdav/sql_domain_controller.py View File

@@ -21,7 +21,7 @@ class SQLDomainController(object):
21 21
         travailler dans la bdd pour vérifier si utilisateur existe
22 22
         """
23 23
         try:
24
-            self._api.get_one_by_email(username)
24
+            environ['user'] = self._api.get_one_by_email(username)
25 25
             return True
26 26
         except:
27 27
             return False

File diff suppressed because it is too large
+ 291 - 287
tracim/tracim/lib/webdav/sql_resources.py


+ 2 - 2
tracim/tracim/lib/workspace.py View File

@@ -61,8 +61,8 @@ class WorkspaceApi(object):
61 61
     def get_one(self, id):
62 62
         return self._base_query().filter(Workspace.workspace_id==id).one()
63 63
 
64
-    def get_one_by_label(self, label: str):
65
-        return self._base_query().filter(Workspace.label==label).one()
64
+    def get_one_by_label(self, label: str) -> Workspace:
65
+        return self._base_query().filter(Workspace.label == label).one()
66 66
 
67 67
     """
68 68
     def get_one_for_current_user(self, id):

+ 26 - 1
tracim/tracim/model/data.py View File

@@ -1245,4 +1245,29 @@ class VirtualEvent(object):
1245 1245
         if not delta_from_datetime:
1246 1246
             delta_from_datetime = datetime.now()
1247 1247
         return format_timedelta(delta_from_datetime - self.created,
1248
-                                locale=tg.i18n.get_lang()[0])
1248
+                                locale=tg.i18n.get_lang()[0])
1249
+
1250
+    def create_readable_date(self, delta_from_datetime:datetime=None):
1251
+        aff = ''
1252
+
1253
+        if not delta_from_datetime:
1254
+            delta_from_datetime = datetime.now()
1255
+
1256
+        delta = delta_from_datetime - self.created
1257
+        
1258
+        if delta.days > 0:
1259
+            if delta.days >= 365:
1260
+                aff = '%d year%s ago' % (delta.days/365, 's' if delta.days/365>=2 else '')
1261
+            elif delta.days >= 30:
1262
+                aff = '%d month%s ago' % (delta.days/30, 's' if delta.days/30>=2 else '')
1263
+            else:
1264
+                aff = '%d day%s ago' % (delta.days, 's' if delta.days>=2 else '')
1265
+        else:
1266
+            if delta.seconds < 60:
1267
+                aff = '%d second%s ago' % (delta.seconds, 's' if delta.seconds>1 else '')
1268
+            elif delta.seconds/60 < 60:
1269
+                aff = '%d minute%s ago' % (delta.seconds/60, 's' if delta.seconds/60>=2 else '')
1270
+            else:
1271
+                aff = '%d hour%s ago' % (delta.seconds/3600, 's' if delta.seconds/3600>=2 else '')
1272
+
1273
+        return aff