def tearDown(self) -> None: """ Clean up all dependencies after tests""" for alias in self.aliases: try: if AttrAlias.get_by_user_id(self.user.id): alias.delete() alias.commit() except Exception: pass for attr in self.attributes: try: if Attributes.get_by_id(attr.id): attr.delete() attr.commit() except Exception: pass for sub_theme in self.sub_themes: try: if SubTheme.get_by_id(sub_theme.id): sub_theme.delete() sub_theme.commit() except Exception: pass for unit in self.units: try: if Unit.get_by_id(unit.id): unit.delete() unit.commit() except Exception: pass try: if Theme.get_by_id(self.theme.id): self.theme.delete() self.theme.commit() except Exception: pass self.client.post('/logout', headers=self.auth_header) if self.user: if Users.find_by_id(self.user.id): try: self.user.delete() self.user.commit() except Exception: pass self.app_context.pop()
def get(self) -> (dict, HTTPStatus): """ Check for triggered alerts. :return: On success, an HTTP response with a JSON body content containing the maximum and minimum alerts that have been exceeded with an HTTP status code of 200 (OK), otherwise an HTTP response with a JSON body content containing an appropriate error message and appropriate an HTTP status code """ args = self.reqparser.parse_args() # Use current user_id if not user_id was parsed if "user_id" not in args: user = Users.find_by_email(get_jwt_identity()) if user: args["user_id"] = user.id else: # Return Error current user id not found return (dict(error="User id not found for Current user, " "User session may have timed out"), HTTPStatus.INTERNAL_SERVER_ERROR) else: user = Users.find_by_id(args["user_id"]) if not user: # Return error Parsed User Id not found return dict(error="User with id {} not found.".format( args["user_id"])), HTTPStatus.NOT_FOUND # Check Attribute exists if not Attributes.get_by_id(args["attribute_id"]): # Return Error Attribute ID not found return dict(error="Attribute id {} not found.".format( args["attribute_id"])), HTTPStatus.NOT_FOUND # Get Attribute Max and Minimums attribute_range = AttributeRange.get_by_attr_id(args["attribute_id"]) if not attribute_range: # Return eroor Attribute range not found return (dict(error="Attribute range not found", **args), HTTPStatus.NOT_FOUND) # Check Alerts max_alerts = AlertWidgetModel.get_max_alerts(attribute_range, user_id=user.id) min_alerts = AlertWidgetModel.get_min_alerts(attribute_range, user_id=user.id) return dict(max=max_alerts, min=min_alerts), 200
def post(self) -> ({str: str}, HTTPStatus): """ Update AttrAlias (Attribute Alias) if it exists otherwise create an AttrAlias :param attribute_id: Parent Attribute id to alias :param user_id: Owner user id :param name: Alias for Attribute name :param table_name: Alias for Attribute table_name :param unit_id: Id of unit to be used :param description: Custom user description for Attribute :return: AttrAlias with an HTTPstatus code OK (200) or an error message and a the appropriate HTTPStatus code """ args = self.reqpaser.parse_args() attribute = Attributes.get_by_id(args["attribute_id"]) if not attribute: return {"error": "Attribute Not Found."}, HTTPStatus.NOT_FOUND user = Users.find_by_id(args["user_id"]) if not user: return {"error": "User Not Found."}, HTTPStatus.NOT_FOUND alias = AttrAlias.get_by(user_id=args["user_id"], attribute_id=args["attribute_id"]) if alias: alias.name = args["name"] if "name" in args else alias.name alias.table_name = args["table_name"] if "table_name" in args else alias.table_name alias.unit_id = args["unit_id"] if "unit_id" in args else alias.unit_id alias.description = args["description"] if "description" in args else alias.description else: alias = AttrAlias(args["attribute_id"], args["user_id"], name=args.get("name"), table_name=args.get("table_name"), description=args.get("description")) try: alias.save() alias.commit() except Exception as e: logger.error("failed to persist attribute alias", e) return {"error": "Failed to commit attribute alias to database", "exception": e}, HTTPStatus.INTERNAL_SERVER_ERROR return alias.json(), HTTPStatus.OK
def get(self) -> ({str: str}, HTTPStatus): """ Fetch AttrAlias (Attribute Alias) from the database :param attribute_id: Parent Attribute id :param user_id: Owner user id :return: AttrAlias with an HTTPstatus code OK (200) or an error message and a the appropriate HTTPStatus code """ args = self.reqpaser.parse_args() attribute = Attributes.get_by_id(args["attribute_id"]) if not attribute: return {"error": "Attribute Not Found."}, HTTPStatus.NOT_FOUND user = Users.find_by_id(args["user_id"]) if not user: return {"error": "User Not Found."}, HTTPStatus.NOT_FOUND alias = AttrAlias.get_by(user_id=args["user_id"], attribute_id=args["attribute_id"]) if not alias: return attribute.json(), HTTPStatus.OK return alias.json(), HTTPStatus.OK
def post(self) -> ({str: str}, HTTPStatus): """ Update Attributes SubTheme :param attribute_id: Attributes identification number :param sub_theme_id: SubTheme identification number :type attribute_id: str :type sub_theme_id: int :return: A JSON containing a message, Attribute id, SubTheme id and a HTTPStatus 200 (OK) on success otherwise a JSON with a error message and a HTTPStatus 404 (NotFound) """ args = self.reqpaser.parse_args() attribute = AttrAlias.get_by_attr_id(args["attribute_id"]) if not attribute: attribute = Attributes.get_by_id(args["attribute_id"]) if not attribute: return { "error": "Attribute not found", "id": args["attribute_id"] }, HTTPStatus.NOT_FOUND sub_theme = SubTheme.get_by(id=args["sub_theme_id"]) if not sub_theme: return { "error": "SubTheme not found", "id": args["sub_theme_id"] }, HTTPStatus.NOT_FOUND attribute.sub_theme_id(sub_theme.id) attribute.save() attribute.commit() return { "message": "Attribute SubTheme updated", "attribute_id": args["attribute_id"], "sub_theme_id": args["sub_theme_id"] }, HTTPStatus.OK
def get(self) -> [db.Model]: """ Fetch Attributes from the database :param attribute_id: Attribute id :param subtheme_id: SubTheme id :return: A list of Attributes with an HTTPstatus code OK (200) or an error message and a the appropriate HTTPStatus code """ args = self.reqpaser.parse_args() # Fetch by attribute_id if "attribute_id" in args and "subtheme_id" not in args: attribute = Attributes.get_by_id(args["attribute_id"]) if not attribute: return { "error": "Attribute not found", "id": args["attribute_id"] }, HTTPStatus.NOT_FOUND content = [attribute] return [attr.json() for attr in content], HTTPStatus.OK # Fetch by subtheme_id elif "attribute_id" not in args and "subtheme_id" in args: attributes = Attributes.get_by_sub_theme_id(args["subtheme_id"]) if not attributes: return { "error": "Attributes not found", "subtheme_id": args["subtheme_id"] }, HTTPStatus.NOT_FOUND if isinstance(attributes, Attributes): content = [attributes] return [attr.json() for attr in content], HTTPStatus.OK return [attr.json() for attr in attributes], HTTPStatus.OK # Fetch all attribute attributes = Attributes.get_all() return [attr.json() for attr in attributes], HTTPStatus.OK
def check_min_and_max_alert_widgets(attribute_range_entry: db.Model): """ Send emails to users for alert widgets that have been triggered :param attribute_range_entry: Entry in the attribute range table """ if attribute_range_entry.maximum: max_alerts = AlertWidgetModel.get_max_alerts(attribute_range_entry) for alert in max_alerts: user_details = Users.find_by_id(alert["user_id"]) if user_details: attr = Attributes.get_by_id(attribute_range_entry.attribute_id) if attr: if not send_alert_email( user_details.email, user_details.fullname, attr.name, attribute_range_entry.maximum, attribute_range_entry.maximum_recorded_date, attribute_range_entry.maximum_sensor_id, alert["max_threshold"], "exceeded"): logger.error("Server error prevented the " "sending of a max alert email " "to {} regarding attribute with " "id {}".format( user_details.email, attribute_range_entry.attribute_id)) else: logger.error("Could not send max alert email to " "user with id {} as the attribute with " "id {} does not exist ".format( alert["user_id"], attribute_range_entry.attribute_id)) else: logger.error("Could not send max alert email to " "user with id {} as the user does " "not exist ".format(alert["user_id"])) if attribute_range_entry.minimum: min_alerts = AlertWidgetModel.get_min_alerts(attribute_range_entry) for alert in min_alerts: user_details = Users.find_by_id(alert["user_id"]) if user_details: attr = Attributes.get_by_id(attribute_range_entry.attribute_id) if attr: if not send_alert_email( user_details.email, user_details.fullname, attr.name, attribute_range_entry.minimum, attribute_range_entry.minimum_recorded_date, attribute_range_entry.minimum_sensor_id, alert["min_threshold"], "fell short of"): logger.error("Server error prevented the sending of " "a min alert email to {} regarding " "attribute with id {}".format( user_details.email, attribute_range_entry.attribute_id)) else: logger.error("Could not send min alert email to " "user with id {} as the attribute with " "id {} does not exist ".format( alert["user_id"], attribute_range_entry.attribute_id)) else: logger.error("Could not send min alert email to " "user with id {} as the user does " "not exist ".format(alert["user_id"]))