Skip to content

Commit

Permalink
Merge pull request #1151 from akvo/feature/1150-parentchild-monitorin…
Browse files Browse the repository at this point in the history
…g-data

[#1150] Seed Monitoring Data when it gets Approved
  • Loading branch information
dedenbangkit authored Feb 7, 2024
2 parents 1541f84 + ae3db78 commit 249adf1
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 9 deletions.
34 changes: 25 additions & 9 deletions backend/api/v1/v1_data/management/commands/fake_data_seeder.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from faker import Faker

from api.v1.v1_data.models import FormData, Answers
from api.v1.v1_data.models import FormData, Answers, PendingAnswers
from api.v1.v1_forms.constants import QuestionTypes, FormTypes
from api.v1.v1_forms.models import Forms
from api.v1.v1_profile.constants import UserRoleTypes
Expand Down Expand Up @@ -51,7 +51,9 @@ def set_answer_data(data, question):
return name, value, option


def add_fake_answers(data: FormData, form_type):
def add_fake_answers(data: FormData,
form_type=FormTypes.county,
pending=False):
form = data.form
meta_name = []
for question in form.form_questions.all().order_by('question_group__order',
Expand All @@ -73,20 +75,34 @@ def add_fake_answers(data: FormData, form_type):
seed = True
if question.dependency:
for d in question.dependency:
prev_answer = Answers.objects.filter(
if not pending:
prev_answer = Answers.objects.filter(
data=data, question_id=d.get('id')).first()
else:
prev_answer = PendingAnswers.objects.filter(
pending_data=data, question_id=d.get('id')).first()
if prev_answer:
seed = False
for o in prev_answer.options:
if o in d.get("options"):
seed = True
if seed:
Answers.objects.create(data=data,
question=question,
name=name,
value=value,
options=option,
created_by=data.created_by)
if not pending:
Answers.objects.create(data=data,
question=question,
name=name,
value=value,
options=option,
created_by=data.created_by)
else:
PendingAnswers.objects.create(
pending_data=data,
question=question,
name=name,
value=value,
options=option,
created_by=data.created_by
)
data.name = ' - '.join(meta_name) if \
form_type != FormTypes.national else data.name
data.save()
Expand Down
19 changes: 19 additions & 0 deletions backend/api/v1/v1_data/migrations/0029_formdata_parent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 4.0.4 on 2024-02-07 06:41

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('v1_data', '0028_rename_deleted_pendingformdata_deleted_at'),
]

operations = [
migrations.AddField(
model_name='formdata',
name='parent',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='children', to='v1_data.formdata'),
),
]
4 changes: 4 additions & 0 deletions backend/api/v1/v1_data/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@


class FormData(models.Model):
parent = models.ForeignKey('self',
on_delete=models.CASCADE,
related_name='children',
null=True, blank=True)
name = models.TextField()
form = models.ForeignKey(to=Forms,
on_delete=models.CASCADE,
Expand Down
7 changes: 7 additions & 0 deletions backend/api/v1/v1_data/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@


def seed_approved_data(data):
parent_data = FormData.objects.filter(
form=data.form,
uuid=data.uuid,
parent=None,
).first()
if data.data:
form_data: FormData = data.data
form_data.parent = parent_data
form_data.name = data.name
form_data.uuid = data.uuid
form_data.form = data.form
Expand All @@ -28,6 +34,7 @@ def seed_approved_data(data):
form_answer.delete()
else:
form_data = FormData.objects.create(
parent=parent_data,
name=data.name,
uuid=data.uuid,
form=data.form,
Expand Down
85 changes: 85 additions & 0 deletions backend/api/v1/v1_data/tests/tests_monitoring.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
from django.test import TestCase
from django.core.management import call_command
from api.v1.v1_users.models import SystemUser
from api.v1.v1_data.models import (
FormData,
PendingFormData,
PendingDataBatch
)
from api.v1.v1_forms.models import Forms
from api.v1.v1_forms.constants import FormTypes
from api.v1.v1_profile.models import Administration, Access
from api.v1.v1_profile.constants import UserRoleTypes
from api.v1.v1_data.management.commands.fake_data_seeder import (
add_fake_answers
)
from api.v1.v1_data.tasks import seed_approved_data


class MonitoringDataTestCase(TestCase):
def setUp(self):
call_command('administration_seeder', '--test')
call_command('form_seeder', '--test')
self.user = SystemUser.objects.create_user(
email='[email protected]',
password='test1234',
first_name='test',
last_name='testing',
)
self.administration = Administration.objects.filter(
parent__isnull=True
).first()
role = UserRoleTypes.user
self.user_access = Access.objects.create(
user=self.user, role=role, administration=self.administration
)
self.uuid = '1234567890'
self.form = Forms.objects.filter(type=FormTypes.county).first()
self.data = FormData.objects.create(
parent=None,
uuid=self.uuid,
form=self.form,
administration=self.administration,
created_by=self.user,
)
add_fake_answers(self.data, FormTypes.county)

def test_parent_data(self):
self.assertTrue(self.data.name)
self.assertEqual(self.data.parent, None)
self.assertEqual(self.data.uuid, self.uuid)
self.assertEqual(self.data.form, self.form)
self.assertEqual(self.data.administration, self.administration)
self.assertEqual(self.data.created_by, self.user)
self.assertTrue(self.data.data_answer.count() > 0)

def test_seed_monitoring_batch(self):
for i in range(2):
pending_data = PendingFormData.objects.create(
uuid=self.uuid if i == 0 else f'{self.uuid}{i}',
form=self.form,
administration=self.administration,
created_by=self.user,
)
add_fake_answers(pending_data,
form_type=FormTypes.county,
pending=True)
self.assertTrue(PendingFormData.objects.count() == 2)
batch = PendingDataBatch.objects.create(
name='test batch',
administration=self.administration,
form=self.form,
user=self.user,
approved=True
)
batch.batch_pending_data_batch.add(*PendingFormData.objects.all())
self.assertTrue(batch.batch_pending_data_batch.count() == 2)
for pending_data in batch.batch_pending_data_batch.all():
seed_approved_data(pending_data)
self.assertTrue(FormData.objects.count() == 3)
child_data = FormData.objects.filter(
parent__isnull=False
).first()
self.assertEqual(child_data.parent.uuid, self.uuid)
self.assertEqual(self.data.children.first().id, child_data.id)
self.assertEqual(self.data.children.count(), 1)

0 comments on commit 249adf1

Please sign in to comment.