https://github.com/alexey-sveshnikov/pyrus-orm
Simple ORM for Pyrus
https://github.com/alexey-sveshnikov/pyrus-orm
client orm pyrus python
Last synced: 10 months ago
JSON representation
Simple ORM for Pyrus
- Host: GitHub
- URL: https://github.com/alexey-sveshnikov/pyrus-orm
- Owner: alexey-sveshnikov
- License: mit
- Created: 2022-12-06T21:52:09.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-03-23T13:51:34.000Z (about 3 years ago)
- Last Synced: 2024-12-08T00:46:02.719Z (over 1 year ago)
- Topics: client, orm, pyrus, python
- Language: Python
- Homepage: https://pypi.org/project/pyrus-orm/
- Size: 25.4 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
pyrus-orm
=========
Radically simple, django/peewee-like, easy and incomplete ORM for [Pyrus](https://pyrus.com).
With pyrus-orm, you can read, create and modify [tasks](https://pyrus.com/en/help/api/models#form-registry-task).
Works with [pyrus-api](https://github.com/simplygoodsoftware/pyrusapi-python) under the hood.
### This is an early development version
### Features:
- Define models with:
- [x] simple fields (text, number, dates, checkmark, flag, ...)
- [x] catalog fields, single item
- [ ] catalog fields, multiple items
- [ ] "title" fields (pyrus-orm ignores the nested structure of 'title' fields, all its contents are treated as usual root-level fields)
- [x] multiple choice fields (without nested fields at this moment)
- Operations with models:
- [x] Create and save
- [x] Read from registry by ID
- [x] Modify and save changes
- Filtering:
- [x] by include_archived and steps fields
- [x] by value of simple or catalog fields
- [ ] less than, greater than
- [ ] value in a list
- [ ] ranges
Installation
-----------
```shell
pip install pyrus-orm
```
Examples
-------
### Define model and initialize
```python
class Book(PyrusModel):
title = TextField(1) # 1 is a field ID in pyrus's form
time = TimeField(2)
date = DateField(3)
number = NumericField(4)
round_number = IntegerField(5)
author = CatalogField(6, catalog=)
class Meta:
form_id =
pyrus_api = PyrusAPI(...)
session = PyrusORMSession(pyrus_api)
set_session_global(session)
```
### Create item
```python
book = Book(
title='Don Quixote',
date='1605-01-01',
author=Book.author.find({'Name': 'Alonso Fernández de Avellaneda'})
)
book.save()
book.id
>>>
```
### Read and modify item
```python
book = Book.objects.get(id=...)
# simple field
book.title
>>> 'Don Quixote'
book.title = 'Don Quixote, Part Two'
book.save('title changed')
# catalog field
book.author
>>> CatalogItem(item_id=..., values={'Name': 'Alonso Fernández de Avellaneda'}) # values comes from the catalog definition
book.author.find_and_set({'Name': 'Miguel de Cervantes'}) # may raise ValueError if no value found
book.save('changed an author to the real one')
```
### Catalog Enum fields
Enums can be mapped to catalog items by ID or by custom property name.
#### Enums mapped to specific catalog items ID
No catalog lookups are preformed on reading or writing of such fields.
```python
class Genre(Enum):
fiction = 100001
nonfiction = 100002
class Book(PyrusModel):
genre = CatalogEnumField(, catalog_id=, enum=Genre, id_field='item_id')
book = Book.objects.get(id=...)
book.genre
>>> Genre.fiction
book.genre = Genre.nonfiction
book.save()
book.genre
>>> Genre.nonfiction
```
#### Enums mapped to catalog item properties
(imagine book has a property 'media' with field 'Name')
```python
class Media(Enum):
paper = 'paper'
papirus = 'papirus'
pdf = 'pdf'
class Book(PyrusModel):
media = CatalogEnumField(, catalog_id=, enum=Genre, id_field='Name')
```
### Filtering
Only basic filtering is supported:
```python
Book.objects.get_filtered(
title='Don Quixote',
)
>>> [Book(...), ...]
Book.objects.get_filtered(
genre=Book.genre.find({'Name': 'Fiction'})
)
>>> [Book(...), ...]
Book.objects.get_filtered(
...
include_archived=True,
steps=[1, 2],
)
>>> [Book(...), ...]
```
### Catalog fields, all the API
```python
# Read values
# Non-empty value
book.author
>>> CatalogItem(item_id=..., values={})
assert bool(book.author) == True
# Empty value
book.author
>>> CatalogEmptyValue()
assert bool(book.author) == False
# Get all possible values (works for empty fields as well)
book.author.catalog()
>>> [CatalogItem(...), CatalogItem(...), ...]
# Find a value in a catalog
new_author = book.author.catalog().find({'Name': 'Miguel de Cervantes'})
new_author
>>> CatalogItem(item_id=..., values={'Name': 'Miguel de Cervantes'}) # or None
book.author = new_author
book.save()
# Find and set shortcut
book.author.catalog().find_and_set({'Name': 'William Shakespeare'})
book.author.find_and_set({'Name': 'NonExistent'})
>>> ValueError raised
# Set value to a specific item_id
book.author = CatalogItem(item_id=123456)
```