Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to retrieve images from media server via Jinja template #1088

Open
yokwejuste opened this issue Jan 25, 2024 · 2 comments
Open

Unable to retrieve images from media server via Jinja template #1088

yokwejuste opened this issue Jan 25, 2024 · 2 comments
Assignees
Labels
api backend issue backend bug Something isn't working

Comments

@yokwejuste
Copy link
Collaborator

yokwejuste commented Jan 25, 2024

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 yokwejuste added bug Something isn't working api backend issue backend labels Jan 25, 2024
@NdibeRaymond
Copy link
Collaborator

NdibeRaymond commented Jan 26, 2024

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.

@yokwejuste
Copy link
Collaborator Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api backend issue backend bug Something isn't working
Projects
Status: Todo
Development

No branches or pull requests

2 participants