Skip to content

Commit

Permalink
Code review updates #2
Browse files Browse the repository at this point in the history
  • Loading branch information
pszulczewski committed Mar 25, 2024
1 parent 2e39c9f commit 94b409e
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion docs/admin/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ PLUGINS = ["nautobot_floor_plan"]
# Optionally you can override default settings for config items to make grid labels like a chessboard (as seen in this example)
PLUGINS_CONFIG = {
"nautobot_floor_plan": {
"grid_x_axis_labels": "letters",
"default_x_axis_labels": "letters",
}
}
```
Expand Down
4 changes: 2 additions & 2 deletions nautobot_floor_plan/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class FloorPlanConfig(NautobotAppConfig):
min_version = "2.0.0"
max_version = "2.9999"
default_settings = {
"grid_x_axis_labels": choices.AxisLabelsChoices.NUMBERS,
"grid_y_axis_labels": choices.AxisLabelsChoices.NUMBERS,
"default_x_axis_labels": choices.AxisLabelsChoices.NUMBERS,
"default_y_axis_labels": choices.AxisLabelsChoices.NUMBERS,
}
caching_config = {}

Expand Down
12 changes: 6 additions & 6 deletions nautobot_floor_plan/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class Meta:
def __init__(self, *args, **kwargs):
"""Overwrite the constructor to set initial values for select widget."""
super().__init__(*args, **kwargs)
self.initial["x_axis_labels"] = get_app_settings_or_config("nautobot_floor_plan", "grid_x_axis_labels")
self.initial["y_axis_labels"] = get_app_settings_or_config("nautobot_floor_plan", "grid_y_axis_labels")
self.initial["x_axis_labels"] = get_app_settings_or_config("nautobot_floor_plan", "default_x_axis_labels")
self.initial["y_axis_labels"] = get_app_settings_or_config("nautobot_floor_plan", "default_y_axis_labels")


class FloorPlanBulkEditForm(TagsBulkEditFormMixin, NautobotBulkEditForm):
Expand Down 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.col_num_to_letter(self.instance.x_origin)
self.initial["x_origin"] = utils.num_to_letter(self.instance.x_origin)
if self.y_letters:
self.initial["y_origin"] = utils.col_num_to_letter(self.instance.y_origin)
self.initial["y_origin"] = utils.num_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.column_letter_to_num(x_origin)
return utils.letter_to_num(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.column_letter_to_num(y_origin)
return utils.letter_to_num(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 col_num_to_letter
from nautobot_floor_plan.utils import num_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 = col_num_to_letter(x) if self.floor_plan.x_axis_labels == AxisLabelsChoices.LETTERS else str(x)
label = num_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 = col_num_to_letter(y) if self.floor_plan.y_axis_labels == AxisLabelsChoices.LETTERS else str(y)
label = num_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": col_num_to_letter(x) if x_letters else x,
"y_origin": col_num_to_letter(y) if y_letters else y,
"x_origin": num_to_letter(x) if x_letters else x,
"y_origin": num_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_col_num_to_letter(self):
def test_num_to_letter(self):
test_cases = [
(1, "A"),
(26, "Z"),
Expand All @@ -24,9 +24,9 @@ def test_col_num_to_letter(self):

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

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

for letter, expected in test_cases:
with self.subTest(letter=letter):
self.assertEqual(utils.column_letter_to_num(letter), expected)
self.assertEqual(utils.letter_to_num(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 col_num_to_letter(number):
def num_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 col_num_to_letter(number):
return col_str


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

0 comments on commit 94b409e

Please sign in to comment.