entity.py 2.19 KB
Newer Older
Gerion Entrup's avatar
Gerion Entrup committed
1
2
3
4
from collections import namedtuple


def dummy(*args, **kwargs):
Gerion Entrup's avatar
Gerion Entrup committed
5
    """Take all arguments and do absolutly nothing."""
Gerion Entrup's avatar
Gerion Entrup committed
6
7
8
    pass


9
10
Entity_ = namedtuple('Entity', ['parameter', 'query_filter', 'web',
                                'mapping', 'reverse_mapping'])
Gerion Entrup's avatar
Gerion Entrup committed
11
# set reverse_mapping default to dummy and make it optional
Gerion Entrup's avatar
Gerion Entrup committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Entity_.__new__.__defaults__ = (dummy, )


class Entity(Entity_):
    """Collection of data and functions necessary for retrieval and adding
    of an object to the database.

    Attributes:
    parameter       -- tuple of strings, that code the arguments later
                       commited.
    query_filter    -- The Filter object necessary for the checking SQL query.
    web             -- The codification of the ID attribute for the web query.
                       Must be existant in parameter, too.
    mapping         -- A function that gets an ORM object, if existent a web
                       query result and the given parameter as arguments. It is
                       expected that the function then maps the result entries
                       and parameter to the object.
    reverse_mapping -- A function with the same arguments as mapping, but gets
                       the existent object, instead of a new constructed. This
                       function is optional and does nothing as default.
    """
Gerion Entrup's avatar
Gerion Entrup committed
33
34
35


class Filter():
Gerion Entrup's avatar
Gerion Entrup committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
    """Represention of a SQLAlchemy filter expression. Used to "cache" the
    filter expression, before execution the real query.

    The class took every key, value combination as keywords arguments.  This
    arguments are forwarded to the filter expression. The key in it's original
    form, the value is replaced with the relevant parameter value, that can
    be specified with Entity.parameter.

    Example:
    Entity.parameter = ('value',)
    Entity.mb_class = Foo
    Filter(key='value')
    would lead to
    session.query(Foo).filter_by(key=<value>)
    where <value> is replaced with the appripriate parameter value.
    """

Gerion Entrup's avatar
Gerion Entrup committed
53
54
55
56
    def __init__(self, **kwargs):
        self._kwargs = kwargs

    def get_kwargs(self):
Gerion Entrup's avatar
Gerion Entrup committed
57
58
59
        """Return a copy of the keyword arguments that represents the
        filter.
        """
Gerion Entrup's avatar
Gerion Entrup committed
60
        return self._kwargs.copy()