Skip to content

Commit

Permalink
Update CHANGELOG and other small changes to code
Browse files Browse the repository at this point in the history
  • Loading branch information
DevChima committed Nov 28, 2024
1 parent bd3bf0d commit 67d147a
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 23 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

<!--
## Unreleased
- Importing assessments with commas in inflection values will now be caught with a helpful error message
-->

## v1.3.0
Expand All @@ -29,7 +30,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Importing certain ContentPage fields with whitespace-only values will no longer throw unhandled exceptions
- UI: ContentPage listed multiple times in sidebar
- Ordered content set XLSX export
- Importing assessments with commas in inflection values will now be caught with a helpful error message

### Changed
- Submit WA templates with Buttons if they exist, only submit with Quick Replies if no Buttons
Expand Down
35 changes: 17 additions & 18 deletions home/import_assessments.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import contextlib
import csv
import re
from dataclasses import dataclass, field, fields
from datetime import datetime
from io import BytesIO, StringIO
Expand Down Expand Up @@ -116,18 +115,22 @@ def clean_excel_cell(cell_value: str | float | datetime | None) -> str:
first_row = next(worksheet.iter_rows(max_row=1, values_only=True))
header = [clean_excel_cell(cell) if cell else None for cell in first_row]
rows: list[AssessmentRow] = []
i = 2
for row in worksheet.iter_rows(min_row=2, values_only=True):
r = {}
for name, cell in zip(header, row): # noqa: B905 (TODO: strict?)
if name and cell:
r[name] = clean_excel_cell(cell)
if r:
rows.append(AssessmentRow.from_flat(r))
rows.append(AssessmentRow.from_flat(r, i))
i += 1
return rows

def parse_csv(self) -> list["AssessmentRow"]:
reader = csv.DictReader(StringIO(self.file_content.decode()))
rows = [AssessmentRow.from_flat(row) for row in reader]
rows = [
AssessmentRow.from_flat(row, i) for i, row in enumerate(reader, start=2)
]
return rows

def set_progress(self, message: str, progress: int) -> None:
Expand Down Expand Up @@ -366,15 +369,15 @@ def fields(cls) -> list[str]:
return [field.name for field in fields(cls)]

@classmethod
def from_flat(cls, row: dict[str, str]) -> "AssessmentRow":
def from_flat(cls, row: dict[str, str], row_num: int) -> "AssessmentRow":
"""
Creates an AssessmentRow instance from a row in the import, deserialising the
fields.
"""

high_inflection = row.get("high_inflection")
medium_inflection = row.get("medium_inflection")
check_punctuation(high_inflection, medium_inflection)
check_punctuation(high_inflection, medium_inflection, row_num)

try:
row = {
Expand Down Expand Up @@ -416,29 +419,25 @@ def get_content_page_id_from_slug(slug: str) -> int:


def check_punctuation(
high_inflection: str | None, medium_inflection: str | None
high_inflection: str | None, medium_inflection: str | None, row_num: int
) -> None:
if high_inflection is not None:
try:
high_inflection.isdigit()
except AttributeError:
high_inflection = str(high_inflection)
punctuaton = bool(re.search(r",", high_inflection))
high_inflection = str(high_inflection)
punctuaton = "," in high_inflection
if punctuaton:
raise ImportAssessmentException(
"Invalid number format for high inflection. "
"Please use '.' instead of ',' for decimals."
"Please use '.' instead of ',' for decimals.",
row_num,
)
if medium_inflection is not None:
try:
medium_inflection.isdigit()
except AttributeError:
medium_inflection = str(medium_inflection)
punctuation = bool(re.search(r",", medium_inflection))
medium_inflection = str(medium_inflection)
punctuation = "," in medium_inflection
if punctuation:
raise ImportAssessmentException(
"Invalid number format for medium inflection. "
"Please use '.' instead of ',' for decimals."
"Please use '.' instead of ',' for decimals.",
row_num,
)


Expand Down
Binary file not shown.
Binary file not shown.
11 changes: 7 additions & 4 deletions home/tests/test_assessment_import_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@ def test_invalid_high_inflecton_format(self, xlsx_impexp: ImportExport) -> None:
e.value.message == "Invalid number format for high inflection. "
"Please use '.' instead of ',' for decimals."
)
assert e.value.row_num == 5

def test_invalid_medium_inflecton_format(self, xlsx_impexp: ImportExport) -> None:
"""
Expand All @@ -509,6 +510,7 @@ def test_invalid_medium_inflecton_format(self, xlsx_impexp: ImportExport) -> Non
e.value.message == "Invalid number format for medium inflection. "
"Please use '.' instead of ',' for decimals."
)
assert e.value.row_num == 2

def test_invalid_high_inflecton_csv_format(self, csv_impexp: ImportExport) -> None:
"""
Expand All @@ -526,18 +528,19 @@ def test_invalid_high_inflecton_csv_format(self, csv_impexp: ImportExport) -> No
e.value.message == "Invalid number format for high inflection. "
"Please use '.' instead of ',' for decimals."
)
assert e.value.row_num == 2

def test_extra_rows(self, csv_impexp: ImportExport) -> None:
def test_extra_columns(self, csv_impexp: ImportExport) -> None:
"""
Importing a csv with an extra comma so there are more
row values than headers should return an intuitive error message
column values than headers should return an intuitive error message
(This uses extra_rows.csv)
(This uses extra_columns.csv)
"""

with pytest.raises(ImportAssessmentException) as e:
csv_impexp.import_content_file("assessment_results.csv", purge=False)
csv_impexp.import_file("extra_rows.csv")
csv_impexp.import_file("extra_columns.csv")
assert (
e.value.message == "Invalid format. Please check that all row values "
"have headers."
Expand Down

0 comments on commit 67d147a

Please sign in to comment.