[web2py] Re: Saving an image from another database into your database

2012-07-06 Thread Massimo Di Pierro
That only sets the default behavior. Only on systems that do not have a 
writable filesystem (like on GAE and couchdb) files go in blob by default.

On relational databases, uploads are stored in files, not in blobs (by 
default).



On Friday, 6 July 2012 16:22:36 UTC-5, RCTYCO wrote:
>
> I believe i found my problem. The problem is with MySQL. I noticed in the 
> MySQL._db._adapter.uploads_in_blob = FALSE.
>
> The database adapter doesn't allow uploads_in_blob. MySQLAdapter* *inherents 
> the uploads_in_blob of FALSE from BaseAdapter. 
> Do you know why the uploads_in_blob isn't set to true?
>
>
>
> On Friday, July 6, 2012 12:27:41 PM UTC-7, Massimo Di Pierro wrote:
>>
>> Notice that these two lines:
>>
>> Field('image_file','blob'),
>> Field('image_upload','upload',uploadfield= Field('image_file','blob'))
>>
>> should be
>>
>> Field('image_file','blob'),
>> Field('image_upload','upload',uploadfield= 'image_file'),
>>
>> or simply
>>
>> Field('image_upload','upload',uploadfield= Field('image_file','blob')) 
>>
>> The blob should be created automatically.
>>
>>
>> On Thursday, 5 July 2012 06:23:31 UTC-5, RCTYCO wrote:
>>>
>>>
>>> I am trying to save an image from another database into my 
>>> database, essentially i am caching the a copy into my database so I don't 
>>> have to query the external site. 
>>>
>>> I have a link 
>>> http://eandata.com/image/products/004/900/000/004900443.jpg. 
>>>
>>> I want to save the jpg into a blob in my database.
>>>
>>> Model
>>> db.define_table('image',
>>> Field('url','string'),
>>> Field('image_upload','upload',uploadfield='image_file'),
>>> Field('image_file','blob'))
>>>
>>> Function. I have this running a module and called when I need to 
>>> download the external image.
>>>
>>> def getImage(url,db): 
>>> 
>>> user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
>>> headers = {'User-Agent': user_agent}
>>> 
>>> req = urllib2.Request(url,"",headers)
>>> 
>>> try:
>>> response = urllib2.urlopen(req)
>>> except urllib2.URLError, e:
>>> if hasattr(e, 'reason'):
>>> print 'We failed to reach a server.'
>>> print 'Reason: ', e.reason
>>> elif hasattr(e, 'code'):
>>> print 'The server couldn\'t fulfill the request.'
>>> print 'Error code: ', e.code
>>> else:
>>> pass # everything is fine
>>> 
>>> print response.geturl()
>>> print response.info()
>>> print response.read()
>>> f = open(response.read(),'rb')
>>> #stream = open(response,'rb')
>>> 
>>> return 
>>> db.image.insert(url=url,image=db.image.image_upload.store(f,url))
>>>
>>>
>>> *I am having a problem with my insert statement. I can't seem to figure 
>>> out how to save the image into database.* urllib2.urlopen(req) will 
>>> download the file and provide a file object. But I don't know if i can just 
>>> save the file or do i need to read the file object within 
>>> the urllib2.urlopen(req).
>>>
>>> I would like the image to be available to be via 
>>> http://mydomain.com/myapp/imageLookup?id=2 
>>> 
>>>
>>> Controller
>>> def imageLookup():
>>> id = request.vars.id
>>> #response.headers['Content-Type']='image/jpeg' 
>>> 
>>> return db.image[id].image_file
>>>
>>>
>>> Do you have any suggestions? The code abo
>>>
>>

[web2py] Re: Saving an image from another database into your database

2012-07-06 Thread RCTYCO
I believe i found my problem. The problem is with MySQL. I noticed in the 
MySQL._db._adapter.uploads_in_blob = FALSE.

The database adapter doesn't allow uploads_in_blob. MySQLAdapter* *inherents 
the uploads_in_blob of FALSE from BaseAdapter. 
Do you know why the uploads_in_blob isn't set to true?



On Friday, July 6, 2012 12:27:41 PM UTC-7, Massimo Di Pierro wrote:
>
> Notice that these two lines:
>
> Field('image_file','blob'),
> Field('image_upload','upload',uploadfield= Field('image_file','blob'))
>
> should be
>
> Field('image_file','blob'),
> Field('image_upload','upload',uploadfield= 'image_file'),
>
> or simply
>
> Field('image_upload','upload',uploadfield= Field('image_file','blob')) 
>
> The blob should be created automatically.
>
>
> On Thursday, 5 July 2012 06:23:31 UTC-5, RCTYCO wrote:
>>
>>
>> I am trying to save an image from another database into my 
>> database, essentially i am caching the a copy into my database so I don't 
>> have to query the external site. 
>>
>> I have a link 
>> http://eandata.com/image/products/004/900/000/004900443.jpg. 
>>
>> I want to save the jpg into a blob in my database.
>>
>> Model
>> db.define_table('image',
>> Field('url','string'),
>> Field('image_upload','upload',uploadfield='image_file'),
>> Field('image_file','blob'))
>>
>> Function. I have this running a module and called when I need to download 
>> the external image.
>>
>> def getImage(url,db): 
>> 
>> user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
>> headers = {'User-Agent': user_agent}
>> 
>> req = urllib2.Request(url,"",headers)
>> 
>> try:
>> response = urllib2.urlopen(req)
>> except urllib2.URLError, e:
>> if hasattr(e, 'reason'):
>> print 'We failed to reach a server.'
>> print 'Reason: ', e.reason
>> elif hasattr(e, 'code'):
>> print 'The server couldn\'t fulfill the request.'
>> print 'Error code: ', e.code
>> else:
>> pass # everything is fine
>> 
>> print response.geturl()
>> print response.info()
>> print response.read()
>> f = open(response.read(),'rb')
>> #stream = open(response,'rb')
>> 
>> return 
>> db.image.insert(url=url,image=db.image.image_upload.store(f,url))
>>
>>
>> *I am having a problem with my insert statement. I can't seem to figure 
>> out how to save the image into database.* urllib2.urlopen(req) will 
>> download the file and provide a file object. But I don't know if i can just 
>> save the file or do i need to read the file object within 
>> the urllib2.urlopen(req).
>>
>> I would like the image to be available to be via 
>> http://mydomain.com/myapp/imageLookup?id=2 
>> 
>>
>> Controller
>> def imageLookup():
>> id = request.vars.id
>> #response.headers['Content-Type']='image/jpeg' 
>> 
>> return db.image[id].image_file
>>
>>
>> Do you have any suggestions? The code abo
>>
>

[web2py] Re: Saving an image from another database into your database

2012-07-06 Thread Massimo Di Pierro
Notice that these two lines:

Field('image_file','blob'),
Field('image_upload','upload',uploadfield= Field('image_file','blob'))

should be

Field('image_file','blob'),
Field('image_upload','upload',uploadfield= 'image_file'),

or simply

Field('image_upload','upload',uploadfield= Field('image_file','blob'))

The blob should be created automatically.


On Thursday, 5 July 2012 06:23:31 UTC-5, RCTYCO wrote:
>
>
> I am trying to save an image from another database into my 
> database, essentially i am caching the a copy into my database so I don't 
> have to query the external site. 
>
> I have a link 
> http://eandata.com/image/products/004/900/000/004900443.jpg. 
>
> I want to save the jpg into a blob in my database.
>
> Model
> db.define_table('image',
> Field('url','string'),
> Field('image_upload','upload',uploadfield='image_file'),
> Field('image_file','blob'))
>
> Function. I have this running a module and called when I need to download 
> the external image.
>
> def getImage(url,db): 
> 
> user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
> headers = {'User-Agent': user_agent}
> 
> req = urllib2.Request(url,"",headers)
> 
> try:
> response = urllib2.urlopen(req)
> except urllib2.URLError, e:
> if hasattr(e, 'reason'):
> print 'We failed to reach a server.'
> print 'Reason: ', e.reason
> elif hasattr(e, 'code'):
> print 'The server couldn\'t fulfill the request.'
> print 'Error code: ', e.code
> else:
> pass # everything is fine
> 
> print response.geturl()
> print response.info()
> print response.read()
> f = open(response.read(),'rb')
> #stream = open(response,'rb')
> 
> return 
> db.image.insert(url=url,image=db.image.image_upload.store(f,url))
>
>
> *I am having a problem with my insert statement. I can't seem to figure 
> out how to save the image into database.* urllib2.urlopen(req) will 
> download the file and provide a file object. But I don't know if i can just 
> save the file or do i need to read the file object within 
> the urllib2.urlopen(req).
>
> I would like the image to be available to be via 
> http://mydomain.com/myapp/imageLookup?id=2 
> 
>
> Controller
> def imageLookup():
> id = request.vars.id
> #response.headers['Content-Type']='image/jpeg' 
> 
> return db.image[id].image_file
>
>
> Do you have any suggestions? The code abo
>


[web2py] Re: Saving an image from another database into your database

2012-07-05 Thread RCTYCO
Do I have to do something like 

db.define_table('image',
Field('url','string'),
Field('image_file','blob'),
Field('image_upload','upload',uploadfield= 
Field('image_file','blob'))

Where i provided a filed in the uploadfield

On Thursday, July 5, 2012 10:30:22 PM UTC-7, RCTYCO wrote:
>
> Thanks.
>
> I need to solve the problem of the file not been uploaded to the blob 
> field.
>
> I believe i am doing it correctly.
>
> I defined the model 
> db.define_table('image',
> Field('url','string'),
> Field('image_file','blob'),
> Field('image_upload','upload',uploadfield='image_file'))
>
>
> I stepped through the code for the store function 
> (db.image.image_upload.store(response,filename=url))).
>
> I expect line 7345 to turn out to be true to it eval to false. So my file 
> is never uploaded to the blob file. 
> File: gluon.dal
>
> Line 7345:   if isinstance(self.uploadfield,Field):
> Line 7346:blob_uploadfield_name = self.uploadfield.uploadfield
> Line 7347:keys={self.uploadfield.name: newfilename,
> Line 7348:  blob_uploadfield_name: file.read()}
> Line 7349:self.uploadfield.table.insert(**keys)
>
>
> I am assuming that I am messing up my define_table. Or I am using the 
> wrong field when i call the store function. 
>
>
>
> On Thursday, July 5, 2012 7:36:39 PM UTC-7, Massimo Di Pierro wrote:
>>
>> I did not know you were storing it in a blob. Try:
>>
>>
>> def getImage(url,db): 
>> user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
>> headers = {'User-Agent': user_agent}
>> 
>> req = urllib2.Request(url,"",headers)
>> 
>> try:
>> response = urllib2.urlopen(req)
>> except urllib2.URLError, e:
>> if hasattr(e, 'reason'):
>> print 'We failed to reach a server.'
>> print 'Reason: ', e.reason
>> elif hasattr(e, 'code'):
>> print 'The server couldn\'t fulfill the request.'
>> print 'Error code: ', e.code
>> id = 
>> db.image.insert(url=url,image_uplaod=db.image.image_upload.store(response,filename=url))
>> db.commit() # not sure if needed, depends on where executed
>> return id
>>
>> def imageLookup():
>> id = request.vars.id
>> response.headers['Content-Type']='image/jpeg' 
>> return db.image[id].image_file
>>
>> On Thursday, 5 July 2012 14:16:55 UTC-5, RCTYCO wrote:
>>>
>>> Thank you for the quick response.
>>>
>>> I added the lines above : (  id = 
>>> db.image.insert(url=url,image=db.image.image_upload.store(response,filename=url))
>>> db.commit() )
>>>
>>> It uploaded the information (url and renamed the file) but I don't 
>>> believe it upload the image to the database.
>>>
>>> This is what it uploaded. Image_file is empty. 
>>> id,url,image_upload,image_file
>>> '1', 'http://eandata.com/image/products/000/000/015/000151122.jpg', 
>>> 'image.image_upload.94df8d0ebb1047e0.303030303030303135313132322e6a7067.jpg',
>>>  
>>> 
>>>
>>> This is my model.
>>> Model
>>> db.define_table('image',
>>> Field('url','string'),
>>> Field('image_upload','upload',uploadfield='image_file'),
>>> Field('image_file','blob'))
>>>
>>> Any suggestions?
>>>
>>

[web2py] Re: Saving an image from another database into your database

2012-07-05 Thread RCTYCO
Thanks.

I need to solve the problem of the file not been uploaded to the blob field.

I believe i am doing it correctly.

I defined the model 
db.define_table('image',
Field('url','string'),
Field('image_file','blob'),
Field('image_upload','upload',uploadfield='image_file'))


I stepped through the code for the store function 
(db.image.image_upload.store(response,filename=url))).

I expect line 7345 to turn out to be true to it eval to false. So my file 
is never uploaded to the blob file. 
File: gluon.dal

Line 7345:   if isinstance(self.uploadfield,Field):
Line 7346:blob_uploadfield_name = self.uploadfield.uploadfield
Line 7347:keys={self.uploadfield.name: newfilename,
Line 7348:  blob_uploadfield_name: file.read()}
Line 7349:self.uploadfield.table.insert(**keys)


I am assuming that I am messing up my define_table. Or I am using the wrong 
field when i call the store function. 



On Thursday, July 5, 2012 7:36:39 PM UTC-7, Massimo Di Pierro wrote:
>
> I did not know you were storing it in a blob. Try:
>
>
> def getImage(url,db): 
> user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
> headers = {'User-Agent': user_agent}
> 
> req = urllib2.Request(url,"",headers)
> 
> try:
> response = urllib2.urlopen(req)
> except urllib2.URLError, e:
> if hasattr(e, 'reason'):
> print 'We failed to reach a server.'
> print 'Reason: ', e.reason
> elif hasattr(e, 'code'):
> print 'The server couldn\'t fulfill the request.'
> print 'Error code: ', e.code
> id = 
> db.image.insert(url=url,image_uplaod=db.image.image_upload.store(response,filename=url))
> db.commit() # not sure if needed, depends on where executed
> return id
>
> def imageLookup():
> id = request.vars.id
> response.headers['Content-Type']='image/jpeg' 
> return db.image[id].image_file
>
> On Thursday, 5 July 2012 14:16:55 UTC-5, RCTYCO wrote:
>>
>> Thank you for the quick response.
>>
>> I added the lines above : (  id = 
>> db.image.insert(url=url,image=db.image.image_upload.store(response,filename=url))
>> db.commit() )
>>
>> It uploaded the information (url and renamed the file) but I don't 
>> believe it upload the image to the database.
>>
>> This is what it uploaded. Image_file is empty. 
>> id,url,image_upload,image_file
>> '1', 'http://eandata.com/image/products/000/000/015/000151122.jpg', 
>> 'image.image_upload.94df8d0ebb1047e0.303030303030303135313132322e6a7067.jpg',
>>  
>> 
>>
>> This is my model.
>> Model
>> db.define_table('image',
>> Field('url','string'),
>> Field('image_upload','upload',uploadfield='image_file'),
>> Field('image_file','blob'))
>>
>> Any suggestions?
>>
>

[web2py] Re: Saving an image from another database into your database

2012-07-05 Thread Massimo Di Pierro
I did not know you were storing it in a blob. Try:


def getImage(url,db): 
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
headers = {'User-Agent': user_agent}

req = urllib2.Request(url,"",headers)

try:
response = urllib2.urlopen(req)
except urllib2.URLError, e:
if hasattr(e, 'reason'):
print 'We failed to reach a server.'
print 'Reason: ', e.reason
elif hasattr(e, 'code'):
print 'The server couldn\'t fulfill the request.'
print 'Error code: ', e.code
id = 
db.image.insert(url=url,image_uplaod=db.image.image_upload.store(response,filename=url))
db.commit() # not sure if needed, depends on where executed
return id

def imageLookup():
id = request.vars.id
response.headers['Content-Type']='image/jpeg' 
return db.image[id].image_file

On Thursday, 5 July 2012 14:16:55 UTC-5, RCTYCO wrote:
>
> Thank you for the quick response.
>
> I added the lines above : (  id = 
> db.image.insert(url=url,image=db.image.image_upload.store(response,filename=url))
> db.commit() )
>
> It uploaded the information (url and renamed the file) but I don't believe 
> it upload the image to the database.
>
> This is what it uploaded. Image_file is empty. 
> id,url,image_upload,image_file
> '1', 'http://eandata.com/image/products/000/000/015/000151122.jpg', 
> 'image.image_upload.94df8d0ebb1047e0.303030303030303135313132322e6a7067.jpg', 
> 
>
> This is my model.
> Model
> db.define_table('image',
> Field('url','string'),
> Field('image_upload','upload',uploadfield='image_file'),
> Field('image_file','blob'))
>
> Any suggestions?
>


[web2py] Re: Saving an image from another database into your database

2012-07-05 Thread RCTYCO
Thank you for the quick response.

I added the lines above : (  id = 
db.image.insert(url=url,image=db.image.image_upload.store(response,filename=url))
db.commit() )

It uploaded the information (url and renamed the file) but I don't believe 
it upload the image to the database.

This is what it uploaded. Image_file is empty. 
id,url,image_upload,image_file
'1', 'http://eandata.com/image/products/000/000/015/000151122.jpg', 
'image.image_upload.94df8d0ebb1047e0.303030303030303135313132322e6a7067.jpg', 


This is my model.
Model
db.define_table('image',
Field('url','string'),
Field('image_upload','upload',uploadfield='image_file'),
Field('image_file','blob'))

Any suggestions?


[web2py] Re: Saving an image from another database into your database

2012-07-05 Thread Massimo Di Pierro
I would do:

def getImage(url,db): 
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
headers = {'User-Agent': user_agent}

req = urllib2.Request(url,"",headers)

try:
response = urllib2.urlopen(req)
except urllib2.URLError, e:
if hasattr(e, 'reason'):
print 'We failed to reach a server.'
print 'Reason: ', e.reason
elif hasattr(e, 'code'):
print 'The server couldn\'t fulfill the request.'
print 'Error code: ', e.code
else:
pass # everything is fine
print response.geturl()
print response.info()
# print response.read() # <<< do not read it
# f = open(response.read(),'rb') # << response is already a stream
# stream = open(response,'rb')
id = 
db.image.insert(url=url,image=db.image.image_upload.store(response,filename=url))
db.commit() # not sure if needed, depends on where executed
return id

then return it with the provided download action or

def imageLookup():
id = request.vars.id
#response.headers['Content-Type']='image/jpeg' 
path = os.path.join(request.folder,'uploads',db.image[id].image_file)
return response.stream(open(path,'rb'))



On Thursday, 5 July 2012 06:23:31 UTC-5, RCTYCO wrote:
>
>
> I am trying to save an image from another database into my 
> database, essentially i am caching the a copy into my database so I don't 
> have to query the external site. 
>
> I have a link 
> http://eandata.com/image/products/004/900/000/004900443.jpg. 
>
> I want to save the jpg into a blob in my database.
>
> Model
> db.define_table('image',
> Field('url','string'),
> Field('image_upload','upload',uploadfield='image_file'),
> Field('image_file','blob'))
>
> Function. I have this running a module and called when I need to download 
> the external image.
>
> def getImage(url,db): 
> 
> user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
> headers = {'User-Agent': user_agent}
> 
> req = urllib2.Request(url,"",headers)
> 
> try:
> response = urllib2.urlopen(req)
> except urllib2.URLError, e:
> if hasattr(e, 'reason'):
> print 'We failed to reach a server.'
> print 'Reason: ', e.reason
> elif hasattr(e, 'code'):
> print 'The server couldn\'t fulfill the request.'
> print 'Error code: ', e.code
> else:
> pass # everything is fine
> 
> print response.geturl()
> print response.info()
> print response.read()
> f = open(response.read(),'rb')
> #stream = open(response,'rb')
> 
> return 
> db.image.insert(url=url,image=db.image.image_upload.store(f,url))
>
>
> *I am having a problem with my insert statement. I can't seem to figure 
> out how to save the image into database.* urllib2.urlopen(req) will 
> download the file and provide a file object. But I don't know if i can just 
> save the file or do i need to read the file object within 
> the urllib2.urlopen(req).
>
> I would like the image to be available to be via 
> http://mydomain.com/myapp/imageLookup?id=2 
> 
>
> Controller
> def imageLookup():
> id = request.vars.id
> #response.headers['Content-Type']='image/jpeg' 
> 
> return db.image[id].image_file
>
>
> Do you have any suggestions? The code abo
>