Browse Source

Merge branch 'dev/233/files_on_disk' into dev/232/preview_generator

Adrien Panay 7 years ago
parent
commit
da02c1249d

+ 9 - 2
tracim/tracim/model/data.py View File

@@ -24,9 +24,8 @@ from sqlalchemy.types import Integer
24 24
 from sqlalchemy.types import LargeBinary
25 25
 from sqlalchemy.types import Text
26 26
 from sqlalchemy.types import Unicode
27
-
28
-from depot.manager import DepotManager
29 27
 from depot.fields.sqlalchemy import UploadedFileField
28
+from depot.fields.upload import UploadedFile
30 29
 
31 30
 from tracim.lib.utils import lazy_ugettext as l_
32 31
 from tracim.lib.exception import ContentRevisionUpdateError
@@ -1058,6 +1057,14 @@ class Content(DeclarativeBase):
1058 1057
     def is_editable(self) -> bool:
1059 1058
         return not self.is_archived and not self.is_deleted
1060 1059
 
1060
+    @property
1061
+    def depot_file_uid(self) -> UploadedFile:
1062
+        return self.revision.depot_file_uid
1063
+
1064
+    @depot_file_uid.setter
1065
+    def depot_file_uid(self, value):
1066
+        self.revision.depot_file_uid = value
1067
+
1061 1068
     def get_current_revision(self) -> ContentRevisionRO:
1062 1069
         if not self.revisions:
1063 1070
             return self.new_revision()

+ 6 - 6
tracim/tracim/model/serializers.py View File

@@ -1,10 +1,8 @@
1 1
 # -*- coding: utf-8 -*-
2 2
 import cherrypy
3
-import os
4 3
 
5 4
 import types
6 5
 
7
-from bs4 import BeautifulSoup
8 6
 from babel.dates import format_timedelta
9 7
 from babel.dates import format_datetime
10 8
 
@@ -410,12 +408,14 @@ def serialize_node_for_page(content: Content, context: Context):
410 408
             })
411 409
         )
412 410
 
413
-        if content.type==ContentType.File:
411
+        if content.type == ContentType.File:
412
+            dpt = DepotManager.get()
413
+            dpt_file = dpt.get(data_container.depot_file_uid)
414 414
             result.label = content.label
415 415
             result['file'] = DictLikeClass(
416
-                name = data_container.file_name,
417
-                size = DepotManager.get().get(data_container.revision.depot_file_uid).content_length,
418
-                mimetype = data_container.file_mimetype)
416
+                name=data_container.file_name,
417
+                size=dpt_file.content_length,
418
+                mimetype=data_container.file_mimetype)
419 419
         return result
420 420
 
421 421
     if content.type==ContentType.Folder:

+ 46 - 4
tracim/tracim/tests/models/test_content.py View File

@@ -1,13 +1,22 @@
1 1
 # -*- coding: utf-8 -*-
2 2
 import time
3
-from nose.tools import raises, ok_
3
+
4
+from nose.tools import ok_
5
+from nose.tools import raises
4 6
 from sqlalchemy.sql.elements import and_
5 7
 from sqlalchemy.testing import eq_
8
+from depot.fields.upload import UploadedFile
6 9
 
7 10
 from tracim.lib.content import ContentApi
8 11
 from tracim.lib.exception import ContentRevisionUpdateError
9
-from tracim.model import DBSession, User, Content, new_revision
10
-from tracim.model.data import ContentRevisionRO, Workspace, ActionDescription, ContentType
12
+from tracim.model import Content
13
+from tracim.model import DBSession
14
+from tracim.model import new_revision
15
+from tracim.model import User
16
+from tracim.model.data import ActionDescription
17
+from tracim.model.data import ContentRevisionRO
18
+from tracim.model.data import ContentType
19
+from tracim.model.data import Workspace
11 20
 from tracim.tests import TestStandard
12 21
 
13 22
 
@@ -111,7 +120,7 @@ class TestContent(TestStandard):
111 120
             revision_type=ActionDescription.CREATION,
112 121
             is_deleted=False,  # TODO: pk ?
113 122
             is_archived=False,  # TODO: pk ?
114
-            #file_content=None,  # TODO: pk ? (J'ai du mettre nullable=True)
123
+            # file_content=None,  # TODO: pk ? (J'ai du mettre nullable=True)
115 124
         )
116 125
 
117 126
         eq_(1, DBSession.query(ContentRevisionRO).filter(ContentRevisionRO.label == 'TEST_CONTENT_1').count())
@@ -163,9 +172,42 @@ class TestContent(TestStandard):
163 172
 
164 173
         return created_content
165 174
 
175
+    def _get_user(self):
176
+        email = 'admin@admin.admin'
177
+        user_query = DBSession.query(User)
178
+        user_filter = user_query.filter(User.email == email)
179
+        user = user_filter.one()
180
+        return user
181
+
166 182
     def _create_content(self, *args, **kwargs):
167 183
         content = Content(*args, **kwargs)
168 184
         DBSession.add(content)
169 185
         DBSession.flush()
186
+        return content
170 187
 
188
+    def _create_content_from_nothing(self):
189
+        user_admin = self._get_user()
190
+        workspace = Workspace(label="TEST_WORKSPACE_1")
191
+        content = self._create_content(
192
+            owner=user_admin,
193
+            workspace=workspace,
194
+            type=ContentType.File,
195
+            label='TEST_CONTENT_1',
196
+            description='TEST_CONTENT_DESCRIPTION_1',
197
+            revision_type=ActionDescription.CREATION,
198
+        )
171 199
         return content
200
+
201
+    def test_unit__content_depot_file_uid(self):
202
+        """ Depot file access thought content property methods. """
203
+        content = self._create_content_from_nothing()
204
+        # tests uninitialized depot file
205
+        eq_(content.depot_file_uid, None)
206
+        # initializes depot file
207
+        content.depot_file_uid = b'test'
208
+        # tests initialized depot file
209
+        ok_(content.depot_file_uid)
210
+        # tests type of initialized depot file
211
+        eq_(type(content.depot_file_uid), UploadedFile)
212
+        # tests content of initialized depot file
213
+        eq_(content.depot_file_uid.file.read(), b'test')