zubhub
zubhub copied to clipboard
Unable to retrieve images from media server via Jinja template
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
To Reproduce
- Import the ActivityImage Model
- Build a view logic that relies on a Jinja template.
- Setup the context to be used in the template
- Try calling an image from the media server into the template
- 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.
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.
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