def stop(self): self._stopped = True end = forceOpsiTimestamp(timestamp()) with self._db_lock, self._sql.session() as session: self._sql.update(session, 'EVENT', '`durationEvent` = 1 AND `end` is NULL', {'end': end})
def addEvent(self, title, description='', isError=False, category=None, durationEvent=False, start=None, end=None): # pylint: disable=too-many-arguments if self._stopped: return -1 with self._db_lock, self._sql.session() as session: try: if category: category = forceUnicode(category) if not start: start = timestamp() start = forceOpsiTimestamp(start) if end: end = forceOpsiTimestamp(end) durationEvent = True event = { 'title': forceUnicode(title), 'category': category, 'description': forceUnicode(description), 'isError': forceBool(isError), 'durationEvent': forceBool(durationEvent), 'start': start, 'end': end, } try: return self._sql.insert(session, 'EVENT', event) except sqlite3.DatabaseError as db_error: logger.error( "Failed to add event '%s': %s, recreating database", title, db_error) self._sql.delete_db() self._createDatabase(delete_existing=True) return self._sql.insert(session, 'EVENT', event) except Exception as add_error: # pylint: disable=broad-except logger.error("Failed to add event '%s': %s", title, add_error) return -1
def setEventEnd(self, eventId, end=None): if self._stopped: return -1 with self._db_lock, self._sql.session() as session: try: eventId = forceInt(eventId) if not end: end = timestamp() end = forceOpsiTimestamp(end) return self._sql.update(session, 'EVENT', f'`id` = {eventId}', { 'end': end, 'durationEvent': True }) except Exception as end_error: # pylint: disable=broad-except logger.error("Failed to set end of event '%s': %s", eventId, end_error) return -1
def testForceOpsiTimestampRaisesErrorsOnWrongInput(value): with pytest.raises(ValueError): forceOpsiTimestamp(value)
def testForceOpsiTimestamp(value, expected): result = forceOpsiTimestamp(value) assert expected == result assert isinstance(result, unicode)