0

I have written a series of functions get_user_image_filename() gets the filename from the google bucket, then it is passed to the next function view_user_image() to get the image and process it. I then call this function from view_messages() which should display messages from google datastore and their corresponding image from google bucket.

Currently the view_messages() correctly pulls messages from datastore. The issue I am having is that every message is getting assigned the same profile image.

def get_user_image_filename(user_id):
    client = storage.Client()
    bucket_name = 'my_bucket_name'
    prefix = 'user-images/user_' + user_id + '/'
    blobs = list(client.list_blobs(bucket_name, prefix=prefix))

    if len(blobs) == 1:
        # If there is only one blob, return the file name
        filename = blobs[0].name.split('/')[-1]
        return filename

def view_user_image(user_id):
    client = storage.Client()
    bucket_name = 'my_bucket_name'
    filename = get_user_image_filename(user_id)
    blob_name = f'user-images/user_default/user.jpg' if not filename else f'user-images/user_{user_id}/{filename}'
    bucket = client.bucket(bucket_name)
    blob = bucket.blob(blob_name)

    image_data = blob.download_as_bytes()
    if not image_data:
        return None

    with Image.open(BytesIO(image_data)) as img:
        img.thumbnail((120, 120), Image.ANTIALIAS)
        buffered = BytesIO()
        img.save(buffered, format="JPEG")
        image_data = buffered.getvalue()

    return image_data

def view_messages():

            query = datastore_client.query(kind="message")
            query.order = ["-post_date_time"]
            messages = list(query.fetch(limit=10))
            user_images = {}
            message_data = {}
            for message in messages:
                message_id = message.key.id_or_name
                message_user_id = message['user_id']
                print(message_user_id)
                message_profile_image = view_user_image(message_user_id)
                print(message_profile_image)
                user_images[message_user_id] = message_profile_image
                print(user_images)
                message_data[message_id] = message

            return render_template('messages.html', messages=messages, message_profile_image=message_profile_image)
            

This part i think is successful as it prints the correct user_id for each message.

message_user_id = message['user_id']
print(message_user_id)

However this part only ever prints the data for the first message received from datastore and then every message post shows this image instead of showing the image based on their user_id.

user_images[message_user_id] = message_profile_image
print(user_images)

Any help with this would be hugely appreciated.

  • From your code, I'd expect it to the last profile image that is shown for all messages. Your `render_template` call doesn't pass in `user_images`, and only passes in message_profile_image . – Jim Morrison Mar 23 '23 at 22:40

0 Answers0