From e8c6a506aac286269c6b22ca783a27607596b3e0 Mon Sep 17 00:00:00 2001 From: jrolle Date: Sat, 9 Dec 2023 13:10:13 -0500 Subject: [PATCH 1/6] Added method "only" to model class. --- src/masoniteorm/models/Model.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/masoniteorm/models/Model.py b/src/masoniteorm/models/Model.py index bc9b0ff1..d9c9e084 100644 --- a/src/masoniteorm/models/Model.py +++ b/src/masoniteorm/models/Model.py @@ -810,6 +810,12 @@ def method(*args, **kwargs): return None + def only(self, attributes: list[str]): + results: dict[str, Any] = {} + for attribute in attributes: + results[attribute] = self.get_raw_attribute(attribute) + return results + def __setattr__(self, attribute, value): if hasattr(self, "set_" + attribute + "_attribute"): method = getattr(self, "set_" + attribute + "_attribute") From 5da542390adf50c91635f06776b238d0ed3ba301 Mon Sep 17 00:00:00 2001 From: jrolle Date: Sat, 9 Dec 2023 13:44:59 -0500 Subject: [PATCH 2/6] updated only to support aliasing attribute names. This can be useful when mapping return data into new datasources which expect different naming conventions. This could be done at the database query level but is much easier to do using this method and only adds few extra LOC. --- src/masoniteorm/models/Model.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/masoniteorm/models/Model.py b/src/masoniteorm/models/Model.py index d9c9e084..5967874f 100644 --- a/src/masoniteorm/models/Model.py +++ b/src/masoniteorm/models/Model.py @@ -5,7 +5,7 @@ from datetime import datetime from datetime import time as datetimetime from decimal import Decimal -from typing import Any, Dict +from typing import Any, Dict, List import pendulum from inflection import tableize, underscore @@ -810,10 +810,19 @@ def method(*args, **kwargs): return None - def only(self, attributes: list[str]): + def only(self, attributes: List[str]) -> dict: results: dict[str, Any] = {} for attribute in attributes: - results[attribute] = self.get_raw_attribute(attribute) + if " as " in attribute: + attribute, alias = attribute.split(" as ") + alias = alias.strip() + attribute = attribute.strip() + else: + alias = attribute.strip() + attribute = attribute.strip() + + results[alias] = self.get_raw_attribute(attribute) + return results def __setattr__(self, attribute, value): From bf8ff3f017f9bcb173ab1e1c7c3f9bc26604958c Mon Sep 17 00:00:00 2001 From: Jarriq Rolle <36413952+JarriqTheTechie@users.noreply.github.com> Date: Sat, 9 Dec 2023 14:01:11 -0500 Subject: [PATCH 3/6] Update Model.py --- src/masoniteorm/models/Model.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/masoniteorm/models/Model.py b/src/masoniteorm/models/Model.py index 5967874f..6d06a958 100644 --- a/src/masoniteorm/models/Model.py +++ b/src/masoniteorm/models/Model.py @@ -5,7 +5,7 @@ from datetime import datetime from datetime import time as datetimetime from decimal import Decimal -from typing import Any, Dict, List +from typing import Any, Dict import pendulum from inflection import tableize, underscore @@ -810,7 +810,7 @@ def method(*args, **kwargs): return None - def only(self, attributes: List[str]) -> dict: + def only(self, attributes) -> dict: results: dict[str, Any] = {} for attribute in attributes: if " as " in attribute: From 57aa5cfac1bff5b4db64bdaee672025ed89e2ee2 Mon Sep 17 00:00:00 2001 From: Joseph Mancuso Date: Sun, 10 Dec 2023 12:59:29 -0500 Subject: [PATCH 4/6] Update setup.py --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 287a3b13..592f7b16 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ # Versions should comply with PEP440. For a discussion on single-sourcing # the version across setup.py and the project code, see # https://packaging.python.org/en/latest/single_source_version.html - version="2.19.2", + version="2.20.0", package_dir={"": "src"}, description="The Official Masonite ORM", long_description=long_description, From b9ba91d57a256d83c613c73b0b574594126d51a8 Mon Sep 17 00:00:00 2001 From: Joe Mancuso Date: Wed, 3 Jan 2024 22:21:57 -0500 Subject: [PATCH 5/6] added check for strings --- src/masoniteorm/models/Model.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/masoniteorm/models/Model.py b/src/masoniteorm/models/Model.py index 6d06a958..052f08ee 100644 --- a/src/masoniteorm/models/Model.py +++ b/src/masoniteorm/models/Model.py @@ -810,7 +810,9 @@ def method(*args, **kwargs): return None - def only(self, attributes) -> dict: + def only(self, attributes: list) -> dict: + if isinstance(attributes, str): + attributes = [attributes] results: dict[str, Any] = {} for attribute in attributes: if " as " in attribute: From 427fc9deabca6c425a9a86a269acc4da8c5679c9 Mon Sep 17 00:00:00 2001 From: Joe Mancuso Date: Wed, 3 Jan 2024 22:27:04 -0500 Subject: [PATCH 6/6] added tests --- tests/models/test_models.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/models/test_models.py b/tests/models/test_models.py index 520244fc..353884de 100644 --- a/tests/models/test_models.py +++ b/tests/models/test_models.py @@ -186,6 +186,15 @@ def test_force_update_on_model_class(self): self.assertIn("username", sql) self.assertIn("name", sql) + def test_only_method(self): + model = ModelTestForced.hydrate( + {"id": 1, "username": "joe", "name": "Joe", "admin": True} + ) + + + self.assertEquals({"username": "joe"}, model.only("username")) + self.assertEquals({"username": "joe"}, model.only(["username"])) + def test_model_update_without_changes_at_all(self): model = ModelTest.hydrate( {"id": 1, "username": "joe", "name": "Joe", "admin": True}