{ "info": { "author": "Guillaume Camera", "author_email": "camera.g@gmail.com", "bugtrack_url": null, "classifiers": [ "License :: OSI Approved :: BSD License", "Natural Language :: French", "Operating System :: OS Independent", "Programming Language :: Python", "Programming Language :: Python :: 2.7", "Topic :: Software Development :: Libraries :: Python Modules" ], "description": "('Helpers SQLAlchemy - :class:`sqla_helpers.base_model.BaseModel`\\n===============================================================\\nInstallation\\n-------------\\n\\n.. rubric:: Git\\n\\nInstallation from git\\n\\n.. code-block:: console\\n\\n $> git clone git@github.com:moumoutte/sqla_helpers.git\\n $> cd sqla_helpers\\n $> sudo python2.7 setup.py install\\n\\n.. rubric:: Eggs\\n\\nInstallation from pypi `eggs`\\n\\n.. code-block:: console\\n\\n $> sudo pip install sqla_helpers\\n\\n\\nGetting Started\\n----------------\\n\\nThe goal of :class:`sqla_helpers.base_model.BaseModel` is to provide syntactic sugar for :mod:`SQLAlchemy`.\\n\\n\\n:class:`sqla_helpers.base_model.BaseModel` is to use as mixin class. This class inherits from nothing and shouldn\\'t be inherited.\\nFor access method from model, models need to be declared as bellow:\\n\\n.. code-block:: python\\n\\n from somewhere import DeclarativeBase\\n from sqla_helpers.base_model import BaseModel\\n\\n class MyModel(DeclarativeBase, BaseModel):\\n id = ... # Clef primaire , l\\'identifiant sous forme d\\'entier\\n awesome_attr = ... # Attribut quelconque du mod\\xc3\\xa8le\\n other_model = relationship(\\'MyOtherModel\\', backref=\\'mymodel\\')\\n\\n\\n class MyOtherModel(DeclarativeBase, BaseModel):\\n id = ... # Clef primaire\\n name = ...\\n model_id = ... # Clef \\xc3\\xa9trang\\xc3\\xa8re sur MyModel\\n\\n\\nThe :class:`DeclarativeBase` class is generated by :func:`declarative_base` function from `SQLAlchemy`.\\n\\nTo avoid mixin uses, :class:`sqla_helpers.base_model.BaseModel` class can be used as `cls` parameter in :func:`declarative_base`\\nfunction. \\n\\n.. code-block:: python\\n\\n from sqlalchemy.ext.declarative import declarative_base\\n from sqla_helpers.base_model import BaseModel\\n DeclarativeBase = declarative_base(cls=BaseModel)\\n\\n\\n.. code-block:: python\\n\\n class MyModel(DeclarativeBase):\\n # ...\\n\\n\\n:class:`sqla_helpers.base_model.BaseModel` needs to build a session when queries are done.\\nIn order to access a session when needing, the class uses the stored function :attr:`sqla_helpers.base_model.BaseModel.sessionmaker`. \\nThis function will be called each time a session is needed.\\nSo we need to store a session_maker by calling `sqla_helpers.base_model.BaseModel.register_sessionmaker` method.\\n\\n.. code-block:: python\\n\\n # Application\\'s initialization\\n def main():\\n # ...\\n BaseModel.register_sessionmaker(scoped_session(sessionmaker(bind=engine)))\\n # ...\\n\\nFor a global session, you can just give a Session which is not a `callable`\\n\\n.. code-block:: python\\n\\n from somwhere import DBSession\\n\\n # Application\\'s initialization\\n def main():\\n # ...\\n BaseModel.register_sessionmaker(DBSession)\\n # ...\\n\\nRegistering a session maker can be dangerous. Because, technically, we change dynamically a class method. To prevent errors, an exception , :exc:`sqla_helpers.base_model.SessionMakerExists`, is raised if a session maker is already registered.\\n\\nBut sometimes, perhaps you need to change it while application is running. So, you can force a new record even if a session maker is already registered\\n\\n.. code-block:: python\\n\\n >>> BaseModel.register_sessionmaker(db_session)\\n >>> new_db_session = amazing_function()\\n >>> BaseModel.register_sessionmaker(new_db_session)\\n Traceback (most recent call last):\\n File \"\", line 1, in \\n SessionMakerExists: A session maker is already registered.\\n >>> BaseModel.register_sessionmaker(new_db_session, force=True)\\n \\nBasic use case :\\n\\n.. code-block:: python\\n\\n >>> MyModel.all()\\n []\\n >>> MyModel.get(id=2)\\n \\n >>> MyModel.get(id=3)\\n *** NoResultFound: No row was found for one()\\n >>> MyModel.filter(id=2)\\n []\\n >>> MyModel.filter(id=3)\\n []\\n >>> MyModel.count(id=2)\\n 1\\n\\n\\n* :meth:`sqla_helpers.base_model.BaseModel.all` returns all the database objects\\n* :meth:`sqla_helpers.base_model.BaseModel.filter` returns a list of matching objects.\\n* :meth:`sqla_helpers.base_model.BaseModel.get` returns an uniq matching object.\\n* :meth:`sqla_helpers.base_model.BaseModel.count` returns the number of matching objects.\\n\\nQuerying criterions can be chained with an `&&` (logical and) operator.\\n\\n.. code-block:: python\\n\\n >>> MyOtherModel.filter(name=\\'toto\\')\\n [, ]\\n >>> MyOtherModel.filter(name=\\'toto\\', id=2)\\n []\\n\\n\\nQuerying for criterions on relations\\n------------------------------------\\n\\nValid querying criterions for a class are defined by the class attributes.\\nIE : in case of `MyOtherModel`, criterions can be `id`, `name` and `model_id`.\\n\\nThis is still true for a Sqlachemy relation.\\n\\nIE: querying all `MyModel` witch `MyOtherModel` have a name \\'foo\\'.\\n\\n.. code-block:: python\\n\\n >>> MyModel.filter(awesome_attr__name=\\'foo\\')\\n []\\n\\n\\nQuerying with entire object.\\n\\n.. code-block:: python\\n\\n >>> otherModel = MyOtherModel.get(name=\\'foo\\')\\n >>> MyModel.filter(awesome_attr=otherModel)\\n []\\n\\n\\nThe `__` separator (double underscore) allows to split between the differents entities.\\n\\nQuering with relations attributes can be done recursively.\\nIf `MyOtherObject` has an `other_attr` attribute which is in relation with a `MyOtherOtherObject` object.\\n\\nQuerying all `MyModel` with a `MyOtherObject` has `MyOtherOtherObject` has a `name` attribute is \\'foo\\'.\\n\\n.. code-block:: python\\n\\n >>> MyModel.filter(awesome_attr__other_attr__name=\\'foo\\')\\n []\\n\\n\\n\\nOperators\\n---------\\n\\nOthers operators than equality can be used. Those operators should be written\\nwith the attribute name following \\'__\\' (double underscore) and operator\\'s name.\\n\\nIE: if all `MyModel` with `id` different than 2 are wanted:\\n\\n.. code-block:: python\\n\\n >>> MyModel.filter(id__not=2)\\n []\\n\\nAvailable operators are:\\n\\n* \\'not\\': Non-equal,\\n* \\'lt\\': letter than,\\n* \\'le\\': letter or equals than,\\n* \\'gt\\': gretter than,\\n* \\'ge\\': gretter or equal than,\\n* \\'in\\': in a list,\\n* \\'like\\': SQL `LIKE` operator,\\n* \\'ilike\\': SQL `ILIKE` operator.\\n\\n\\nMore complex querying\\n--------------------\\n\\nAs the Django way, :mod:`sqla_helpers` provides a :class:`sqla_helpers.logical.Q` object for more complex queries.\\nThe :class:`sqla_helpers.logical.Q` object can use the :mod:`sqla_helpers\\' syntax.\\n\\n.. code-block:: python\\n\\n >>> from sqla_helpers.logical import Q\\n >>> Q(status__name=\\'test\\')\\n \\n\\n\\nThese objects are usable as criterions for query.\\n\\n:class:`sqla_helpers.base_model.BaseModel`\\n\\n.. code-block:: python\\n\\n >>> Treatment.get(Q(id=2))\\n >>> \\n\\nThe goal of those objects is to allow SQL logical conditions in a python syntax.\\n\\nIf all `Treatment` objects wih an `id` == 2 or a `Status` name == \\'KO\\' are wanted.\\n\\n.. code-block:: python\\n\\n >>> Treatment.filter(Q(id=2) | Q(status__name=\\'KO\\'))\\n [, ]\\n\\n\\nFor getting, all `Treatment` objects with an `id\\' attribute different than 2 :\\n\\n.. code-block:: python\\n\\n >>> Treatment.filter(~Q(id=2))\\n [, ,\\n ]\\n\\nLogical operators can be chained :\\n\\n.. code-block:: python\\n\\n >>> Treatment.filter((Q(id=2) | Q(name=\\'toto\\')) & (Q(name=\\'OK\\') | ~Q(status__id=3)))\\n 2013-02-10 16:39:49,485 INFO sqlalchemy.engine.base.Engine SELECT\\n treatment.id AS treatment_id, treatment.name AS treatment_name,\\n treatment.status_id AS treatment_status_id\\n FROM treatment JOIN status ON status.id = treatment.status_id\\n WHERE (treatment.id = ? OR treatment.name = ?) AND (treatment.name = ? OR\\n status.id != ?)\\n 2013-02-10 16:39:49,485 INFO sqlalchemy.engine.base.Engine (2, \\'toto\\', \\'OK\\',\\n 3)\\n >>> []\\n\\n\\nJSON\\n----\\n\\nOften in web oriented applications, client and server exchange with JSON format.\\nIn order to have easier loading, :mod:`sqla_helpers` provides methods for loading from a regular python dictionary or a SQLAlchemy model object.\\n\\nThe :meth:`sqla_helpers.base_model.BaseModel.dump` method allows a JSON compatible dictionary.\\n\\n.. code-block:: python\\n\\n >>> print json.dumps(t.dump(), indent=4)\\n {\\n \"status\": {\\n \"id\": 1,\\n \"name\": \"Ok\"\\n },\\n \"status_id\": 1,\\n \"id\": 1,\\n \"name\": \"Great Treatment\"\\n }\\n\\n\\nThe method `sqla_helpers.base_model.BaseModel.load` can build objects from a dictionary.\\nThe meaning of use a dictionary is to facilitate access to data in JSON or generate JSON from dictionary.\\n\\nObjects are getting from database if primary key attributes are found on the dictionnary. Otherwise new object\\nare created.\\n\\n.. code-block:: python\\n\\n >>> t = Treatment.get(id=7)\\n >>> t.name\\n \\'YEAH \\\\\\\\o/\\'\\n >>> t.id\\n 7\\n >>> t.status.name\\n \\'Holy status !\\'\\n >>> t.status.id\\n 7\\n >>> t = Treatment.load({\\'id\\': 7, \\'name\\': \\'hello\\'})\\n >>> t.name, t.id\\n (\\'hello\\', 7)\\n >>> session.commit()\\n >>> t.dump()\\n {\\n \\'id\\': 7,\\n \\'name\\': u\\'hello\\',\\n \\'status\\': {\\'id\\': 7, \\'name\\': u\\'Holy status !\\'},\\n \\'status_id\\': 7\\n }\\n >>> tr = Treatment.load(t.dump())\\n >>> tr == t\\n True\\n >>> tr.status == t.status\\n True\\n >>> Treatment.load(tr.dump()).dump()\\n {\\n \\'id\\': 7,\\n \\'name\\': u\\'hello\\',\\n \\'status\\': {\\'id\\': 7, \\'name\\': u\\'Holy status !\\'},\\n \\'status_id\\': 7\\n }\\n >>> tr = Treatment.load({\\'name\\': \\'new treatment\\', \\'status\\': {\\'name\\': \\'new status\\'}})\\n >>> tr.id\\n None\\n >>> tr.status.id\\n None\\n >>> session.add(tr)\\n >>> session.commit()\\n >>> tr.id\\n 10\\n >>> tr.status.id\\n 8\\n\\n\\n:class:`sqla_helpers.base_model.BaseModel` class\\n================================================\\n\\n.. automodule:: sqla_helpers.base_model\\n',)", "description_content_type": null, "docs_url": null, "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/moumoutte/sqla_helpers/", "keywords": null, "license": "UNKNOWN", "maintainer": null, "maintainer_email": null, "name": "sqla_helpers", "package_url": "https://pypi.org/project/sqla_helpers/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/sqla_helpers/", "project_urls": { "Download": "UNKNOWN", "Homepage": "https://github.com/moumoutte/sqla_helpers/" }, "release_url": "https://pypi.org/project/sqla_helpers/0.5.1/", "requires_dist": null, "requires_python": null, "summary": "Provide some methods for getting objects with SQLAlchemy", "version": "0.5.1" }, "last_serial": 1007452, "releases": { "0.2.0": [ { "comment_text": "", "digests": { "md5": "8c3c426a99d46af69c6753e269bd10e1", "sha256": "4b8d0a23ef6dab95eb2801433844b6c454684c937fc4494bdab981137d1e2de8" }, "downloads": -1, "filename": "sqla_helpers-0.2.0.tar.gz", "has_sig": false, "md5_digest": "8c3c426a99d46af69c6753e269bd10e1", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4143, "upload_time": "2013-01-28T14:50:29", "url": "https://files.pythonhosted.org/packages/68/b5/935aec050335ea40fb07ac1bdf59bb4bf6ca86f2d9224a652c5065bcb411/sqla_helpers-0.2.0.tar.gz" } ], "0.3.0": [], "0.3.0-dev": [ { "comment_text": "", "digests": { "md5": "7262070dbfb5e5fe87cfa9059f986cc3", "sha256": "0c9bbcb8bac17a79bea78d767aa5e7700d3f1b4e0ed3f7fc78ccac22aecd0b90" }, "downloads": -1, "filename": "sqla_helpers-0.3.0-dev.tar.gz", "has_sig": false, "md5_digest": "7262070dbfb5e5fe87cfa9059f986cc3", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4156, "upload_time": "2013-02-06T16:42:40", "url": "https://files.pythonhosted.org/packages/21/61/f1b0cd186c21027276c2e43b793bb85133ffa7eb13577fcd636b4785779d/sqla_helpers-0.3.0-dev.tar.gz" } ], "0.3.1": [ { "comment_text": "", "digests": { "md5": "056b467ce77e8acb9e6e6729cf4c9058", "sha256": "2f30c205eb27d73436c11815c964dbd70914535d29481f9d6a3864f623c6006d" }, "downloads": -1, "filename": "sqla_helpers-0.3.1.tar.gz", "has_sig": false, "md5_digest": "056b467ce77e8acb9e6e6729cf4c9058", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 12224, "upload_time": "2013-02-14T11:13:53", "url": "https://files.pythonhosted.org/packages/b2/e7/ca9a247a7a5858ae19415e11d97d561d5a7d748ce73ec75855af99afe1c8/sqla_helpers-0.3.1.tar.gz" } ], "0.3.2": [ { "comment_text": "", "digests": { "md5": "be074b60b0fa57eae7cd115d1d204a24", "sha256": "d8fe7ef0eaf08d818bd5a91d02af02932ee00c2b3855e68c280b1f57aff3c085" }, "downloads": -1, "filename": "sqla_helpers-0.3.2.tar.gz", "has_sig": false, "md5_digest": "be074b60b0fa57eae7cd115d1d204a24", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 13506, "upload_time": "2013-02-14T11:27:55", "url": "https://files.pythonhosted.org/packages/97/e4/f7d56f9e9b09f94d2d8edb7b8d79827a9b7d2206e7c93441ad2a922f7e35/sqla_helpers-0.3.2.tar.gz" } ], "0.3.3": [ { "comment_text": "", "digests": { "md5": "43e990afe57b01468f747c5efb6b67ad", "sha256": "01444d55428bf7afb77778e6e2a7f2f2130f66b2af275470ebb9d8e9dd5a1da5" }, "downloads": -1, "filename": "sqla_helpers-0.3.3.tar.gz", "has_sig": false, "md5_digest": "43e990afe57b01468f747c5efb6b67ad", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 14380, "upload_time": "2013-02-14T11:33:05", "url": "https://files.pythonhosted.org/packages/02/39/79b179d72c3f092f1e6a76ffb8efc0b828b67b11d9d0a18c9e6a8283e6b3/sqla_helpers-0.3.3.tar.gz" } ], "0.3.4": [ { "comment_text": "", "digests": { "md5": "9597aa055c704d8a80320563117c9d7d", "sha256": "07417d037a826f21ac7325f7f43518dcbb919c6dc92827280b782f8f930bde72" }, "downloads": -1, "filename": "sqla_helpers-0.3.4.tar.gz", "has_sig": false, "md5_digest": "9597aa055c704d8a80320563117c9d7d", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 14944, "upload_time": "2013-02-28T17:01:50", "url": "https://files.pythonhosted.org/packages/27/e8/662fae4de7861de6c7aed90948f86d17a0aaadd96943a5248781d1b79b9f/sqla_helpers-0.3.4.tar.gz" } ], "0.3.5": [ { "comment_text": "", "digests": { "md5": "69b26171cfef0fd2ec9709a6f38016f9", "sha256": "22e535ca5e4d1ce3a17ac4d78588fa858eb0eeeafcb74c988f856817f5bcda85" }, "downloads": -1, "filename": "sqla_helpers-0.3.5.tar.gz", "has_sig": false, "md5_digest": "69b26171cfef0fd2ec9709a6f38016f9", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 12460, "upload_time": "2013-09-15T09:37:12", "url": "https://files.pythonhosted.org/packages/00/ba/96811e7fe126ad4dbd37aa1a7bfcc7716ce89f8ac34658713006fcd638e4/sqla_helpers-0.3.5.tar.gz" } ], "0.4.0": [ { "comment_text": "", "digests": { "md5": "71671bbbb7628702347f62072c3de052", "sha256": "6dc35b3b84f6cc0ef5a8a048a3f1382c47e8cb1eeaf13cf78358dbee13f24d59" }, "downloads": -1, "filename": "sqla_helpers-0.4.0.tar.gz", "has_sig": false, "md5_digest": "71671bbbb7628702347f62072c3de052", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 14149, "upload_time": "2013-09-29T13:45:28", "url": "https://files.pythonhosted.org/packages/a7/ec/dffe9c0c226ab265c303aa7daa2b6f18bac62a3274106a474a6006fc6316/sqla_helpers-0.4.0.tar.gz" } ], "0.4.1": [ { "comment_text": "", "digests": { "md5": "7cd2089ad80006ed4608b7334d17d8f9", "sha256": "cc90af30efb0871b5d4d52696d68788d6afe66b1a429f504070223ca1425c612" }, "downloads": -1, "filename": "sqla_helpers-0.4.1.tar.gz", "has_sig": false, "md5_digest": "7cd2089ad80006ed4608b7334d17d8f9", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 14140, "upload_time": "2013-09-29T14:34:06", "url": "https://files.pythonhosted.org/packages/6d/11/4140f4263284cf1de20a2d79d93c3033b9cedd8cbf7823673a53d56b4f82/sqla_helpers-0.4.1.tar.gz" } ], "0.5.0": [ { "comment_text": "", "digests": { "md5": "a902357c0073131acc13f0b1f4480619", "sha256": "f61f6fba69ccb9011cffa53d8fc1359ab7acd59a64d844e82ccfb30080196a4a" }, "downloads": -1, "filename": "sqla_helpers-0.5.0.tar.gz", "has_sig": false, "md5_digest": "a902357c0073131acc13f0b1f4480619", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 14942, "upload_time": "2014-02-20T13:42:34", "url": "https://files.pythonhosted.org/packages/31/d7/5fac5dfbd3e78c1e5b29a012420103155e12a140a82a280c2dfe17137895/sqla_helpers-0.5.0.tar.gz" } ], "0.5.1": [ { "comment_text": "", "digests": { "md5": "c4f68dbbd3cd58f5b361340e73266041", "sha256": "8daa26878419f10f5df145d9a31bdb803dd8cb221a1feed22286ae9956b6d89a" }, "downloads": -1, "filename": "sqla_helpers-0.5.1.tar.gz", "has_sig": false, "md5_digest": "c4f68dbbd3cd58f5b361340e73266041", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 15111, "upload_time": "2014-02-21T12:07:15", "url": "https://files.pythonhosted.org/packages/64/fa/4792e5e2d060b39f8e9b28020b8fa5bf138b7723be08bdd51e260bc177a2/sqla_helpers-0.5.1.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "c4f68dbbd3cd58f5b361340e73266041", "sha256": "8daa26878419f10f5df145d9a31bdb803dd8cb221a1feed22286ae9956b6d89a" }, "downloads": -1, "filename": "sqla_helpers-0.5.1.tar.gz", "has_sig": false, "md5_digest": "c4f68dbbd3cd58f5b361340e73266041", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 15111, "upload_time": "2014-02-21T12:07:15", "url": "https://files.pythonhosted.org/packages/64/fa/4792e5e2d060b39f8e9b28020b8fa5bf138b7723be08bdd51e260bc177a2/sqla_helpers-0.5.1.tar.gz" } ] }