In a new record a thumbnail is correctly generated from photo. But if I didn't upload a photo or want to change the photo the thumbnail doesn't generate. So basically a new record works properly but when updating nothing in the thumbnail changes at all whether or not is currently has a photo in it.
____________________________________________________ model: from image import THUMBER db.define_table('park', Field('photo1', 'upload'), Field('photo_thumb1', 'upload', readable=False, writable=False)) db.park.photo_thumb1.compute = lambda row: THUMBER(row.photo1, "photo_thumb1", 144, 115) ____________________________________________________ image.py: from gluon import current def THUMBER(image, db_photo_var, nx=120, ny=120, name='thumb'): if image: try: request = current.request from PIL import Image import os img = Image.open(request.folder + 'uploads/' + image) img.db_photo_var((nx, ny), Image.ANTIALIAS) root, ext = os.path.splitext(image) thumb = '%s_%s%s' % (root, name, ext) img.save(request.folder + 'uploads/' + thumb) return thumb except Exception: return image On Monday, June 18, 2012 1:17:07 PM UTC-6, Massimo Di Pierro wrote: > > Let me understand this better. > > on insert, the thumbnails are created. > on update, if you upload a new photo1 is photo_thing1 generated? > > Can you provide a simpler code to reproduce the problem with a just one > upload field and one thumbnail field? > > massimo > > > > > On Monday, 18 June 2012 11:24:05 UTC-5, Brandon Reynolds wrote: >> >> I have this problem when i try to generate thumbnails. If the field is >> empty it inserts the photo thumb into the thumbnail. But when i try to >> update that record the thumbnail doesn't change. >> >> Here is my model: >> >> # coding: utf8 >> from image import THUMBER >> >> db.define_table('park', >> Field('park_name', requires=IS_NOT_EMPTY()), >> Field('park_city', requires=IS_NOT_EMPTY()), >> Field('park_state', requires=IS_NOT_EMPTY()), >> Field('park_address', requires=IS_NOT_EMPTY()), >> Field('park_zip', requires=IS_NOT_EMPTY()), >> Field('country', default="USA", notnull=True, readable=False, >> writable=False), >> Field('park_phone', requires=IS_MATCH('[\d\-\(\) ]+')), >> Field('park_fax', requires=IS_EMPTY_OR(IS_MATCH('[\d\-\(\) ]+'))), >> Field('park_phone_2', 'string', requires=IS_EMPTY_OR(IS_MATCH('[\d\-\(\) >> ]+'))), >> Field('photo1', 'upload'), >> Field('photo_thumb1', 'upload', readable=False, writable=False), >> Field('photo2', 'upload'), >> Field('photo_thumb2', 'upload', readable=False, writable=False), >> Field('photo3', 'upload'), >> Field('photo_thumb3', 'upload', readable=False, writable=False), >> Field('photo4', 'upload'), >> Field('photo_thumb4', 'upload', readable=False, writable=False), >> Field('photo5', 'upload'), >> Field('photo_thumb5', 'upload', readable=False, writable=False), >> Field('manager', requires=IS_NOT_EMPTY()), >> Field('manager_email', requires=IS_EMAIL()), >> Field('spaces', 'integer', requires=IS_NOT_EMPTY()), >> Field('vacant', 'integer'), >> Field('lot_rent', 'integer', requires=IS_NOT_EMPTY()), >> Field('water', 'boolean'), >> Field('sewer', 'boolean'), >> Field('trash', 'boolean'), >> Field('pool', 'boolean'), >> Field('playground', 'boolean'), >> Field('clubhouse', 'boolean'), >> Field('laundromat', 'boolean'), >> Field('rv_spaces', 'boolean'), >> Field('storage', 'boolean'), >> Field('handicap_accessible', 'boolean'), >> Field('community_description', 'text'), >> format='%(park_name)s') >> >> db.define_table('home', >> Field('pid', notnull=True, readable=False, writable=False), >> Field('lot'), >> Field('year', length=4, requires=IS_NOT_EMPTY()), >> Field('make'), >> Field('model'), >> Field('width', requires=IS_NOT_EMPTY()), >> Field('length', requires=IS_NOT_EMPTY()), >> Field('wide', requires=IS_NOT_EMPTY()), >> Field('for_sale', 'boolean', default=True), >> Field('beds', requires=IS_NOT_EMPTY()), >> Field('baths', requires=IS_NOT_EMPTY()), >> Field('fridge', 'boolean'), >> Field('stove', 'boolean'), >> Field('dishwasher', 'boolean'), >> Field('microwave', 'boolean'), >> Field('washer', 'boolean'), >> Field('dryer', 'boolean'), >> Field('photo1', 'upload'), >> Field('photo1_text'), >> Field('photo_thumb1', 'upload', readable=False, writable=False), >> Field('photo2', 'upload'), >> Field('photo2_text'), >> Field('photo_thumb2', 'upload', readable=False, writable=False), >> Field('photo3', 'upload'), >> Field('photo3_text'), >> Field('photo_thumb3', 'upload', readable=False, writable=False), >> Field('photo4', 'upload'), >> Field('photo4_text'), >> Field('photo_thumb4', 'upload', readable=False, writable=False), >> Field('photo5', 'upload'), >> Field('photo5_text'), >> Field('photo_thumb5', 'upload', readable=False, writable=False), >> Field('price',requires=IS_NOT_EMPTY()), >> Field('description', 'text', requires=IS_NOT_EMPTY()), >> Field('posted_on', 'datetime', readable=False, writable=False)) >> >> db.define_table('state', >> Field('name'), >> Field('full_name')) >> >> db.define_table('wide', >> Field('type'), >> format='%(type)s') >> >> >> db.park.park_state.requires = IS_IN_DB(db, 'state.name', '%(full_name)s >> (%(name)s)', zero=T('Select State')) >> db.home.wide.requires = IS_IN_DB(db, 'wide.type', '%(type)s', >> zero=T('Select Home Type')) >> >> db.park.photo_thumb1.compute = lambda row: THUMBER(row.photo1, >> "photo_thumb1", 144, 115) >> db.park.photo_thumb2.compute = lambda row: THUMBER(row.photo2, >> "photo_thumb2", 144, 115) >> db.park.photo_thumb3.compute = lambda row: THUMBER(row.photo3, >> "photo_thumb3", 144, 115) >> db.park.photo_thumb4.compute = lambda row: THUMBER(row.photo4, >> "photo_thumb4", 144, 115) >> db.park.photo_thumb5.compute = lambda row: THUMBER(row.photo5, >> "photo_thumb5", 144, 115) >> db.home.photo_thumb1.compute = lambda row: THUMBER(row.photo1, >> "photo_thumb1", 144, 115) >> db.home.photo_thumb2.compute = lambda row: THUMBER(row.photo2, >> "photo_thumb2", 144, 115) >> db.home.photo_thumb3.compute = lambda row: THUMBER(row.photo3, >> "photo_thumb3", 144, 115) >> db.home.photo_thumb4.compute = lambda row: THUMBER(row.photo4, >> "photo_thumb4", 144, 115) >> db.home.photo_thumb5.compute = lambda row: THUMBER(row.photo5, >> "photo_thumb5", 144, 115) >> >> >> Here is my controller: >> >> def index(): >> return locals() >> >> def parks(): >> if request.args(0): >> parks = >> db(db.park.park_state==request.args(0)).select(orderby=db.park.park_city|db.park.park_name) >> else: >> parks = >> db(db.park).select(orderby=db.park.park_state|db.park.park_city|db.park.park_name) >> return locals() >> >> def park(): >> park = db.park(request.args(0)) or redirect(URL('parks')) >> homes = db(db.home.pid==(request.args(0))).select(orderby=db.home.id) >> return locals() >> >> def home(): >> home = db.home(request.args(0)) or redirect(URL('parks')) >> park = db.park(home.pid) >> return locals() >> >> def home_feed(): >> home = db(db.home).select(orderby=db.home.id) >> return locals() >> >> @auth.requires_membership('Admin') >> def new_park(): >> form = crud.create(db.park, next='park/[id]') >> return locals() >> >> @auth.requires_membership('Admin') >> def new_home(): >> db.home.pid.default = request.args(0) or redirect(URL('parks')) >> db.home.price.default = "$0000.00" >> db.home.posted_on.default = request.now >> form = crud.create(db.home, next='home/[id]') >> return locals() >> >> @auth.requires_membership('Admin') >> def edit_park(): >> park = db.park(request.args(0)) or redirect(URL('parks')) >> form = crud.update(db.park, park, next='park/[id]') >> return locals() >> >> >> @auth.requires_membership('Admin') >> def edit_home(): >> home = db.home(request.args(0)) or redirect(URL('parks')) >> form = crud.update(db.home, home, next='home/[id]') >> return locals() >> >> def about_us(): >> return locals() >> >> def employment_opportunities(): >> return locals() >> >> def park_management(): >> return locals() >> >> def make_park_thumbs(): >> from image import THUMBER >> parks=db(db.park).select(orderby=db.park.park_state) >> for park in parks: >> park.update_record(photo_thumb1 = THUMBER(park.photo1, "photo_thumb1", >> 144, 115)) >> park.update_record(photo_thumb2 = THUMBER(park.photo2, "photo_thumb2", >> 144, 115)) >> park.update_record(photo_thumb3 = THUMBER(park.photo3, "photo_thumb3", >> 144, 115)) >> park.update_record(photo_thumb4 = THUMBER(park.photo4, "photo_thumb4", >> 144, 115)) >> park.update_record(photo_thumb5 = THUMBER(park.photo5, "photo_thumb5", >> 144, 115)) >> db.commit() >> >> >> Here is the THUMBER function >> from gluon import current >> >> def THUMBER(image, db_photo_var, nx=120, ny=120, name='thumb'): >> if image: >> try: >> request = current.request >> from PIL import Image >> import os >> img = Image.open(request.folder + 'uploads/' + image) >> img.db_photo_var((nx, ny), Image.ANTIALIAS) >> root, ext = os.path.splitext(image) >> thumb = '%s_%s%s' % (root, name, ext) >> img.save(request.folder + 'uploads/' + thumb) >> return thumb >> except Exception: >> return image >> >> >> What should i do to get these working on update? At the bottom of the >> controller you will notice a the make_park_thumbs() function. That was one >> way of try to fix this and having it set to run on updates. However won't >> generate a thumbnail it just returns the original photo. This would work if >> you can tell why that function won't work. Let me know what to do. >> >> Braandon >> >