Fixed #35117 -- Added support for the hectare unit in Area.

This commit is contained in:
Alexis Athlani 2024-01-15 23:16:12 +01:00 committed by Mariusz Felisiak
parent 6debeac9e7
commit c7e986fc9f
4 changed files with 33 additions and 2 deletions

View File

@ -347,8 +347,13 @@ class Distance(MeasureBase):
class Area(MeasureBase): class Area(MeasureBase):
STANDARD_UNIT = AREA_PREFIX + Distance.STANDARD_UNIT STANDARD_UNIT = AREA_PREFIX + Distance.STANDARD_UNIT
# Getting the square units values and the alias dictionary. # Getting the square units values and the alias dictionary.
UNITS = {"%s%s" % (AREA_PREFIX, k): v**2 for k, v in Distance.UNITS.items()} 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()} "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()} LALIAS = {k.lower(): v for k, v in ALIAS.items()}
def __truediv__(self, other): def __truediv__(self, other):

View File

@ -116,6 +116,19 @@ Unit Attribute Full name or alias(es)
For example, ``Area(sq_m=2)`` creates an :class:`Area` object For example, ``Area(sq_m=2)`` creates an :class:`Area` object
representing two square meters. 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 Measurement API
=============== ===============

View File

@ -70,6 +70,8 @@ Minor features
``metro_code`` and ``region_code``, but the previous keys are also retained ``metro_code`` and ``region_code``, but the previous keys are also retained
for backward compatibility. for backward compatibility.
* :class:`~django.contrib.gis.measure.Area` now supports the ``ha`` unit.
:mod:`django.contrib.messages` :mod:`django.contrib.messages`
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -38,6 +38,10 @@ class DistanceTest(SimpleTestCase):
with self.assertRaises(AttributeError): with self.assertRaises(AttributeError):
D(banana=100) D(banana=100)
def test_init_invalid_area_only_units(self):
with self.assertRaises(AttributeError):
D(ha=100)
def test_access(self): def test_access(self):
"Testing access in different units" "Testing access in different units"
d = D(m=100) d = D(m=100)
@ -294,6 +298,13 @@ class AreaTest(unittest.TestCase):
self.assertEqual(repr(a1), "Area(sq_m=100.0)") self.assertEqual(repr(a1), "Area(sq_m=100.0)")
self.assertEqual(repr(a2), "Area(sq_km=3.5)") 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): def test_hash(self):
a1 = A(sq_m=100) a1 = A(sq_m=100)
a2 = A(sq_m=1000000) a2 = A(sq_m=1000000)