Skip to content

Commit 48d3a5c

Browse files
committed
Add new fields to project import
1 parent c04f3b1 commit 48d3a5c

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

deployments/forms.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
Country,
1818
District,
1919
DisasterType,
20+
VisibilityCharChoices,
2021
)
2122

2223
from .models import (
@@ -43,7 +44,7 @@ class Meta:
4344

4445
class ProjectImportForm(forms.Form):
4546
file = forms.FileField(label=_('file'), widget=forms.FileInput(attrs={'accept': '.csv'}))
46-
field_delimiter = forms.CharField(label=_('file delimiter'), initial=';')
47+
field_delimiter = forms.CharField(label=_('field delimiter'), initial=',')
4748
string_delimiter = forms.CharField(label=_('string delimiter'), initial='"')
4849

4950
class Columns:
@@ -69,6 +70,12 @@ class Columns:
6970
REACHED_FEMALES = 'Reached Females'
7071
REACHED_OTHERS = 'Reached Others'
7172
REACHED_TOTAL = 'Reached Total'
73+
VISIBILITY = 'Visibility'
74+
ACTUAL_EXPENDITURE = 'Actual Expenditure'
75+
REPORTING_NS_CONTACT_EMAIL = 'Contact Email'
76+
REPORTING_NS_CONTACT_NAME = 'Contact Name'
77+
REPORTING_NS_CONTACT_ROLE = 'Contact Role'
78+
DESCRIPTION = 'Description'
7279

7380
@classmethod
7481
def generate_template(cls):
@@ -109,6 +116,12 @@ def generate_template(cls):
109116
c.REACHED_FEMALES,
110117
c.REACHED_OTHERS,
111118
c.REACHED_TOTAL,
119+
c.VISIBILITY,
120+
c.ACTUAL_EXPENDITURE,
121+
c.REPORTING_NS_CONTACT_EMAIL,
122+
c.REPORTING_NS_CONTACT_NAME,
123+
c.REPORTING_NS_CONTACT_ROLE,
124+
c.DESCRIPTION
112125
]
113126

114127
rows = [
@@ -152,7 +165,7 @@ def _parse_integer(integer):
152165
try:
153166
if isinstance(integer, str):
154167
# ALL are integer fields. Change this if not
155-
return int(float(integer.replace(',', '')))
168+
return int(integer)
156169
return integer
157170
except ValueError:
158171
return None
@@ -182,6 +195,7 @@ def _parse_integer(integer):
182195
sectors.update(add_to_sectors)
183196
sector_tags = {t.title.lower(): t.id for t in SectorTag.objects.all()}
184197
disaster_types = {t.name.lower(): t.id for t in DisasterType.objects.all()}
198+
visibilities = {t.label: t.value for t in VisibilityCharChoices}
185199

186200
c = self.Columns
187201

@@ -253,6 +267,7 @@ def _parse_integer(integer):
253267
# Optional, but can be invalid
254268
if disaster_type_id is None and disaster_type_name != '':
255269
row_errors['disaster_type'] = [f'Given disaster type "{disaster_type_name}" is not available.']
270+
visibility = row[c.VISIBILITY].strip()
256271

257272
project = Project(
258273
user=user,
@@ -270,7 +285,7 @@ def _parse_integer(integer):
270285
name=row[c.PROJECT_NAME],
271286
start_date=_parse_date(row[c.START_DATE], 'start_date', row_errors),
272287
end_date=_parse_date(row[c.END_DATE], 'end_date', row_errors),
273-
budget_amount=_parse_integer(row[c.BUDGET]),
288+
budget_amount=_parse_integer(row[c.BUDGET].strip()),
274289

275290
# Optional fields
276291
target_male=_parse_integer(row[c.TARGETED_MALES]),
@@ -281,6 +296,12 @@ def _parse_integer(integer):
281296
reached_female=_parse_integer(row[c.REACHED_FEMALES]),
282297
reached_other=_parse_integer(row[c.REACHED_OTHERS]),
283298
reached_total=_parse_integer(row[c.REACHED_TOTAL]),
299+
visibility=visibilities[visibility] if visibility in visibilities else None,
300+
actual_expenditure=_parse_integer(row[c.ACTUAL_EXPENDITURE]),
301+
reporting_ns_contact_email=row[c.REPORTING_NS_CONTACT_EMAIL].strip(),
302+
reporting_ns_contact_name=row[c.REPORTING_NS_CONTACT_NAME].strip(),
303+
reporting_ns_contact_role=row[c.REPORTING_NS_CONTACT_ROLE].strip(),
304+
description=row[c.DESCRIPTION].strip(),
284305
)
285306
try:
286307
project.full_clean()

0 commit comments

Comments
 (0)