db.py:
db.define_table('Images',
Field('Name',length=512),
Field('Image','upload'),
Field('thumb','upload',writable=False,readable=False),
format = '%(Name)s' # <<< important
)
db.define_table('Article',
Field('Title',length=512),
Field('Content','text'),
Field('Submitted','datetime',default=datetime.datetime.now()),
Field('Views','integer',default=0),
Field('TopImage',db.Images)
)
default.py:
def download():
return response.download(request, db)
def makeThumbnail(dbtable,ImageID,size=(200,200)):
try:
thisImage=db(dbtable.id==ImageID).select()[0]
import os, uuid
except:
print "Error while loading libraries"
return
try:
from PIL import Image
except:
print "Error while Importing PIL library"
return
print request.folder + 'uploads/' + thisImage.Image
im=Image.open(request.folder + 'uploads/' + thisImage.Image)
im.thumbnail(size,Image.ANTIALIAS)
thumbName='uploads.thumb.%s.jpg' % (uuid.uuid4())
im.save(request.folder + 'uploads/' + thumbName,'jpeg')
thisImage.update_record(thumb=thumbName)
response.flash = 'Thumb created everything went fine'
return
def newImage():
dbtable = db.Images #uploads table name
if len(request.args):
records = db(dbtable.id==request.args[0]).select()
if len(request.args) and len(records):
form = SQLFORM(dbtable, records[0], deletable=True)
else:
form = SQLFORM(dbtable)
if form.accepts(request.vars, session):
response.flash = 'Entry for Images Database accepted,start creating thumb'
makeThumbnail(dbtable,form.vars.id,(200,200))
elif form.errors:
response.flash = 'Error in Form for Images Database'
## Quick list just to demonstrate...
list = crud.select(dbtable)
return dict(form=form,list=list)
def Article():
id=request.vars.id
row=db(db.Article.id==id).select()
row[0].update_record(Views=row[0].Views+1)
if len(row)==0:
redirect(URL(r=request,f='Articles'))
return dict(Article=row[0])
article.html
{{extend 'layout.html'}}
<h1> {{=Article.Title}} </h1>
<br>
<center>
{{print URL(r=request, c='default', f='download',args=Article.TopImage.thumb)}}
{{=IMG(_src=URL(r=request, c='default', f='download',args=Article.TopImage.thumb),_style="display:block;")}}
</center>
<br>
{{=XML(Article.Content)}}<br><br>
def Article():
id=request.vars.id
row=db(db.Article.id==id).select()
Not sure how you get the Images filenames. The code does not seem join the Article to the Image?
Maybe something like:
def Article():
id=request.vars.id # <<-- only ok for testing
row=db((db.Article.id==id)&(db.Article.TopImage==db.Images.id)).select()
Field('TopImage',db.Images)
id=request.vars.id #<<-- only ok for testingdef Article():id=request.vars.id
row=db(db.Article.id==id).select()
Not sure how you get the Images filenames. The code does not seem join the Article to the Image?
thumbName='uploads.thumb.%s.jpg' % (uuid.uuid4())
im.save(request.folder + 'uploads/' + thumbName,'jpeg')
thisImage.update_record(thumb=db.Images.thumb.store(im, filename='thumbnail.jpg'))thisImage.update_record(thumb=db.Images.thumb.store(im, filename='thumbnail.jpg'))the link directs to this group Anthony.
I didn't try:this would store the thumb directly in the database?thisImage.update_record(thumb=db.Images.thumb.store(im, filename='thumbnail.jpg'))
I tried your solution and it just leaves the thumb field empty and no thumbnail is created in the upload folder.
thisImage.update_record(thumb=db.Images.thumb.store(im, filename='thumbnail.jpg'))and how did you get this Database view I really like that^^
I was referring to paolos post
your solution gives me just the error message I don't understand Anthony. I posted the error message before but it got somehow deleted.
On Friday, September 7, 2012 5:38:15 PM UTC+2, Anthony wrote:
Traceback (most recent call last):
File "F:\Website\web2py\gluon\restricted.py", line 209, in restricted
exec ccode in environment
File "F:/Website/web2py/applications/testthumb/controllers/default.py", line 128, in <module>
File "F:\Website\web2py\gluon\globals.py", line 185, in <lambda>
self._caller = lambda f: f()
File "F:/Website/web2py/applications/testthumb/controllers/default.py", line 121, in newImage
makeThumbnail(dbtable,form.vars.id,(200,200))
File "F:/Website/web2py/applications/testthumb/controllers/default.py", line 106, in makeThumbnail
thisImage.update_record(thumb=db.Images.thumb.store(im, filename='thumbnail.jpg'))
File "F:\Website\web2py\gluon\dal.py", line 8442, in store
shutil.copyfileobj(file, dest_file)
File "C:\Python27\lib\shutil.py", line 48, in copyfileobj
buf = fsrc.read(length)
File "C:\Python27\lib\site-packages\PIL\Image.py", line 512, in __getattr__
raise AttributeError(name)
AttributeError: read
that was the error message
from cStringIO import StringIO
tmp = StringIO()
im.save(tmp, 'jpeg')
tmp.seek(0)
thisImage.update_record(thumb=db.Images.thumb.store(tmp, filename='thumbnail.jpg'))