__init__.py 3.18 KB
Newer Older
Jonny Lamb's avatar
Jonny Lamb committed
1
2
3
4
# -*- coding: utf-8 -*-
#
#   __init__.py — Model initialisation code
#
Arno Töll's avatar
Arno Töll committed
5
#   This file is part of debexpo - https://alioth.debian.org/projects/debexpo/
Jonny Lamb's avatar
Jonny Lamb committed
6
#
Jonny Lamb's avatar
Jonny Lamb committed
7
#   Copyright © 2008 Jonny Lamb <jonny@debian.org>
Jonny Lamb's avatar
Jonny Lamb committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#
#   Permission is hereby granted, free of charge, to any person
#   obtaining a copy of this software and associated documentation
#   files (the "Software"), to deal in the Software without
#   restriction, including without limitation the rights to use,
#   copy, modify, merge, publish, distribute, sublicense, and/or sell
#   copies of the Software, and to permit persons to whom the
#   Software is furnished to do so, subject to the following
#   conditions:
#
#   The above copyright notice and this permission notice shall be
#   included in all copies or substantial portions of the Software.
#
#   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
#   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
#   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
#   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
#   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
#   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
#   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
#   OTHER DEALINGS IN THE SOFTWARE.

30
31
32
33
"""
Model initialization functions.
"""

Jonny Lamb's avatar
Jonny Lamb committed
34
35
36
37
38
39
40
41
42
43
__author__ = 'Jonny Lamb'
__copyright__ = 'Copyright © 2008 Jonny Lamb'
__license__ = 'MIT'

import sqlalchemy as sa
from sqlalchemy import orm

from debexpo.model import meta

def init_model(engine):
44
45
46
47
48
49
50
    """
    Initializes the model.
    This should be called before using any of the tables or classes in the model.

    ``engine``
        SQLAlchemy engine to bind to.
    """
Jonny Lamb's avatar
Jonny Lamb committed
51

52
    sm = orm.sessionmaker(autoflush=True, autocommit=False, bind=engine)
Jonny Lamb's avatar
Jonny Lamb committed
53
54

    meta.engine = engine
55
    meta.session = orm.scoped_session(sm)
Jonny Lamb's avatar
Jonny Lamb committed
56
57

def import_all_models():
58
59
60
61
    """
    Import all models from debexpo.models. This is useful when creating tables.
    """

Jonny Lamb's avatar
Jonny Lamb committed
62
63
    from debexpo.model import binary_packages, package_files, packages, source_packages, \
        user_metrics, package_comments, package_info, package_versions, user_countries, \
64
        users, package_subscriptions, user_upload_key, password_reset, sponsor_metrics, \
65
        data_store, plugin_results
66
67
68
69
70
71
72
73
74
75
76

class OrmObject(object):
    """
    A base class for ORM mapped objects.

    This class was found and then altered for debexpo from
    http://www.sqlalchemy.org/trac/wiki/UsageRecipes/GenericOrmBaseClass

    Contributed by ltbarcly (Justin Van Winkle).
    """
    def __init__(self, **kw):
77
        if not hasattr(self, 'foreign'):
78
            self.foreign = []
79

80
        items = dir(self)
81
        for key in kw:
82
            if key in items or key in self.foreign:
83
84
                setattr(self, key, kw[key])
            else:
85
                raise AttributeError('Cannot set attribute which is ' +
86
87
88
                                     'not column in mapped table: %s' % (key,))

    def __repr__(self):
89
90
91
92
93
        attrs = []
        for key in self.__dict__:
            if not key.startswith('_'):
                attrs.append((key, getattr(self, key)))
        return self.__class__.__name__ + '(' + ', '.join(x[0] + '=' + repr(x[1]) for x in attrs) + ')'