forked from xlongfeng/manekineko
-
Notifications
You must be signed in to change notification settings - Fork 13
/
ebay_user.py
331 lines (294 loc) · 13.9 KB
/
ebay_user.py
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import os
import sys
import logging
from datetime import datetime, timedelta, tzinfo
import dateutil.parser as parser
from dateutil.relativedelta import relativedelta
from operator import itemgetter
import time
import pytz
from openerp import SUPERUSER_ID
from openerp import pooler, tools
from openerp.osv import fields, osv, orm
from openerp.tools.translate import _
from openerp.tools.float_utils import float_round
import openerp.addons.decimal_precision as dp
import base64
import urllib2
import json
import ebaysdk
from ebaysdk.exception import ConnectionError, ConnectionResponseError
from requests.exceptions import RequestException
_logger = logging.getLogger(__name__)
class ebay_user_authorize(osv.TransientModel):
_name = 'ebay.user.authorize'
_description = 'eBay User Authentication'
_columns = {
'user_id': fields.many2one('ebay.user', 'User', readonly=True),
'session_id': fields.char('SessionID', size=40, readonly=True),
'sign_in_url': fields.char('SignInUrl', size=256, readonly=True),
'state': fields.selection([
('confirm', 'confirm'),
('login', 'login')]),
}
_defaults = {
'state': 'confirm',
}
def get_session_id(self, cr, uid, ids, context=None):
if context is None:
context = {}
this = self.browse(cr, uid, ids)[0]
record_id = context and context.get('active_id', False)
user = self.pool.get('ebay.user').browse(cr, uid, record_id, context=context)
call_data = dict(
RuName=user.ru_name,
)
error_msg = 'Get Session ID failed the specified user %s' % user.name
reply = self.pool.get('ebay.ebay').call(cr, uid, user, 'GetSessionID', call_data, error_msg, context=context).response.reply
session_id = reply.SessionID
sign_in_url = self.pool.get('ebay.ebay').get_ebay_sign_in_url(cr, uid, user.sale_site, user.sandbox, user.ru_name, session_id)
self.write(cr, uid, ids, {'user_id': record_id,
'session_id': session_id,
'sign_in_url': sign_in_url,
'state': 'login'}, context=context)
return {
'name': "eBay User Authentication",
'type': 'ir.actions.act_window',
'res_model': 'ebay.user.authorize',
'view_mode': 'form',
'view_type': 'form',
'res_id': this.id,
'views': [(False, 'form')],
'target': 'new',
}
def fetch_token(self, cr, uid, ids, context=None):
if context is None:
context = {}
this = self.browse(cr, uid, ids)[0]
user = this.user_id
call_data = dict(
SessionID=this.session_id,
)
error_msg = 'Fetch token failed for the user %s' % user.name
reply = self.pool.get('ebay.ebay').call(cr, uid, user, 'FetchToken', call_data, error_msg, context=context).response.reply
user.write(dict(
ebay_auth_token=reply.eBayAuthToken,
hard_expiration_time=reply.HardExpirationTime,
#rest_token=reply.RESTToken
))
return {'type': 'ir.actions.act_window',
'view_mode': 'form,tree',
'view_type': 'form',
'res_model': 'ebay.user',
'res_id': user.id}
ebay_user_authorize()
class ebay_user(osv.osv):
_name = "ebay.user"
_description = "a registered eBay user"
@staticmethod
def get_shipping_service_type():
return [
('cnam', _('China Post Normal Air Mail')),
('cnram', _('China Post Registered Air Mail')),
('hkam', _('HongKong Post Normal Air Mail')),
('hkram', _('HongKong Post Registered Air Mail')),
('sgam', _('Sing Post Normal Air Mail')),
('sgram', _('Sing Post Registered Air Mail')),
]
def _get_shipping_service_type(self, cr, uid, context=None):
return self.get_shipping_service_type()
_columns = {
'email': fields.char('Email', size=128, readonly=True),
'feedback_rating_star': fields.selection([
('Blue', 'Blue Star'),
('CustomCode', 'Reserved for internal or future use.'),
('Green', 'Green Star'),
('GreenShooting','Green Shooting Star'),
('None', 'No graphic displayed'),
('Purple', 'Purple Star'),
('PurpleShooting', 'Purple Shooting Star'),
('Red', 'Red Star'),
('RedShooting', 'Red Shooting Star'),
('SilverShooting', 'Silver Shooting Star'),
('Turquoise', 'Turquoise Star'),
('TurquoiseShooting', 'Turquoise Shooting Star'),
('Yellow', 'Yellow Star'),
('YellowShooting', 'Yellow Shooting Star')
], 'Feedback Rating Star', readonly=True),
'feedback_score': fields.integer('Feedback Score', readonly=True),
'positive_feedback_percent': fields.float('Feedback Percent', readonly=True),
'registration_date': fields.datetime('Registration Date', readonly=True),
'store_owner': fields.boolean('Store Owner', readonly=True),
'store_site': fields.char('Store Site', readonly=True),
'store_url': fields.char('Store URL', readonly=True),
'top_rated_seller': fields.boolean('Top-rated Seller', readonly=True),
'site': fields.char('Site', readonly=True),
'unique_negative_feedback_count': fields.integer('Negative', readonly=True),
'unique_neutral_feedback_count': fields.integer('Neutral', readonly=True),
'unique_positive_feedback_count': fields.integer('Positive', readonly=True),
'name': fields.char('User ID', required=True, select=True),
# Selleris
'seller_list_ids': fields.one2many('ebay.seller.list', 'user_id', 'Seller Lists', readonly=True),
'last_updated': fields.datetime('Last Updated'),
# Application keys for authorization
'ownership': fields.boolean('Ownership', readonly=True),
'sandbox': fields.boolean('Sandbox'),
'sale_site': fields.selection([
('0', 'US'),
('2', 'Canada'),
('3', 'UK'),
('15', 'Australia'),
('201', 'HongKong'),
], 'Sale Site'),
'app_id': fields.char('AppID', size=64),
'dev_id': fields.char('DevID', size=64),
'cert': fields.char('CERT', size=64),
'ru_name': fields.char('RuName', size=64),
# Auth info, get from FetchToken
'ebay_auth_token': fields.char('eBayAuthToken', readonly=True),
'hard_expiration_time': fields.datetime('HardExpirationTime', readonly=True),
'rest_token': fields.char('RESTToken', readonly=True),
# Sale status
'monthly_sales': fields.float('Monthly Sales', readonly=True),
'monthly_sales_volume': fields.integer('Monthly Sales Volume', readonly=True),
# Additional Info
'ebay_item_ids': fields.one2many('ebay.item', 'ebay_user_id', 'Items'),
'paypal_email_address': fields.char('Paypal Email Address'),
'country': fields.char('Country', size=2),
'location': fields.char('Location'),
'shipping_service': fields.selection(
_get_shipping_service_type, 'Shipping service'
),
'after_service_7_template': fields.text('7 days template'),
'after_service_15_template': fields.text('15 days template'),
'after_service_25_template': fields.text('25 days template'),
# User Preferences
'exclude_ship_to_location': fields.text('Exclude Ship To Location', readonly=True),
}
_defaults = {
'feedback_score': 0,
'store_owner': 0,
'ownership': 0,
'sandbox': 0,
'sale_site': '0',
'country': 'CN',
'location': 'ShenZhen',
'shipping_service': 'sgam',
'after_service_7_template': '''
Hi friend.
Your item has been shipped on {{ shipped_time }} by air mail,
and it may take about 10~20 days to arrive,
sometimes it may be delayed by unexpected reason like holiday,
custom`s process, weather condition etc.
It may be delayed up to 35 days to arrive.
We will be very appreciated for your patience.
If you have any question, feel free to contact us asap.
Thanks for your purchase.
Yours Sincerely
''',
'after_service_15_template': '''
Hi friend.
Your item has been shipped on {{ shipped_time }} by air mail.
{{ elapse }} days have passed since your item was shipped,
When you receive it, we sincerely hope that you will like it
and appreciate our customer services.
If there is anything you feel unsatisfied with, please do tell us.
This will help us know what we should do to help you as well as how we should improve.
If you are satisfied, we sincerely hope that you can leave us a positive comment,
which is of vital importance to the growth of our small company.
PLEASE DO NOT leaves us negative feedback. If you are not satisfied in any regard,
please tell us.
Thanks once more for your purchase.
Yours Sincerely
''',
'after_service_25_template': '''
Hi friend.
Your item has been shipped on {{ shipped_time }} by air mail.
If you haven't received your item and this situation lasts to the 35th day,
please do contact us. WE WILL DO OUR BEST TO SOLVE YOUR PROBLEM.
We do not want to give you a bad buying experience even when the shipping is out of our control.
But if you receive it, we sincerely hope you can leave us a positive comment if you like it and
appreciate our customer services.
Thanks once more for your purchase.
Yours Sincerely
'''
}
_order = 'monthly_sales desc'
_sql_constraints = [
('name_uniq', 'unique(name, sandbox)', 'User ID must be unique!'),
]
def copy(self, cr, uid, id, default=None, context=None):
if default is None:
default = {}
name = self.read(cr, uid, id, ['name'], context=context)['name']
default = default.copy()
default.update({
'name': name + _(' (Copy)'),
'session_id': '',
'ebay_auth_token': '',
})
return super(ebay_user, self).copy(cr, uid, id, default, context)
def action_get_user(self, cr, uid, ids, context=None):
for user in self.browse(cr, uid, ids, context=context):
call_data=dict()
call_data['UserID'] = user.name
error_msg = 'Get the data for the specified user %s' % user.name
reply = self.pool.get('ebay.ebay').call(cr, uid, user, 'GetUser', call_data, error_msg, context=context).response.reply
vals = dict()
user_dict = reply.User
vals['email'] = user_dict.Email
vals['feedback_rating_star'] = user_dict.FeedbackRatingStar
vals['feedback_score'] = user_dict.FeedbackScore
vals['positive_feedback_percent'] = user_dict.PositiveFeedbackPercent
vals['registration_date'] = user_dict.RegistrationDate
seller_info = user_dict.SellerInfo
vals['store_owner'] = seller_info.StoreOwner == "true"
if vals['store_owner']:
vals['store_site'] = seller_info.StoreSite
vals['store_url'] = seller_info.StoreURL
vals['top_rated_seller'] = seller_info.get('TopRatedSeller', False)
vals['site'] = user_dict.Site
vals['unique_negative_feedback_count'] = user_dict.UniqueNegativeFeedbackCount
vals['unique_neutral_feedback_count'] = user_dict.UniqueNeutralFeedbackCount
vals['unique_positive_feedback_count'] = user_dict.UniquePositiveFeedbackCount
call_data=dict()
call_data['ShowSellerExcludeShipToLocationPreference'] = 'true'
error_msg = 'Get the user perferences for the user %s' % user.name
reply = self.pool.get('ebay.ebay').call(cr, uid, user, 'GetUserPreferences', call_data, error_msg, context=context).response.reply
exclude_ship_to_location = reply.SellerExcludeShipToLocationPreferences.ExcludeShipToLocation
if type(exclude_ship_to_location) != list:
vals['exclude_ship_to_location'] = exclude_ship_to_location
else:
vals['exclude_ship_to_location'] = '|'.join(exclude_ship_to_location)
user.write(vals)
def action_get_seller_list(self, cr, uid, ids, context=None):
ebay_seller_list_obj = self.pool.get('ebay.seller.list')
try:
for user in self.browse(cr, uid, ids, context=context):
ebay_seller_list_obj.get_seller_list(cr, uid, user, context=context)
except (ConnectionError, ConnectionResponseError, RequestException) as e:
return self.pool.get('ebay.ebay').exception(cr, uid, 'GetSellerList', e, context=context)
else:
return True
ebay_user()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: