Skip to content

Commit

Permalink
Ajustes finos nas views de importação dos dados em .csv
Browse files Browse the repository at this point in the history
  • Loading branch information
gitnnolabs committed Aug 22, 2022
1 parent 6d46809 commit 8c97c9a
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 83 deletions.
20 changes: 12 additions & 8 deletions education_directory/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,14 @@ def import_file(request):
ed.link = row['Link']
ed.description = row['Description']
ed.institution = row['Institution']
ed.start_date = datetime.strptime(row['Start Date'], '%d/%m/%Y')
ed.end_date = datetime.strptime(row['End Date'], '%d/%m/%Y')
ed.start_time = row['Start Time']
ed.end_time = row['End Time']
if row['Start Date']:
ed.start_date = datetime.strptime(row['Start Date'], '%d/%m/%Y')
if row['End Date']:
ed.end_date = datetime.strptime(row['End Date'], '%d/%m/%Y')
if row['Start Time']:
ed.start_time = row['Start Time']
if row['End Time']:
ed.end_time = row['End Time']
ed.creator = request.user
ed.save()

Expand All @@ -105,7 +109,7 @@ def import_file(request):
level2 = row['Thematic Area Level2']
the_area = ThematicArea.get_or_create(level0, level1, level2, request.user)

ed.thematic_areas.add(the_area)
ed.thematic_areas.add(the_area)

# Keywords
if row['Keywords']:
Expand All @@ -122,7 +126,7 @@ def import_file(request):
pratice = Pratice.objects.get(name=pratice_name)
ed.pratice = pratice
else:
messages.error(request, _("Unknown pratice, line: %s") % str(line + 1))
messages.error(request, _("Unknown pratice, line: %s") % str(line + 2))

# Action
if row['Action']:
Expand All @@ -131,11 +135,11 @@ def import_file(request):
action = Action.objects.get(name=action_name)
ed.action = action
else:
messages.error(request, _("Unknown action, line: %s") % str(line + 1))
messages.error(request, _("Unknown action, line: %s") % str(line + 2))

ed.save()
except Exception as ex:
messages.error(request, _("Import error: %s") % ex)
messages.error(request, _("Import error: %s, Line: %s") % (ex, str(line + 2)))
else:
messages.success(request, _("File imported successfully!"))

Expand Down
20 changes: 12 additions & 8 deletions event_directory/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,14 @@ def import_file(request):
di.link = row['Link']
di.description = row['Description']
di.organization = row['Organization']
di.start_date = datetime.strptime(row['Start Date'], '%d/%m/%Y')
di.end_date = datetime.strptime(row['End Date'], '%d/%m/%Y')
di.start_time = row['Start Time']
di.end_time = row['End Time']
if row['Start Date']:
di.start_date = datetime.strptime(row['Start Date'], '%d/%m/%Y')
if row['End Date']:
di.end_date = datetime.strptime(row['End Date'], '%d/%m/%Y')
if row['Start Time']:
di.start_time = row['Start Time']
if row['End Time']:
di.end_time = row['End Time']
di.creator = request.user
di.save()

Expand All @@ -105,7 +109,7 @@ def import_file(request):
level2 = row['Thematic Area Level2']
the_area = ThematicArea.get_or_create(level0, level1, level2, request.user)

di.thematic_areas.add(the_area)
di.thematic_areas.add(the_area)

# Keywords
if row['Keywords']:
Expand All @@ -122,7 +126,7 @@ def import_file(request):
pratice = Pratice.objects.get(name=pratice_name)
di.pratice = pratice
else:
messages.error(request, _("Unknown pratice, line: %s") % str(line + 1))
messages.error(request, _("Unknown pratice, line: %s") % str(line + 2))

# Action
if row['Action']:
Expand All @@ -131,12 +135,12 @@ def import_file(request):
action = Action.objects.get(name=action_name)
di.action = action
else:
messages.error(request, _("Unknown action, line: %s") % str(line + 1))
messages.error(request, _("Unknown action, line: %s") % str(line + 2))

di.save()

except Exception as ex:
messages.error(request, _("Import error: %s") % ex)
messages.error(request, _("Import error: %s, Line: %s") % (ex, str(line + 2)))
else:
messages.success(request, _("File imported successfully!"))

Expand Down
126 changes: 63 additions & 63 deletions infrastructure_directory/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,72 +68,72 @@ def import_file(request):

file_path = file_upload.attachment.file.path

try:
with open(file_path, 'r') as csvfile:
data = csv.DictReader(csvfile)

for line, row in enumerate(data):
isd = InfrastructureDirectory()
isd.title = row['Title']
isd.link = row['Link']
isd.description = row['Description']

isd.creator = request.user
isd.save()

# Institution
inst_name = row['Institution Name']
if inst_name:
inst_country = row['Institution Country']
inst_region = row['Institution Region']
inst_state = row['Institution State']
inst_city = row['Institution City']

institution = Institution.get_or_create(inst_name, inst_country, inst_region,
inst_state, inst_city, request.user)
isd.institutions.add(institution)

# Thematic Area
level0 = row['Thematic Area Level0']
if level0:
level1 = row['Thematic Area Level1']
level2 = row['Thematic Area Level2']
the_area = ThematicArea.get_or_create(level0, level1, level2, request.user)
# try:
with open(file_path, 'r') as csvfile:
data = csv.DictReader(csvfile)

for line, row in enumerate(data):
isd = InfrastructureDirectory()
isd.title = row['Title']
isd.link = row['Link']
isd.description = row['Description']

isd.creator = request.user
isd.save()

# Institution
inst_name = row['Institution Name']
if inst_name:
inst_country = row['Institution Country']
inst_region = row['Institution Region']
inst_state = row['Institution State']
inst_city = row['Institution City']

institution = Institution.get_or_create(inst_name, inst_country, inst_region,
inst_state, inst_city, request.user)
isd.institutions.add(institution)

# Thematic Area
level0 = row['Thematic Area Level0']
if level0:
level1 = row['Thematic Area Level1']
level2 = row['Thematic Area Level2']
the_area = ThematicArea.get_or_create(level0, level1, level2, request.user)

isd.thematic_areas.add(the_area)

# Keywords
if row['Keywords']:
for key in row['Keywords'].split('|'):
isd.keywords.add(key)

if row['Classification']:
isd.classification = row['Classification']

# Pratice
if row['Pratice']:
pratice_name = row['Pratice']
if Pratice.objects.filter(name=pratice_name).exists():
pratice = Pratice.objects.get(name=pratice_name)
isd.pratice = pratice
else:
messages.error(request, _("Unknown pratice, line: %s") % str(line + 1))

# Action
if row['Action']:
action_name = row['Action']
if Action.objects.filter(name=action_name).exists():
action = Action.objects.get(name=action_name)
isd.action = action
else:
messages.error(request, _("Unknown action, line: %s") % str(line + 1))

isd.save()

except Exception as ex:
messages.error(request, _("Import error: %s") % ex)
else:
messages.success(request, _("File imported successfully!"))
# Keywords
if row['Keywords']:
for key in row['Keywords'].split('|'):
isd.keywords.add(key)

if row['Classification']:
isd.classification = row['Classification']

# Pratice
if row['Pratice']:
pratice_name = row['Pratice']
if Pratice.objects.filter(name=pratice_name).exists():
pratice = Pratice.objects.get(name=pratice_name)
isd.pratice = pratice
else:
messages.error(request, _("Unknown pratice, line: %s") % str(line + 1))

# Action
if row['Action']:
action_name = row['Action']
if Action.objects.filter(name=action_name).exists():
action = Action.objects.get(name=action_name)
isd.action = action
else:
messages.error(request, _("Unknown action, line: %s") % str(line + 1))

isd.save()

# except Exception as ex:
# messages.error(request, _("Import error: %s, Line: %s") % (ex, str(line + 2)))
# else:
# messages.success(request, _("File imported successfully!"))

return redirect(request.META.get('HTTP_REFERER'))

Expand Down
8 changes: 4 additions & 4 deletions policy_directory/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def import_file(request):
level2 = row['Thematic Area Level2']
the_area = ThematicArea.get_or_create(level0, level1, level2, request.user)

po.thematic_areas.add(the_area)
po.thematic_areas.add(the_area)

# Keywords
if row['Keywords']:
Expand All @@ -120,7 +120,7 @@ def import_file(request):
pratice = Pratice.objects.get(name=pratice_name)
po.pratice = pratice
else:
messages.error(request, _("Unknown pratice, line: %s") % str(line + 1))
messages.error(request, _("Unknown pratice, line: %s") % str(line + 2))

# Action
if row['Action']:
Expand All @@ -129,11 +129,11 @@ def import_file(request):
action = Action.objects.get(name=action_name)
po.action = action
else:
messages.error(request, _("Unknown action, line: %s") % str(line + 1))
messages.error(request, _("Unknown action, line: %s") % str(line + 2))

po.save()
except Exception as ex:
messages.error(request, _("Import error: %s") % ex)
messages.error(request, _("Import error: %s, Line: %s") % (ex, str(line + 2)))
else:
messages.success(request, _("File imported successfully!"))

Expand Down

0 comments on commit 8c97c9a

Please sign in to comment.