__init__.py
139 lines
| 4.3 KiB
| text/x-python
|
PythonLexer
r759 | # -*- coding: utf-8 -*- | |||
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/>. | |||
|
r4116 | """ | ||
rhodecode.model.__init__ | ||||
~~~~~~~~~~~~~~~~~~~~~~~~ | ||||
The application's model objects | ||||
:created_on: Nov 25, 2010 | ||||
:author: marcink | ||||
:copyright: (c) 2013 RhodeCode GmbH. | ||||
:license: GPLv3, see LICENSE for more details. | ||||
:example: | ||||
.. code-block:: python | ||||
from paste.deploy import appconfig | ||||
from pylons import config | ||||
from sqlalchemy import engine_from_config | ||||
from rhodecode.config.environment import load_environment | ||||
conf = appconfig('config:development.ini', relative_to = './../../') | ||||
load_environment(conf.global_conf, conf.local_conf) | ||||
engine = engine_from_config(config, 'sqlalchemy.') | ||||
init_model(engine) | ||||
# RUN YOUR CODE HERE | ||||
""" | ||||
r759 | ||||
r547 | import logging | |||
from rhodecode.model import meta | ||||
r2882 | from rhodecode.lib.utils2 import safe_str, obfuscate_url_pw | |||
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 | |||
""" | ||||
r2882 | engine_str = obfuscate_url_pw(str(engine.url)) | |||
log.info("initializing db for %s" % engine_str) | ||||
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 | ||||
r2521 | cls = None # override in child class | |||
r752 | def __init__(self, sa=None): | |||
if sa is not None: | ||||
self.sa = sa | ||||
else: | ||||
r2521 | self.sa = meta.Session() | |||
r1713 | ||||
r1982 | def _get_instance(self, cls, instance, callback=None): | |||
r1713 | """ | |||
r4089 | Gets instance of given cls using some simple lookup mechanism. | |||
r1800 | ||||
r1713 | :param cls: class to fetch | |||
:param instance: int or Instance | ||||
r1982 | :param callback: callback to call if all lookups failed | |||
r1713 | """ | |||
if isinstance(instance, cls): | ||||
return instance | ||||
r2672 | elif isinstance(instance, (int, long)) or safe_str(instance).isdigit(): | |||
r1713 | return cls.get(instance) | |||
else: | ||||
if instance: | ||||
r1982 | if callback is None: | |||
raise Exception( | ||||
r2149 | 'given object must be int, long or Instance of %s ' | |||
'got %s, no callback provided' % (cls, type(instance)) | ||||
r1982 | ) | |||
else: | ||||
return callback(instance) | ||||
r2432 | ||||
def _get_user(self, user): | ||||
""" | ||||
Helper method to get user by ID, or username fallback | ||||
r3840 | :param user: UserID, username, or User instance | |||
r2432 | """ | |||
r2521 | from rhodecode.model.db import User | |||
r2432 | return self._get_instance(User, user, | |||
callback=User.get_by_username) | ||||
def _get_repo(self, repository): | ||||
""" | ||||
Helper method to get repository by ID, or repository name | ||||
r3840 | :param repository: RepoID, repository name or Repository Instance | |||
r2432 | """ | |||
r2521 | from rhodecode.model.db import Repository | |||
r2432 | return self._get_instance(Repository, repository, | |||
callback=Repository.get_by_repo_name) | ||||
def _get_perm(self, permission): | ||||
""" | ||||
Helper method to get permission by ID, or permission name | ||||
r3840 | :param permission: PermissionID, permission_name or Permission instance | |||
r2432 | """ | |||
r2521 | from rhodecode.model.db import Permission | |||
r2432 | return self._get_instance(Permission, permission, | |||
callback=Permission.get_by_key) | ||||
r2521 | ||||
|
r4116 | @classmethod | ||
def get_all(cls): | ||||
r2521 | """ | |||
Returns all instances of what is defined in `cls` class variable | ||||
""" | ||||
|
r4116 | return cls.cls.getAll() | ||