-
Notifications
You must be signed in to change notification settings - Fork 26
74: add entity_list create and add_property #87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
237a8ca
chg: remove pyodk examples requirements.txt files
lindsay-stevens d26f6e3
chg: update usages of wrap_error / str_validator to use validate_str
lindsay-stevens 0772526
fix: validators wrap_error not catching all pydantic errors
lindsay-stevens 8bd94ff
add: convenience func to PyODKError for detecting Central HTTP error
lindsay-stevens 349a8ed
add: entity_list methods create, add_property, get; plus example script
lindsay-stevens 3444dc0
fix: typos
lindsay-stevens File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.