ProjectFlask/FlaskMigrateExtension/MigrateProjectExtension.py

changeset 33
c82b296a21ae
parent 32
80fc8deac8fe
child 34
a91c6a1eb23f
--- a/ProjectFlask/FlaskMigrateExtension/MigrateProjectExtension.py	Wed Nov 25 19:39:04 2020 +0100
+++ b/ProjectFlask/FlaskMigrateExtension/MigrateProjectExtension.py	Wed Nov 25 20:10:41 2020 +0100
@@ -8,9 +8,10 @@
 """
 
 import os
+import glob
 
 from PyQt5.QtCore import pyqtSlot, QObject, QProcess
-from PyQt5.QtWidgets import QMenu, QDialog
+from PyQt5.QtWidgets import QMenu, QDialog, QInputDialog, QLineEdit
 
 from E5Gui import E5MessageBox
 from E5Gui.E5Application import e5App
@@ -21,6 +22,7 @@
 from ..FlaskCommandDialog import FlaskCommandDialog
 
 
+# TODO: add a submenu with action for the commands with command options
 class MigrateProject(QObject):
     """
     Class implementing the flask-migrate project support.
@@ -47,7 +49,6 @@
         """
         Public method to define the flask-migrate actions.
         """
-        # TODO: implement flask-migrate actions
         self.actions = []
         
         self.migrateConfigAct = E5Action(
@@ -117,12 +118,26 @@
         self.migrateInitAct.triggered.connect(
             self.__initMigrations)
         self.actions.append(self.migrateInitAct)
-        # TODO: add action for flask db init
         
         #########################################################
         ## action to create a new database migration
         #########################################################
-        # TODO: add action for flask db migrate
+        
+        self.migrateCreateAct = E5Action(
+            self.tr('Create Migration'),
+            self.tr('&Create Migration'),
+            0, 0,
+            self, 'flask_create_migration')
+        self.migrateCreateAct.setStatusTip(self.tr(
+            'Create a new migration for the current database'))
+        self.migrateCreateAct.setWhatsThis(self.tr(
+            """<b>Create Migration</b>"""
+            """<p>Creates a new migration for the current database"""
+            """ and stores it  in the configured migrations directory.</p>"""
+        ))
+        self.migrateCreateAct.triggered.connect(
+            self.__createMigration)
+        self.actions.append(self.migrateCreateAct)
         
         #########################################################
         ## action to up- and downgrade a databse
@@ -144,6 +159,8 @@
         menu.addSeparator()
         menu.addAction(self.migrateInitAct)
         menu.addSeparator()
+        menu.addAction(self.migrateCreateAct)
+        menu.addSeparator()
         menu.addAction(self.migrateAvailabilityAct)
         menu.addAction(self.migrateInstallAct)
         
@@ -332,6 +349,41 @@
     ## slot to create a new database migration
     #########################################################
     
+    @pyqtSlot()
+    def __createMigration(self):
+        """
+        Private slot to create a new database migration.
+        """
+        title = self.tr("Create Migration")
+        
+        self.__ensureMigrateConfigured()
+        migrations = self.__migrationsDirectory()
+        
+        message, ok = QInputDialog.getText(
+            None,
+            title,
+            self.tr("Enter a short message for the migration:"),
+            QLineEdit.Normal)
+        if ok:
+            args = ["migrate"]
+            if migrations:
+                args += ["--directory", migrations]
+            if message:
+                args += ["--message", message]
+            
+            dlg = FlaskCommandDialog(
+                self.__project, title=title,
+                msgSuccess=self.tr("\nMigration created successfully.")
+            )
+            if dlg.startCommand("db", args):
+                dlg.exec()
+            if dlg.normalExit():
+                versionsPattern = os.path.join(
+                    self.__migrationsDirectory(abspath=True),
+                    "versions", "*.py")
+                for fileName in glob.iglob(versionsPattern):
+                    self.__e5project.appendFile(fileName)
+    
     #########################################################
     ## slots to up- and downgrade a databse
     #########################################################

eric ide

mercurial