Skip to content

Commit

Permalink
Code review updates #3 update naming
Browse files Browse the repository at this point in the history
  • Loading branch information
pszulczewski committed Mar 25, 2024
1 parent 94b409e commit f94234f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
8 changes: 4 additions & 4 deletions nautobot_floor_plan/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ def __init__(self, *args, **kwargs):

if self.instance.x_origin or self.instance.y_origin:
if self.x_letters:
self.initial["x_origin"] = utils.num_to_letter(self.instance.x_origin)
self.initial["x_origin"] = utils.grid_number_to_letter(self.instance.x_origin)
if self.y_letters:
self.initial["y_origin"] = utils.num_to_letter(self.instance.y_origin)
self.initial["y_origin"] = utils.grid_number_to_letter(self.instance.y_origin)

def letter_validator(self, value, axis):
"""Validate that origin uses combination of letters."""
Expand All @@ -137,7 +137,7 @@ def clean_x_origin(self):
x_origin = self.cleaned_data.get("x_origin")
if self.x_letters:
self.letter_validator(x_origin, "X")
return utils.letter_to_num(x_origin)
return utils.grid_letter_to_number(x_origin)
self.number_validator(x_origin, "X")
return int(x_origin)

Expand All @@ -146,6 +146,6 @@ def clean_y_origin(self):
y_origin = self.cleaned_data.get("y_origin")
if self.y_letters:
self.letter_validator(y_origin, "Y")
return utils.letter_to_num(y_origin)
return utils.grid_letter_to_number(y_origin)
self.number_validator(y_origin, "Y")
return int(y_origin)
10 changes: 5 additions & 5 deletions nautobot_floor_plan/svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from nautobot.core.templatetags.helpers import fgcolor

from nautobot_floor_plan.choices import RackOrientationChoices, AxisLabelsChoices
from nautobot_floor_plan.utils import num_to_letter
from nautobot_floor_plan.utils import grid_number_to_letter


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -110,7 +110,7 @@ def _draw_grid(self, drawing):
)
# Axis labels
for x in range(1, self.floor_plan.x_size + 1):
label = num_to_letter(x) if self.floor_plan.x_axis_labels == AxisLabelsChoices.LETTERS else str(x)
label = grid_number_to_letter(x) if self.floor_plan.x_axis_labels == AxisLabelsChoices.LETTERS else str(x)
drawing.add(
drawing.text(
label,
Expand All @@ -122,7 +122,7 @@ def _draw_grid(self, drawing):
)
)
for y in range(1, self.floor_plan.y_size + 1):
label = num_to_letter(y) if self.floor_plan.y_axis_labels == AxisLabelsChoices.LETTERS else str(y)
label = grid_number_to_letter(y) if self.floor_plan.y_axis_labels == AxisLabelsChoices.LETTERS else str(y)
drawing.add(
drawing.text(
label,
Expand All @@ -142,8 +142,8 @@ def _draw_grid(self, drawing):
query_params = urlencode(
{
"floor_plan": self.floor_plan.pk,
"x_origin": num_to_letter(x) if x_letters else x,
"y_origin": num_to_letter(y) if y_letters else y,
"x_origin": grid_number_to_letter(x) if x_letters else x,
"y_origin": grid_number_to_letter(y) if y_letters else y,
"return_url": self.return_url,
}
)
Expand Down
8 changes: 4 additions & 4 deletions nautobot_floor_plan/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class TestUtils(unittest.TestCase):
"""Test class."""

def test_num_to_letter(self):
def test_grid_number_to_letter(self):
test_cases = [
(1, "A"),
(26, "Z"),
Expand All @@ -24,9 +24,9 @@ def test_num_to_letter(self):

for num, expected in test_cases:
with self.subTest(num=num):
self.assertEqual(utils.num_to_letter(num), expected)
self.assertEqual(utils.grid_number_to_letter(num), expected)

def test_letter_to_num(self):
def test_gird_letter_to_number(self):
test_cases = [
("A", 1),
("Z", 26),
Expand All @@ -42,4 +42,4 @@ def test_letter_to_num(self):

for letter, expected in test_cases:
with self.subTest(letter=letter):
self.assertEqual(utils.letter_to_num(letter), expected)
self.assertEqual(utils.grid_letter_to_number(letter), expected)
6 changes: 3 additions & 3 deletions nautobot_floor_plan/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Utilities module."""


def num_to_letter(number):
def grid_number_to_letter(number):
"""Returns letter for number [1 - 26] --> [A - Z], [27 - 52] --> [AA - AZ]."""
col_str = ""
while number:
Expand All @@ -14,9 +14,9 @@ def num_to_letter(number):
return col_str


def letter_to_num(letter):
def grid_letter_to_number(letter):
"""Returns number for letter [A - Z] --> [1 - 26], [AA - AZ] --> [27 - 52]."""
number = ord(letter[-1]) - 64
if letter[:-1]:
return 26 * (letter_to_num(letter[:-1])) + number
return 26 * (grid_letter_to_number(letter[:-1])) + number
return number

0 comments on commit f94234f

Please sign in to comment.