%global _empty_manifest_terminate_build 0 Name: python-pymongoose Version: 1.3.7 Release: 1 Summary: A pymongo helper with methods and classes License: MIT License URL: https://github.com/djcharles26/pymongoose Source0: https://mirrors.aliyun.com/pypi/web/packages/e4/56/2be0e9bc993c2a74ae16400b58db234a86e99553c753754a14ebf44227ed/pymongoose-1.3.7.tar.gz BuildArch: noarch Requires: python3-pymongo %description # Pymongoose This is a pymongo helper package, that let you make more complicated actions with your collections. It gives you the possibility to populate between packages without needs of creating an aggregation by yourself. Gives you the basic actions such as find, update and delete, with more simplicity. Pymongoose came with a Schema class that let you work with more efficiency and organization # First Steps ```bash pip install pymongoose pymongo ``` ## Create a model: models/role.py ```python import datetime from pymongoose import methods from pymongoose.mongo_types import Types, Schema, MongoException, MongoError from bson import json_util from bson.objectid import ObjectId class Role(Schema): schema_name = "roles" # Name of the schema that mongo uses # Attributes id = None name = None action = None def __init__(self, **kwargs): self.schema = { "name": { "type": Types.String, "required": True }, "actions": [{ "type": Types.String, "required": True }] } super().__init__(self.schema_name, self.schema, kwargs) def __str__(self): return f"Role: {self.name}, Actions: {self.action}" ``` **Is higly recommended to follow this model for any schema** ## Mongo Configuration This mongo configuration is required for pymongoose to work correctly db.py: ```python import os, traceback import signal from pymongo import MongoClient from pymongoose.methods import set_schemas, get_cursor_length from models.role import Role MONGO_URI = os.environ.get("MONGO_URI") mongo_db = None def mongo_init (): global mongo_db client = MongoClient(MONGO_URI) db = client.test try: # Now schemas can be set in two ways #-> In a dict mode schemas = { "roles": Role(empty=True).schema } set_schemas(db, schemas) #-> Or in a list mode set_schemas_from_list (db, [Role (empty=True)]) print("MongoDB Connected!") except: traceback.print_exc() print("Error initializing database") exit(1) if __name__ == "__main__": mongo_init() ``` ## Examples: - For more model examples check examples/ ### Insert function: ```python user = User( name="TestA", password="test", role=role.id ) id = user.save() ``` user.save() will throw an exception if a field marked as required in schema is **None**. ### Find function: ```python users = User.find({}) for user in users: user = User.parse(user) ``` This will return a cursor of elements, which can be parsed into User model for a better management. ### Find one function: ```python user = User.find({}, one=True) # OR user = User.find_one ({}) ``` This will return a User element with fields obtained by database, if you prefer a json just add ```python parse = False ``` At last of the **find** statement. ### Find by id function: ```python # this Function will search for "_id" field # so id must be a hex24 string or ObjectId user = User.find_by_id(id) # for a complete user user = User.find_by_id(id, select={"name": 1, "username": 1}) # For a custom user ``` This will return a User element with fields obtained by database, if you prefer a json just add ```python parse = False ``` At last of the **find** statement. ### Populate a search: ```python user = User.find_by_id(id, populate=[ { "path": "role", "select": ["name", "actions"], "options": ["actions"] } ]) # For an extensive populate # or user = User.find_by_id(id, populate=["role"]) # For a simple populate ``` This will return a User element with fields obtained by database Populate is a really useful tool when we work with difficult or complex models, with pymongoose you wont need to create an extensive aggregation to lookup for elements. Populate works with simple ids like: ```python schema = { "name": { "type": Types.String, "required": True }, "role": { # <- Simple field "type": Types.ObjectId, "ref": "roles" } } ``` lists: ```python schema = { "name": { "type": Types.String, "required": True }, "logs": [{ # <- List field "type": Types.ObjectId, "ref": "logs" }] } ``` and complex models ```python schema = { "name": { "type": Types.String, "required": True }, "friends": [{ "friend": { "type": Types.ObjectId, # <- Complex list field "ref": "friends" }, "frequent": { "type": Types.Boolean, "default": False } }] } ``` Pymongoose can recursively populate fields like this: ```python #menu schema: menu_schema = { # ... "items":[ { "type":Types.ObjectId, "ref":"items" } ], # ... } #item schema: item_schema: { # ... "drink":{ "type":Types.ObjectId, "ref":"drinks" }, "dish":{ "type":Types.ObjectId, "ref":"dishes" } # ... } #Populate menus = Menu.find({}, populate=[{ "path": "items", "options": ["dish", "drink"] }]) for menu in menus: print(menu) ``` This will return a CursorCommand element In this example all menus populate each item of their array, at same time element dish and drink are populated, returning a complete array of populated menus with populated items. ## Parsed finds: **pymongoose** has the ability to parse the return cursor as different types: Available types are - IF **AS_DEFAULT(0)**, will return cursor if one == False, else a dict if parse == False - IF **AS_DICT(1)**, will return a list of dicts if one == False, else as serializable dict - IF **AS_STRING(2)**, will return a parsed list of dicts as str if one == False, else as a str parsed serializable dict *Note: In case you don't need a serializable dict, left cursor **AS_DEFAULT** ## Update item: ```python count = User.update({}, { "$set": { "password": "0" } }, many=True ) #many is set to False by default ``` ## Delete item: ```python count = User.delete({}, many=True) #many is set to False by default ``` # Note: If you are working with vscode I have already created a model snippet to save you a lots of time: https://github.com/Djcharles26/pymongoose/blob/master/pymongoose-snippets.json For using this you must copy them to your python snippets: 1. CTRL + SHIFT + P 2. Configure User snippets 3. python.json 4. Copy snippets in blob 5. Paste them in your user python.json %package -n python3-pymongoose Summary: A pymongo helper with methods and classes Provides: python-pymongoose BuildRequires: python3-devel BuildRequires: python3-setuptools BuildRequires: python3-pip %description -n python3-pymongoose # Pymongoose This is a pymongo helper package, that let you make more complicated actions with your collections. It gives you the possibility to populate between packages without needs of creating an aggregation by yourself. Gives you the basic actions such as find, update and delete, with more simplicity. Pymongoose came with a Schema class that let you work with more efficiency and organization # First Steps ```bash pip install pymongoose pymongo ``` ## Create a model: models/role.py ```python import datetime from pymongoose import methods from pymongoose.mongo_types import Types, Schema, MongoException, MongoError from bson import json_util from bson.objectid import ObjectId class Role(Schema): schema_name = "roles" # Name of the schema that mongo uses # Attributes id = None name = None action = None def __init__(self, **kwargs): self.schema = { "name": { "type": Types.String, "required": True }, "actions": [{ "type": Types.String, "required": True }] } super().__init__(self.schema_name, self.schema, kwargs) def __str__(self): return f"Role: {self.name}, Actions: {self.action}" ``` **Is higly recommended to follow this model for any schema** ## Mongo Configuration This mongo configuration is required for pymongoose to work correctly db.py: ```python import os, traceback import signal from pymongo import MongoClient from pymongoose.methods import set_schemas, get_cursor_length from models.role import Role MONGO_URI = os.environ.get("MONGO_URI") mongo_db = None def mongo_init (): global mongo_db client = MongoClient(MONGO_URI) db = client.test try: # Now schemas can be set in two ways #-> In a dict mode schemas = { "roles": Role(empty=True).schema } set_schemas(db, schemas) #-> Or in a list mode set_schemas_from_list (db, [Role (empty=True)]) print("MongoDB Connected!") except: traceback.print_exc() print("Error initializing database") exit(1) if __name__ == "__main__": mongo_init() ``` ## Examples: - For more model examples check examples/ ### Insert function: ```python user = User( name="TestA", password="test", role=role.id ) id = user.save() ``` user.save() will throw an exception if a field marked as required in schema is **None**. ### Find function: ```python users = User.find({}) for user in users: user = User.parse(user) ``` This will return a cursor of elements, which can be parsed into User model for a better management. ### Find one function: ```python user = User.find({}, one=True) # OR user = User.find_one ({}) ``` This will return a User element with fields obtained by database, if you prefer a json just add ```python parse = False ``` At last of the **find** statement. ### Find by id function: ```python # this Function will search for "_id" field # so id must be a hex24 string or ObjectId user = User.find_by_id(id) # for a complete user user = User.find_by_id(id, select={"name": 1, "username": 1}) # For a custom user ``` This will return a User element with fields obtained by database, if you prefer a json just add ```python parse = False ``` At last of the **find** statement. ### Populate a search: ```python user = User.find_by_id(id, populate=[ { "path": "role", "select": ["name", "actions"], "options": ["actions"] } ]) # For an extensive populate # or user = User.find_by_id(id, populate=["role"]) # For a simple populate ``` This will return a User element with fields obtained by database Populate is a really useful tool when we work with difficult or complex models, with pymongoose you wont need to create an extensive aggregation to lookup for elements. Populate works with simple ids like: ```python schema = { "name": { "type": Types.String, "required": True }, "role": { # <- Simple field "type": Types.ObjectId, "ref": "roles" } } ``` lists: ```python schema = { "name": { "type": Types.String, "required": True }, "logs": [{ # <- List field "type": Types.ObjectId, "ref": "logs" }] } ``` and complex models ```python schema = { "name": { "type": Types.String, "required": True }, "friends": [{ "friend": { "type": Types.ObjectId, # <- Complex list field "ref": "friends" }, "frequent": { "type": Types.Boolean, "default": False } }] } ``` Pymongoose can recursively populate fields like this: ```python #menu schema: menu_schema = { # ... "items":[ { "type":Types.ObjectId, "ref":"items" } ], # ... } #item schema: item_schema: { # ... "drink":{ "type":Types.ObjectId, "ref":"drinks" }, "dish":{ "type":Types.ObjectId, "ref":"dishes" } # ... } #Populate menus = Menu.find({}, populate=[{ "path": "items", "options": ["dish", "drink"] }]) for menu in menus: print(menu) ``` This will return a CursorCommand element In this example all menus populate each item of their array, at same time element dish and drink are populated, returning a complete array of populated menus with populated items. ## Parsed finds: **pymongoose** has the ability to parse the return cursor as different types: Available types are - IF **AS_DEFAULT(0)**, will return cursor if one == False, else a dict if parse == False - IF **AS_DICT(1)**, will return a list of dicts if one == False, else as serializable dict - IF **AS_STRING(2)**, will return a parsed list of dicts as str if one == False, else as a str parsed serializable dict *Note: In case you don't need a serializable dict, left cursor **AS_DEFAULT** ## Update item: ```python count = User.update({}, { "$set": { "password": "0" } }, many=True ) #many is set to False by default ``` ## Delete item: ```python count = User.delete({}, many=True) #many is set to False by default ``` # Note: If you are working with vscode I have already created a model snippet to save you a lots of time: https://github.com/Djcharles26/pymongoose/blob/master/pymongoose-snippets.json For using this you must copy them to your python snippets: 1. CTRL + SHIFT + P 2. Configure User snippets 3. python.json 4. Copy snippets in blob 5. Paste them in your user python.json %package help Summary: Development documents and examples for pymongoose Provides: python3-pymongoose-doc %description help # Pymongoose This is a pymongo helper package, that let you make more complicated actions with your collections. It gives you the possibility to populate between packages without needs of creating an aggregation by yourself. Gives you the basic actions such as find, update and delete, with more simplicity. Pymongoose came with a Schema class that let you work with more efficiency and organization # First Steps ```bash pip install pymongoose pymongo ``` ## Create a model: models/role.py ```python import datetime from pymongoose import methods from pymongoose.mongo_types import Types, Schema, MongoException, MongoError from bson import json_util from bson.objectid import ObjectId class Role(Schema): schema_name = "roles" # Name of the schema that mongo uses # Attributes id = None name = None action = None def __init__(self, **kwargs): self.schema = { "name": { "type": Types.String, "required": True }, "actions": [{ "type": Types.String, "required": True }] } super().__init__(self.schema_name, self.schema, kwargs) def __str__(self): return f"Role: {self.name}, Actions: {self.action}" ``` **Is higly recommended to follow this model for any schema** ## Mongo Configuration This mongo configuration is required for pymongoose to work correctly db.py: ```python import os, traceback import signal from pymongo import MongoClient from pymongoose.methods import set_schemas, get_cursor_length from models.role import Role MONGO_URI = os.environ.get("MONGO_URI") mongo_db = None def mongo_init (): global mongo_db client = MongoClient(MONGO_URI) db = client.test try: # Now schemas can be set in two ways #-> In a dict mode schemas = { "roles": Role(empty=True).schema } set_schemas(db, schemas) #-> Or in a list mode set_schemas_from_list (db, [Role (empty=True)]) print("MongoDB Connected!") except: traceback.print_exc() print("Error initializing database") exit(1) if __name__ == "__main__": mongo_init() ``` ## Examples: - For more model examples check examples/ ### Insert function: ```python user = User( name="TestA", password="test", role=role.id ) id = user.save() ``` user.save() will throw an exception if a field marked as required in schema is **None**. ### Find function: ```python users = User.find({}) for user in users: user = User.parse(user) ``` This will return a cursor of elements, which can be parsed into User model for a better management. ### Find one function: ```python user = User.find({}, one=True) # OR user = User.find_one ({}) ``` This will return a User element with fields obtained by database, if you prefer a json just add ```python parse = False ``` At last of the **find** statement. ### Find by id function: ```python # this Function will search for "_id" field # so id must be a hex24 string or ObjectId user = User.find_by_id(id) # for a complete user user = User.find_by_id(id, select={"name": 1, "username": 1}) # For a custom user ``` This will return a User element with fields obtained by database, if you prefer a json just add ```python parse = False ``` At last of the **find** statement. ### Populate a search: ```python user = User.find_by_id(id, populate=[ { "path": "role", "select": ["name", "actions"], "options": ["actions"] } ]) # For an extensive populate # or user = User.find_by_id(id, populate=["role"]) # For a simple populate ``` This will return a User element with fields obtained by database Populate is a really useful tool when we work with difficult or complex models, with pymongoose you wont need to create an extensive aggregation to lookup for elements. Populate works with simple ids like: ```python schema = { "name": { "type": Types.String, "required": True }, "role": { # <- Simple field "type": Types.ObjectId, "ref": "roles" } } ``` lists: ```python schema = { "name": { "type": Types.String, "required": True }, "logs": [{ # <- List field "type": Types.ObjectId, "ref": "logs" }] } ``` and complex models ```python schema = { "name": { "type": Types.String, "required": True }, "friends": [{ "friend": { "type": Types.ObjectId, # <- Complex list field "ref": "friends" }, "frequent": { "type": Types.Boolean, "default": False } }] } ``` Pymongoose can recursively populate fields like this: ```python #menu schema: menu_schema = { # ... "items":[ { "type":Types.ObjectId, "ref":"items" } ], # ... } #item schema: item_schema: { # ... "drink":{ "type":Types.ObjectId, "ref":"drinks" }, "dish":{ "type":Types.ObjectId, "ref":"dishes" } # ... } #Populate menus = Menu.find({}, populate=[{ "path": "items", "options": ["dish", "drink"] }]) for menu in menus: print(menu) ``` This will return a CursorCommand element In this example all menus populate each item of their array, at same time element dish and drink are populated, returning a complete array of populated menus with populated items. ## Parsed finds: **pymongoose** has the ability to parse the return cursor as different types: Available types are - IF **AS_DEFAULT(0)**, will return cursor if one == False, else a dict if parse == False - IF **AS_DICT(1)**, will return a list of dicts if one == False, else as serializable dict - IF **AS_STRING(2)**, will return a parsed list of dicts as str if one == False, else as a str parsed serializable dict *Note: In case you don't need a serializable dict, left cursor **AS_DEFAULT** ## Update item: ```python count = User.update({}, { "$set": { "password": "0" } }, many=True ) #many is set to False by default ``` ## Delete item: ```python count = User.delete({}, many=True) #many is set to False by default ``` # Note: If you are working with vscode I have already created a model snippet to save you a lots of time: https://github.com/Djcharles26/pymongoose/blob/master/pymongoose-snippets.json For using this you must copy them to your python snippets: 1. CTRL + SHIFT + P 2. Configure User snippets 3. python.json 4. Copy snippets in blob 5. Paste them in your user python.json %prep %autosetup -n pymongoose-1.3.7 %build %py3_build %install %py3_install install -d -m755 %{buildroot}/%{_pkgdocdir} if [ -d doc ]; then cp -arf doc %{buildroot}/%{_pkgdocdir}; fi if [ -d docs ]; then cp -arf docs %{buildroot}/%{_pkgdocdir}; fi if [ -d example ]; then cp -arf example %{buildroot}/%{_pkgdocdir}; fi if [ -d examples ]; then cp -arf examples %{buildroot}/%{_pkgdocdir}; fi pushd %{buildroot} if [ -d usr/lib ]; then find usr/lib -type f -printf "\"/%h/%f\"\n" >> filelist.lst fi if [ -d usr/lib64 ]; then find usr/lib64 -type f -printf "\"/%h/%f\"\n" >> filelist.lst fi if [ -d usr/bin ]; then find usr/bin -type f -printf "\"/%h/%f\"\n" >> filelist.lst fi if [ -d usr/sbin ]; then find usr/sbin -type f -printf "\"/%h/%f\"\n" >> filelist.lst fi touch doclist.lst if [ -d usr/share/man ]; then find usr/share/man -type f -printf "\"/%h/%f.gz\"\n" >> doclist.lst fi popd mv %{buildroot}/filelist.lst . mv %{buildroot}/doclist.lst . %files -n python3-pymongoose -f filelist.lst %dir %{python3_sitelib}/* %files help -f doclist.lst %{_docdir}/* %changelog * Thu Jun 08 2023 Python_Bot - 1.3.7-1 - Package Spec generated