forked from xlongfeng/manekineko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
photobucket.py
234 lines (197 loc) · 9.64 KB
/
photobucket.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
# -*- 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 base64
from openerp.osv import fields,osv
from openerp import pooler, tools
from openerp.tools.translate import _
sys.path.insert(0, '%s/PbApi/' % os.path.dirname(__file__))
import pbapi
from pbapi.error import *
class photobucket_authorize(osv.TransientModel):
_name = 'photobucket.authorize'
_description = 'Photobucket Authentication'
_columns = {
'consumer_id': fields.many2one('photobucket.consumer', 'Consumer'),
'oauth_token_key': fields.char('Oauth Token Key', readonly=True),
'oauth_token_secret': fields.char('Oauth Token Secret', readonly=True),
'login_url': fields.char('Login URL', size=256, readonly=True),
'state': fields.selection([
('confirm', 'confirm'),
('login', 'login')]),
}
_defaults = {
'state': 'confirm',
}
def get_login_url(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)
consumer = self.pool.get('photobucket.consumer').browse(cr, uid, record_id, context=context)
api = pbapi.PbApi(consumer.consumer_key, consumer.consumer_secret)
api.set_response_parser('xmldomdict')
api.login().request().post().load_token_from_response()
self.write(cr, uid, ids, {'consumer_id': record_id,
'login_url': api.login_url,
'oauth_token_key': api.oauth_token.key,
'oauth_token_secret': api.oauth_token.secret,
'state': 'login'}, context=context)
return {
'name': "Photobucket Authentication",
'type': 'ir.actions.act_window',
'res_model': 'photobucket.authorize',
'view_mode': 'form',
'view_type': 'form',
'res_id': this.id,
'views': [(False, 'form')],
'target': 'new',
}
def get_oauth_token(self, cr, uid, ids, context=None):
if context is None:
context = {}
this = self.browse(cr, uid, ids)[0]
consumer = this.consumer_id
api = pbapi.PbApi(consumer.consumer_key, consumer.consumer_secret)
api.set_response_parser('xmldomdict')
api.set_oauth_token(this.oauth_token_key, this.oauth_token_secret, consumer.name)
api.login().access().post().load_token_from_response()
oauth_token = api.oauth_token
consumer.write(dict(
oauth_token_key=oauth_token.key,
oauth_token_secret=oauth_token.secret,
))
return {'type': 'ir.actions.act_window',
'view_mode': 'form,tree',
'view_type': 'form',
'res_model': 'photobucket.consumer',
'res_id': consumer.id}
photobucket_authorize()
class photobucket_consumer(osv.osv):
_name = "photobucket.consumer"
_description = "Photobucket consumer"
_columns = {
'name': fields.char('Consumer', required=True),
'consumer_key': fields.char('Consumer Key', required=True),
'consumer_secret': fields.char('Consumer Secret', required=True),
'oauth_token_key': fields.char('Oauth Token Key', readonly=True),
'oauth_token_secret': fields.char('Oauth Token Secret', readonly=True),
'media_ids': fields.one2many('photobucket.media', 'consumer_id', 'Medias'),
}
_defaults = {
'consumer_key': '149833735',
'consumer_secret': '9583020d7838b0a56c161fa65800e1c0',
}
photobucket_consumer()
class photobucket_media(osv.osv):
_name = "photobucket.media"
_description = "Photobucket media"
def _get_image(self, cr, uid, ids, name, args, context=None):
result = dict.fromkeys(ids, False)
for obj in self.browse(cr, uid, ids, context=context):
result[obj.id] = tools.image_get_resized_images(obj.image)
return result
def _set_image(self, cr, uid, id, name, value, args, context=None):
return self.write(cr, uid, [id], {'image': tools.image_resize_image_big(value)}, context=context)
def _has_image(self, cr, uid, ids, name, args, context=None):
result = {}
for obj in self.browse(cr, uid, ids, context=context):
result[obj.id] = obj.image != False
return result
_columns = {
'name': fields.char('Name', required=True),
'description': fields.text('Description'),
'browseurl': fields.char('Browse URL', readonly=True),
'thumb': fields.char('Thumb', readonly=True),
'url': fields.char('URL', readonly=True),
# image: all image fields are base64 encoded and PIL-supported
'image': fields.binary("Image", required=True,
help="This field holds the image used as avatar for this contact, limited to 1024x1024px"),
'image_medium': fields.function(_get_image, fnct_inv=_set_image,
string="Medium-sized image", type="binary", multi="_get_image",
store={
'photobucket.media': (lambda self, cr, uid, ids, c={}: ids, ['image'], 10),
},
help="Medium-sized image of this contact. It is automatically "\
"resized as a 128x128px image, with aspect ratio preserved. "\
"Use this field in form views or some kanban views."),
'image_small': fields.function(_get_image, fnct_inv=_set_image,
string="Small-sized image", type="binary", multi="_get_image",
store={
'photobucket.media': (lambda self, cr, uid, ids, c={}: ids, ['image'], 10),
},
help="Small-sized image of this contact. It is automatically "\
"resized as a 64x64px image, with aspect ratio preserved. "\
"Use this field anywhere a small image is required."),
'has_image': fields.function(_has_image, type="boolean"),
'consumer_id': fields.many2one('photobucket.consumer', 'Consumer', required=True, ondelete='no action'),
}
def _get_default_consumer_id(self, cr, uid, context=None):
res = self.pool.get('photobucket.consumer').search(cr, uid, [('oauth_token_key','!=','')], context=context)
return res and res[0] or False
_defaults = {
'consumer_id': _get_default_consumer_id,
}
def copy(self, cr, uid, id, default=None, context=None):
raise osv.except_osv(_('Forbbiden to duplicate'), _('Is not possible to duplicate the record, please create a new one.'))
def unlink(self, cr, uid, ids, context=None):
reserve_ids = list()
for media in self.browse(cr, uid, ids, context=context):
if media.url:
consumer = media.consumer_id
api = pbapi.PbApi(consumer.consumer_key, consumer.consumer_secret)
api.set_response_parser('xmldomdict')
api.set_oauth_token(consumer.oauth_token_key, consumer.oauth_token_secret, consumer.name)
try:
response = api.media(media.url).delete().parsed_response
except PbApiErrorResponse, err:
if err.code != '141':
reserve_ids.append(media.id)
else:
if response['status'] != 'OK':
reserve_ids.append(media.id)
ids = list(set(ids) - set(reserve_ids))
return super(photobucket_media, self).unlink(cr, uid, ids, context=context)
def action_upload(self, cr, uid, ids, context=None):
for media in self.browse(cr, uid, ids, context=context):
consumer = media.consumer_id
api = pbapi.PbApi(consumer.consumer_key, consumer.consumer_secret)
api.set_response_parser('xmldomdict')
api.set_oauth_token(consumer.oauth_token_key, consumer.oauth_token_secret, consumer.name)
params = dict(
type='base64',
uploadfile=media.image,
title=media.name,
filename='oe-%s.jpg' % media.id,
)
if media.description:
params['description'] = media.description
response = api.album(consumer.name).upload(params).post().parsed_response
if response['status'] == 'OK':
content = response['content']
media.write(dict(
browseurl=content['browseurl'],
thumb=content['thumb'],
url=content['url'],
))
photobucket_media()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: