-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #87 from lindsay-stevens/pyodk-74
74: add entity_list create and add_property
- Loading branch information
Showing
19 changed files
with
411 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
pyodk==0.3.0 | ||
segno==1.6.1 | ||
Pillow==10.3.0 |
30 changes: 30 additions & 0 deletions
30
docs/examples/create_entities_from_submissions/create_entities_from_submissions.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
""" | ||
A script that uses CSV data to create an entity list and populate it with entities. | ||
""" | ||
|
||
import csv | ||
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") | ||
|
||
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): | ||
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, | ||
) |
4 changes: 4 additions & 0 deletions
4
docs/examples/create_entities_from_submissions/imported_answers.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
first_name,age,favorite_color,favorite_color_other,location | ||
John,30,r,,37.7749 -122.4194 0 10 | ||
Alice,25,y,,-33.8651 151.2099 0 5 | ||
Bob,35,o,orange,51.5074 -0.1278 0 15 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
pyodk==0.3.0 | ||
docx-mailmerge2=0.8.0 | ||
docx-mailmerge2==0.8.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import logging | ||
from datetime import datetime | ||
|
||
from pyodk._endpoints import bases | ||
from pyodk._utils import validators as pv | ||
from pyodk._utils.session import Session | ||
from pyodk.errors import PyODKError | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
class EntityListProperty(bases.Model): | ||
name: str | ||
odataName: str | ||
publishedAt: datetime | ||
forms: list[str] | ||
|
||
|
||
class URLs(bases.Model): | ||
class Config: | ||
frozen = True | ||
|
||
post: str = "projects/{project_id}/datasets/{entity_list_name}/properties" | ||
|
||
|
||
class EntityListPropertyService(bases.Service): | ||
__slots__ = ( | ||
"urls", | ||
"session", | ||
"default_project_id", | ||
"default_entity_list_name", | ||
) | ||
|
||
def __init__( | ||
self, | ||
session: Session, | ||
default_project_id: int | None = None, | ||
default_entity_list_name: str | None = None, | ||
urls: URLs = None, | ||
): | ||
self.urls: URLs = urls if urls is not None else URLs() | ||
self.session: Session = session | ||
self.default_project_id: int | None = default_project_id | ||
self.default_entity_list_name: str | None = default_entity_list_name | ||
|
||
def create( | ||
self, | ||
name: str, | ||
entity_list_name: str | None = None, | ||
project_id: int | None = None, | ||
) -> bool: | ||
""" | ||
Create an Entity List Property. | ||
:param name: The name of the Property. Property names follow the same rules as | ||
form field names (valid XML identifiers) and cannot use the reserved names of | ||
name or label, or begin with the reserved prefix __. | ||
:param entity_list_name: The name of the Entity List (Dataset) being referenced. | ||
:param project_id: The id of the project this Entity List belongs to. | ||
""" | ||
try: | ||
pid = pv.validate_project_id(project_id, self.default_project_id) | ||
eln = pv.validate_entity_list_name( | ||
entity_list_name, self.default_entity_list_name | ||
) | ||
req_data = {"name": pv.validate_str(name, key="name")} | ||
except PyODKError as err: | ||
log.error(err, exc_info=True) | ||
raise | ||
|
||
response = self.session.response_or_error( | ||
method="POST", | ||
url=self.session.urlformat( | ||
self.urls.post, | ||
project_id=pid, | ||
entity_list_name=eln, | ||
), | ||
logger=log, | ||
json=req_data, | ||
) | ||
data = response.json() | ||
return data["success"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.