Skip to content

Commit 77fac75

Browse files
committed
fix(bulk-upload) update bulk upload logic for XLSX files
1 parent 2960f07 commit 77fac75

14 files changed

+248
-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: 214 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
@@ -1083,15 +1083,15 @@ def setUp(self):
10831083
global_group.permissions.add(global_permission)
10841084
self.global_validator_user.groups.add(global_group)
10851085

1086-
file_path = os.path.join(settings.TEST_DIR, "local_unit/test.csv")
1086+
file_path = os.path.join(settings.TEST_DIR, "local_unit/test-admin.xlsx")
10871087
with open(file_path, "rb") as f:
10881088
self._file_content = f.read()
10891089

1090-
def create_upload_file(self, filename="test.csv"):
1090+
def create_upload_file(self, filename="test-admin.xlsx"):
10911091
"""
10921092
Always return a new file instance to prevent stream exhaustion.
10931093
"""
1094-
return SimpleUploadedFile(filename, self._file_content, content_type="text/csv")
1094+
return SimpleUploadedFile(filename, self._file_content, content_type="text/xlsx")
10951095

10961096
@mock.patch("local_units.tasks.process_bulk_upload_local_unit.delay")
10971097
def test_bulk_upload_local_unit(self, mock_delay):
@@ -1236,16 +1236,16 @@ def setUpTestData(cls):
12361236
cls.local_unit_type = LocalUnitType.objects.create(code=1, name="Administrative")
12371237
cls.local_unit_type2 = LocalUnitType.objects.create(code=2, name="Health Care")
12381238
cls.level = LocalUnitLevel.objects.create(level=0, name="National")
1239-
file_path = os.path.join(settings.TEST_DIR, "local_unit/test.csv")
1239+
file_path = os.path.join(settings.TEST_DIR, "local_unit/test-admin.xlsx")
12401240
with open(file_path, "rb") as f:
12411241
cls._file_content = f.read()
12421242

1243-
def create_upload_file(cls, filename="test.csv"):
1244-
return SimpleUploadedFile(filename, cls._file_content, content_type="text/csv")
1243+
def create_upload_file(cls, filename="test-admin.xlsx"):
1244+
return SimpleUploadedFile(filename, cls._file_content, content_type="text/xlsx")
12451245

12461246
def test_bulk_upload_with_incorrect_country(cls):
12471247
"""
1248-
Test bulk upload fails when the country does not match CSV data.
1248+
Test bulk upload fails when the country does not match xlsx data.
12491249
"""
12501250
cls.bulk_upload = LocalUnitBulkUploadFactory.create(
12511251
country=cls.country1,
@@ -1268,13 +1268,13 @@ def test_bulk_upload_with_incorrect_country(cls):
12681268

12691269
def test_bulk_upload_with_valid_country(cls):
12701270
"""
1271-
Test bulk upload succeeds when the country matches CSV data
1271+
Test bulk upload succeeds when the country matches xlsx data
12721272
"""
12731273
cls.bulk_upload = LocalUnitBulkUploadFactory.create(
12741274
country=cls.country2, # Brazil
12751275
local_unit_type=cls.local_unit_type,
12761276
triggered_by=cls.user,
1277-
file=cls.create_upload_file(), # CSV with Brazil rows
1277+
file=cls.create_upload_file(), # xlsx with Brazil rows
12781278
status=LocalUnitBulkUpload.Status.PENDING,
12791279
)
12801280
runner = BaseBulkUploadLocalUnit(cls.bulk_upload)
@@ -1287,7 +1287,7 @@ def test_bulk_upload_with_valid_country(cls):
12871287

12881288
def test_bulk_upload_fails_and_delete(cls):
12891289
"""
1290-
Test bulk upload fails and delete when CSV has incorrect data.
1290+
Test bulk upload fails and delete when xlsx has incorrect data.
12911291
"""
12921292
LocalUnitFactory.create_batch(
12931293
5,
@@ -1316,7 +1316,7 @@ def test_bulk_upload_fails_and_delete(cls):
13161316

13171317
def test_bulk_upload_deletes_old_and_creates_new_local_units(cls):
13181318
"""
1319-
Test bulk upload with correct CSV data.
1319+
Test bulk upload with correct data.
13201320
"""
13211321
old_local_unit = LocalUnitFactory.create(
13221322
country=cls.country2,
@@ -1348,10 +1348,14 @@ def test_empty_administrative_file(cls):
13481348
Test bulk upload file is empty
13491349
"""
13501350

1351-
file_path = os.path.join(settings.STATICFILES_DIRS[0], "files", "local_units", "local-unit-bulk-upload-template.csv")
1351+
file_path = os.path.join(
1352+
settings.STATICFILES_DIRS[0], "files", "local_units", "Administrative Bulk Import Template - Local Units.xlsx"
1353+
)
13521354
with open(file_path, "rb") as f:
13531355
file_content = f.read()
1354-
empty_file = SimpleUploadedFile(name="local-unit-bulk-upload-template.csv", content=file_content, content_type="text/csv")
1356+
empty_file = SimpleUploadedFile(
1357+
name="Administrative Bulk Import Template - Local Units.xlsx", content=file_content, content_type="text/xlsx"
1358+
)
13551359
LocalUnitFactory.create_batch(
13561360
5,
13571361
country=cls.country2,
@@ -1437,16 +1441,16 @@ def setUpTestData(cls):
14371441
cls.professional_training_facilities = ProfessionalTrainingFacility.objects.create(code=1, name="Nurses")
14381442
cls.general_medical_services = GeneralMedicalService.objects.create(code=1, name="Minor Trauma")
14391443

1440-
file_path = os.path.join(settings.TEST_DIR, "local_unit/test-health.csv")
1444+
file_path = os.path.join(settings.TEST_DIR, "local_unit/test-health.xlsx")
14411445
with open(file_path, "rb") as f:
14421446
cls._file_content = f.read()
14431447

1444-
def create_upload_file(cls, filename="test-health.csv"):
1445-
return SimpleUploadedFile(filename, cls._file_content, content_type="text/csv")
1448+
def create_upload_file(cls, filename="test-health.xlsx"):
1449+
return SimpleUploadedFile(filename, cls._file_content, content_type="text/xlsx")
14461450

14471451
def test_bulk_upload_health_with_incorrect_country(cls):
14481452
"""
1449-
Should fail when CSV rows are not equal to bulk upload country.
1453+
Should fail when rows are not equal to bulk upload country.
14501454
"""
14511455
cls.bulk_upload = LocalUnitBulkUploadFactory.create(
14521456
country=cls.country1,
@@ -1546,12 +1550,12 @@ def test_empty_health_template_file(cls):
15461550
"""
15471551

15481552
file_path = os.path.join(
1549-
settings.STATICFILES_DIRS[0], "files", "local_units", "local-unit-health-bulk-upload-template.csv"
1553+
settings.STATICFILES_DIRS[0], "files", "local_units", "Health Care Bulk Import Template - Local Units.xlsx"
15501554
)
15511555
with open(file_path, "rb") as f:
15521556
file_content = f.read()
15531557
empty_file = SimpleUploadedFile(
1554-
name="local-unit-health-bulk-upload-template.csv", content=file_content, content_type="text/csv"
1558+
name="Health Care Bulk Import Template - Local Units.xlsx", content=file_content, content_type="text/xlsx"
15551559
)
15561560
health_data = HealthDataFactory.create_batch(
15571561
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)