def decrease(counter_name, allow_negative=False): """ Decrease the counter value in the database. If the counter does not exist yet, create the counter. Also checks whether the counter is allowed to become negative. :param counter_name: The name/identifier of the counter :param allow_negative: Whether the counter can become negative :return: the new integer value of the counter """ counter = EventCounter.query.filter_by(counter_name=counter_name).first() if not counter: counter = EventCounter(counter_name, 0) counter.save() counter.decrease(allow_negative) return counter.counter_value
def decrease(counter_name, allow_negative=False): """ Decrease the counter value in the database. If the counter does not exist yet, create the counter. Also checks whether the counter is allowed to become negative. :param counter_name: The name/identifier of the counter :param allow_negative: Whether the counter can become negative. Note that even if this flag is not set, the counter may become negative due to concurrent queries. :return: None """ node = get_privacyidea_node() counter = EventCounter.query.filter_by(counter_name=counter_name, node=node).first() if not counter: counter = EventCounter(counter_name, 0, node=node) counter.save() # We are allowed to decrease the current counter object only if the overall # counter value is positive (because individual rows may be negative then), # or if we allow negative values. Otherwise, we need to reset all rows of all nodes. if read(counter_name) > 0 or allow_negative: counter.decrease() else: _reset_counter_on_all_nodes(counter_name)