theenglishway (time) преди 6 години
родител
ревизия
c76bbebd2d
променени са 3 файла, в които са добавени 29 реда и са изтрити 1 реда
  1. 1 0
      pyplanner/cli.py
  2. 3 1
      pyplanner/models/__init__.py
  3. 25 0
      pyplanner/models/index.py

+ 1 - 0
pyplanner/cli.py

@@ -16,6 +16,7 @@ def main(ctx, db_url):
     """Basic CLI project-handling"""
     ctx.ensure_object(dict)
     ctx.obj['db'] = Database(db_url)
+    UniversalIndex.listen(ctx.obj['db'])
     ctx.obj['output'] = TerminalOutput()
     ctx.obj['converter'] = ConverterYaml(ctx.obj['db'].models.values())
 

+ 3 - 1
pyplanner/models/__init__.py

@@ -64,6 +64,7 @@ from .unplanned import (UnplannedItem, UnplannedItemType,
                         Idea, Experimentation)
 from .comments import Comment
 from .tasks import Task
+from .index import UniversalIndex
 
 
 __all__ = [
@@ -77,5 +78,6 @@ __all__ = [
     'UnplannedItem', 'UnplannedItemType',
     'Experimentation', 'Idea',
     'Comment',
-    'Task'
+    'Task',
+    'UniversalIndex'
 ]

+ 25 - 0
pyplanner/models/index.py

@@ -0,0 +1,25 @@
+from pyplanner.database import SQLABase
+from sqlalchemy import Column, Integer, String, event
+import uuid
+
+
+class UniversalIndex(SQLABase):
+    __tablename__ = 'universal_index'
+
+    id = Column(Integer, primary_key=True)
+    table = Column(String)
+    object_id = Column(Integer)
+    uuid = Column(String)
+
+    @staticmethod
+    def listen(database):
+        def do_something(session, flush_context):
+            "listen for the 'after_flush' event"
+            print(session.new, session.dirty, session.deleted)
+
+            uis = [UniversalIndex(
+                table=obj.__class__.__name__, object_id=obj.id, uuid=uuid.uuid4().hex
+            ) for obj in session.new if not isinstance(obj, UniversalIndex)]
+            session.add_all(uis)
+
+        event.listen(database.session_factory, 'after_flush', do_something)