zubhub icon indicating copy to clipboard operation
zubhub copied to clipboard

Unable to retrieve images from media server via Jinja template

Open yokwejuste opened this issue 1 year ago • 2 comments

Description

Retrieving images URL through a jinja template from a view context, involves calling the file URL of the saved image. Anytime we try to get the images from the media server of Zubhub on local development we get a Connection Error.

Here is the code:

# The view code

    def get(self, request, *args, **kwargs):
        activity = self.get_object()
        activity_images = ActivityImage.objects.filter(activity=activity)
        activity_steps = ActivityMakingStep.objects.filter(activity=activity)
        if settings.ENVIRONMENT == 'production':
            qr_code = generate_qr_code(
                link=f"https://zubhub.unstructured.studio/activities/{activity.id}"
            )
        else:
            qr_code = generate_qr_code(
                link=f"{settings.DEFAULT_BACKEND_PROTOCOL}//{settings.DEFAULT_BACKEND_DOMAIN}/activities/{activity.id}"
            )
        context = {
            'activity': activity,
            'activity_id': activity.id,
            'activity_images': activity_images,
            'activity_steps': activity_steps,
            'activity_steps_images': [step.image.all() for step in activity_steps],
            'activity_category': [category.name for category in activity.category.all()],
            'creators': [creator for creator in activity.creators.all()],
            'qr_code': qr_code
        }
        return generate_pdf(
            template_path=self.template_path,
            context=context
        )


# The util function
def download_file(file_url):
    """
    Download a file from a given URL and save it to the local filesystem.

    Args:
        file_url (str): The URL of the file to download.

    Returns:
        bytes: The file data.
    """
    response = requests.get(file_url, stream=True)
    response.raise_for_status()
    file_data = b""
    for chunk in response.iter_content(chunk_size=4096):
        if chunk:
            file_data += chunk
    return file_data

The Behavior

image image image image image image image

To Reproduce

  1. Import the ActivityImage Model
  2. Build a view logic that relies on a Jinja template.
  3. Setup the context to be used in the template
  4. Try calling an image from the media server into the template
  5. The Connection Error

Expected behavior

Under normal circumstances we were to get the image from the media server

Notes

  • This behavior does not happen when we call images from the media server in production.

yokwejuste avatar Jan 25 '24 15:01 yokwejuste

Hello @yokwejuste , your description of the issue is a bit vague. Can you give a more detailed explanation and issue reproduction steps? if possible make a video? It'd be helpful to know EXACTLY what you are trying to do.

NdibeRaymond avatar Jan 26 '24 06:01 NdibeRaymond

Hello @yokwejuste , your description of the issue is a bit vague. Can you give a more detailed explanation and issue reproduction steps? if possible make a video? It'd be helpful to know EXACTLY what you are trying to do.

I added more details onto that issue

yokwejuste avatar Jan 26 '24 16:01 yokwejuste