Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion backend/api/v1/v1_data/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def to_data_frame(self):
data = {
"id": self.id,
"datapoint_name": self.name,
"administration": self.administration.name,
"administration": self.administration.administration_column,
"geolocation":
f"{self.geo[0]}, {self.geo[1]}" if self.geo else None,
"created_by":
Expand Down Expand Up @@ -208,6 +208,9 @@ def to_data_frame(self) -> dict:
QuestionTypes.text, QuestionTypes.photo, QuestionTypes.date
]:
answer = self.name
elif q.type == QuestionTypes.administration:
answer = Administration.objects.get(
pk=self.value).administration_column
else:
answer = self.value
return {qname: answer}
Expand Down
1 change: 1 addition & 0 deletions backend/api/v1/v1_jobs/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def job_generate_download(job_id, **kwargs):
Administration.objects.filter(
path__startswith=filter_path).values_list('id',
flat=True))
administration_ids.append(administration.id)

administration_name = list(
Administration.objects.filter(
Expand Down
28 changes: 17 additions & 11 deletions backend/api/v1/v1_jobs/seed_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ def save_data(user: SystemUser, batch: PendingDataBatch, dp: dict, qs: dict):

if q.type == QuestionTypes.geo:
if aw:
geo = [float(g.strip()) for g in aw.split(",")]
aw = aw.strip().replace('|', ',')
geo = [float(g) for g in aw.split(",")]
answer.options = geo
else:
valid = False
Expand Down Expand Up @@ -82,18 +83,21 @@ def save_data(user: SystemUser, batch: PendingDataBatch, dp: dict, qs: dict):
if q.meta and aw:
names.append(aw)
if q.type == QuestionTypes.multiple_option:
answer.options = aw
answer.options = aw.split('|')
if q.meta:
names = names + aw
names = names + aw.replace('|', '-')
if valid:
if data_id:
form_answer = Answers.objects.get(
data_id=data_id,
question_id=answer.question_id
)
if not (form_answer.name == answer.name and
form_answer.options == answer.options and
form_answer.value == answer.value):
try:
form_answer = Answers.objects.get(
data_id=data_id,
question_id=answer.question_id
)
if not (form_answer.name == answer.name and
form_answer.options == answer.options and
form_answer.value == answer.value):
answerlist.append(answer)
except Answers.DoesNotExist:
answerlist.append(answer)
else:
answerlist.append(answer)
Expand All @@ -116,6 +120,8 @@ def save_data(user: SystemUser, batch: PendingDataBatch, dp: dict, qs: dict):
def seed_excel_data(job: Jobs):
file = f"./tmp/{job.info.get('file')}"
df = pd.read_excel(file, sheet_name="data")
df = df.rename(columns={'id': 'data_id'})
df = df[list(filter(lambda x: "|" in x, list(df))) + ['data_id']]
questions = {}
columns = {}
for q in list(df):
Expand All @@ -130,7 +136,7 @@ def seed_excel_data(job: Jobs):
form_id=job.info.get('form'),
administration_id=job.info.get('administration'),
user=job.user,
name='Auto Generated'
name=job.info.get('file')
)
records = []
for datapoint in datapoints:
Expand Down
10 changes: 8 additions & 2 deletions backend/api/v1/v1_jobs/validate_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ def validate_number(answer, question):
rule = question.rule
qname = question.name
for r in rule:
if r == "max" and rule[r] < answer:
if r == "max" and float(rule[r]) < answer:
return {
"error_message":
ValidationText.numeric_max_rule.value.replace(
"--question--", qname).replace("--rule--",
str(rule[r]))
}
if r == "min" and rule[r] > answer:
if r == "min" and float(rule[r]) > answer:
return {
"error_message":
ValidationText.numeric_min_rule.value.replace(
Expand All @@ -80,6 +80,7 @@ def validate_number(answer, question):

def validate_geo(answer):
answer = str(answer)
answer = answer.strip().replace('|', ',')
try:
for a in answer.split(","):
float(a.strip())
Expand Down Expand Up @@ -246,6 +247,9 @@ def validate(form: int, administration: int, file: str):
questions = Questions.objects.filter(form_id=form)
header_names = [q.to_excel_header for q in questions]
df = pd.read_excel(file, sheet_name='data')
if 'id' in list(df):
df = df.rename(columns={'id': 'data_id'})
# df = df[header_names + ['data_id']]
if df.shape[0] == 0:
return [{
"error": ExcelError.sheet,
Expand All @@ -263,6 +267,8 @@ def validate(form: int, administration: int, file: str):
adm = {"id": adm.id, "name": adm.name}
for col in excel_head:
header = excel_head[col]
if header not in header_names + ['data_id']:
continue
error = validate_header_names(header, f"{col}1", header_names)

if error:
Expand Down
10 changes: 9 additions & 1 deletion backend/api/v1/v1_profile/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def __str__(self):
def ancestors(self):
if self.path:
ids = self.path.split(".")[:-1]
administrations = Administration.objects.filter(id__in=ids).all()
administrations = Administration.objects.filter(
id__in=ids).order_by('level__level')
return administrations
return None

Expand All @@ -47,6 +48,13 @@ def full_name(self):
return "{} - {}".format(names, self.name)
return self.name

@property
def administration_column(self):
if self.path:
names = "|".join([a.name for a in self.ancestors])
return "{}|{}".format(names, self.name)
return self.name

class Meta:
db_table = 'administrator'

Expand Down