Skip to content

Commit

Permalink
Merge branch 'develop' into feature/1142-only-allow-county-and-supera…
Browse files Browse the repository at this point in the history
…dmin-to-edit-data-point-in-manage-data
  • Loading branch information
ifirmawan authored Feb 8, 2024
2 parents 6e69362 + dcc0d60 commit a39e416
Show file tree
Hide file tree
Showing 15 changed files with 166 additions and 447 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)
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.0.4 on 2024-02-07 06:04

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('v1_users', '0008_reset_organisation_id_seq'),
]

operations = [
migrations.AlterField(
model_name='systemuser',
name='deleted_at',
field=models.DateTimeField(blank=True, null=True),
),
]
12 changes: 0 additions & 12 deletions frontend/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import {
AddUser,
Forms,
ManageData,
Questionnaires,
QuestionnairesAdmin,
Approvals,
ApproversTree,
Profile,
Expand Down Expand Up @@ -192,16 +190,6 @@ const RouteList = () => {
path="approvals"
element={<Private element={Approvals} alias="approvals" />}
/>
<Route
path="questionnaires"
element={<Private element={Questionnaires} alias="questionnaires" />}
/>
<Route
path="questionnaires/admin"
element={
<Private element={QuestionnairesAdmin} alias="questionnaires" />
}
/>
<Route
path="organisation/add"
element={<Private element={AddOrganisation} alias="organisation" />}
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/pages/approvals/ApprovalDetail.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ const ApprovalDetail = ({
{
title: "Question",
dataIndex: "name",
width: "50%",
},
{
title: "Response",
Expand All @@ -467,6 +468,7 @@ const ApprovalDetail = ({
resetButton={resetButton}
/>
),
width: "50%",
},
Table.EXPAND_COLUMN,
]}
Expand Down
2 changes: 0 additions & 2 deletions frontend/src/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ export { default as ControlCenter } from "./control-center/ControlCenter";
export { default as Users } from "./users/Users";
export { default as AddUser } from "./add-user/AddUser";
export { default as ManageData } from "./manage-data/ManageData";
export { default as Questionnaires } from "./questionnaires/Questionnaires";
export { default as QuestionnairesAdmin } from "./questionnaires/QuestionnairesAdmin";
export { default as Approvals } from "./approvals/Approvals";
export { default as ApproversTree } from "./approvers-tree/ApproversTree";
export { default as Profile } from "./profile/Profile";
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/pages/manage-data/DataDetail.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ const DataDetail = ({
{
title: "Question",
dataIndex: "name",
width: "50%",
},
{
title: "Response",
Expand All @@ -228,6 +229,7 @@ const DataDetail = ({
resetButton={resetButton}
/>
),
width: "50%",
},
Table.EXPAND_COLUMN,
]}
Expand Down
Loading

0 comments on commit a39e416

Please sign in to comment.