ORM-like API MongoDB for Python language.
Project description
ramifice
ORM-like API MongoDB for Python language.
Ramifice is built around PyMongo.
For simulate relationship Many-to-One and Many-to-Many,
a simplified alternative (Types of selective fields with dynamic addition of elements) is used.
The project is more concentrated for web development or for applications with a graphic interface.
Supports MongoDB 3.6, 4.0, 4.2, 4.4, 5.0, 6.0, 7.0, and 8.0.
For more information see PyMongo.
Documentation
Online browsable documentation is available at https://kebasyaty.github.io/ramifice/.
Requirements
View the list of requirements.
Installation
# Fedora:
sudo dnf install gettext
gettext --version
# Ubuntu:
sudo apt install gettext
gettext --version
# Windows:
https://mlocati.github.io/articles/gettext-iconv-windows.html
gettext --version
cd project_name
uv add ramifice
- Add
configandpublicdirectories in root of your project:
Download config directory
Download public directory
Usage
It is recommended to look at examples here.
import asyncio
import pprint
from datetime import datetime
from pymongo import AsyncMongoClient
from ramifice import model, translations
from ramifice.fields import DateField, EmailField, ImageField, PasswordField, TextField
from ramifice.migration import Monitor
@model(service_name="Accounts")
class User:
def fields(self):
gettext = translations.gettext
# ngettext = translations.ngettext
self.avatar = ImageField(
label=gettext("Avatar"),
default="public/media/default/no-photo.png",
# Available 4 sizes from lg to xs or None.
# Hint: By default = None
thumbnails={"lg": 512, "md": 256, "sm": 128, "xs": 64},
# True - high quality and low performance for thumbnails.
# Hint: By default = False
high_quality=True,
# The maximum size of the original image in bytes.
# Hint: By default = 2 MB
max_size=524288 # 0.5 MB = 524288 Bytes (in binary)
)
self.username = TextField(
label=gettext("Username"),
required=True,
unique=True,
)
self.first_name = TextField(label=gettext("First name"), required=True)
self.last_name = TextField(
label=gettext("Last name"),
required=True,
)
self.email = EmailField(
label=gettext("Email"),
required=True,
unique=True,
)
self.birthday = DateField(label=gettext("Birthday"))
self.password = PasswordField(label=gettext("Password"))
self.сonfirm_password = PasswordField(
label=gettext("Confirm password"),
# If true, the value of this field is not saved in the database.
ignored=True,
)
async def add_validation(self) -> dict[str, str]:
"""For additional validation of fields."""
error_map: dict[str, str] = {}
if self.password != self.сonfirm_password:
error_map["password"] = "Passwords do not match!"
return error_map
async def main():
client = AsyncMongoClient()
await Monitor(
database_name="test_db",
mongo_client=client,
).migrat()
# If you need to change the language of translation.
# translations.change_locale("ru")
user = User()
user.username.value = "pythondev"
user.avatar.from_path("public/media/default/no-photo.png")
user.first_name.value = "John"
user.last_name.value = "Smith"
user.email.value = "John_Smith@gmail.com"
user.birthday.value = datetime(2000, 1, 25)
user.password.value = "12345678"
user.сonfirm_password.value = "12345678"
if not await user.save():
# Convenient to use during development.
user.print_err()
doc_count = await User.estimated_document_count()
print(f"Document count: {doc_count}") # => 1
print("User details:")
user_details = await User.find_one_to_raw_doc({"_id": user._id.value})
pprint.pprint(user_details)
# await user.delete()
# doc_count = await User.estimated_document_count()
# print(f"Document count: {doc_count}") # => 0
await client.close()
if __name__ == "__main__":
asyncio.run(main())
For create custom translations
from ramifice import translations
translations.DEFAULT_LOCALE = "en" # For Ramifice by default = "en"
translations.LANGUAGES = ["en", "ru"] # For Ramifice by default = ["en", "ru"]
cd project_name
# Add your custom translations:
uv run pybabel extract -o config/translations/custom.pot src
uv run pybabel init -i config/translations/custom.pot -d config/translations/custom -l en
uv run pybabel init -i config/translations/custom.pot -d config/translations/custom -l ru
...
uv run pybabel compile -d config/translations/custom
# Update your custom translations:
uv run pybabel extract -o config/translations/custom.pot src
uv run pybabel update -i config/translations/custom.pot -d config/translations/custom
uv run pybabel compile -d config/translations/custom
#
# Add new languages to Ramifice:
# Example:
uv run pybabel init -i config/translations/ramifice.pot -d config/translations/ramifice -l de
uv run pybabel init -i config/translations/ramifice.pot -d config/translations/ramifice -l de_ch
...
uv run pybabel compile -d config/translations/ramifice
# Update translations to Ramifice:
uv run pybabel extract -o config/translations/ramifice.pot ramifice
uv run pybabel update -i config/translations/ramifice.pot -d config/translations/ramifice
uv run pybabel compile -d config/translations/ramifice
See more examples here.
Model Parameters
See the documentation here.
( only service_name is a required parameter )
| Parameter | Default | Description |
|---|---|---|
| service_name | no | Examples: Accounts | Smartphones | Washing machines | etc ... |
| fixture_name | no |
The name of the fixture in the config/fixtures directory (without extension).
Examples: SiteSettings | AppSettings | etc ... |
| db_query_docs_limit | 1000 | limiting query results. |
| is_migrate_model | True |
Set to False if you do not need to migrate the Model to the database. This can be use to validate a web forms - Search form, Contact form, etc. |
| is_create_doc | True |
Can a Model create new documents in a collection? Set to False if you only need one document in the collection and the Model is using a fixture. |
| is_update_doc | True | Can a Model update documents in a collection? |
| is_delete_doc | True | Can a Model remove documents from a collection? |
Example:
@model(
service_name="ServiceName",
fixture_name="FixtureName",
db_query_docs_limit=1000,
is_migrate_model=True,
is_create_doc = True,
is_update_doc = True,
is_delete_doc = True,
)
class User:
def fields(self):
self.username = TextField(
label=gettext("Username"),
required=True,
unique=True,
)
Contributing
- Fork it (https://github.com/kebasyaty/ramifice/fork)
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request
Install for development of Ramifice
# Fedora:
sudo dnf install gettext
gettext --version
# Ubuntu:
sudo apt install gettext
gettext --version
# Windows:
https://mlocati.github.io/articles/gettext-iconv-windows.html
gettext --version
cd project_name
uv sync
Contributors
- kebasyaty Gennady Kostyunin - creator and maintainer
Changelog
License
This project is licensed under the MIT.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ramifice-0.3.11-py3-none-any.whl.
File metadata
- Download URL: ramifice-0.3.11-py3-none-any.whl
- Upload date:
- Size: 76.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dc20875b1cbcea7a980289e58daf8c0e7b63235e2fe59f0172b1b66f2f5043ac
|
|
| MD5 |
535ed8be49a97749a753412e97751231
|
|
| BLAKE2b-256 |
1bf6a07d35059e42818c5226a967ea93c44d5edaabade18426bb7cad2f42bf9b
|