From c7e986fc9f4848bd757d4b9b70a40586d2cee9fb Mon Sep 17 00:00:00 2001 From: Alexis Athlani Date: Mon, 15 Jan 2024 23:16:12 +0100 Subject: [PATCH] Fixed #35117 -- Added support for the hectare unit in Area. --- django/contrib/gis/measure.py | 9 +++++++-- docs/ref/contrib/gis/measure.txt | 13 +++++++++++++ docs/releases/5.1.txt | 2 ++ tests/gis_tests/test_measure.py | 11 +++++++++++ 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/django/contrib/gis/measure.py b/django/contrib/gis/measure.py index 590a80293ab..707c061a29a 100644 --- a/django/contrib/gis/measure.py +++ b/django/contrib/gis/measure.py @@ -347,8 +347,13 @@ class Distance(MeasureBase): class Area(MeasureBase): STANDARD_UNIT = AREA_PREFIX + Distance.STANDARD_UNIT # Getting the square units values and the alias dictionary. - UNITS = {"%s%s" % (AREA_PREFIX, k): v**2 for k, v in Distance.UNITS.items()} - ALIAS = {k: "%s%s" % (AREA_PREFIX, v) for k, v in Distance.ALIAS.items()} + UNITS = {"%s%s" % (AREA_PREFIX, k): v**2 for k, v in Distance.UNITS.items()} | { + "ha": 10000, + } + ALIAS = {k: "%s%s" % (AREA_PREFIX, v) for k, v in Distance.ALIAS.items()} | { + "hectare": "ha", + } + LALIAS = {k.lower(): v for k, v in ALIAS.items()} def __truediv__(self, other): diff --git a/docs/ref/contrib/gis/measure.txt b/docs/ref/contrib/gis/measure.txt index ad02db87fac..cee42112206 100644 --- a/docs/ref/contrib/gis/measure.txt +++ b/docs/ref/contrib/gis/measure.txt @@ -116,6 +116,19 @@ Unit Attribute Full name or alias(es) For example, ``Area(sq_m=2)`` creates an :class:`Area` object representing two square meters. +In addition to unit with the ``sq_`` prefix, the following units are also +supported on :class:`Area`: + +================================= ======================================== +Unit Attribute Full name or alias(es) +================================= ======================================== +``ha`` Hectare +================================= ======================================== + +.. versionchanged:: 5.1 + + Support for the ``ha`` unit was added. + Measurement API =============== diff --git a/docs/releases/5.1.txt b/docs/releases/5.1.txt index 30317eaa191..1f7d26d7b82 100644 --- a/docs/releases/5.1.txt +++ b/docs/releases/5.1.txt @@ -70,6 +70,8 @@ Minor features ``metro_code`` and ``region_code``, but the previous keys are also retained for backward compatibility. +* :class:`~django.contrib.gis.measure.Area` now supports the ``ha`` unit. + :mod:`django.contrib.messages` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/tests/gis_tests/test_measure.py b/tests/gis_tests/test_measure.py index 2a922abf9b0..19644c1f98d 100644 --- a/tests/gis_tests/test_measure.py +++ b/tests/gis_tests/test_measure.py @@ -38,6 +38,10 @@ class DistanceTest(SimpleTestCase): with self.assertRaises(AttributeError): D(banana=100) + def test_init_invalid_area_only_units(self): + with self.assertRaises(AttributeError): + D(ha=100) + def test_access(self): "Testing access in different units" d = D(m=100) @@ -294,6 +298,13 @@ class AreaTest(unittest.TestCase): self.assertEqual(repr(a1), "Area(sq_m=100.0)") self.assertEqual(repr(a2), "Area(sq_km=3.5)") + def test_hectare(self): + a = A(sq_m=10000) + self.assertEqual(a.ha, 1) + + def test_hectare_unit_att_name(self): + self.assertEqual(A.unit_attname("Hectare"), "ha") + def test_hash(self): a1 = A(sq_m=100) a2 = A(sq_m=1000000)