Skip to content

Commit

Permalink
Merge pull request #1155 from akvo/feature/1148-update-sqlite-when-a-…
Browse files Browse the repository at this point in the history
…new-administrative-and-entity-added-or-edited

Feature/1148 update sqlite when a new administrative and entity added or edited
  • Loading branch information
dedenbangkit authored Feb 8, 2024
2 parents 463ae16 + b67843c commit 710fbe5
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 2 deletions.
51 changes: 50 additions & 1 deletion backend/api/v1/v1_mobile/tests/tests_sqlite_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from api.v1.v1_profile.models import Administration, Entity, EntityData
from api.v1.v1_users.models import Organisation
from django.core.management import call_command
from utils.custom_generator import generate_sqlite
from utils.custom_generator import generate_sqlite, update_sqlite


class SQLiteGenerationTest(TestCase):
Expand Down Expand Up @@ -61,6 +61,55 @@ def test_sqlite_file_endpoint(self):
response = self.client.get(endpoint)
self.assertEqual(response.status_code, 200)

def test_update_sqlite_org_added(self):
# Test for adding new org
file_name = generate_sqlite(Organisation)
self.assertTrue(os.path.exists(file_name))

org = Organisation.objects.create(name="SQLite Company")
update_sqlite(
model=Organisation,
data={'id': org.id, 'name': org.name}
)
conn = sqlite3.connect(file_name)
self.assertEqual(
1,
len(pd.read_sql_query(
'SELECT * FROM nodes where id = ?',
conn,
params=[org.id]
)),
)
conn.close()
os.remove(file_name)

def test_update_sqlite_org_updated(self):
# Test for adding new org
file_name = generate_sqlite(Organisation)
self.assertTrue(os.path.exists(file_name))

new_org_name = 'Edited Company'
org = Organisation.objects.last()
org.name = new_org_name
org.save()
update_sqlite(
model=Organisation,
data={'name': new_org_name},
id=org.id
)

conn = sqlite3.connect(file_name)
self.assertEqual(
1,
len(pd.read_sql_query(
'SELECT * FROM nodes where name = ?',
conn,
params=[new_org_name]
)),
)
conn.close()
os.remove(file_name)


class EntitiesSQLiteGenerationTest(TestCase):
def setUp(self):
Expand Down
54 changes: 53 additions & 1 deletion backend/api/v1/v1_profile/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Entity, EntityData, Levels
)
from utils.custom_serializer_fields import CustomPrimaryKeyRelatedField
from utils.custom_generator import update_sqlite


class RelatedAdministrationField(serializers.PrimaryKeyRelatedField):
Expand Down Expand Up @@ -168,6 +169,17 @@ def create(self, validated_data):
self._assign_level(validated_data)
self._set_code(validated_data)
instance = super().create(validated_data)
update_sqlite(
model=Administration,
data={
'id': instance.id,
'name': instance.name,
'code': instance.code,
'parent': instance.parent.id,
'level': instance.level.id,
'path': instance.path
}
)
for attribute in attributes:
instance.attributes.create(**attribute)
return instance
Expand All @@ -184,7 +196,17 @@ def update(self, instance, validated_data):
if not created:
AdministrationAttributeValue.objects\
.filter(id=target.id).update(**it)

update_sqlite(
model=Administration,
data={
'name': instance.name,
'code': instance.code,
'parent': instance.parent.id,
'level': instance.level.id,
'path': instance.path
},
id=instance.id
)
return instance

def _set_code(self, validated_data):
Expand Down Expand Up @@ -231,6 +253,36 @@ class Meta:
model = EntityData
fields = ['id', 'name', 'code', 'administration', 'entity']

def create(self, validated_data):
instance = super().create(validated_data)
update_sqlite(
model=EntityData,
data={
'id': instance.id,
'name': instance.name,
'code': instance.code,
'entity': instance.entity.id,
'administration': instance.administration.id,
'parent': instance.administration.id
}
)
return instance

def update(self, instance, validated_data):
instance = super().update(instance, validated_data)
update_sqlite(
model=EntityData,
data={
'name': instance.name,
'code': instance.code,
'entity': instance.entity.id,
'administration': instance.administration.id,
'parent': instance.administration.id
},
id=instance.id
)
return instance


class GenerateDownloadRequestSerializer(serializers.Serializer):
level = CustomPrimaryKeyRelatedField(queryset=Levels.objects.none())
Expand Down
13 changes: 13 additions & 0 deletions backend/api/v1/v1_users/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
CustomPrimaryKeyRelatedField, CustomChoiceField, CustomBooleanField, \
CustomMultipleChoiceField
from utils.custom_helper import CustomPasscode
from utils.custom_generator import update_sqlite


class OrganisationSerializer(serializers.ModelSerializer):
Expand Down Expand Up @@ -76,6 +77,13 @@ def create(self, validated_data):
for attr in attributes:
OrganisationAttribute.objects.create(organisation=instance,
type=attr)
update_sqlite(
model=Organisation,
data={
'id': instance.id,
'name': instance.name,
}
)
return instance

def update(self, instance, validated_data):
Expand All @@ -92,6 +100,11 @@ def update(self, instance, validated_data):
attr, created = OrganisationAttribute.objects.get_or_create(
organisation=instance, type=attr)
attr.save()
update_sqlite(
model=Organisation,
data={'name': instance.name},
id=instance.id
)
return instance

class Meta:
Expand Down
40 changes: 40 additions & 0 deletions backend/utils/custom_generator.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import os
import sqlite3
import pandas as pd
import logging
from rtmis.settings import MASTER_DATA

logger = logging.getLogger(__name__)


def generate_sqlite(model):
table_name = model._meta.db_table
Expand All @@ -29,3 +32,40 @@ def generate_sqlite(model):
data.to_sql("nodes", conn, if_exists="replace", index=False)
conn.close()
return file_name


def update_sqlite(model, data, id=None):
table_name = model._meta.db_table
fields = data.keys()
field_names = ', '.join([f for f in fields])
placeholders = ', '.join(['?' for _ in range(len(fields))])
update_placeholders = ', '.join([f"{f} = ?" for f in fields])
params = list(data.values())
if id:
params += [id]
file_name = f"{MASTER_DATA}/{table_name}.sqlite"
conn = sqlite3.connect(file_name)
try:
with conn:
c = conn.cursor()
if id:
c.execute("SELECT * FROM nodes WHERE id = ?", (id,))
if c.fetchone():
query = f"UPDATE nodes \
SET {update_placeholders} WHERE id = ?"
c.execute(query, params)
if not id:
query = f"INSERT INTO nodes({field_names}) \
VALUES ({placeholders})"
c.execute(query, params)
except Exception as error:
logger.error({
'context': 'update_sqlite',
'error': error,
'table_name': table_name,
'data': data,
'id': id
})
conn.rollback()
finally:
conn.close()

0 comments on commit 710fbe5

Please sign in to comment.