def delete_parameter(self, proxy) -> None: """ Override the base method to include additional logic. If there are multiple `ActivityParameters` for a single activity, only delete the selected instance, otherwise use `bw.parameters.remove_from_group` to clear out the `ParameterizedExchanges` as well. """ key = self.get_key(proxy) query = (ActivityParameter.select().where( ActivityParameter.database == key[0], ActivityParameter.code == key[1])) if query.count() > 1: super().delete_parameter(proxy) else: act = bw.get_activity(key) group = self.get_current_group(proxy) bw.parameters.remove_from_group(group, act) # Also clear the group if there are no more parameters in it if ActivityParameter.get_or_none(group=group) is None: with bw.parameters.db.atomic(): Group.get(name=group).delete_instance() bw.parameters.recalculate() signals.parameters_changed.emit()
def parameterize_exchanges(self, key: tuple) -> None: """ Used whenever a formula is set on an exchange in an activity. If no `ActivityParameter` exists for the key, generate one immediately """ if ActivityParameter.get_or_none(database=key[0], code=key[1]) is None: signals.add_activity_parameter.emit(key) param = ActivityParameter.get(database=key[0], code=key[1]) act = bw.get_activity(key) bw.parameters.remove_exchanges_from_group(param.group, act) bw.parameters.add_exchanges_to_group(param.group, act) ActivityParameter.recalculate_exchanges(param.group) signals.parameters_changed.emit()
def get_interpreter(self) -> Interpreter: """ Use the activity key to determine which symbols are added to the formula interpreter. """ interpreter = Interpreter() act = ActivityParameter.get_or_none(database=self.key[0], code=self.key[1]) if act: interpreter.symtable.update( ActivityParameter.static(act.group, full=True)) else: print("No parameter found for {}, creating one on formula save". format(self.key)) interpreter.symtable.update(ProjectParameter.static()) interpreter.symtable.update(DatabaseParameter.static(self.key[0])) return interpreter
def remove_formula(self) -> None: """ Remove the formulas for all of the selected exchanges. This will also check if the exchange has `original_amount` and attempt to overwrite the `amount` with that value after removing the `formula` field. """ indexes = (self.get_source_index(p) for p in self.selectedIndexes()) exchanges = [ self.model.index(index.row(), self.exchange_column).data() for index in indexes ] for exchange in exchanges: signals.exchange_modified.emit(exchange, "formula", "") # Clear out all ParameterizedExchanges before recalculating param = ActivityParameter.get_or_none(database=self.key[0], code=self.key[1]) if param: signals.exchange_formula_changed.emit(self.key)