~camptocamp/openerp-connector-magento/7-htaccess-auth-support-dbl-next-release-gbr

« back to all changes in this revision

Viewing changes to magentoerpconnect/unit/backend_adapter.py

  • Committer: Guewen Baconnier
  • Date: 2014-04-04 08:46:16 UTC
  • Revision ID: guewen.baconnier@camptocamp.com-20140404084616-7jj1yjq0qoe0822i
Use the MagentoLocation instance to store the informations about the Magento connection. This object is also responsible to build the complete location.

Rename display_auth_basic to use_auth_basic, because if it is not checked and the username and password are filled, they should not be used

Show diffs side-by-side

added added

removed removed

Lines of Context:
75
75
 
76
76
class MagentoLocation(object):
77
77
 
78
 
    def __init__(self, location, username, password):
79
 
        self.location = location
 
78
    def __init__(self, location, username, password,
 
79
                 use_custom_api_path=False):
 
80
        self._location = location
80
81
        self.username = username
81
82
        self.password = password
 
83
        self.use_custom_api_path = use_custom_api_path
 
84
 
 
85
        self.use_auth_basic = False
 
86
        self.auth_basic_username = None
 
87
        self.auth_basic_password = None
 
88
 
 
89
    @property
 
90
    def location(self):
 
91
        location = self._location
 
92
        if not self.use_auth_basic:
 
93
            return location
 
94
        assert self.auth_basic_username and self.auth_basic_password
 
95
        replacement = "%s:%s@" % (self.auth_basic_username,
 
96
                                  self.auth_basic_password)
 
97
        location = location.replace('://', '://' + replacement)
 
98
        return location
82
99
 
83
100
 
84
101
class MagentoCRUDAdapter(CRUDAdapter):
91
108
        :type environment: :py:class:`connector.connector.Environment`
92
109
        """
93
110
        super(MagentoCRUDAdapter, self).__init__(environment)
94
 
        self.magento = MagentoLocation(self.backend_record.location,
95
 
                                       self.backend_record.username,
96
 
                                       self.backend_record.password)
 
111
        backend = self.backend_record
 
112
        magento = MagentoLocation(
 
113
            backend.location,
 
114
            backend.username,
 
115
            backend.password,
 
116
            use_custom_api_path=backend.use_custom_api_path)
 
117
        if backend.use_auth_basic:
 
118
            magento.use_auth_basic = True
 
119
            magento.auth_basic_username = backend.auth_basic_username
 
120
            magento.auth_basic_password = backend.auth_basic_password
 
121
        self.magento = magento
97
122
 
98
123
    def search(self, filters=None):
99
124
        """ Search records according to some criterias
121
146
        """ Delete a record on the external system """
122
147
        raise NotImplementedError
123
148
 
124
 
    def _complete_url(self):
125
 
        location = self.magento.location
126
 
        if (self.backend_record.auth_basic_username
127
 
                and self.backend_record.auth_basic_password):
128
 
            replacement = self.backend_record.auth_basic_username + ':'
129
 
            replacement += self.backend_record.auth_basic_password + '@'
130
 
            location = location.replace('://', '://' + replacement)
131
 
        return location
132
 
 
133
149
    def _call(self, method, arguments):
134
150
        try:
135
 
            location = self._complete_url()
136
 
            full_url = self.backend_record.use_custom_api_path
137
 
            with magentolib.API(location,
 
151
            custom_url = self.magento.use_custom_api_path
 
152
            with magentolib.API(self.magento.location,
138
153
                                self.magento.username,
139
154
                                self.magento.password,
140
 
                                full_url=full_url) as api:
 
155
                                full_url=custom_url) as api:
141
156
                result = api.call(method, arguments)
142
157
                # Uncomment to record requests/responses in ``recorder``
143
158
                # record(method, arguments, result)