Skip to content

Commit

Permalink
add: entities methods create_many, delete, merge
Browse files Browse the repository at this point in the history
  • Loading branch information
lindsay-stevens committed Jul 31, 2024
1 parent e14a612 commit 531cb2a
Show file tree
Hide file tree
Showing 6 changed files with 897 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,47 @@
A script that uses CSV data to create an entity list and populate it with entities.
"""

import csv
from csv import DictReader
from pathlib import Path
from uuid import uuid4

from pyodk import Client

if __name__ == "__main__":
project_id = 1
entity_list_name = f"previous_survey_{uuid4()}"
entity_label_field = "first_name"
entity_properties = ("age", "location")
csv_path = Path("./imported_answers.csv")
project_id = 1
entity_list_name = f"previous_survey_{uuid4()}"
entity_label_field = "first_name"
entity_properties = ("age", "location")
csv_path = Path("./imported_answers.csv")


def create_one_at_a_time():
with Client(project_id=project_id) as client, open(csv_path) as csv_file:
# Create the entity list.
client.entity_lists.create(entity_list_name=entity_list_name)
for prop in entity_properties:
client.entity_lists.add_property(name=prop, entity_list_name=entity_list_name)

# Create the entities from the CSV data.
for row in csv.DictReader(csv_file):
for row in DictReader(csv_file):
client.entities.create(
label=row[entity_label_field],
data={k: str(v) for k, v in row.items() if k in entity_properties},
entity_list_name=entity_list_name,
)


def create_with_merge():
with Client(project_id=project_id) as client, open(csv_path) as csv_file:
client.entity_lists.default_entity_list_name = client.session.get_xform_uuid()
entity_list = client.entity_lists.create()
client.entities.merge(
source_data=list(DictReader(csv_file)),
entity_list_name=entity_list.name,
source_label_key=entity_label_field,
source_keys=entity_properties,
)


if __name__ == "__main__":
# create_one_at_a_time()
create_with_merge()
Loading

0 comments on commit 531cb2a

Please sign in to comment.