from collective.tin.item import Table from collective.tin.container import TableFolder, TableSubFolder from collective.tin.factory import ItemFactory, FolderFactory from collective.tin.declarative import DeclarativeMetaExtensionClass from collective.tin.vocabulary import TableVocabulary from collective.tin import interfaces as tin from zope.interface import implements import interfaces from sqlalchemy.ext.declarative import declarative_base import sqlalchemy as sa from sqlalchemy import orm SCOPED_SESSION_NAME = 'collective.tin.tests' Base = declarative_base(metaclass=DeclarativeMetaExtensionClass) class User(Base, Table): '''User item class ''' implements( interfaces.IUser, tin.IIndexable, ) portal_type = meta_type = 'TinTestUser' # The declarative stuff __tablename__ = 'tintestusers' userid = sa.Column(sa.String, primary_key=True) fullname = sa.Column(sa.String) @property # basic metadata def title(self): return self.fullname UserFactory = ItemFactory(User) class UserFolder(TableFolder): '''Users folder class, the ZODB root for database user items ''' implements( tin.IStringItemKey, # marker interface to indicate table has a string primay key tin.IDeleteDeleted, ) portal_type = meta_type = 'TinTestUserFolder' scoped_session_name = SCOPED_SESSION_NAME item_class = User UserFolderFactory = FolderFactory(UserFolder) UserVocabulary = TableVocabulary('tintestusers', 'userid', 'fullname') class Timesheet(Base, Table): '''Project class ''' implements( interfaces.ITimesheet, tin.ITableWorkflowHistory, ) portal_type = meta_type = 'TinTestTimesheet' # The declarative stuff __tablename__ = 'tintesttimesheets' timesheetid = sa.Column(sa.Integer, primary_key=True) userid = sa.Column(sa.String, sa.ForeignKey("tintestusers.userid"), nullable=False) projectname = sa.Column(sa.String, sa.ForeignKey("tintestprojects.name"), nullable=False) hours = sa.Column(sa.Integer, nullable=False) description = sa.Column(sa.Text) # Workflow storage workflow_table_name = 'tintesttimesheetswf' workflow_id_column_name = 'workflow_id' TimesheetFactory = ItemFactory(Timesheet) # We only need the Table defined here, but this syntax is nice class TimesheetWorkflowRecord(Base): __tablename__ = 'tintesttimesheetswf' row_id = sa.Column(sa.Integer, primary_key=True) timesheetid = sa.Column(sa.Integer, sa.ForeignKey("tintesttimesheets.timesheetid"), nullable=False) workflow_id = sa.Column(sa.String) action = sa.Column(sa.String) actor = sa.Column(sa.String) comments = sa.Column(sa.String) review_state = sa.Column(sa.String) time = sa.Column(sa.DateTime(timezone=True)) class Project(Base, TableSubFolder): '''Project class ''' implements( interfaces.IProject, tin.IIntegerItemKey, tin.IAttributeWorkflowHistory, tin.IIndexable, ) portal_type = meta_type = 'TinTestProject' item_class = Timesheet # The declarative stuff __tablename__ = 'tintestprojects' name = sa.Column(sa.String, primary_key=True) title = sa.Column(sa.String) description = sa.Column(sa.Text) wf_data = sa.Column(sa.Text) # workflow storage workflow_attribute_name = 'wf_data' ProjectFactory = ItemFactory(Project) # We add the relations here once all the classes have been defined User.timesheets = orm.relation(Timesheet) Project.timesheets = orm.relation(Timesheet) class ProjectFolder(TableFolder): '''Projects folder class, the ZODB root for database project items ''' implements( tin.IStringItemKey, # marker interface to indicate table has a string primay key ) portal_type = meta_type = 'TinTestProjectFolder' scoped_session_name = SCOPED_SESSION_NAME item_class = Project ProjectFolderFactory = FolderFactory(ProjectFolder)