__init__.py
93 lines
| 2.8 KiB
| text/x-python
|
PythonLexer
r759 | # -*- coding: utf-8 -*- | |||
""" | ||||
r811 | rhodecode.model.__init__ | |||
~~~~~~~~~~~~~~~~~~~~~~~~ | ||||
r1203 | ||||
r759 | The application's model objects | |||
r1203 | ||||
r759 | :created_on: Nov 25, 2010 | |||
:author: marcink | ||||
r1203 | :copyright: (C) 2009-2011 Marcin Kuzminski <marcin@python-works.com> | |||
r759 | :license: GPLv3, see COPYING for more details. | |||
r1203 | ||||
r759 | :example: | |||
r1203 | ||||
r811 | .. code-block:: python | |||
r1203 | ||||
r811 | from paste.deploy import appconfig | |||
from pylons import config | ||||
from sqlalchemy import engine_from_config | ||||
from rhodecode.config.environment import load_environment | ||||
r1203 | ||||
r811 | conf = appconfig('config:development.ini', relative_to = './../../') | |||
load_environment(conf.global_conf, conf.local_conf) | ||||
r1203 | ||||
r811 | engine = engine_from_config(config, 'sqlalchemy.') | |||
init_model(engine) | ||||
# RUN YOUR CODE HERE | ||||
r1203 | ||||
r759 | """ | |||
r1206 | # This program is free software: you can redistribute it and/or modify | |||
# it under the terms of the GNU General Public License as published by | ||||
# the Free Software Foundation, either version 3 of the License, or | ||||
# (at your option) any later version. | ||||
r1203 | # | |||
r759 | # This program is distributed in the hope that it will be useful, | |||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
# GNU General Public License for more details. | ||||
r1203 | # | |||
r759 | # You should have received a copy of the GNU General Public License | |||
r1206 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
r759 | ||||
r547 | import logging | |||
r1155 | ||||
r547 | from rhodecode.model import meta | |||
r1155 | ||||
r547 | log = logging.getLogger(__name__) | |||
r1271 | ||||
r547 | def init_model(engine): | |||
r1271 | """ | |||
Initializes db session, bind the engine with the metadata, | ||||
Call this before using any of the tables or classes in the model, | ||||
preferably once in application start | ||||
r1203 | ||||
r811 | :param engine: engine to bind to | |||
""" | ||||
r1155 | log.info("initializing db for %s", engine) | |||
r547 | meta.Base.metadata.bind = engine | |||
r752 | ||||
r1271 | ||||
r752 | class BaseModel(object): | |||
r1751 | """ | |||
Base Model for all RhodeCode models, it adds sql alchemy session | ||||
r811 | into instance of model | |||
r1203 | ||||
r811 | :param sa: If passed it reuses this session instead of creating a new one | |||
""" | ||||
r752 | ||||
def __init__(self, sa=None): | ||||
if sa is not None: | ||||
self.sa = sa | ||||
else: | ||||
r1749 | self.sa = meta.Session | |||
r1713 | ||||
r1716 | def _get_instance(self, cls, instance): | |||
r1713 | """ | |||
Get's instance of given cls using some simple lookup mechanism | ||||
:param cls: class to fetch | ||||
:param instance: int or Instance | ||||
""" | ||||
if isinstance(instance, cls): | ||||
return instance | ||||
elif isinstance(instance, int) or str(instance).isdigit(): | ||||
return cls.get(instance) | ||||
else: | ||||
if instance: | ||||
raise Exception('given object must be int or Instance' | ||||
' of %s got %s' % (type(cls), | ||||
type(instance))) | ||||