6 """ |
6 """ |
7 Module implementing the project support for flask-migrate. |
7 Module implementing the project support for flask-migrate. |
8 """ |
8 """ |
9 |
9 |
10 import os |
10 import os |
|
11 import glob |
11 |
12 |
12 from PyQt5.QtCore import pyqtSlot, QObject, QProcess |
13 from PyQt5.QtCore import pyqtSlot, QObject, QProcess |
13 from PyQt5.QtWidgets import QMenu, QDialog |
14 from PyQt5.QtWidgets import QMenu, QDialog, QInputDialog, QLineEdit |
14 |
15 |
15 from E5Gui import E5MessageBox |
16 from E5Gui import E5MessageBox |
16 from E5Gui.E5Application import e5App |
17 from E5Gui.E5Application import e5App |
17 from E5Gui.E5Action import E5Action |
18 from E5Gui.E5Action import E5Action |
18 |
19 |
19 import Utilities |
20 import Utilities |
20 |
21 |
21 from ..FlaskCommandDialog import FlaskCommandDialog |
22 from ..FlaskCommandDialog import FlaskCommandDialog |
22 |
23 |
23 |
24 |
|
25 # TODO: add a submenu with action for the commands with command options |
24 class MigrateProject(QObject): |
26 class MigrateProject(QObject): |
25 """ |
27 """ |
26 Class implementing the flask-migrate project support. |
28 Class implementing the flask-migrate project support. |
27 """ |
29 """ |
28 def __init__(self, plugin, project, parent=None): |
30 def __init__(self, plugin, project, parent=None): |
45 |
47 |
46 def initActions(self): |
48 def initActions(self): |
47 """ |
49 """ |
48 Public method to define the flask-migrate actions. |
50 Public method to define the flask-migrate actions. |
49 """ |
51 """ |
50 # TODO: implement flask-migrate actions |
|
51 self.actions = [] |
52 self.actions = [] |
52 |
53 |
53 self.migrateConfigAct = E5Action( |
54 self.migrateConfigAct = E5Action( |
54 self.tr('Configure Migrate'), |
55 self.tr('Configure Migrate'), |
55 self.tr('&Configure Migrate'), |
56 self.tr('&Configure Migrate'), |
115 """ stored in the configured migrations directory.</p>""" |
116 """ stored in the configured migrations directory.</p>""" |
116 )) |
117 )) |
117 self.migrateInitAct.triggered.connect( |
118 self.migrateInitAct.triggered.connect( |
118 self.__initMigrations) |
119 self.__initMigrations) |
119 self.actions.append(self.migrateInitAct) |
120 self.actions.append(self.migrateInitAct) |
120 # TODO: add action for flask db init |
|
121 |
121 |
122 ######################################################### |
122 ######################################################### |
123 ## action to create a new database migration |
123 ## action to create a new database migration |
124 ######################################################### |
124 ######################################################### |
125 # TODO: add action for flask db migrate |
125 |
|
126 self.migrateCreateAct = E5Action( |
|
127 self.tr('Create Migration'), |
|
128 self.tr('&Create Migration'), |
|
129 0, 0, |
|
130 self, 'flask_create_migration') |
|
131 self.migrateCreateAct.setStatusTip(self.tr( |
|
132 'Create a new migration for the current database')) |
|
133 self.migrateCreateAct.setWhatsThis(self.tr( |
|
134 """<b>Create Migration</b>""" |
|
135 """<p>Creates a new migration for the current database""" |
|
136 """ and stores it in the configured migrations directory.</p>""" |
|
137 )) |
|
138 self.migrateCreateAct.triggered.connect( |
|
139 self.__createMigration) |
|
140 self.actions.append(self.migrateCreateAct) |
126 |
141 |
127 ######################################################### |
142 ######################################################### |
128 ## action to up- and downgrade a databse |
143 ## action to up- and downgrade a databse |
129 ######################################################### |
144 ######################################################### |
130 # TODO: add action for flask db upgrade |
145 # TODO: add action for flask db upgrade |
141 menu.setTearOffEnabled(True) |
156 menu.setTearOffEnabled(True) |
142 |
157 |
143 menu.addAction(self.migrateConfigAct) |
158 menu.addAction(self.migrateConfigAct) |
144 menu.addSeparator() |
159 menu.addSeparator() |
145 menu.addAction(self.migrateInitAct) |
160 menu.addAction(self.migrateInitAct) |
|
161 menu.addSeparator() |
|
162 menu.addAction(self.migrateCreateAct) |
146 menu.addSeparator() |
163 menu.addSeparator() |
147 menu.addAction(self.migrateAvailabilityAct) |
164 menu.addAction(self.migrateAvailabilityAct) |
148 menu.addAction(self.migrateInstallAct) |
165 menu.addAction(self.migrateInstallAct) |
149 |
166 |
150 return menu |
167 return menu |
330 |
347 |
331 ######################################################### |
348 ######################################################### |
332 ## slot to create a new database migration |
349 ## slot to create a new database migration |
333 ######################################################### |
350 ######################################################### |
334 |
351 |
|
352 @pyqtSlot() |
|
353 def __createMigration(self): |
|
354 """ |
|
355 Private slot to create a new database migration. |
|
356 """ |
|
357 title = self.tr("Create Migration") |
|
358 |
|
359 self.__ensureMigrateConfigured() |
|
360 migrations = self.__migrationsDirectory() |
|
361 |
|
362 message, ok = QInputDialog.getText( |
|
363 None, |
|
364 title, |
|
365 self.tr("Enter a short message for the migration:"), |
|
366 QLineEdit.Normal) |
|
367 if ok: |
|
368 args = ["migrate"] |
|
369 if migrations: |
|
370 args += ["--directory", migrations] |
|
371 if message: |
|
372 args += ["--message", message] |
|
373 |
|
374 dlg = FlaskCommandDialog( |
|
375 self.__project, title=title, |
|
376 msgSuccess=self.tr("\nMigration created successfully.") |
|
377 ) |
|
378 if dlg.startCommand("db", args): |
|
379 dlg.exec() |
|
380 if dlg.normalExit(): |
|
381 versionsPattern = os.path.join( |
|
382 self.__migrationsDirectory(abspath=True), |
|
383 "versions", "*.py") |
|
384 for fileName in glob.iglob(versionsPattern): |
|
385 self.__e5project.appendFile(fileName) |
|
386 |
335 ######################################################### |
387 ######################################################### |
336 ## slots to up- and downgrade a databse |
388 ## slots to up- and downgrade a databse |
337 ######################################################### |
389 ######################################################### |