root / lib / pyzinn / erp / IpartyAddress.py @ a899390a2a8aca3b3b4617dd7cd96a19e64b489c

View | Annotate | Download (3.8 KB)

1
# Copyright (c) 2009 Lost Oasis, IELO <info@ielo.net>
2
#
3
# This file is part of ZINN.
4
# 
5
# SINN is free software: you can redistribute it and/or modify it under
6
# the terms of the GNU General Public License as published by the Free
7
# Software Foundation, either version 3 of the License, or (at your
8
# option) any later version.
9
# 
10
# SINN is distributed in the hope that it will be useful, but WITHOUT
11
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13
# for more details.
14
# 
15
# You should have received a copy of the GNU General Public License
16
# along with ZINN.  If not, see <http://www.gnu.org/licenses/>.
17
18
"""
19
    Common interface that erp party address (client address) plugins must implement
20
"""
21
22
class IpartyAddress(object):
23
    fields = [
24
            'id',
25
            'name',
26
            'label',
27
            'country',
28
            'street',
29
            'streetbis',
30
            'city',
31
            'zip',
32
            'subdivision',
33
            'siret',
34
            'full_address',
35
            'siret_nic',
36
            ]
37
38
    def __init__(self, transport):
39
        pass
40
41
    def list(self, client, label=None):
42
        '''
43
        Get an address by its label
44
45
        :param label : a string, address label
46
        '''
47
        raise NotImplementedError
48
49
    def create(self, client, label, attributes):
50
        '''
51
        Create an address
52
53
        :param client : a string client label
54
        :param label : a string address label
55
        :param attributes : a dictionary with fields names as key
56
                        and created values as value
57
        '''
58
        raise NotImplementedError
59
60
    def update(self, client, label, attributes):
61
        '''
62
        Update an address
63
64
        :param client : a string client label
65
        :param label : a string address label
66
        :param attributes : a dictionary with fields names as key
67
                        and created values as value
68
        '''
69
        raise NotImplementedError
70
71
    def delete(self, client, label):
72
        '''
73
        Update an address
74
75
        :param client : a string client label
76
        :param label : a string address label
77
        '''
78
        raise NotImplementedError
79
80
    def attributes(self, onlyMutables=False):
81
        raise NotImplementedError
82
83
    def getAttributes(self, onlyMutables=False):
84
        """
85
        @param onlyMutables: bool (optionnal) filters unmutable attributes.
86
        """
87
        if onlyMutables:
88
            return [a for a in self.getAttributes() if a not in ('label', 'siret',
89
                'full_address')]
90
        else:
91
            return [a for a in self.fields if a not in ('id', 'name')]
92
93
    def AttributesFilter(self, attributes, default=None):
94
        mutables = self.getAttributes(onlyMutables=True)
95
        args = default if isinstance(default, dict) else {}
96
        if isinstance(attributes, dict):
97
            for k, v in attributes.iteritems():
98
                if k in mutables:
99
                    args[k] = v
100
        return args
101
102
    def byLabel(self, label=None, fields=[]):
103
        '''
104
        Get an address its label
105
106
        :param id : an integer, address label
107
        '''
108
        raise NotImplementedError
109
110
    def _filter(self, addr):
111
        r = {}
112
        fields = self.getAttributes()
113
        if len(addr) > 0:
114
            for k, v in addr.iteritems():
115
                if k in fields:
116
                    r[k] = v
117
        return r
118
119
    def _decorate(self, addresses):
120
        ret = []
121
        for addr in addresses:
122
            addr['label'] = addr['name']
123
            addr = self._filter(addr)
124
            if addr['country']:
125
                addr['country'] = self.country.byId(addr['country'])
126
            if addr['subdivision']:
127
                addr['subdivision'] = self.country.subdivision.byId(addr['subdivision'])
128
            ret.append(addr)
129
        return ret
130