Skip to content

Commit e818755

Browse files
committed
fix(bulk-upload) update bulk upload logic for XLSX files
1 parent e3cf7df commit e818755

14 files changed

+249
-138
lines changed
Binary file not shown.
Binary file not shown.

go-static/files/local_units/local-unit-bulk-upload-template.csv

Lines changed: 0 additions & 1 deletion
This file was deleted.

go-static/files/local_units/local-unit-health-bulk-upload-template.csv

Lines changed: 0 additions & 1 deletion
This file was deleted.

local_units/bulk_upload.py

Lines changed: 215 additions & 94 deletions
Large diffs are not rendered by default.

local_units/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ class HealthData(models.Model):
203203
null=True,
204204
blank=True,
205205
)
206+
206207
ambulance_type_a = models.IntegerField(verbose_name=_("Ambulance Type A"), blank=True, null=True)
207208
ambulance_type_b = models.IntegerField(verbose_name=_("Ambulance Type B"), blank=True, null=True)
208209
ambulance_type_c = models.IntegerField(verbose_name=_("Ambulance Type C"), blank=True, null=True)

local_units/serializers.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -703,8 +703,8 @@ class Meta:
703703
)
704704

705705
def validate_file(self, file):
706-
if not file.name.endswith(".csv"):
707-
raise serializers.ValidationError(gettext("File must be a CSV file."))
706+
if not file.name.endswith(".xlsx"):
707+
raise serializers.ValidationError(gettext("File must be a xlsx file."))
708708
if file.size > 10 * 1024 * 1024:
709709
raise serializers.ValidationError(gettext("File must be less than 10 MB."))
710710
return file
@@ -871,7 +871,7 @@ class LocalUnitBulkUploadDetailSerializer(serializers.ModelSerializer):
871871
visibility = serializers.CharField(required=True, allow_blank=True)
872872
date_of_data = serializers.CharField(required=False, allow_null=True)
873873
level = serializers.CharField(required=False, allow_null=True)
874-
health = HealthDataBulkUploadSerializer(required=False)
874+
health = serializers.PrimaryKeyRelatedField(queryset=HealthData.objects.all(), required=False, allow_null=True)
875875

876876
class Meta:
877877
model = LocalUnit
@@ -952,8 +952,4 @@ def validate(self, validated_data):
952952
validated_data["status"] = LocalUnit.Status.EXTERNALLY_MANAGED
953953

954954
# NOTE: Bulk upload doesn't call create() method
955-
health_data = validated_data.pop("health", None)
956-
if health_data:
957-
health_instance = HealthData.objects.create(**health_data)
958-
validated_data["health"] = health_instance
959955
return validated_data

local_units/test_views.py

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,15 +1177,15 @@ def setUp(self):
11771177
global_group.permissions.add(global_permission)
11781178
self.global_validator_user.groups.add(global_group)
11791179

1180-
file_path = os.path.join(settings.TEST_DIR, "local_unit/test.csv")
1180+
file_path = os.path.join(settings.TEST_DIR, "local_unit/test-admin.xlsx")
11811181
with open(file_path, "rb") as f:
11821182
self._file_content = f.read()
11831183

1184-
def create_upload_file(self, filename="test.csv"):
1184+
def create_upload_file(self, filename="test-admin.xlsx"):
11851185
"""
11861186
Always return a new file instance to prevent stream exhaustion.
11871187
"""
1188-
return SimpleUploadedFile(filename, self._file_content, content_type="text/csv")
1188+
return SimpleUploadedFile(filename, self._file_content, content_type="text/xlsx")
11891189

11901190
@mock.patch("local_units.tasks.process_bulk_upload_local_unit.delay")
11911191
def test_bulk_upload_local_unit(self, mock_delay):
@@ -1330,16 +1330,16 @@ def setUpTestData(cls):
13301330
cls.local_unit_type = LocalUnitType.objects.create(code=1, name="Administrative")
13311331
cls.local_unit_type2 = LocalUnitType.objects.create(code=2, name="Health Care")
13321332
cls.level = LocalUnitLevel.objects.create(level=0, name="National")
1333-
file_path = os.path.join(settings.TEST_DIR, "local_unit/test.csv")
1333+
file_path = os.path.join(settings.TEST_DIR, "local_unit/test-admin.xlsx")
13341334
with open(file_path, "rb") as f:
13351335
cls._file_content = f.read()
13361336

1337-
def create_upload_file(cls, filename="test.csv"):
1338-
return SimpleUploadedFile(filename, cls._file_content, content_type="text/csv")
1337+
def create_upload_file(cls, filename="test-admin.xlsx"):
1338+
return SimpleUploadedFile(filename, cls._file_content, content_type="text/xlsx")
13391339

13401340
def test_bulk_upload_with_incorrect_country(cls):
13411341
"""
1342-
Test bulk upload fails when the country does not match CSV data.
1342+
Test bulk upload fails when the country does not match xlsx data.
13431343
"""
13441344
cls.bulk_upload = LocalUnitBulkUploadFactory.create(
13451345
country=cls.country1,
@@ -1362,13 +1362,13 @@ def test_bulk_upload_with_incorrect_country(cls):
13621362

13631363
def test_bulk_upload_with_valid_country(cls):
13641364
"""
1365-
Test bulk upload succeeds when the country matches CSV data
1365+
Test bulk upload succeeds when the country matches xlsx data
13661366
"""
13671367
cls.bulk_upload = LocalUnitBulkUploadFactory.create(
13681368
country=cls.country2, # Brazil
13691369
local_unit_type=cls.local_unit_type,
13701370
triggered_by=cls.user,
1371-
file=cls.create_upload_file(), # CSV with Brazil rows
1371+
file=cls.create_upload_file(), # xlsx with Brazil rows
13721372
status=LocalUnitBulkUpload.Status.PENDING,
13731373
)
13741374
runner = BaseBulkUploadLocalUnit(cls.bulk_upload)
@@ -1381,7 +1381,7 @@ def test_bulk_upload_with_valid_country(cls):
13811381

13821382
def test_bulk_upload_fails_and_delete(cls):
13831383
"""
1384-
Test bulk upload fails and delete when CSV has incorrect data.
1384+
Test bulk upload fails and delete when xlsx has incorrect data.
13851385
"""
13861386
LocalUnitFactory.create_batch(
13871387
5,
@@ -1410,7 +1410,7 @@ def test_bulk_upload_fails_and_delete(cls):
14101410

14111411
def test_bulk_upload_deletes_old_and_creates_new_local_units(cls):
14121412
"""
1413-
Test bulk upload with correct CSV data.
1413+
Test bulk upload with correct data.
14141414
"""
14151415
old_local_unit = LocalUnitFactory.create(
14161416
country=cls.country2,
@@ -1442,10 +1442,14 @@ def test_empty_administrative_file(cls):
14421442
Test bulk upload file is empty
14431443
"""
14441444

1445-
file_path = os.path.join(settings.STATICFILES_DIRS[0], "files", "local_units", "local-unit-bulk-upload-template.csv")
1445+
file_path = os.path.join(
1446+
settings.STATICFILES_DIRS[0], "files", "local_units", "Administrative Bulk Import Template - Local Units.xlsx"
1447+
)
14461448
with open(file_path, "rb") as f:
14471449
file_content = f.read()
1448-
empty_file = SimpleUploadedFile(name="local-unit-bulk-upload-template.csv", content=file_content, content_type="text/csv")
1450+
empty_file = SimpleUploadedFile(
1451+
name="Administrative Bulk Import Template - Local Units.xlsx", content=file_content, content_type="text/xlsx"
1452+
)
14491453
LocalUnitFactory.create_batch(
14501454
5,
14511455
country=cls.country2,
@@ -1531,16 +1535,16 @@ def setUpTestData(cls):
15311535
cls.professional_training_facilities = ProfessionalTrainingFacility.objects.create(code=1, name="Nurses")
15321536
cls.general_medical_services = GeneralMedicalService.objects.create(code=1, name="Minor Trauma")
15331537

1534-
file_path = os.path.join(settings.TEST_DIR, "local_unit/test-health.csv")
1538+
file_path = os.path.join(settings.TEST_DIR, "local_unit/test-health.xlsx")
15351539
with open(file_path, "rb") as f:
15361540
cls._file_content = f.read()
15371541

1538-
def create_upload_file(cls, filename="test-health.csv"):
1539-
return SimpleUploadedFile(filename, cls._file_content, content_type="text/csv")
1542+
def create_upload_file(cls, filename="test-health.xlsx"):
1543+
return SimpleUploadedFile(filename, cls._file_content, content_type="text/xlsx")
15401544

15411545
def test_bulk_upload_health_with_incorrect_country(cls):
15421546
"""
1543-
Should fail when CSV rows are not equal to bulk upload country.
1547+
Should fail when rows are not equal to bulk upload country.
15441548
"""
15451549
cls.bulk_upload = LocalUnitBulkUploadFactory.create(
15461550
country=cls.country1,
@@ -1640,12 +1644,12 @@ def test_empty_health_template_file(cls):
16401644
"""
16411645

16421646
file_path = os.path.join(
1643-
settings.STATICFILES_DIRS[0], "files", "local_units", "local-unit-health-bulk-upload-template.csv"
1647+
settings.STATICFILES_DIRS[0], "files", "local_units", "Health Care Bulk Import Template - Local Units.xlsx"
16441648
)
16451649
with open(file_path, "rb") as f:
16461650
file_content = f.read()
16471651
empty_file = SimpleUploadedFile(
1648-
name="local-unit-health-bulk-upload-template.csv", content=file_content, content_type="text/csv"
1652+
name="Health Care Bulk Import Template - Local Units.xlsx", content=file_content, content_type="text/xlsx"
16491653
)
16501654
health_data = HealthDataFactory.create_batch(
16511655
5,

local_units/utils.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,13 @@ def get_model_field_names(
7272

7373

7474
def normalize_bool(value):
75-
if isinstance(value, bool):
76-
return value
7775
if not value:
7876
return False
7977
val = str(value).strip().lower()
80-
if val in ("true", "1", "yes", "y"):
78+
if val in ("yes"):
8179
return True
82-
if val in ("false", "0", "no", "n"):
80+
if val in ("no"):
8381
return False
84-
return False
8582

8683

8784
def wash(string):
@@ -91,4 +88,4 @@ def wash(string):
9188

9289

9390
def numerize(value):
94-
return value if value.isdigit() else 0
91+
return value if value else 0

local_units/views.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,10 @@ class LocalUnitBulkUploadViewSet(
436436
def get_bulk_upload_template(self, request):
437437
template_type = request.query_params.get("bulk_upload_template", "local_unit")
438438
if template_type == "health_care":
439-
file_url = request.build_absolute_uri(static("files/local_units/local-unit-health-bulk-upload-template.csv"))
439+
file_url = request.build_absolute_uri(static("files/local_units/Health Care Bulk Import Template - Local Units.xlsx"))
440440
else:
441-
file_url = request.build_absolute_uri(static("files/local_units/local-unit-bulk-upload-template.csv"))
441+
file_url = request.build_absolute_uri(
442+
static("files/local_units/Administrative Bulk Import Template - Local Units.xlsx")
443+
)
442444
template = {"template_url": file_url}
443445
return response.Response(LocalUnitTemplateFilesSerializer(template).data)

0 commit comments

Comments
 (0)