|
@@ -16,6 +16,7 @@ from sqlalchemy.ext.hybrid import hybrid_property
|
16
|
16
|
from sqlalchemy.orm import backref
|
17
|
17
|
from sqlalchemy.orm import deferred
|
18
|
18
|
from sqlalchemy.orm import relationship
|
|
19
|
+from sqlalchemy.orm.attributes import InstrumentedAttribute
|
19
|
20
|
from sqlalchemy.orm.collections import attribute_mapped_collection
|
20
|
21
|
from sqlalchemy.types import Boolean
|
21
|
22
|
from sqlalchemy.types import DateTime
|
|
@@ -51,7 +52,9 @@ class Workspace(DeclarativeBase):
|
51
|
52
|
label = Column(Unicode(1024), unique=False, nullable=False, default='')
|
52
|
53
|
description = Column(Text(), unique=False, nullable=False, default='')
|
53
|
54
|
|
|
55
|
+ # Default value datetime.utcnow, see: http://stackoverflow.com/a/13370382/801924 (or http://pastebin.com/VLyWktUn)
|
54
|
56
|
created = Column(DateTime, unique=False, nullable=False, default=datetime.utcnow)
|
|
57
|
+ # Default value datetime.utcnow, see: http://stackoverflow.com/a/13370382/801924 (or http://pastebin.com/VLyWktUn)
|
55
|
58
|
updated = Column(DateTime, unique=False, nullable=False, default=datetime.utcnow)
|
56
|
59
|
|
57
|
60
|
is_deleted = Column(Boolean, unique=False, nullable=False, default=False)
|
|
@@ -515,13 +518,23 @@ class ContentRevisionRO(DeclarativeBase):
|
515
|
518
|
|
516
|
519
|
""" List of column copied when make a new revision from another """
|
517
|
520
|
_cloned_columns = (
|
518
|
|
- 'content_id', 'owner_id', 'label', 'description', 'file_name', 'file_mimetype', 'properties',
|
519
|
|
- 'file_content', 'type', 'status', 'created', 'updated', 'is_deleted', 'is_archived',
|
520
|
|
- 'revision_type', 'workspace_id', 'workspace', 'parent_id', 'parent', 'node', 'owner'
|
|
521
|
+ 'content_id', 'created', 'description', 'file_content', 'file_mimetype', 'file_name', 'is_archived',
|
|
522
|
+ 'is_deleted', 'label', 'node', 'owner' 'owner_id', 'parent', 'parent_id', 'properties', 'revision_type',
|
|
523
|
+ 'status', 'type', 'updated', 'workspace', 'workspace_id',
|
|
524
|
+ )
|
|
525
|
+
|
|
526
|
+ # Read by must be used like this:
|
|
527
|
+ # read_datetime = revision.ready_by[<User instance>]
|
|
528
|
+ # if user did not read the content, then a key error is raised
|
|
529
|
+ read_by = association_proxy(
|
|
530
|
+ 'revision_read_statuses', # name of the attribute
|
|
531
|
+ 'view_datetime', # attribute the value is taken from
|
|
532
|
+ creator=lambda k, v: \
|
|
533
|
+ RevisionReadStatus(user=k, view_datetime=v)
|
521
|
534
|
)
|
522
|
535
|
|
523
|
536
|
@classmethod
|
524
|
|
- def new_from(cls, revision):
|
|
537
|
+ def new_from(cls, revision: 'ContentRevisionRO') -> 'ContentRevisionRO':
|
525
|
538
|
"""
|
526
|
539
|
|
527
|
540
|
Return new instance of ContentRevisionRO where properties are copied from revision parameter.
|
|
@@ -542,7 +555,7 @@ class ContentRevisionRO(DeclarativeBase):
|
542
|
555
|
|
543
|
556
|
return new_rev
|
544
|
557
|
|
545
|
|
- def __setattr__(self, key, value):
|
|
558
|
+ def __setattr__(self, key: str, value: 'mixed'):
|
546
|
559
|
"""
|
547
|
560
|
ContentRevisionUpdateError is raised if tried to update column and revision own identity
|
548
|
561
|
:param key: attribute name
|
|
@@ -561,25 +574,15 @@ class ContentRevisionRO(DeclarativeBase):
|
561
|
574
|
|
562
|
575
|
super().__setattr__(key, value)
|
563
|
576
|
|
564
|
|
- def get_status(self):
|
|
577
|
+ def get_status(self) -> ContentStatus:
|
565
|
578
|
return ContentStatus(self.status)
|
566
|
579
|
|
567
|
|
- def get_label(self):
|
|
580
|
+ def get_label(self) -> str:
|
568
|
581
|
return self.label if self.label else self.file_name if self.file_name else ''
|
569
|
582
|
|
570
|
583
|
def get_last_action(self) -> ActionDescription:
|
571
|
584
|
return ActionDescription(self.revision_type)
|
572
|
585
|
|
573
|
|
- # Read by must be used like this:
|
574
|
|
- # read_datetime = revision.ready_by[<User instance>]
|
575
|
|
- # if user did not read the content, then a key error is raised
|
576
|
|
- read_by = association_proxy(
|
577
|
|
- 'revision_read_statuses', # name of the attribute
|
578
|
|
- 'view_datetime', # attribute the value is taken from
|
579
|
|
- creator=lambda k, v: \
|
580
|
|
- RevisionReadStatus(user=k, view_datetime=v)
|
581
|
|
- )
|
582
|
|
-
|
583
|
586
|
def has_new_information_for(self, user: User) -> bool:
|
584
|
587
|
"""
|
585
|
588
|
:param user: the session current user
|
|
@@ -621,9 +624,9 @@ class Content(DeclarativeBase):
|
621
|
624
|
.filter(Content.label == 'foo')
|
622
|
625
|
.one()
|
623
|
626
|
|
624
|
|
- ContentApi provide also prepared Content at tracim.lib.content.ContentApi#get_base_query:
|
|
627
|
+ ContentApi provide also prepared Content at tracim.lib.content.ContentApi#get_canonical_query:
|
625
|
628
|
|
626
|
|
- content = ContentApi.get_base_query()
|
|
629
|
+ content = ContentApi.get_canonical_query()
|
627
|
630
|
.filter(Content.label == 'foo')
|
628
|
631
|
.one()
|
629
|
632
|
"""
|
|
@@ -641,87 +644,87 @@ class Content(DeclarativeBase):
|
641
|
644
|
back_populates="parent")
|
642
|
645
|
|
643
|
646
|
@hybrid_property
|
644
|
|
- def content_id(self):
|
|
647
|
+ def content_id(self) -> int:
|
645
|
648
|
return self.revision.content_id
|
646
|
649
|
|
647
|
650
|
@content_id.setter
|
648
|
|
- def content_id(self, value):
|
|
651
|
+ def content_id(self, value: int) -> None:
|
649
|
652
|
self.revision.content_id = value
|
650
|
653
|
|
651
|
654
|
@content_id.expression
|
652
|
|
- def content_id(cls):
|
|
655
|
+ def content_id(cls) -> InstrumentedAttribute:
|
653
|
656
|
return ContentRevisionRO.content_id
|
654
|
657
|
|
655
|
658
|
@hybrid_property
|
656
|
|
- def revision_id(self):
|
|
659
|
+ def revision_id(self) -> int:
|
657
|
660
|
return self.revision.revision_id
|
658
|
661
|
|
659
|
662
|
@revision_id.setter
|
660
|
|
- def revision_id(self, value):
|
|
663
|
+ def revision_id(self, value: int):
|
661
|
664
|
self.revision.revision_id = value
|
662
|
665
|
|
663
|
666
|
@revision_id.expression
|
664
|
|
- def revision_id(cls):
|
|
667
|
+ def revision_id(cls) -> InstrumentedAttribute:
|
665
|
668
|
return ContentRevisionRO.revision_id
|
666
|
669
|
|
667
|
670
|
@hybrid_property
|
668
|
|
- def owner_id(self):
|
|
671
|
+ def owner_id(self) -> int:
|
669
|
672
|
return self.revision.owner_id
|
670
|
673
|
|
671
|
674
|
@owner_id.setter
|
672
|
|
- def owner_id(self, value):
|
|
675
|
+ def owner_id(self, value: int) -> None:
|
673
|
676
|
self.revision.owner_id = value
|
674
|
677
|
|
675
|
678
|
@owner_id.expression
|
676
|
|
- def owner_id(cls):
|
|
679
|
+ def owner_id(cls) -> InstrumentedAttribute:
|
677
|
680
|
return ContentRevisionRO.owner_id
|
678
|
681
|
|
679
|
682
|
@hybrid_property
|
680
|
|
- def label(self):
|
|
683
|
+ def label(self) -> str:
|
681
|
684
|
return self.revision.label
|
682
|
685
|
|
683
|
686
|
@label.setter
|
684
|
|
- def label(self, value):
|
|
687
|
+ def label(self, value: str) -> None:
|
685
|
688
|
self.revision.label = value
|
686
|
689
|
|
687
|
690
|
@label.expression
|
688
|
|
- def label(cls):
|
|
691
|
+ def label(cls) -> InstrumentedAttribute:
|
689
|
692
|
return ContentRevisionRO.label
|
690
|
693
|
|
691
|
694
|
@hybrid_property
|
692
|
|
- def description(self):
|
|
695
|
+ def description(self) -> str:
|
693
|
696
|
return self.revision.description
|
694
|
697
|
|
695
|
698
|
@description.setter
|
696
|
|
- def description(self, value):
|
|
699
|
+ def description(self, value: str) -> None:
|
697
|
700
|
self.revision.description = value
|
698
|
701
|
|
699
|
702
|
@description.expression
|
700
|
|
- def description(cls):
|
|
703
|
+ def description(cls) -> InstrumentedAttribute:
|
701
|
704
|
return ContentRevisionRO.description
|
702
|
705
|
|
703
|
706
|
@hybrid_property
|
704
|
|
- def file_name(self):
|
|
707
|
+ def file_name(self) -> str:
|
705
|
708
|
return self.revision.file_name
|
706
|
709
|
|
707
|
710
|
@file_name.setter
|
708
|
|
- def file_name(self, value):
|
|
711
|
+ def file_name(self, value: str) -> None:
|
709
|
712
|
self.revision.file_name = value
|
710
|
713
|
|
711
|
714
|
@file_name.expression
|
712
|
|
- def file_name(cls):
|
|
715
|
+ def file_name(cls) -> InstrumentedAttribute:
|
713
|
716
|
return ContentRevisionRO.file_name
|
714
|
717
|
|
715
|
718
|
@hybrid_property
|
716
|
|
- def file_mimetype(self):
|
|
719
|
+ def file_mimetype(self) -> str:
|
717
|
720
|
return self.revision.file_mimetype
|
718
|
721
|
|
719
|
722
|
@file_mimetype.setter
|
720
|
|
- def file_mimetype(self, value):
|
|
723
|
+ def file_mimetype(self, value: str) -> None:
|
721
|
724
|
self.revision.file_mimetype = value
|
722
|
725
|
|
723
|
726
|
@file_mimetype.expression
|
724
|
|
- def file_mimetype(cls):
|
|
727
|
+ def file_mimetype(cls) -> InstrumentedAttribute:
|
725
|
728
|
return ContentRevisionRO.file_mimetype
|
726
|
729
|
|
727
|
730
|
@hybrid_property
|
|
@@ -733,179 +736,179 @@ class Content(DeclarativeBase):
|
733
|
736
|
self.revision.file_content = value
|
734
|
737
|
|
735
|
738
|
@file_content.expression
|
736
|
|
- def file_content(cls):
|
|
739
|
+ def file_content(cls) -> InstrumentedAttribute:
|
737
|
740
|
return ContentRevisionRO.file_content
|
738
|
741
|
|
739
|
742
|
@hybrid_property
|
740
|
|
- def _properties(self):
|
|
743
|
+ def _properties(self) -> str:
|
741
|
744
|
return self.revision.properties
|
742
|
745
|
|
743
|
746
|
@_properties.setter
|
744
|
|
- def _properties(self, value):
|
|
747
|
+ def _properties(self, value: str) -> None:
|
745
|
748
|
self.revision.properties = value
|
746
|
749
|
|
747
|
750
|
@_properties.expression
|
748
|
|
- def _properties(cls):
|
|
751
|
+ def _properties(cls) -> InstrumentedAttribute:
|
749
|
752
|
return ContentRevisionRO.properties
|
750
|
753
|
|
751
|
754
|
@hybrid_property
|
752
|
|
- def type(self):
|
|
755
|
+ def type(self) -> str:
|
753
|
756
|
return self.revision.type
|
754
|
757
|
|
755
|
758
|
@type.setter
|
756
|
|
- def type(self, value):
|
|
759
|
+ def type(self, value: str) -> None:
|
757
|
760
|
self.revision.type = value
|
758
|
761
|
|
759
|
762
|
@type.expression
|
760
|
|
- def type(cls):
|
|
763
|
+ def type(cls) -> InstrumentedAttribute:
|
761
|
764
|
return ContentRevisionRO.type
|
762
|
765
|
|
763
|
766
|
@hybrid_property
|
764
|
|
- def status(self):
|
|
767
|
+ def status(self) -> str:
|
765
|
768
|
return self.revision.status
|
766
|
769
|
|
767
|
770
|
@status.setter
|
768
|
|
- def status(self, value):
|
|
771
|
+ def status(self, value: str) -> None:
|
769
|
772
|
self.revision.status = value
|
770
|
773
|
|
771
|
774
|
@status.expression
|
772
|
|
- def status(cls):
|
|
775
|
+ def status(cls) -> InstrumentedAttribute:
|
773
|
776
|
return ContentRevisionRO.status
|
774
|
777
|
|
775
|
778
|
@hybrid_property
|
776
|
|
- def created(self):
|
|
779
|
+ def created(self) -> datetime:
|
777
|
780
|
return self.revision.created
|
778
|
781
|
|
779
|
782
|
@created.setter
|
780
|
|
- def created(self, value):
|
|
783
|
+ def created(self, value: datetime) -> None:
|
781
|
784
|
self.revision.created = value
|
782
|
785
|
|
783
|
786
|
@created.expression
|
784
|
|
- def created(cls):
|
|
787
|
+ def created(cls) -> InstrumentedAttribute:
|
785
|
788
|
return ContentRevisionRO.created
|
786
|
789
|
|
787
|
790
|
@hybrid_property
|
788
|
|
- def updated(self):
|
|
791
|
+ def updated(self) -> datetime:
|
789
|
792
|
return self.revision.updated
|
790
|
793
|
|
791
|
794
|
@updated.setter
|
792
|
|
- def updated(self, value):
|
|
795
|
+ def updated(self, value: datetime) -> None:
|
793
|
796
|
self.revision.updated = value
|
794
|
797
|
|
795
|
798
|
@updated.expression
|
796
|
|
- def updated(cls):
|
|
799
|
+ def updated(cls) -> InstrumentedAttribute:
|
797
|
800
|
return ContentRevisionRO.updated
|
798
|
801
|
|
799
|
802
|
@hybrid_property
|
800
|
|
- def is_deleted(self):
|
|
803
|
+ def is_deleted(self) -> bool:
|
801
|
804
|
return self.revision.is_deleted
|
802
|
805
|
|
803
|
806
|
@is_deleted.setter
|
804
|
|
- def is_deleted(self, value):
|
|
807
|
+ def is_deleted(self, value: bool) -> None:
|
805
|
808
|
self.revision.is_deleted = value
|
806
|
809
|
|
807
|
810
|
@is_deleted.expression
|
808
|
|
- def is_deleted(cls):
|
|
811
|
+ def is_deleted(cls) -> InstrumentedAttribute:
|
809
|
812
|
return ContentRevisionRO.is_deleted
|
810
|
813
|
|
811
|
814
|
@hybrid_property
|
812
|
|
- def is_archived(self):
|
|
815
|
+ def is_archived(self) -> bool:
|
813
|
816
|
return self.revision.is_archived
|
814
|
817
|
|
815
|
818
|
@is_archived.setter
|
816
|
|
- def is_archived(self, value):
|
|
819
|
+ def is_archived(self, value: bool) -> None:
|
817
|
820
|
self.revision.is_archived = value
|
818
|
821
|
|
819
|
822
|
@is_archived.expression
|
820
|
|
- def is_archived(cls):
|
|
823
|
+ def is_archived(cls) -> InstrumentedAttribute:
|
821
|
824
|
return ContentRevisionRO.is_archived
|
822
|
825
|
|
823
|
826
|
@hybrid_property
|
824
|
|
- def revision_type(self):
|
|
827
|
+ def revision_type(self) -> str:
|
825
|
828
|
return self.revision.revision_type
|
826
|
829
|
|
827
|
830
|
@revision_type.setter
|
828
|
|
- def revision_type(self, value):
|
|
831
|
+ def revision_type(self, value: str) -> None:
|
829
|
832
|
self.revision.revision_type = value
|
830
|
833
|
|
831
|
834
|
@revision_type.expression
|
832
|
|
- def revision_type(cls):
|
|
835
|
+ def revision_type(cls) -> InstrumentedAttribute:
|
833
|
836
|
return ContentRevisionRO.revision_type
|
834
|
837
|
|
835
|
838
|
@hybrid_property
|
836
|
|
- def workspace_id(self):
|
|
839
|
+ def workspace_id(self) -> int:
|
837
|
840
|
return self.revision.workspace_id
|
838
|
841
|
|
839
|
842
|
@workspace_id.setter
|
840
|
|
- def workspace_id(self, value):
|
|
843
|
+ def workspace_id(self, value: int) -> None:
|
841
|
844
|
self.revision.workspace_id = value
|
842
|
845
|
|
843
|
846
|
@workspace_id.expression
|
844
|
|
- def workspace_id(cls):
|
|
847
|
+ def workspace_id(cls) -> InstrumentedAttribute:
|
845
|
848
|
return ContentRevisionRO.workspace_id
|
846
|
849
|
|
847
|
850
|
@hybrid_property
|
848
|
|
- def workspace(self):
|
|
851
|
+ def workspace(self) -> Workspace:
|
849
|
852
|
return self.revision.workspace
|
850
|
853
|
|
851
|
854
|
@workspace.setter
|
852
|
|
- def workspace(self, value):
|
|
855
|
+ def workspace(self, value: Workspace) -> None:
|
853
|
856
|
self.revision.workspace = value
|
854
|
857
|
|
855
|
858
|
@workspace.expression
|
856
|
|
- def workspace(cls):
|
|
859
|
+ def workspace(cls) -> InstrumentedAttribute:
|
857
|
860
|
return ContentRevisionRO.workspace
|
858
|
861
|
|
859
|
862
|
@hybrid_property
|
860
|
|
- def parent_id(self):
|
|
863
|
+ def parent_id(self) -> int:
|
861
|
864
|
return self.revision.parent_id
|
862
|
865
|
|
863
|
866
|
@parent_id.setter
|
864
|
|
- def parent_id(self, value):
|
|
867
|
+ def parent_id(self, value: int) -> None:
|
865
|
868
|
self.revision.parent_id = value
|
866
|
869
|
|
867
|
870
|
@parent_id.expression
|
868
|
|
- def parent_id(cls):
|
|
871
|
+ def parent_id(cls) -> InstrumentedAttribute:
|
869
|
872
|
return ContentRevisionRO.parent_id
|
870
|
873
|
|
871
|
874
|
@hybrid_property
|
872
|
|
- def parent(self):
|
|
875
|
+ def parent(self) -> 'Content':
|
873
|
876
|
return self.revision.parent
|
874
|
877
|
|
875
|
878
|
@parent.setter
|
876
|
|
- def parent(self, value):
|
|
879
|
+ def parent(self, value: 'Content') -> None:
|
877
|
880
|
self.revision.parent = value
|
878
|
881
|
|
879
|
882
|
@parent.expression
|
880
|
|
- def parent(cls):
|
|
883
|
+ def parent(cls) -> InstrumentedAttribute:
|
881
|
884
|
return ContentRevisionRO.parent
|
882
|
885
|
|
883
|
886
|
@hybrid_property
|
884
|
|
- def node(self):
|
|
887
|
+ def node(self) -> 'Content':
|
885
|
888
|
return self.revision.node
|
886
|
889
|
|
887
|
890
|
@node.setter
|
888
|
|
- def node(self, value):
|
|
891
|
+ def node(self, value: 'Content') -> None:
|
889
|
892
|
self.revision.node = value
|
890
|
893
|
|
891
|
894
|
@node.expression
|
892
|
|
- def node(cls):
|
|
895
|
+ def node(cls) -> InstrumentedAttribute:
|
893
|
896
|
return ContentRevisionRO.node
|
894
|
897
|
|
895
|
898
|
@hybrid_property
|
896
|
|
- def owner(self):
|
|
899
|
+ def owner(self) -> User:
|
897
|
900
|
return self.revision.owner
|
898
|
901
|
|
899
|
902
|
@owner.setter
|
900
|
|
- def owner(self, value):
|
|
903
|
+ def owner(self, value: User) -> None:
|
901
|
904
|
self.revision.owner = value
|
902
|
905
|
|
903
|
906
|
@owner.expression
|
904
|
|
- def owner(cls):
|
|
907
|
+ def owner(cls) -> InstrumentedAttribute:
|
905
|
908
|
return ContentRevisionRO.owner
|
906
|
909
|
|
907
|
910
|
@hybrid_property
|
908
|
|
- def children(self):
|
|
911
|
+ def children(self) -> ['Content']:
|
909
|
912
|
"""
|
910
|
913
|
:return: list of children Content
|
911
|
914
|
:rtype Content
|
|
@@ -914,10 +917,10 @@ class Content(DeclarativeBase):
|
914
|
917
|
return list(set([revision.node for revision in self.children_revisions]))
|
915
|
918
|
|
916
|
919
|
@property
|
917
|
|
- def revision(self):
|
|
920
|
+ def revision(self) -> ContentRevisionRO:
|
918
|
921
|
return self.get_current_revision()
|
919
|
922
|
|
920
|
|
- def get_current_revision(self):
|
|
923
|
+ def get_current_revision(self) -> ContentRevisionRO:
|
921
|
924
|
if not self.revisions:
|
922
|
925
|
return self.new_revision()
|
923
|
926
|
|
|
@@ -929,7 +932,7 @@ class Content(DeclarativeBase):
|
929
|
932
|
revisions = sorted(self.revisions, key=lambda revision: revision.revision_id)
|
930
|
933
|
return revisions[-1]
|
931
|
934
|
|
932
|
|
- def new_revision(self):
|
|
935
|
+ def new_revision(self) -> None:
|
933
|
936
|
"""
|
934
|
937
|
Return and assign to this content a new revision.
|
935
|
938
|
If it's a new content, revision is totally new.
|
|
@@ -951,14 +954,14 @@ class Content(DeclarativeBase):
|
951
|
954
|
yield child.node
|
952
|
955
|
|
953
|
956
|
@hybrid_property
|
954
|
|
- def properties(self):
|
|
957
|
+ def properties(self) -> dict:
|
955
|
958
|
""" return a structure decoded from json content of _properties """
|
956
|
959
|
if not self._properties:
|
957
|
960
|
ContentChecker.reset_properties(self)
|
958
|
961
|
return json.loads(self._properties)
|
959
|
962
|
|
960
|
963
|
@properties.setter
|
961
|
|
- def properties(self, properties_struct):
|
|
964
|
+ def properties(self, properties_struct: dict) -> None:
|
962
|
965
|
""" encode a given structure into json and store it in _properties attribute"""
|
963
|
966
|
self._properties = json.dumps(properties_struct)
|
964
|
967
|
ContentChecker.check_properties(self)
|
|
@@ -1130,7 +1133,8 @@ class RevisionReadStatus(DeclarativeBase):
|
1130
|
1133
|
|
1131
|
1134
|
revision_id = Column(Integer, ForeignKey('content_revisions.revision_id', ondelete='CASCADE', onupdate='CASCADE'), primary_key=True)
|
1132
|
1135
|
user_id = Column(Integer, ForeignKey('users.user_id', ondelete='CASCADE', onupdate='CASCADE'), primary_key=True)
|
1133
|
|
- view_datetime = Column(DateTime, unique=False, nullable=False, server_default=func.now())
|
|
1136
|
+ # Default value datetime.utcnow, see: http://stackoverflow.com/a/13370382/801924 (or http://pastebin.com/VLyWktUn)
|
|
1137
|
+ view_datetime = Column(DateTime, unique=False, nullable=False, default=datetime.utcnow)
|
1134
|
1138
|
|
1135
|
1139
|
content_revision = relationship(
|
1136
|
1140
|
'ContentRevisionRO',
|