diff --git a/eos/saveddata/module.py b/eos/saveddata/module.py index 53f611ddb..8d4a6d5af 100644 --- a/eos/saveddata/module.py +++ b/eos/saveddata/module.py @@ -239,8 +239,15 @@ class Module(HandledItem, HandledCharge, ItemAttrShortcut, ChargeAttrShortcut): @property def modPosition(self): - if self.owner: - return self.owner.modules.index(self) if not self.isProjected else self.owner.projectedModules.index(self) + return self.getModPosition() + + def getModPosition(self, fit=None): + # Pass in fit for reliability. When it's not passed, we rely on owner and owner + # is set by sqlalchemy during flush + fit = fit if fit is not None else self.owner + if fit: + return fit.modules.index(self) if not self.isProjected else fit.projectedModules.index(self) + @property def isProjected(self): @@ -570,7 +577,7 @@ class Module(HandledItem, HandledCharge, ItemAttrShortcut, ChargeAttrShortcut): current = 0 # if self.owner != fit else -1 # Disabled, see #1278 for mod in fit.modules: if (mod.item and mod.item.groupID == self.item.groupID and - self.modPosition != mod.modPosition): + self.getModPosition(fit) != mod.getModPosition(fit)): current += 1 if current >= max: diff --git a/gui/fitCommands/calc/module/localAdd.py b/gui/fitCommands/calc/module/localAdd.py index 259a4cc95..ef21348c7 100644 --- a/gui/fitCommands/calc/module/localAdd.py +++ b/gui/fitCommands/calc/module/localAdd.py @@ -34,14 +34,18 @@ class CalcAddLocalModuleCommand(wx.Command): if newMod.item.category.name == 'Subsystem': for oldMod in fit.modules: if oldMod.getModifiedItemAttr('subSystemSlot') == newMod.getModifiedItemAttr('subSystemSlot') and newMod.slot == oldMod.slot: + if oldMod.itemID == self.newModInfo.itemID: + return False from .localReplace import CalcReplaceLocalModuleCommand - self.subsystemCmd = CalcReplaceLocalModuleCommand(fitID=self.fitID, position=oldMod.modPosition, newModInfo=self.newModInfo) + self.subsystemCmd = CalcReplaceLocalModuleCommand( + fitID=self.fitID, + position=fit.modules.index(oldMod), + newModInfo=self.newModInfo) return self.subsystemCmd.Do() if not newMod.fits(fit): pyfalog.warning('Module does not fit') return False - newMod.owner = fit try: fit.modules.append(newMod) except HandledListActionError: @@ -49,7 +53,7 @@ class CalcAddLocalModuleCommand(wx.Command): if self.commit: eos.db.commit() return False - self.savedPosition = newMod.modPosition + self.savedPosition = fit.modules.index(newMod) sFit.recalc(fit) self.savedStateCheckChanges = sFit.checkStates(fit, newMod) if self.commit: @@ -61,9 +65,9 @@ class CalcAddLocalModuleCommand(wx.Command): # We added a subsystem module, which actually ran the replace command. Run the undo for that guy instead if self.subsystemCmd is not None: return self.subsystemCmd.Undo() - from .localRemove import CalcRemoveLocalModuleCommand if self.savedPosition is None: return False + from .localRemove import CalcRemoveLocalModuleCommand cmd = CalcRemoveLocalModuleCommand(fitID=self.fitID, positions=[self.savedPosition], commit=self.commit) if not cmd.Do(): return False diff --git a/gui/fitCommands/calc/module/localClone.py b/gui/fitCommands/calc/module/localClone.py index dbe6ce0d8..d94180666 100644 --- a/gui/fitCommands/calc/module/localClone.py +++ b/gui/fitCommands/calc/module/localClone.py @@ -31,7 +31,6 @@ class CalcCloneLocalModuleCommand(wx.Command): return False if not fit.modules[self.dstPosition].isEmpty: return False - copyMod.owner = fit try: fit.modules.replace(self.dstPosition, copyMod) except HandledListActionError: diff --git a/gui/fitCommands/calc/module/localReplace.py b/gui/fitCommands/calc/module/localReplace.py index bf9e1717f..4452381f5 100644 --- a/gui/fitCommands/calc/module/localReplace.py +++ b/gui/fitCommands/calc/module/localReplace.py @@ -85,7 +85,6 @@ class CalcReplaceLocalModuleCommand(wx.Command): pyfalog.warning('Module does not fit') self.Do() return False - oldMod.owner = fit try: fit.modules.replace(self.position, oldMod) except HandledListActionError: diff --git a/gui/fitCommands/gui/itemsRebase.py b/gui/fitCommands/gui/itemsRebase.py index 437c2c151..00cbae666 100644 --- a/gui/fitCommands/gui/itemsRebase.py +++ b/gui/fitCommands/gui/itemsRebase.py @@ -27,7 +27,7 @@ class GuiRebaseItemsCommand(wx.Command): cmd = CalcRebaseItemCommand( fitID=self.fitID, containerName='modules', - position=mod.modPosition, + position=fit.modules.index(mod), itemID=self.rebaseMap[mod.itemID], commit=False) self.internalHistory.submit(cmd) @@ -35,7 +35,8 @@ class GuiRebaseItemsCommand(wx.Command): cmd = CalcChangeModuleChargesCommand( fitID=self.fitID, projected=False, - chargeMap={mod.modPosition: self.rebaseMap[mod.chargeID]}) + chargeMap={fit.modules.index(mod): self.rebaseMap[mod.chargeID]}, + commit=False) self.internalHistory.submit(cmd) for containerName in ('drones', 'fighters', 'implants', 'boosters'): container = getattr(fit, containerName) @@ -53,8 +54,14 @@ class GuiRebaseItemsCommand(wx.Command): for cargo in fit.cargo: if cargo.itemID in self.rebaseMap: amount = cargo.amount - cmdRemove = CalcRemoveCargoCommand(fitID=self.fitID, cargoInfo=CargoInfo(itemID=cargo.itemID, amount=amount)) - cmdAdd = CalcAddCargoCommand(fitID=self.fitID, cargoInfo=CargoInfo(itemID=self.rebaseMap[cargo.itemID], amount=amount)) + cmdRemove = CalcRemoveCargoCommand( + fitID=self.fitID, + cargoInfo=CargoInfo(itemID=cargo.itemID, amount=amount), + commit=False) + cmdAdd = CalcAddCargoCommand( + fitID=self.fitID, + cargoInfo=CargoInfo(itemID=self.rebaseMap[cargo.itemID], amount=amount), + commit=False) self.internalHistory.submitBatch(cmdRemove, cmdAdd) eos.db.commit() sFit.recalc(fit) diff --git a/gui/fitCommands/gui/localModuleCargo/cargoToLocalModule.py b/gui/fitCommands/gui/localModuleCargo/cargoToLocalModule.py index dee4d5bf2..eacce7aa8 100644 --- a/gui/fitCommands/gui/localModuleCargo/cargoToLocalModule.py +++ b/gui/fitCommands/gui/localModuleCargo/cargoToLocalModule.py @@ -56,7 +56,7 @@ class GuiCargoToLocalModuleCommand(wx.Command): commands.append(CalcChangeModuleChargesCommand( fitID=self.fitID, projected=False, - chargeMap={dstMod.modPosition: self.srcCargoItemID}, + chargeMap={fit.modules.index(dstMod): self.srcCargoItemID}, commit=False)) success = self.internalHistory.submitBatch(*commands) # Moving/copying/replacing module