[FIXED] Saving the same image issue

This is a bit of a weird issue. Might be a bug, but I could be messing something up. I have a form that displays fields from a data table row in an alert popup. Everything is working as it should except the image box.

Uploading a new image works fine, but when saving the form without updating the image the image in the data table somehow gets messed up. Here is a gif showing that the uploaded image is working fine.

Here is a gif showing that saving the data without updating the image kills the image. To be clear, it is still in the data table, but it is 0 bytes in size.

The image is displayed on the form via a server call. Server code:

@anvil.server.callable
def get_coupon_info(coupon_id):
  coupon_info = app_tables.coupons.get(id=coupon_id)
  return coupon_info

Client code:

coupon_details = anvil.server.call('get_coupon_info', coupon_id)
# many other elements being assigned up here
self.image.source = coupon_details['image']
# other elements being assigned down here

When the save button is clicked the following functions calls the server (I know the positional arguments are insane):

    if anvil.alert(ec, buttons=[("Save", True, "primary"), ("Back", False)],large=True):
      response = anvil.server.call('update_coupon', 
                                   self.item['id'], 
                                   ec.title.text,
                                   ec.short_description.text,
                                   ec.long_description.text,
                                   ec.category.selected_value,
                                   ec.location.text,
                                   ec.original_price.text,
                                   ec.price.text,
                                   ec.restriction.text,
                                   ec.image.source,
                                   ec.start_date.date,
                                   ec.end_date.date,
                                   ec.phone.text,
                                   ec.address.text,
                                   ec.special_instructions.text,
                                   ec.coordinates.text
                                  )
      self.raise_event("x-close-alert")
      n = Notification(response)
      n.show()

Then on the server the data is being saved with this function. For troubleshooting I removed any other error handling.

@anvil.server.callable
@anvil.tables.in_transaction
def update_coupon(coupon_id, title, short_description, long_description, category, location, original_price, price, restriction, image, start_date, end_date, phone, address, special_instructions, coordinates):
  coupon = app_tables.coupons.get(id = int(coupon_id))
  coupon['title'] = title
  coupon['short_description'] = short_description
  coupon['long_description'] = long_description
  coupon['category'] = category
  coupon['location'] = location
  coupon['original_price'] = original_price
  coupon['price'] = price
  coupon['restriction'] = restriction
  coupon['image'] = image
  coupon['start'] = start_date
  coupon['end'] = end_date
  coupon['phone'] = phone
  coupon['address'] = address
  coupon['special_instructions'] = special_instructions
  coupon['coordinates'] = coordinates
    # write success response
  response = 'Coupon Updated Successfully'
    
  return response

Thanks in advance!

1 Like

I have not gone through all of this carefully, but in case it helps, recently I experienced something similar and was able to get around it.

Much of the media in one of my tables was suddenly coming up as having zero bytes. I realized upon closer inspection that it had to do with me updating the media column with a “lazy media object” type. Such objects appear to have some kind of temporary existence. Once I explicitly converted the media to a BlobMedia object before updating, everything updated fine.

I’m not sure if this information is relevant in your case

1 Like

That seemed to do the trick! Here is my updated client code:

    ec = EditCoupon(coupon_id=self.item['id'])
    if anvil.alert(ec, buttons=[("Save", True, "primary"), ("Back", False)],large=True):
      image_source = anvil.BlobMedia(ec.image.source.content_type, ec.image.source.get_bytes(), name=ec.image.source.name)
      response = anvil.server.call('update_coupon', 
                                   self.item['id'], 
                                   #lots of updates here
                                   image_source,
                                   #more updates here
                                  )
      self.raise_event("x-close-alert")
      n = Notification(response)
      n.show()

Everything else is the same. I used the relative MIME type and filename, which are attributes of image.source object. Thanks!

2 Likes

Ohhhh, that’s a bug. When we save a Media object into a row+column that already has something in it, of course the first thing we do is remove the existing contents to write the new contents in. If you try to overwrite something with itself…splat.

Moved to Bug Reports.

1 Like

Aand fixed. Can you try it yourself and verify that this now works?

1 Like

Can confirm that this is now fixed!

1 Like