def editRule(self, ruleId, priority, buildingName, roomName, editorUuid, groupId, ruleBody = None, antecedent = None, consequent = None, enabled = True): checkData(locals()) if not ruleBody: if not antecedent: raise MissingInputDataError("") if not consequent: raise MissingInputDataError("") if ruleBody and antecedent: raise TooManyInputParametersError("") if ruleBody and consequent: raise TooManyInputParametersError("") from app.backend.model.rule import Rule oldRule = Rule(id = ruleId) oldRule.retrieve() from app.backend.model.user import User author = User(uuid = oldRule.authorUuid) author.retrieve() editor = User(uuid = editorUuid) editor.retrieve() writePermission = False errorMessage = "" # If this room is not related to a room, I cannot modify it from this method if not oldRule.roomName: raise UserCredentialError("You cannot modify this group related rule.") # If this rule is not about this building if oldRule.buildingName != buildingName: raise UserCredentialError("You cannot modify the rule of another building.") if author.uuid != editor.uuid: if not groupId and not oldRule.getRoom().roomName in list(r.roomName for r in editor.getRooms()): raise UserCredentialError("You cannot modify a rule of a room you do not own.") #if oldRule.getRoom() not in editor.getRooms(): # raise UserCredentialError("You cannot modify a rule of a room you do not own.") if groupId: from app.backend.model.group import Group group = Group(buildingName = buildingName, id = groupId) group.retrieve() if not group: raise UserCredentialError("You cannot modify a rule of a room you do not own.") if not group.crossRoomsValidation: raise UserCredentialError("You cannot modify a rule you do not own.") if not group.id in list(g.id for g in editor.getGroups()): raise UserCredentialError("You cannot modify a rule belonging to a group you do not own.") if not oldRule.getRoom().roomName in list(r.roomName for r in group.getRooms()): raise UserCredentialError("You cannot modify a rule belonging to room that does not belongs to a group you do not own.") if editor.level < oldRule.getPriority(): raise UserCredentialError("You cannot modify this rule since it has a too high priority for your user level") authorUuid = editorUuid result = self.__addOrModifyRule(ruleId = ruleId, priority = priority, buildingName = buildingName, roomName = roomName, authorUuid = authorUuid, ruleBody = ruleBody, antecedent = antecedent, consequent = consequent, enabled = enabled) from app.backend.controller.notificationsManager import NotificationsManager notifications = NotificationsManager() messageSubject = "Rule modified in building " + str(buildingName) + " room " + str(roomName) messageText = "The user " + str(editor.username) + " edited (or tried to edit) the rule <<" + str(oldRule.getFullRepresentation()) + ">>. The new rule is <<" + str(ruleBody) + ">>" notifications.sendNotification(buildingName = buildingName, roomName = roomName, messageSubject = messageSubject, messageText = messageText) return result
def editRule(self, ruleId = None, priority = None, buildingName = None, groupId = None, authorUuid = None, ruleBody = None): from app.backend.model.rule import Rule oldRule = Rule(id = ruleId) oldRule.retrieve() from app.backend.model.user import User author = User(uuid = authorUuid) author.retrieve() result = self.__addOrModifyRule(ruleId = ruleId, priority = priority, buildingName = buildingName, groupId = groupId, authorUuid = authorUuid, ruleBody = ruleBody) from app.backend.controller.notificationsManager import NotificationsManager notifications = NotificationsManager() messageSubject = "Rule modified in building " + str(buildingName) + " group " + str(groupId) messageText = "The user " + str(author.username) + " edited (or tried to edit) the rule <<" + str(oldRule.getFullRepresentation()) + ">>. The new rule is <<" + str(ruleBody) + ">>" notifications.sendNotification(buildingName = buildingName, groupId = groupId, messageSubject = messageSubject, messageText = messageText) return result