Browse Source

Add Full copy feature

Guénaël Muller 6 years ago
parent
commit
968f28b69f
2 changed files with 56 additions and 25 deletions
  1. 26 25
      tracim/tracim/lib/content.py
  2. 30 0
      tracim/tracim/model/data.py

+ 26 - 25
tracim/tracim/lib/content.py View File

865
         item: Content,
865
         item: Content,
866
         new_parent: Content=None,
866
         new_parent: Content=None,
867
         new_label: str=None,
867
         new_label: str=None,
868
-        do_save: bool=True,
869
         do_notify: bool=True,
868
         do_notify: bool=True,
870
     ) -> None:
869
     ) -> None:
871
         if (not new_parent and not new_label) or (new_parent == item.parent and new_label == item.label):  # nopep8
870
         if (not new_parent and not new_label) or (new_parent == item.parent and new_label == item.label):  # nopep8
883
         else:
882
         else:
884
             label = item.label
883
             label = item.label
885
 
884
 
886
-        with DBSession.no_autoflush:
885
+        # INFO - G.M - 15-03-2018 - Copy content with first_revision
886
+        # to have consistent content
887
+        content = Content()
888
+        cpy_rev = ContentRevisionRO.copy(item.first_revision, parent)
889
+        content.revisions.append(cpy_rev)
890
+        DBSession.add(content)
891
+
892
+        # INFO - G.M - 15-03-2018 - Add all older revision (history)
893
+        for rev in item.revisions:
894
+            if rev == item.first_revision:
895
+                continue
896
+            cpy_rev = ContentRevisionRO.copy(rev, parent)
897
+            content.revisions.append(cpy_rev)
898
+            DBSession.add(content)
887
 
899
 
888
-            file = self.create(
889
-                content_type=item.type,
890
-                workspace=workspace,
891
-                parent=parent,
892
-                label=label,
893
-                do_save=False,
894
-            )
895
-            file.description = item.description
896
-            if item.depot_file:
897
-                self.update_file_data(
898
-                    file,
899
-                    item.file_name,
900
-                    item.file_mimetype,
901
-                    item.depot_file.file
902
-                )
903
-            for child in item.get_comments():
904
-                self.copy(child,
905
-                          new_parent=file,
906
-                          do_notify=False,
907
-                          do_save=False,
908
-                )
909
-        if do_save:
910
-            self.save(file, ActionDescription.CREATION, do_notify=do_notify)
900
+        # INFO - G.M - 15-03-2018 - copy childrens (comments and others things)
901
+        for child in item.children:
902
+            self.copy(child, content)
903
+
904
+        # INFO - GM - 15-03-2018 - add "copy" revision
905
+        content.new_revision()
906
+        content.parent = parent
907
+        content.workspace = workspace
908
+        content.label = label
909
+        content.revision_type = ActionDescription.MOVE
910
+        DBSession.add(content)
911
+        self.save(content, ActionDescription.MOVE, do_notify=do_notify)
911
 
912
 
912
     def move_recursively(self, item: Content,
913
     def move_recursively(self, item: Content,
913
                          new_parent: Content, new_workspace: Workspace):
914
                          new_parent: Content, new_workspace: Workspace):

+ 30 - 0
tracim/tracim/model/data.py View File

640
 
640
 
641
         return new_rev
641
         return new_rev
642
 
642
 
643
+    @classmethod
644
+    def copy(
645
+            cls,
646
+            revision: 'ContentRevisionRO',
647
+            parent: 'Content'
648
+    ) -> 'ContentRevisionRO':
649
+
650
+        copy_rev = cls()
651
+        import copy
652
+        copy_columns = cls._cloned_columns
653
+        for column_name in copy_columns:
654
+            # INFO - G-M - 15-03-2018 - set correct parent
655
+            if column_name == 'parent_id':
656
+                column_value = copy.copy(parent.id)
657
+            elif column_name == 'parent':
658
+                column_value = copy.copy(parent)
659
+            else:
660
+                column_value = copy.copy(getattr(revision, column_name))
661
+            setattr(copy_rev, column_name, column_value)
662
+
663
+        # copy attached_file
664
+        if revision.depot_file:
665
+            copy_rev.depot_file = FileIntent(
666
+                revision.depot_file.file.read(),
667
+                revision.file_name,
668
+                revision.file_mimetype,
669
+            )
670
+        return copy_rev
671
+
672
+
643
     def __setattr__(self, key: str, value: 'mixed'):
673
     def __setattr__(self, key: str, value: 'mixed'):
644
         """
674
         """
645
         ContentRevisionUpdateError is raised if tried to update column and revision own identity
675
         ContentRevisionUpdateError is raised if tried to update column and revision own identity