Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/active_document/document.py
blob: 34e1090990ed171f69581fd8d23c4fac10447554 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# Copyright (C) 2011-2012 Aleksey Lim
#
# 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.
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import logging

from active_document import env
from active_document.metadata import StoredProperty
from active_document.metadata import active_property


_logger = logging.getLogger('active_document.document')


class Document(dict):

    #: `Metadata` object that describes the document
    metadata = None

    def __init__(self, guid, record, cached_props=None):
        dict.__init__(self, cached_props or {})
        self.guid = guid
        self._record = record

    @active_property(slot=1000, prefix='IC', typecast=int,
            permissions=env.ACCESS_READ, default=0)
    def ctime(self, value):
        return value

    @active_property(slot=1001, prefix='IM', typecast=int,
            permissions=env.ACCESS_READ, default=0)
    def mtime(self, value):
        return value

    @active_property(slot=1002, prefix='IS', typecast=int,
            permissions=0, default=0)
    def seqno(self, value):
        return value

    def get(self, prop, accept_language=None):
        """Get document's property value.

        :param prop:
            property name to get value
        :returns:
            `prop` value

        """
        prop = self.metadata[prop]

        value = dict.get(self, prop.name)
        if value is None and self._record is not None:
            meta = self._record.get(prop.name)
            if meta is not None:
                if isinstance(prop, StoredProperty):
                    value = meta.get('value')
                else:
                    value = meta

        if value is not None and accept_language:
            if isinstance(prop, StoredProperty) and prop.localized:
                value = self._localize(value, accept_language)

        return value

    def properties(self, props, accept_language=None):
        result = {}
        for i in props:
            result[i] = self.get(i, accept_language)
        return result

    def meta(self, prop):
        return self._record.get(prop)

    def __getitem__(self, prop):
        return self.get(prop)

    def _localize(self, value, accept_language):
        if not value:
            return ''
        if not isinstance(value, dict):
            return value

        for lang in accept_language + [env.DEFAULT_LANG]:
            result = value.get(lang)
            if result is not None:
                return result
            lang = lang.split('-')
            if len(lang) == 1:
                continue
            result = value.get(lang[0])
            if result is not None:
                return result

        # TODO
        return value[sorted(value.keys())[0]]