diff --git a/models.py b/models.py index 59276ead..c32c2117 100644 --- a/models.py +++ b/models.py @@ -54,6 +54,12 @@ def __init__(self, site_id, website): self.website = website def todata(self): + """ + Convert the Site object to a dictionary. + + Returns: + dict: A dictionary with 'id' and 'website' as keys. + """ result = { 'id': self.id, 'website': self.website @@ -62,6 +68,12 @@ def todata(self): @staticmethod def fieldnames(): + """ + Get the field names for the Site class. + + Returns: + list: A list containing the field names 'id' and 'website'. + """ result = ['id', 'website'] return result @@ -97,6 +109,15 @@ def __init__(self, translation=None, review_show_improvements_only=False): self.review_show_improvements_only = review_show_improvements_only def ensure_correct_points_range(self, points): + """ + Ensure the points are within the correct range [1.0, 5.0]. + + Args: + points (float): The points to be checked. + + Returns: + float: The points adjusted to be within the range [1.0, 5.0]. + """ if points < 1.0: points = 1.0 elif points > 5.0: @@ -104,6 +125,13 @@ def ensure_correct_points_range(self, points): return points def set_overall(self, points, review=''): + """ + Set the overall points and review. + + Args: + points (float): The points to be set. + review (str, optional): The review text. + """ review = review.replace('GOV-IGNORE', '') self.overall = self.ensure_correct_points_range(points) @@ -121,9 +149,22 @@ def set_overall(self, points, review=''): def get_overall(self): + """ + Get the average overall points. + + Returns: + float: The average overall points. + """ return self.transform_value(self.overall / self.overall_count) def set_integrity_and_security(self, points, review=''): + """ + Set the integrity and security points and review. + + Args: + points (float): The points to be set. + review (str, optional): The review text. + """ review = review.replace('GOV-IGNORE', '') self.integrity_and_security = self.ensure_correct_points_range(points) @@ -140,9 +181,22 @@ def set_integrity_and_security(self, points, review=''): review, points) def get_integrity_and_security(self): + """ + Get the average integrity and security points. + + Returns: + float: The average integrity and security points. + """ return self.transform_value(self.integrity_and_security / self.integrity_and_security_count) def set_performance(self, points, review=''): + """ + Set the performance points and review. + + Args: + points (float): The points to be set. + review (str, optional): The review text. + """ review = review.replace('GOV-IGNORE', '') self.performance = self.ensure_correct_points_range(points) @@ -160,9 +214,22 @@ def set_performance(self, points, review=''): def get_performance(self): + """ + Get the average performance points. + + Returns: + float: The average performance points. + """ return self.transform_value(self.performance / self.performance_count) def set_standards(self, points, review=''): + """ + Set the standards points and review. + + Args: + points (float): The points to be set. + review (str, optional): The review text. + """ review = review.replace('GOV-IGNORE', '') self.standards = self.ensure_correct_points_range(points) @@ -181,9 +248,22 @@ def set_standards(self, points, review=''): def get_standards(self): + """ + Get the average standards points. + + Returns: + float: The average standards points. + """ return self.transform_value(self.standards / self.standards_count) def set_a11y(self, points, review=''): + """ + Set the accessibility (a11y) points and review. + + Args: + points (float): The points to be set. + review (str, optional): The review text. + """ review = review.replace('GOV-IGNORE', '') self.a11y = self.ensure_correct_points_range(points) @@ -202,15 +282,54 @@ def set_a11y(self, points, review=''): def get_a11y(self): + """ + Get the average accessibility (a11y) points. + + Returns: + float: The average accessibility (a11y) points. + """ return self.transform_value(self.a11y / self.a11y_count) def isused(self): + """ + Gets info if this rating has any usefull info in it or not. + """ return self.is_set def transform_value(self, value): + """ + Transforms the input value into a float with two decimal places. + + This method takes a numerical input and converts it into a float, + rounded to two decimal places. It's useful for standardizing numerical + inputs to a consistent format. + + Args: + value (int or float): The numerical value to be transformed. + + Returns: + float: The input value transformed into a float with two decimal places. + + Example: + >>> transform_value(3.14159) + 3.14 + """ return float(f"{value:.2f}") def get_reviews(self): + """ + Constructs a review text based on the various review categories. + + This method generates a review text by concatenating the reviews of + different categories such as integrity and security, performance, + accessibility (a11y), and standards. + Each category's review is added to the text only if its value is not -1 and + the review is not an empty string. + The final review text is returned after removing any 'GOV-IGNORE' strings. + + Returns: + str: The constructed review text. + """ text = self.translation('TEXT_TEST_REVIEW_OVERVIEW').format(self.overall_review) if (self.get_integrity_and_security() != -1 and self.integrity_and_security_review != ''): text += self.translation('TEXT_TEST_REVIEW_INTEGRITY_SECURITY').format( @@ -227,6 +346,28 @@ def get_reviews(self): return text.replace('GOV-IGNORE', '') def todata(self): + """ + Converts the ratings into a dictionary. + + This method takes the ratings of various categories such as overall, + integrity and security, performance, standards, and + accessibility (a11y), and converts them into a dictionary. + The keys of the dictionary are the names of the categories and + the values are the corresponding ratings. + + Returns: + dict: A dictionary containing the ratings of various categories. + + Example: + >>> todata() + { + 'rating_overall': 4.5, + 'rating_security': 4.7, + 'rating_performance': 4.2, + 'rating_standards': 4.8, + 'rating_a11y': 4.3 + } + """ result = { 'rating_overall': self.get_overall(), 'rating_security': self.get_integrity_and_security(), @@ -238,6 +379,9 @@ def todata(self): @staticmethod def fieldnames(): + """ + Get the field names for the Rating class. + """ result = ['rating_overall', 'rating_integrity_and_security', 'rating_performance', 'rating_standards', 'rating_a11y'] return result @@ -305,6 +449,23 @@ def __add__(self, other): return tmp def get_combined_value(self, val1, val1_count, val2, val2_count): + """ + Combines two values and their counts based on their validity. + This method takes two values and their counts. + If both values are valid (not equal to -1), + it returns the sum of the values and the sum of the counts. + If neither value is valid, it returns -1 and 1. + If only one value is valid, it returns that value and its count. + + Args: + val1 (int or float): The first value. + val1_count (int): The count of the first value. + val2 (int or float): The second value. + val2_count (int): The count of the second value. + + Returns: + tuple: A tuple containing the combined value and the combined count. + """ val1_has_value = val1 != -1 val2_has_value = val2 != -1 @@ -372,11 +533,32 @@ def __init__(self, site_id, type_of_test, rating, test_date, json_check_data): # self.json_check_data = json_check_data def encode_review(self, review): + """ + Encodes the given review into UTF-8 format after removing 'GOV-IGNORE' from it. + This method is designed to solve encoding problems that might occur when handling reviews. + + Parameters: + review (str): The review text to be encoded. + + Returns: + bytes: The encoded review in UTF-8 format. + """ review_encoded = str(review).replace('GOV-IGNORE', '').encode( 'utf-8') # för att lösa encoding-probs return review_encoded def todata(self): + """ + Converts the object's data into a list of dictionaries. + This method takes the data of the object and + converts it into a list of dictionaries. + Each dictionary contains the site id, type of test, + ratings, date, reports, and data. + The reports and data are decoded from utf-8 format. + + Returns: + list: A list containing a dictionary of the object's data. + """ result = [{ 'site_id': self.site_id, 'type_of_test': self.type_of_test, @@ -397,6 +579,9 @@ def todata(self): @staticmethod def fieldnames(): + """ + Get the field names for the SiteTests class. + """ result = ['site_id', 'type_of_test', 'rating', 'rating_sec', 'rating_perf', 'rating_a11y', 'rating_stand',