def store(self): updateQuery = False database = Database() database.open() query = "SELECT COUNT(id) FROM rules WHERE id = '@@id@@';" query = self.__replaceSqlQueryToken(query) queryResult = database.executeReadQuery(query) if self.lastEditTimestamp == None: self.lastEditTimestamp = datetime.datetime.now() if self.creationTimestamp == None: self.creationTimestamp = datetime.datetime.now() if int(queryResult[0][0]) > 0: self.lastEditTimestamp = datetime.datetime.now() query = """UPDATE rules SET priority = '@@priority@@', category = '@@category@@', building_name = '@@building_name@@', group_id = '@@group_id@@', room_name = '@@room_name@@', author_uuid = '@@author_uuid@@', antecedent = '@@antecedent@@', consequent = '@@consequent@@', enabled = '@@enabled@@', deleted = '@@deleted@@', creation_timestamp = '@@creation_timestamp@@', last_edit_timestamp = '@@last_edit_timestamp@@' WHERE id = '@@id@@';""" updateQuery = True else: query = """INSERT INTO rules (priority, category, building_name, group_id, room_name, author_uuid, antecedent, consequent, enabled, deleted, creation_timestamp, last_edit_timestamp) VALUES ( '@@priority@@', '@@category@@', '@@building_name@@', '@@group_id@@', '@@room_name@@', '@@author_uuid@@', '@@antecedent@@', '@@consequent@@', '@@enabled@@', '@@deleted@@', '@@creation_timestamp@@', '@@last_edit_timestamp@@');""" query = self.__replaceSqlQueryToken(query) database.executeWriteQuery(query) self.id = int(database.getLastInsertedId()) if not self.id else self.id database.close() if not self.groupId and not updateQuery: from app.backend.model.rulePriority import RulePriority rulePriority = RulePriority(buildingName = self.buildingName, roomName = self.roomName, ruleId = self.id, rulePriority = self.__priority) rulePriority.store()
def setPriority(self, priority, roomName = None, buildingName = None): from app.backend.model.rulePriority import RulePriority if self.groupId and not roomName: self.__priority = priority elif self.groupId and roomName and buildingName: rulePriority = RulePriority(buildingName = buildingName, roomName = roomName, ruleId = self.id, rulePriority = priority) rulePriority.store() elif self.roomName: rulePriority = RulePriority(buildingName = self.buildingName, roomName = self.roomName, ruleId = self.id, rulePriority = priority) rulePriority.store() else: raise RuleInitFailedError("Some parameters are missing")
def getPriority(self, roomName = None, buildingName = None): from app.backend.model.rulePriority import RulePriority if self.groupId and not roomName: return self.__priority elif self.groupId and roomName and buildingName: try: rulePriority = RulePriority(buildingName = buildingName, roomName = roomName, ruleId = self.id) rulePriority.retrieve() return rulePriority.rulePriority except RoomRulePriorityNotFoundError as e: return self.__priority elif self.roomName: try: rulePriority = RulePriority(buildingName = self.buildingName, roomName = self.roomName, ruleId = self.id) rulePriority.retrieve() return rulePriority.rulePriority except Exception as e: return self.__priority else: raise RuleInitFailedError("Some parameters are missing")