Skip to content

Commit

Permalink
808 - importing from CSV breaks calculator action link to API action
Browse files Browse the repository at this point in the history
  • Loading branch information
BradHN1 committed Oct 7, 2023
1 parent 3eb2ef6 commit a12344a
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 54 deletions.
33 changes: 20 additions & 13 deletions src/carbon_calculator/CCDefaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,17 @@ def exportDefaults(self,fileName):
if csvfile:
csvfile.close()
return status

def importDefaults(self,fileName):
csvfile = None
try:
status = True
with open(fileName, newline='') as csvfile:
inputlist = csv.reader(csvfile)
first = True
num = 0
for item in inputlist:
if first:
if first: # header row
t = {}
for i in range(len(item)):
it = item[i]
Expand All @@ -151,17 +153,18 @@ def importDefaults(self,fileName):
#valid_date = datetime.date(valid_date)
valid_date = datetime.strptime(valid_date, "%Y-%m-%d").date()

#qs = CalcDefault.objects.filter(variable=variable, locality=locality)
#if qs:
# qs[0].delete()

cd = CalcDefault(variable=variable,
locality=locality,
value=value,
reference=reference,
valid_date = valid_date,
updated=updated)
cd.save()
# update the default value for this variable, localith and valid_date
qs, created = CalcDefault.objects.update_or_create(
variable=variable,
locality=locality,
valid_date=valid_date,
defaults={
'value':value,
'reference':reference,
'updated':updated
})
if created:
num += 1

if not locality in self.DefaultsByLocality:
self.DefaultsByLocality[locality] = {}
Expand Down Expand Up @@ -190,7 +193,11 @@ def importDefaults(self,fileName):
if not f:
var["valid_dates"].append(valid_date)
var["values"].append(value)

if num>0:
msg = "Imported %d Carbon Calculator Defaults" % num
else:
msg = "Carbon Calculator default values updated"
print(msg)
status = True
except Exception as error:
print("Error importing Carbon Calculator Defaults from CSV file")
Expand Down
140 changes: 99 additions & 41 deletions src/carbon_calculator/carbonCalculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ def SavePic2Media(picURL):

def AverageImpact(action, date=None, locality="default"):
averageName = action.name + '_average_points'
return getDefault(locality, averageName, date)
impact = getDefault(locality, averageName, date)
return impact

class CarbonCalculator:
def __init__(self, reset=False) :
Expand Down Expand Up @@ -292,39 +293,80 @@ def ImportQuestions(self, questionsFile):
if item[0] == '':
continue

qs = Question.objects.filter(name=item[0])
if qs:
qs[0].delete()

skip = [[],[],[],[],[],[]]
for i in range(6):
ii = 5+2*i
if item[ii]!='' :
skip[i] = item[ii].split(",")

question = Question(name=item[0],
category=item[1],
question_text=item[2],
question_type=item[3],
response_1=item[4], skip_1=skip[0],
response_2=item[6], skip_2=skip[1],
response_3=item[8], skip_3=skip[2],
response_4=item[10], skip_4=skip[3],
response_5=item[12], skip_5=skip[4],
response_6=item[14], skip_6=skip[5])
#print('Importing Question ',question.name,': ',question.question_text)

minimum_value = maximum_value = typical_value = None
if len(item)>19:
if len(item[16]):
question.minimum_value = eval(item[16])
minimum_value = eval(item[16])
if len(item[17])>0:
question.maximum_value = eval(item[17])
maximum_value = eval(item[17])
if len(item[18])>0:
question.typical_value = eval(item[18])
typical_value = eval(item[18])

# update the unique Question with this name
qs, created = Question.objects.update_or_create(
name=item[0],
defaults={
'category':item[1],
'question_text':item[2],
'question_type':item[3],
'response_1':item[4], 'skip_1':skip[0],
'response_2':item[6], 'skip_2':skip[1],
'response_3':item[8], 'skip_3':skip[2],
'response_4':item[10], 'skip_4':skip[3],
'response_5':item[12], 'skip_5':skip[4],
'response_6':item[14], 'skip_6':skip[5],
'minimum_value':minimum_value,
'maximum_value':maximum_value,
'typical_value':typical_value
}
)

if created:
num += 1

question.save()
num+=1
msg = "Imported %d Carbon Calculator Questions" % num
#qs = Question.objects.filter(name=item[0])
#if qs:
# qs[0].delete()
#
#skip = [[],[],[],[],[],[]]
#for i in range(6):
# ii = 5+2*i
# if item[ii]!='' :
# skip[i] = item[ii].split(",")

#question = Question(name=item[0],
# category=item[1],
# question_text=item[2],
# question_type=item[3],
# response_1=item[4], skip_1=skip[0],
# response_2=item[6], skip_2=skip[1],
# response_3=item[8], skip_3=skip[2],
# response_4=item[10], skip_4=skip[3],
# response_5=item[12], skip_5=skip[4],
# response_6=item[14], skip_6=skip[5])
#print('Importing Question ',question.name,': ',question.question_text)

#if len(item)>19:
# if len(item[16]):
# question.minimum_value = eval(item[16])
# if len(item[17])>0:
# question.maximum_value = eval(item[17])
# if len(item[18])>0:
# question.typical_value = eval(item[18])

#question.save()
#num+=1

if num>0:
msg = "Imported %d Carbon Calculator Questions" % num
else:
msg = "Updated Carbon Calculator Questions from import"
print(msg)
csvfile.close()
return True
Expand All @@ -350,24 +392,40 @@ def ImportActions(self, actionsFile):
if name == '':
continue

qs = Action.objects.filter(name=name)
if qs:
qs[0].delete()

picture = None
if len(item)>=4 and name!='':
picture = SavePic2Media(item[t["Picture"]])
action = Action(name=item[0],
title = item[t["Title"]],
description=item[t["Description"]],
helptext=item[t["Helptext"]],
category=item[t["Category"]],
average_points=int(eval(item[t["Avg points"]])),
questions=item[t["Questions"]].split(","),
picture = picture)
action.save()

msg = "Imported %d Carbon Calculator Actions" % num
# update the unique Action with this name
qs, created = Action.objects.update_or_create(
name=name,
defaults={
'description':item[t["Description"]],
'helptext':item[t["Helptext"]],
'category':item[t["Category"]],
'average_points':int(eval(item[t["Avg points"]])),
'questions':item[t["Questions"]].split(",")
}
)

if created:
num += 1

# NOTE - WE ARE SKIPPING THE IMAGES; not currently used, could re-implement later

# picture = None
# if len(item)>=4 and name!='':
# picture = SavePic2Media(item[t["Picture"]])
# action = Action(name=item[0],
# title = item[t["Title"]],
# description=item[t["Description"]],
# helptext=item[t["Helptext"]],
# category=item[t["Category"]],
# average_points=int(eval(item[t["Avg points"]])),
# questions=item[t["Questions"]].split(","),
# picture = picture)
# action.save()

if num:
msg = "Imported %d Carbon Calculator Actions" % num
else:
msg = "Updated Carbon Calculator Actions from import"
print(msg)
csvfile.close()
return True
Expand Down

0 comments on commit a12344a

Please sign in to comment.