10
10
from octoprint_PrintJobHistory .WrappedLoggingHandler import WrappedLoggingHandler
11
11
from octoprint_PrintJobHistory .api import TransformPrintJob2JSON
12
12
from octoprint_PrintJobHistory .common import StringUtils
13
+ from octoprint_PrintJobHistory .models .CostModel import CostModel
13
14
from octoprint_PrintJobHistory .models .FilamentModel import FilamentModel
14
15
from octoprint_PrintJobHistory .models .PrintJobModel import PrintJobModel
15
16
from octoprint_PrintJobHistory .models .PluginMetaDataModel import PluginMetaDataModel
21
22
FORCE_CREATE_TABLES = False
22
23
SQL_LOGGING = True
23
24
24
- CURRENT_DATABASE_SCHEME_VERSION = 6
25
+ CURRENT_DATABASE_SCHEME_VERSION = 7
25
26
26
27
# List all Models
27
- MODELS = [PluginMetaDataModel , PrintJobModel , FilamentModel , TemperatureModel ]
28
+ MODELS = [PluginMetaDataModel , PrintJobModel , FilamentModel , TemperatureModel , CostModel ]
28
29
29
30
30
31
class DatabaseManager (object ):
@@ -107,6 +108,40 @@ def _upgradeFrom7To8(self):
107
108
108
109
def _upgradeFrom6To7 (self ):
109
110
self ._logger .info (" Starting 6 -> 7" )
111
+
112
+ connection = sqlite3 .connect (self ._databaseFileLocation )
113
+ cursor = connection .cursor ()
114
+
115
+ ## Changeset
116
+ # - NEW CostModel
117
+ # - Droping costUnit, because now there is a general plugin-setting
118
+
119
+ sql = """
120
+ PRAGMA foreign_keys=off;
121
+ BEGIN TRANSACTION;
122
+
123
+ CREATE TABLE "pjh_costmodel" ("databaseId" INTEGER NOT NULL PRIMARY KEY,
124
+ "created" DATETIME NOT NULL,
125
+ "printJob_id" INTEGER NOT NULL,
126
+ "totalCosts" REAL,
127
+ "filamentCost" REAL,
128
+ "electricityCost" REAL,
129
+ "printerCost" REAL,
130
+ "otherCostLabel" VARCHAR(255),
131
+ "otherCost" REAL,
132
+ "withDefaultSpoolValues" INTEGER,
133
+ FOREIGN KEY ("printJob_id") REFERENCES "pjh_printjobmodel" ("databaseId") ON DELETE CASCADE);
134
+
135
+ ALTER TABLE "pjh_filamentmodel" DROP COLUMN "spoolCostUnit";
136
+
137
+ UPDATE 'pjh_pluginmetadatamodel' SET value=7 WHERE key='databaseSchemeVersion';
138
+ COMMIT;
139
+ PRAGMA foreign_keys=on;
140
+ """
141
+ cursor .executescript (sql )
142
+
143
+ connection .close ()
144
+
110
145
self ._logger .info (" Successfully 6 -> 7" )
111
146
pass
112
147
@@ -139,7 +174,6 @@ def _upgradeFrom5To6(self):
139
174
self ._logger .info (" Successfully 5 -> 6" )
140
175
pass
141
176
142
-
143
177
def _upgradeFrom4To5 (self ):
144
178
self ._logger .info (" Starting 4 -> 5" )
145
179
# What is changed:
@@ -426,7 +460,7 @@ def backupDatabaseFile(self, backupFolder):
426
460
currentSchemeVersion = str (currentSchemeVersion .value )
427
461
except Exception as e :
428
462
self ._logger .exception ("Could not read databasescheme version:" + str (e ))
429
- backupDatabaseFileName = "printJobHistory-backup-V" + currentSchemeVersion + "-" + currentDate + ".db"
463
+ backupDatabaseFileName = "printJobHistory-backup-" + currentDate + "-V" + currentSchemeVersion + ".db"
430
464
backupDatabaseFilePath = os .path .join (backupFolder , backupDatabaseFileName )
431
465
if not os .path .exists (backupDatabaseFilePath ):
432
466
shutil .copy (self ._databaseFileLocation , backupDatabaseFilePath )
@@ -473,6 +507,10 @@ def insertPrintJob(self, printJobModel):
473
507
for temperatureModel in printJobModel .getTemperatureModels ():
474
508
temperatureModel .printJob = printJobModel
475
509
temperatureModel .save ()
510
+ # - Costs
511
+ if (printJobModel .getCosts () != None ):
512
+ printJobModel .getCosts ().save ()
513
+
476
514
# do expicit commit
477
515
transaction .commit ()
478
516
except Exception as e :
@@ -498,10 +536,14 @@ def updatePrintJob(self, printJobModel, rollbackHandler = None):
498
536
for filamentModel in printJobModel .getFilamentModels ():
499
537
filamentModel .save ()
500
538
501
- # # - Temperature
539
+ # # - Temperature not needed for an update
502
540
# for temperatureModel in printJobModel.getTemperatureModels():
503
541
# temperatureModel.printJob = printJobModel
504
542
# temperatureModel.save()
543
+
544
+ # - Costs
545
+ if (printJobModel .getCosts () != None ):
546
+ printJobModel .getCosts ().save ()
505
547
except Exception as e :
506
548
# Because this block of code is wrapped with "atomic", a
507
549
# new transaction will begin automatically after the call
@@ -558,7 +600,6 @@ def calculatePrintJobsStatisticByQuery(self, tableQuery):
558
600
if filla .toolId == "total" :
559
601
# exclude totals, otherwise everything is counted twice
560
602
continue
561
-
562
603
if (StringUtils .isEmpty (filla .usedLength ) == False ):
563
604
length = length + filla .usedLength
564
605
if (StringUtils .isEmpty (filla .usedWeight ) == False ):
@@ -797,16 +838,26 @@ def loadAllPrintJobs(self):
797
838
# return allDict
798
839
799
840
def loadPrintJob (self , databaseId ):
800
- return PrintJobModel .get_by_id (databaseId )
841
+ databaseIdAsInt = StringUtils .transformToIntOrNone (databaseId )
842
+ if (databaseIdAsInt == None ):
843
+ self ._logger .error ("Could not load PrintJob, because not a valid databaseId '" + str (databaseId )+ "' maybe not a number" )
844
+ return None
845
+ return PrintJobModel .get_or_none (databaseIdAsInt )
801
846
802
847
def deletePrintJob (self , databaseId ):
848
+ databaseIdAsInt = StringUtils .transformToIntOrNone (databaseId )
849
+ if (databaseIdAsInt == None ):
850
+ self ._logger .error ("Could not delete PrintJob, because not a valid databaseId '" + str (databaseId )+ "' maybe not a number" )
851
+ return None
852
+
803
853
with self ._database .atomic () as transaction : # Opens new transaction.
804
854
try :
805
855
# first delete relations
806
- n = FilamentModel .delete ().where (FilamentModel .printJob == databaseId ).execute ()
807
- n = TemperatureModel .delete ().where (TemperatureModel .printJob == databaseId ).execute ()
856
+ n = FilamentModel .delete ().where (FilamentModel .printJob == databaseIdAsInt ).execute ()
857
+ n = TemperatureModel .delete ().where (TemperatureModel .printJob == databaseIdAsInt ).execute ()
858
+ n = CostModel .delete ().where (CostModel .printJob == databaseIdAsInt ).execute ()
808
859
809
- PrintJobModel .delete_by_id (databaseId )
860
+ PrintJobModel .delete_by_id (databaseIdAsInt )
810
861
except Exception as e :
811
862
# Because this block of code is wrapped with "atomic", a
812
863
# new transaction will begin automatically after the call
0 commit comments