|
@@ -1,38 +1,36 @@
|
1
|
1
|
# -*- coding: utf-8 -*-
|
2
|
2
|
|
3
|
|
-import tg
|
4
|
|
-from datetime import datetime
|
5
|
|
-from babel.dates import format_timedelta
|
6
|
|
-
|
7
|
|
-from bs4 import BeautifulSoup
|
8
|
3
|
import datetime as datetime_root
|
9
|
4
|
import json
|
|
5
|
+from datetime import datetime
|
10
|
6
|
|
11
|
|
-from sqlalchemy.ext.associationproxy import association_proxy
|
12
|
|
-from sqlalchemy import Column
|
13
|
|
-from sqlalchemy import func
|
|
7
|
+import tg
|
|
8
|
+from babel.dates import format_timedelta
|
|
9
|
+from bs4 import BeautifulSoup
|
|
10
|
+from sqlalchemy import Column, inspect, Index
|
14
|
11
|
from sqlalchemy import ForeignKey
|
15
|
12
|
from sqlalchemy import Sequence
|
16
|
|
-
|
|
13
|
+from sqlalchemy import func
|
|
14
|
+from sqlalchemy.ext.associationproxy import association_proxy
|
17
|
15
|
from sqlalchemy.ext.hybrid import hybrid_property
|
18
|
|
-
|
19
|
16
|
from sqlalchemy.orm import backref
|
20
|
|
-from sqlalchemy.orm import relationship
|
21
|
17
|
from sqlalchemy.orm import deferred
|
|
18
|
+from sqlalchemy.orm import relationship
|
|
19
|
+from sqlalchemy.orm.attributes import InstrumentedAttribute
|
22
|
20
|
from sqlalchemy.orm.collections import attribute_mapped_collection
|
23
|
|
-
|
24
|
21
|
from sqlalchemy.types import Boolean
|
25
|
22
|
from sqlalchemy.types import DateTime
|
26
|
23
|
from sqlalchemy.types import Integer
|
27
|
24
|
from sqlalchemy.types import LargeBinary
|
28
|
25
|
from sqlalchemy.types import Text
|
29
|
26
|
from sqlalchemy.types import Unicode
|
30
|
|
-
|
31
|
27
|
from tg.i18n import lazy_ugettext as l_, ugettext as _
|
32
|
28
|
|
33
|
|
-from tracim.model import DeclarativeBase
|
|
29
|
+from tracim.lib.exception import ContentRevisionUpdateError
|
|
30
|
+from tracim.model import DeclarativeBase, RevisionsIntegrity
|
34
|
31
|
from tracim.model.auth import User
|
35
|
32
|
|
|
33
|
+
|
36
|
34
|
class BreadcrumbItem(object):
|
37
|
35
|
|
38
|
36
|
def __init__(self, icon_string: str, label: str, url: str, is_active: bool = False):
|
|
@@ -54,11 +52,20 @@ class Workspace(DeclarativeBase):
|
54
|
52
|
label = Column(Unicode(1024), unique=False, nullable=False, default='')
|
55
|
53
|
description = Column(Text(), unique=False, nullable=False, default='')
|
56
|
54
|
|
57
|
|
- created = Column(DateTime, unique=False, nullable=False)
|
58
|
|
- updated = Column(DateTime, unique=False, nullable=False)
|
|
55
|
+ # Default value datetime.utcnow, see: http://stackoverflow.com/a/13370382/801924 (or http://pastebin.com/VLyWktUn)
|
|
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)
|
|
58
|
+ updated = Column(DateTime, unique=False, nullable=False, default=datetime.utcnow)
|
59
|
59
|
|
60
|
60
|
is_deleted = Column(Boolean, unique=False, nullable=False, default=False)
|
61
|
61
|
|
|
62
|
+ revisions = relationship("ContentRevisionRO")
|
|
63
|
+
|
|
64
|
+ @hybrid_property
|
|
65
|
+ def contents(self):
|
|
66
|
+ # Return a list of unique revisions parent content
|
|
67
|
+ return list(set([revision.node for revision in self.revisions]))
|
|
68
|
+
|
62
|
69
|
def get_user_role(self, user: User) -> int:
|
63
|
70
|
for role in user.roles:
|
64
|
71
|
if role.workspace.workspace_id==self.workspace_id:
|
|
@@ -435,58 +442,526 @@ class ContentType(object):
|
435
|
442
|
label=self.label,
|
436
|
443
|
priority=self.priority)
|
437
|
444
|
|
438
|
|
-class Content(DeclarativeBase):
|
439
|
445
|
|
440
|
|
- __tablename__ = 'contents'
|
|
446
|
+class ContentChecker(object):
|
441
|
447
|
|
442
|
|
- revision_to_serialize = -0 # This flag allow to serialize a given revision if required by the user
|
|
448
|
+ @classmethod
|
|
449
|
+ def check_properties(cls, item):
|
|
450
|
+ if item.type==ContentType.Folder:
|
|
451
|
+ properties = item.properties
|
|
452
|
+ if 'allowed_content' not in properties.keys():
|
|
453
|
+ return False
|
|
454
|
+ if 'folders' not in properties['allowed_content']:
|
|
455
|
+ return False
|
|
456
|
+ if 'files' not in properties['allowed_content']:
|
|
457
|
+ return False
|
|
458
|
+ if 'pages' not in properties['allowed_content']:
|
|
459
|
+ return False
|
|
460
|
+ if 'threads' not in properties['allowed_content']:
|
|
461
|
+ return False
|
443
|
462
|
|
444
|
|
- content_id = Column(Integer, Sequence('seq__contents__content_id'), autoincrement=True, primary_key=True)
|
445
|
|
- parent_id = Column(Integer, ForeignKey('contents.content_id'), nullable=True, default=None)
|
446
|
|
- owner_id = Column(Integer, ForeignKey('users.user_id'), nullable=True, default=None)
|
|
463
|
+ return True
|
447
|
464
|
|
448
|
|
- type = Column(Unicode(32), unique=False, nullable=False)
|
449
|
|
- status = Column(Unicode(32), unique=False, nullable=False, default=ContentStatus.OPEN)
|
|
465
|
+ raise NotImplementedError
|
450
|
466
|
|
451
|
|
- created = Column(DateTime, unique=False, nullable=False)
|
452
|
|
- updated = Column(DateTime, unique=False, nullable=False)
|
|
467
|
+ @classmethod
|
|
468
|
+ def reset_properties(cls, item):
|
|
469
|
+ if item.type==ContentType.Folder:
|
|
470
|
+ item.properties = dict(
|
|
471
|
+ allowed_content = dict (
|
|
472
|
+ folder = True,
|
|
473
|
+ file = True,
|
|
474
|
+ page = True,
|
|
475
|
+ thread = True
|
|
476
|
+ )
|
|
477
|
+ )
|
|
478
|
+ return
|
453
|
479
|
|
454
|
|
- workspace_id = Column(Integer, ForeignKey('workspaces.workspace_id'), unique=False, nullable=True)
|
|
480
|
+ raise NotImplementedError
|
455
|
481
|
|
456
|
|
- workspace = relationship('Workspace', remote_side=[Workspace.workspace_id], backref='contents')
|
457
|
482
|
|
|
483
|
+class ContentRevisionRO(DeclarativeBase):
|
|
484
|
+ """
|
|
485
|
+ Revision of Content. It's immutable, update or delete an existing ContentRevisionRO will throw
|
|
486
|
+ ContentRevisionUpdateError errors.
|
|
487
|
+ """
|
458
|
488
|
|
459
|
|
- is_deleted = Column(Boolean, unique=False, nullable=False, default=False)
|
460
|
|
- is_archived = Column(Boolean, unique=False, nullable=False, default=False)
|
|
489
|
+ __tablename__ = 'content_revisions'
|
461
|
490
|
|
462
|
|
- label = Column(Unicode(1024), unique=False, nullable=False, default='')
|
463
|
|
- description = Column(Text(), unique=False, nullable=False, default='')
|
464
|
|
- _properties = Column('properties', Text(), unique=False, nullable=False, default='')
|
|
491
|
+ revision_id = Column(Integer, primary_key=True)
|
|
492
|
+ content_id = Column(Integer, ForeignKey('content.id'), nullable=False)
|
|
493
|
+ owner_id = Column(Integer, ForeignKey('users.user_id'), nullable=True)
|
465
|
494
|
|
|
495
|
+ label = Column(Unicode(1024), unique=False, nullable=False)
|
|
496
|
+ description = Column(Text(), unique=False, nullable=False, default='')
|
466
|
497
|
file_name = Column(Unicode(255), unique=False, nullable=False, default='')
|
467
|
498
|
file_mimetype = Column(Unicode(255), unique=False, nullable=False, default='')
|
468
|
|
- file_content = deferred(Column(LargeBinary(), unique=False, nullable=False, default=None))
|
|
499
|
+ file_content = deferred(Column(LargeBinary(), unique=False, nullable=True))
|
|
500
|
+ properties = Column('properties', Text(), unique=False, nullable=False, default='')
|
469
|
501
|
|
|
502
|
+ type = Column(Unicode(32), unique=False, nullable=False)
|
|
503
|
+ status = Column(Unicode(32), unique=False, nullable=False, default=ContentStatus.OPEN)
|
|
504
|
+ created = Column(DateTime, unique=False, nullable=False, default=datetime.utcnow)
|
|
505
|
+ updated = Column(DateTime, unique=False, nullable=False, default=datetime.utcnow)
|
|
506
|
+ is_deleted = Column(Boolean, unique=False, nullable=False, default=False)
|
|
507
|
+ is_archived = Column(Boolean, unique=False, nullable=False, default=False)
|
470
|
508
|
revision_type = Column(Unicode(32), unique=False, nullable=False, default='')
|
471
|
509
|
|
472
|
|
- parent = relationship('Content', remote_side=[content_id], backref='children')
|
|
510
|
+ workspace_id = Column(Integer, ForeignKey('workspaces.workspace_id'), unique=False, nullable=True)
|
|
511
|
+ workspace = relationship('Workspace', remote_side=[Workspace.workspace_id])
|
|
512
|
+
|
|
513
|
+ parent_id = Column(Integer, ForeignKey('content.id'), nullable=True, default=None)
|
|
514
|
+ parent = relationship("Content", foreign_keys=[parent_id], back_populates="children_revisions")
|
|
515
|
+
|
|
516
|
+ node = relationship("Content", foreign_keys=[content_id], back_populates="revisions")
|
473
|
517
|
owner = relationship('User', remote_side=[User.user_id])
|
474
|
518
|
|
|
519
|
+ """ List of column copied when make a new revision from another """
|
|
520
|
+ _cloned_columns = (
|
|
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)
|
|
534
|
+ )
|
|
535
|
+
|
|
536
|
+ @classmethod
|
|
537
|
+ def new_from(cls, revision: 'ContentRevisionRO') -> 'ContentRevisionRO':
|
|
538
|
+ """
|
|
539
|
+
|
|
540
|
+ Return new instance of ContentRevisionRO where properties are copied from revision parameter.
|
|
541
|
+ Look at ContentRevisionRO._cloned_columns to see what columns are copieds.
|
|
542
|
+
|
|
543
|
+ :param revision: revision to copy
|
|
544
|
+ :type revision: ContentRevisionRO
|
|
545
|
+ :return: new revision from revision parameter
|
|
546
|
+ :rtype: ContentRevisionRO
|
|
547
|
+ """
|
|
548
|
+ new_rev = cls()
|
|
549
|
+
|
|
550
|
+ for column_name in cls._cloned_columns:
|
|
551
|
+ column_value = getattr(revision, column_name)
|
|
552
|
+ setattr(new_rev, column_name, column_value)
|
|
553
|
+
|
|
554
|
+ new_rev.updated = datetime.now()
|
|
555
|
+
|
|
556
|
+ return new_rev
|
|
557
|
+
|
|
558
|
+ def __setattr__(self, key: str, value: 'mixed'):
|
|
559
|
+ """
|
|
560
|
+ ContentRevisionUpdateError is raised if tried to update column and revision own identity
|
|
561
|
+ :param key: attribute name
|
|
562
|
+ :param value: attribute value
|
|
563
|
+ :return:
|
|
564
|
+ """
|
|
565
|
+ if key in ('_sa_instance_state', ): # Prevent infinite loop from SQLAlchemy code and altered set
|
|
566
|
+ return super().__setattr__(key, value)
|
|
567
|
+
|
|
568
|
+ if inspect(self).has_identity \
|
|
569
|
+ and key in self._cloned_columns \
|
|
570
|
+ and not RevisionsIntegrity.is_updatable(self):
|
|
571
|
+ raise ContentRevisionUpdateError(
|
|
572
|
+ "Can't modify revision. To work on new revision use tracim.model.new_revision " +
|
|
573
|
+ "context manager.")
|
|
574
|
+
|
|
575
|
+ super().__setattr__(key, value)
|
|
576
|
+
|
|
577
|
+ def get_status(self) -> ContentStatus:
|
|
578
|
+ return ContentStatus(self.status)
|
|
579
|
+
|
|
580
|
+ def get_label(self) -> str:
|
|
581
|
+ return self.label if self.label else self.file_name if self.file_name else ''
|
|
582
|
+
|
|
583
|
+ def get_last_action(self) -> ActionDescription:
|
|
584
|
+ return ActionDescription(self.revision_type)
|
|
585
|
+
|
|
586
|
+ def has_new_information_for(self, user: User) -> bool:
|
|
587
|
+ """
|
|
588
|
+ :param user: the session current user
|
|
589
|
+ :return: bool, True if there is new information for given user else False
|
|
590
|
+ False if the user is None
|
|
591
|
+ """
|
|
592
|
+ if not user:
|
|
593
|
+ return False
|
|
594
|
+
|
|
595
|
+ if user not in self.read_by.keys():
|
|
596
|
+ return True
|
|
597
|
+
|
|
598
|
+ return False
|
|
599
|
+
|
|
600
|
+Index('idx__content_revisions__owner_id', ContentRevisionRO.owner_id)
|
|
601
|
+Index('idx__content_revisions__parent_id', ContentRevisionRO.parent_id)
|
|
602
|
+
|
|
603
|
+
|
|
604
|
+class Content(DeclarativeBase):
|
|
605
|
+ """
|
|
606
|
+ Content is used as a virtual representation of ContentRevisionRO.
|
|
607
|
+ content.PROPERTY (except for content.id, content.revisions, content.children_revisions) will return
|
|
608
|
+ value of most recent revision of content.
|
|
609
|
+
|
|
610
|
+ # UPDATE A CONTENT
|
|
611
|
+
|
|
612
|
+ To update an existing Content, you must use tracim.model.new_revision context manager:
|
|
613
|
+ content = my_sontent_getter_method()
|
|
614
|
+ with new_revision(content):
|
|
615
|
+ content.description = 'foo bar baz'
|
|
616
|
+ DBSession.flush()
|
|
617
|
+
|
|
618
|
+ # QUERY CONTENTS
|
|
619
|
+
|
|
620
|
+ To query contents you will need to join your content query with ContentRevisionRO. Join
|
|
621
|
+ condition is available at tracim.lib.content.ContentApi#get_revision_join:
|
|
622
|
+
|
|
623
|
+ content = DBSession.query(Content).join(ContentRevisionRO, ContentApi.get_revision_join())
|
|
624
|
+ .filter(Content.label == 'foo')
|
|
625
|
+ .one()
|
|
626
|
+
|
|
627
|
+ ContentApi provide also prepared Content at tracim.lib.content.ContentApi#get_canonical_query:
|
|
628
|
+
|
|
629
|
+ content = ContentApi.get_canonical_query()
|
|
630
|
+ .filter(Content.label == 'foo')
|
|
631
|
+ .one()
|
|
632
|
+ """
|
|
633
|
+
|
|
634
|
+ __tablename__ = 'content'
|
|
635
|
+
|
|
636
|
+ revision_to_serialize = -0 # This flag allow to serialize a given revision if required by the user
|
|
637
|
+
|
|
638
|
+ id = Column(Integer, primary_key=True)
|
|
639
|
+ revisions = relationship("ContentRevisionRO",
|
|
640
|
+ foreign_keys=[ContentRevisionRO.content_id],
|
|
641
|
+ back_populates="node")
|
|
642
|
+ children_revisions = relationship("ContentRevisionRO",
|
|
643
|
+ foreign_keys=[ContentRevisionRO.parent_id],
|
|
644
|
+ back_populates="parent")
|
|
645
|
+
|
|
646
|
+ @hybrid_property
|
|
647
|
+ def content_id(self) -> int:
|
|
648
|
+ return self.revision.content_id
|
|
649
|
+
|
|
650
|
+ @content_id.setter
|
|
651
|
+ def content_id(self, value: int) -> None:
|
|
652
|
+ self.revision.content_id = value
|
|
653
|
+
|
|
654
|
+ @content_id.expression
|
|
655
|
+ def content_id(cls) -> InstrumentedAttribute:
|
|
656
|
+ return ContentRevisionRO.content_id
|
|
657
|
+
|
|
658
|
+ @hybrid_property
|
|
659
|
+ def revision_id(self) -> int:
|
|
660
|
+ return self.revision.revision_id
|
|
661
|
+
|
|
662
|
+ @revision_id.setter
|
|
663
|
+ def revision_id(self, value: int):
|
|
664
|
+ self.revision.revision_id = value
|
|
665
|
+
|
|
666
|
+ @revision_id.expression
|
|
667
|
+ def revision_id(cls) -> InstrumentedAttribute:
|
|
668
|
+ return ContentRevisionRO.revision_id
|
|
669
|
+
|
|
670
|
+ @hybrid_property
|
|
671
|
+ def owner_id(self) -> int:
|
|
672
|
+ return self.revision.owner_id
|
|
673
|
+
|
|
674
|
+ @owner_id.setter
|
|
675
|
+ def owner_id(self, value: int) -> None:
|
|
676
|
+ self.revision.owner_id = value
|
|
677
|
+
|
|
678
|
+ @owner_id.expression
|
|
679
|
+ def owner_id(cls) -> InstrumentedAttribute:
|
|
680
|
+ return ContentRevisionRO.owner_id
|
|
681
|
+
|
|
682
|
+ @hybrid_property
|
|
683
|
+ def label(self) -> str:
|
|
684
|
+ return self.revision.label
|
|
685
|
+
|
|
686
|
+ @label.setter
|
|
687
|
+ def label(self, value: str) -> None:
|
|
688
|
+ self.revision.label = value
|
|
689
|
+
|
|
690
|
+ @label.expression
|
|
691
|
+ def label(cls) -> InstrumentedAttribute:
|
|
692
|
+ return ContentRevisionRO.label
|
|
693
|
+
|
|
694
|
+ @hybrid_property
|
|
695
|
+ def description(self) -> str:
|
|
696
|
+ return self.revision.description
|
|
697
|
+
|
|
698
|
+ @description.setter
|
|
699
|
+ def description(self, value: str) -> None:
|
|
700
|
+ self.revision.description = value
|
|
701
|
+
|
|
702
|
+ @description.expression
|
|
703
|
+ def description(cls) -> InstrumentedAttribute:
|
|
704
|
+ return ContentRevisionRO.description
|
|
705
|
+
|
|
706
|
+ @hybrid_property
|
|
707
|
+ def file_name(self) -> str:
|
|
708
|
+ return self.revision.file_name
|
|
709
|
+
|
|
710
|
+ @file_name.setter
|
|
711
|
+ def file_name(self, value: str) -> None:
|
|
712
|
+ self.revision.file_name = value
|
|
713
|
+
|
|
714
|
+ @file_name.expression
|
|
715
|
+ def file_name(cls) -> InstrumentedAttribute:
|
|
716
|
+ return ContentRevisionRO.file_name
|
|
717
|
+
|
|
718
|
+ @hybrid_property
|
|
719
|
+ def file_mimetype(self) -> str:
|
|
720
|
+ return self.revision.file_mimetype
|
|
721
|
+
|
|
722
|
+ @file_mimetype.setter
|
|
723
|
+ def file_mimetype(self, value: str) -> None:
|
|
724
|
+ self.revision.file_mimetype = value
|
|
725
|
+
|
|
726
|
+ @file_mimetype.expression
|
|
727
|
+ def file_mimetype(cls) -> InstrumentedAttribute:
|
|
728
|
+ return ContentRevisionRO.file_mimetype
|
|
729
|
+
|
|
730
|
+ @hybrid_property
|
|
731
|
+ def file_content(self):
|
|
732
|
+ return self.revision.file_content
|
|
733
|
+
|
|
734
|
+ @file_content.setter
|
|
735
|
+ def file_content(self, value):
|
|
736
|
+ self.revision.file_content = value
|
|
737
|
+
|
|
738
|
+ @file_content.expression
|
|
739
|
+ def file_content(cls) -> InstrumentedAttribute:
|
|
740
|
+ return ContentRevisionRO.file_content
|
|
741
|
+
|
|
742
|
+ @hybrid_property
|
|
743
|
+ def _properties(self) -> str:
|
|
744
|
+ return self.revision.properties
|
|
745
|
+
|
|
746
|
+ @_properties.setter
|
|
747
|
+ def _properties(self, value: str) -> None:
|
|
748
|
+ self.revision.properties = value
|
|
749
|
+
|
|
750
|
+ @_properties.expression
|
|
751
|
+ def _properties(cls) -> InstrumentedAttribute:
|
|
752
|
+ return ContentRevisionRO.properties
|
|
753
|
+
|
|
754
|
+ @hybrid_property
|
|
755
|
+ def type(self) -> str:
|
|
756
|
+ return self.revision.type
|
|
757
|
+
|
|
758
|
+ @type.setter
|
|
759
|
+ def type(self, value: str) -> None:
|
|
760
|
+ self.revision.type = value
|
|
761
|
+
|
|
762
|
+ @type.expression
|
|
763
|
+ def type(cls) -> InstrumentedAttribute:
|
|
764
|
+ return ContentRevisionRO.type
|
|
765
|
+
|
|
766
|
+ @hybrid_property
|
|
767
|
+ def status(self) -> str:
|
|
768
|
+ return self.revision.status
|
|
769
|
+
|
|
770
|
+ @status.setter
|
|
771
|
+ def status(self, value: str) -> None:
|
|
772
|
+ self.revision.status = value
|
|
773
|
+
|
|
774
|
+ @status.expression
|
|
775
|
+ def status(cls) -> InstrumentedAttribute:
|
|
776
|
+ return ContentRevisionRO.status
|
|
777
|
+
|
|
778
|
+ @hybrid_property
|
|
779
|
+ def created(self) -> datetime:
|
|
780
|
+ return self.revision.created
|
|
781
|
+
|
|
782
|
+ @created.setter
|
|
783
|
+ def created(self, value: datetime) -> None:
|
|
784
|
+ self.revision.created = value
|
|
785
|
+
|
|
786
|
+ @created.expression
|
|
787
|
+ def created(cls) -> InstrumentedAttribute:
|
|
788
|
+ return ContentRevisionRO.created
|
|
789
|
+
|
|
790
|
+ @hybrid_property
|
|
791
|
+ def updated(self) -> datetime:
|
|
792
|
+ return self.revision.updated
|
|
793
|
+
|
|
794
|
+ @updated.setter
|
|
795
|
+ def updated(self, value: datetime) -> None:
|
|
796
|
+ self.revision.updated = value
|
|
797
|
+
|
|
798
|
+ @updated.expression
|
|
799
|
+ def updated(cls) -> InstrumentedAttribute:
|
|
800
|
+ return ContentRevisionRO.updated
|
|
801
|
+
|
|
802
|
+ @hybrid_property
|
|
803
|
+ def is_deleted(self) -> bool:
|
|
804
|
+ return self.revision.is_deleted
|
|
805
|
+
|
|
806
|
+ @is_deleted.setter
|
|
807
|
+ def is_deleted(self, value: bool) -> None:
|
|
808
|
+ self.revision.is_deleted = value
|
|
809
|
+
|
|
810
|
+ @is_deleted.expression
|
|
811
|
+ def is_deleted(cls) -> InstrumentedAttribute:
|
|
812
|
+ return ContentRevisionRO.is_deleted
|
|
813
|
+
|
|
814
|
+ @hybrid_property
|
|
815
|
+ def is_archived(self) -> bool:
|
|
816
|
+ return self.revision.is_archived
|
|
817
|
+
|
|
818
|
+ @is_archived.setter
|
|
819
|
+ def is_archived(self, value: bool) -> None:
|
|
820
|
+ self.revision.is_archived = value
|
|
821
|
+
|
|
822
|
+ @is_archived.expression
|
|
823
|
+ def is_archived(cls) -> InstrumentedAttribute:
|
|
824
|
+ return ContentRevisionRO.is_archived
|
|
825
|
+
|
|
826
|
+ @hybrid_property
|
|
827
|
+ def revision_type(self) -> str:
|
|
828
|
+ return self.revision.revision_type
|
|
829
|
+
|
|
830
|
+ @revision_type.setter
|
|
831
|
+ def revision_type(self, value: str) -> None:
|
|
832
|
+ self.revision.revision_type = value
|
|
833
|
+
|
|
834
|
+ @revision_type.expression
|
|
835
|
+ def revision_type(cls) -> InstrumentedAttribute:
|
|
836
|
+ return ContentRevisionRO.revision_type
|
|
837
|
+
|
|
838
|
+ @hybrid_property
|
|
839
|
+ def workspace_id(self) -> int:
|
|
840
|
+ return self.revision.workspace_id
|
|
841
|
+
|
|
842
|
+ @workspace_id.setter
|
|
843
|
+ def workspace_id(self, value: int) -> None:
|
|
844
|
+ self.revision.workspace_id = value
|
|
845
|
+
|
|
846
|
+ @workspace_id.expression
|
|
847
|
+ def workspace_id(cls) -> InstrumentedAttribute:
|
|
848
|
+ return ContentRevisionRO.workspace_id
|
|
849
|
+
|
|
850
|
+ @hybrid_property
|
|
851
|
+ def workspace(self) -> Workspace:
|
|
852
|
+ return self.revision.workspace
|
|
853
|
+
|
|
854
|
+ @workspace.setter
|
|
855
|
+ def workspace(self, value: Workspace) -> None:
|
|
856
|
+ self.revision.workspace = value
|
|
857
|
+
|
|
858
|
+ @workspace.expression
|
|
859
|
+ def workspace(cls) -> InstrumentedAttribute:
|
|
860
|
+ return ContentRevisionRO.workspace
|
|
861
|
+
|
|
862
|
+ @hybrid_property
|
|
863
|
+ def parent_id(self) -> int:
|
|
864
|
+ return self.revision.parent_id
|
|
865
|
+
|
|
866
|
+ @parent_id.setter
|
|
867
|
+ def parent_id(self, value: int) -> None:
|
|
868
|
+ self.revision.parent_id = value
|
|
869
|
+
|
|
870
|
+ @parent_id.expression
|
|
871
|
+ def parent_id(cls) -> InstrumentedAttribute:
|
|
872
|
+ return ContentRevisionRO.parent_id
|
|
873
|
+
|
|
874
|
+ @hybrid_property
|
|
875
|
+ def parent(self) -> 'Content':
|
|
876
|
+ return self.revision.parent
|
|
877
|
+
|
|
878
|
+ @parent.setter
|
|
879
|
+ def parent(self, value: 'Content') -> None:
|
|
880
|
+ self.revision.parent = value
|
|
881
|
+
|
|
882
|
+ @parent.expression
|
|
883
|
+ def parent(cls) -> InstrumentedAttribute:
|
|
884
|
+ return ContentRevisionRO.parent
|
|
885
|
+
|
|
886
|
+ @hybrid_property
|
|
887
|
+ def node(self) -> 'Content':
|
|
888
|
+ return self.revision.node
|
|
889
|
+
|
|
890
|
+ @node.setter
|
|
891
|
+ def node(self, value: 'Content') -> None:
|
|
892
|
+ self.revision.node = value
|
|
893
|
+
|
|
894
|
+ @node.expression
|
|
895
|
+ def node(cls) -> InstrumentedAttribute:
|
|
896
|
+ return ContentRevisionRO.node
|
|
897
|
+
|
|
898
|
+ @hybrid_property
|
|
899
|
+ def owner(self) -> User:
|
|
900
|
+ return self.revision.owner
|
|
901
|
+
|
|
902
|
+ @owner.setter
|
|
903
|
+ def owner(self, value: User) -> None:
|
|
904
|
+ self.revision.owner = value
|
|
905
|
+
|
|
906
|
+ @owner.expression
|
|
907
|
+ def owner(cls) -> InstrumentedAttribute:
|
|
908
|
+ return ContentRevisionRO.owner
|
|
909
|
+
|
|
910
|
+ @hybrid_property
|
|
911
|
+ def children(self) -> ['Content']:
|
|
912
|
+ """
|
|
913
|
+ :return: list of children Content
|
|
914
|
+ :rtype Content
|
|
915
|
+ """
|
|
916
|
+ # Return a list of unique revisions parent content
|
|
917
|
+ return list(set([revision.node for revision in self.children_revisions]))
|
|
918
|
+
|
|
919
|
+ @property
|
|
920
|
+ def revision(self) -> ContentRevisionRO:
|
|
921
|
+ return self.get_current_revision()
|
|
922
|
+
|
|
923
|
+ def get_current_revision(self) -> ContentRevisionRO:
|
|
924
|
+ if not self.revisions:
|
|
925
|
+ return self.new_revision()
|
|
926
|
+
|
|
927
|
+ # If last revisions revision don't have revision_id, return it we just add it.
|
|
928
|
+ if self.revisions[-1].revision_id is None:
|
|
929
|
+ return self.revisions[-1]
|
|
930
|
+
|
|
931
|
+ # Revisions should be ordred by revision_id but we ensure that here
|
|
932
|
+ revisions = sorted(self.revisions, key=lambda revision: revision.revision_id)
|
|
933
|
+ return revisions[-1]
|
|
934
|
+
|
|
935
|
+ def new_revision(self) -> None:
|
|
936
|
+ """
|
|
937
|
+ Return and assign to this content a new revision.
|
|
938
|
+ If it's a new content, revision is totally new.
|
|
939
|
+ If this content already own revision, revision is build from last revision.
|
|
940
|
+ :return:
|
|
941
|
+ """
|
|
942
|
+ if not self.revisions:
|
|
943
|
+ self.revisions.append(ContentRevisionRO())
|
|
944
|
+ return self.revisions[0]
|
|
945
|
+
|
|
946
|
+ new_rev = ContentRevisionRO.new_from(self.get_current_revision())
|
|
947
|
+ self.revisions.append(new_rev)
|
|
948
|
+ return new_rev
|
|
949
|
+
|
475
|
950
|
def get_valid_children(self, content_types: list=None) -> ['Content']:
|
476
|
951
|
for child in self.children:
|
477
|
952
|
if not child.is_deleted and not child.is_archived:
|
478
|
953
|
if not content_types or child.type in content_types:
|
479
|
|
- yield child
|
|
954
|
+ yield child.node
|
480
|
955
|
|
481
|
956
|
@hybrid_property
|
482
|
|
- def properties(self):
|
|
957
|
+ def properties(self) -> dict:
|
483
|
958
|
""" return a structure decoded from json content of _properties """
|
484
|
959
|
if not self._properties:
|
485
|
960
|
ContentChecker.reset_properties(self)
|
486
|
961
|
return json.loads(self._properties)
|
487
|
962
|
|
488
|
963
|
@properties.setter
|
489
|
|
- def properties(self, properties_struct):
|
|
964
|
+ def properties(self, properties_struct: dict) -> None:
|
490
|
965
|
""" encode a given structure into json and store it in _properties attribute"""
|
491
|
966
|
self._properties = json.dumps(properties_struct)
|
492
|
967
|
ContentChecker.check_properties(self)
|
|
@@ -519,7 +994,6 @@ class Content(DeclarativeBase):
|
519
|
994
|
'html.parser' # Fixes hanging bug - http://stackoverflow.com/questions/12618567/problems-running-beautifulsoup4-within-apache-mod-python-django
|
520
|
995
|
)
|
521
|
996
|
|
522
|
|
-
|
523
|
997
|
for link in soup.findAll('a'):
|
524
|
998
|
href = link.get('href')
|
525
|
999
|
label = link.contents
|
|
@@ -530,7 +1004,6 @@ class Content(DeclarativeBase):
|
530
|
1004
|
## FIXME - Does this return a sorted list ???!
|
531
|
1005
|
return sorted_links
|
532
|
1006
|
|
533
|
|
-
|
534
|
1007
|
def get_child_nb(self, content_type: ContentType, content_status = ''):
|
535
|
1008
|
child_nb = 0
|
536
|
1009
|
for child in self.get_valid_children():
|
|
@@ -586,7 +1059,7 @@ class Content(DeclarativeBase):
|
586
|
1059
|
children = []
|
587
|
1060
|
for child in self.children:
|
588
|
1061
|
if ContentType.Comment==child.type and not child.is_deleted and not child.is_archived:
|
589
|
|
- children.append(child)
|
|
1062
|
+ children.append(child.node)
|
590
|
1063
|
return children
|
591
|
1064
|
|
592
|
1065
|
def get_last_comment_from(self, user: User) -> 'Content':
|
|
@@ -615,19 +1088,6 @@ class Content(DeclarativeBase):
|
615
|
1088
|
|
616
|
1089
|
return None
|
617
|
1090
|
|
618
|
|
- def get_current_revision(self) -> 'ContentRevisionRO':
|
619
|
|
- # TODO - D.A. - 2015-08-26
|
620
|
|
- # This code is not efficient at all!!!
|
621
|
|
- # We should get the revision id directly from the view
|
622
|
|
- rev_ids = [revision.revision_id for revision in self.revisions]
|
623
|
|
- rev_ids.sort()
|
624
|
|
-
|
625
|
|
- for revision in self.revisions:
|
626
|
|
- if revision.revision_id == rev_ids[-1]:
|
627
|
|
- return revision
|
628
|
|
-
|
629
|
|
- return None
|
630
|
|
-
|
631
|
1091
|
def description_as_raw_text(self):
|
632
|
1092
|
# 'html.parser' fixes a hanging bug
|
633
|
1093
|
# see http://stackoverflow.com/questions/12618567/problems-running-beautifulsoup4-within-apache-mod-python-django
|
|
@@ -667,115 +1127,14 @@ class Content(DeclarativeBase):
|
667
|
1127
|
return url_template.format(wid=wid, fid=fid, ctype=ctype, cid=cid)
|
668
|
1128
|
|
669
|
1129
|
|
670
|
|
-class ContentChecker(object):
|
671
|
|
-
|
672
|
|
- @classmethod
|
673
|
|
- def check_properties(cls, item: Content):
|
674
|
|
- if item.type==ContentType.Folder:
|
675
|
|
- properties = item.properties
|
676
|
|
- if 'allowed_content' not in properties.keys():
|
677
|
|
- return False
|
678
|
|
- if 'folders' not in properties['allowed_content']:
|
679
|
|
- return False
|
680
|
|
- if 'files' not in properties['allowed_content']:
|
681
|
|
- return False
|
682
|
|
- if 'pages' not in properties['allowed_content']:
|
683
|
|
- return False
|
684
|
|
- if 'threads' not in properties['allowed_content']:
|
685
|
|
- return False
|
686
|
|
-
|
687
|
|
- return True
|
688
|
|
-
|
689
|
|
- raise NotImplementedError
|
690
|
|
-
|
691
|
|
- @classmethod
|
692
|
|
- def reset_properties(cls, item: Content):
|
693
|
|
- if item.type==ContentType.Folder:
|
694
|
|
- item.properties = dict(
|
695
|
|
- allowed_content = dict (
|
696
|
|
- folder = True,
|
697
|
|
- file = True,
|
698
|
|
- page = True,
|
699
|
|
- thread = True
|
700
|
|
- )
|
701
|
|
- )
|
702
|
|
- return
|
703
|
|
-
|
704
|
|
- raise NotImplementedError
|
705
|
|
-
|
706
|
|
-
|
707
|
|
-class ContentRevisionRO(DeclarativeBase):
|
708
|
|
-
|
709
|
|
- __tablename__ = 'content_revisions'
|
710
|
|
-
|
711
|
|
- revision_id = Column(Integer, Sequence('seq__content_revisions__revision_id'), primary_key=True)
|
712
|
|
- content_id = Column(Integer, ForeignKey('contents.content_id'))
|
713
|
|
- owner_id = Column(Integer, ForeignKey('users.user_id'), nullable=True)
|
714
|
|
- label = Column(Unicode(1024), unique=False, nullable=False)
|
715
|
|
- description = Column(Text(), unique=False, nullable=False, default='')
|
716
|
|
- file_name = Column(Unicode(255), unique=False, nullable=False, default='')
|
717
|
|
- file_mimetype = Column(Unicode(255), unique=False, nullable=False, default='')
|
718
|
|
- file_content = deferred(Column(LargeBinary(), unique=False, nullable=False, default=None))
|
719
|
|
-
|
720
|
|
- type = Column(Unicode(32), unique=False, nullable=False)
|
721
|
|
- status = Column(Unicode(32), unique=False, nullable=False)
|
722
|
|
- created = Column(DateTime, unique=False, nullable=False)
|
723
|
|
- updated = Column(DateTime, unique=False, nullable=False)
|
724
|
|
- is_deleted = Column(Boolean, unique=False, nullable=False)
|
725
|
|
- is_archived = Column(Boolean, unique=False, nullable=False)
|
726
|
|
- revision_type = Column(Unicode(32), unique=False, nullable=False, default='')
|
727
|
|
-
|
728
|
|
- workspace_id = Column(Integer, ForeignKey('workspaces.workspace_id'), unique=False, nullable=True)
|
729
|
|
- workspace = relationship('Workspace', remote_side=[Workspace.workspace_id])
|
730
|
|
-
|
731
|
|
- node = relationship('Content', remote_side=[Content.content_id], backref='revisions')
|
732
|
|
- owner = relationship('User', remote_side=[User.user_id])
|
733
|
|
-
|
734
|
|
- def get_status(self):
|
735
|
|
- return ContentStatus(self.status)
|
736
|
|
-
|
737
|
|
- def get_label(self):
|
738
|
|
- return self.label if self.label else self.file_name if self.file_name else ''
|
739
|
|
-
|
740
|
|
- def get_last_action(self) -> ActionDescription:
|
741
|
|
- return ActionDescription(self.revision_type)
|
742
|
|
-
|
743
|
|
- # Read by must be used like this:
|
744
|
|
- # read_datetime = revision.ready_by[<User instance>]
|
745
|
|
- # if user did not read the content, then a key error is raised
|
746
|
|
- read_by = association_proxy(
|
747
|
|
- 'revision_read_statuses', # name of the attribute
|
748
|
|
- 'view_datetime', # attribute the value is taken from
|
749
|
|
- creator=lambda k, v: \
|
750
|
|
- RevisionReadStatus(user=k, view_datetime=v)
|
751
|
|
- )
|
752
|
|
-
|
753
|
|
- def has_new_information_for(self, user: User) -> bool:
|
754
|
|
- """
|
755
|
|
- :param user: the session current user
|
756
|
|
- :return: bool, True if there is new information for given user else False
|
757
|
|
- False if the user is None
|
758
|
|
- """
|
759
|
|
- if not user:
|
760
|
|
- return False
|
761
|
|
-
|
762
|
|
- if user not in self.read_by.keys():
|
763
|
|
- return True
|
764
|
|
-
|
765
|
|
- return False
|
766
|
|
-
|
767
|
1130
|
class RevisionReadStatus(DeclarativeBase):
|
768
|
1131
|
|
769
|
1132
|
__tablename__ = 'revision_read_status'
|
770
|
1133
|
|
771
|
1134
|
revision_id = Column(Integer, ForeignKey('content_revisions.revision_id', ondelete='CASCADE', onupdate='CASCADE'), primary_key=True)
|
772
|
1135
|
user_id = Column(Integer, ForeignKey('users.user_id', ondelete='CASCADE', onupdate='CASCADE'), primary_key=True)
|
773
|
|
- view_datetime = Column(DateTime, unique=False, nullable=False, server_default=func.now())
|
774
|
|
-
|
775
|
|
- # content_revision = relationship(
|
776
|
|
- # 'ContentRevisionRO',
|
777
|
|
- # remote_side=[ContentRevisionRO.revision_id],
|
778
|
|
- # backref='revision_read_statuses')
|
|
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)
|
779
|
1138
|
|
780
|
1139
|
content_revision = relationship(
|
781
|
1140
|
'ContentRevisionRO',
|
|
@@ -831,7 +1190,7 @@ class VirtualEvent(object):
|
831
|
1190
|
action_description = ActionDescription(revision.revision_type)
|
832
|
1191
|
|
833
|
1192
|
return VirtualEvent(id=revision.revision_id,
|
834
|
|
- created=revision.created,
|
|
1193
|
+ created=revision.updated,
|
835
|
1194
|
owner=revision.owner,
|
836
|
1195
|
type=action_description,
|
837
|
1196
|
label=action_description.label,
|