Skip to content

Commit

Permalink
Merge pull request #93 from lindsay-stevens/pyodk-88
Browse files Browse the repository at this point in the history
88: add entities methods create_many, delete, merge
  • Loading branch information
lindsay-stevens authored Aug 30, 2024
2 parents e14a612 + b95ee56 commit 0430222
Show file tree
Hide file tree
Showing 7 changed files with 950 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,48 @@
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_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)
entity_list = client.entity_lists.create(
entity_list_name=f"previous_survey_{uuid4()}"
)
for prop in entity_properties:
client.entity_lists.add_property(name=prop, entity_list_name=entity_list_name)
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,
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 = f"previous_survey_{uuid4()}"
entity_list = client.entity_lists.create()
client.entities.merge(
data=DictReader(csv_file),
entity_list_name=entity_list.name,
source_label_key=entity_label_field,
source_keys=(entity_label_field, *entity_properties),
)


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

0 comments on commit 0430222

Please sign in to comment.