-
Notifications
You must be signed in to change notification settings - Fork 27
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
Logo Icons too big fix (Issue: 1290) #1306
Conversation
@bbearce Some questions:
|
Benjamin's instructions: Let's walk through doing this methodically to be safeShell into djangodocker compose exec django bash
python manage.py shell_plus --plain Get competitions that don't have logo icons import io, os
from PIL import Image
from django.core.files.base import ContentFile
comps_no_icon_logo = Competition.objects.filter(logo_icon__isnull=True)
all = Competition.objects.all()
len(Competition.objects.all())
len(comps_no_icon_logo) 1 at a time to gain confidence
test = comps_no_icon_logo[0]
print(test.logo) # Ex: >>> logos/2024-01-29-1706501898/c0a9208bca26/logo.jpg
print(test.logo_icon) # Ex: >>> logos/2024-01-29-1706501898/c0a9208bca26/logo.jpg
icon_dirname_only = os.path.dirname(test.logo.name) # Get just the path
icon_basename_only = os.path.basename(test.logo.name) # Get filename plus ext
file_name = os.path.splitext(icon_basename_only)[0] # Get just the filename
ext = os.path.splitext(icon_basename_only)[1] # Get ext
new_path = os.path.join(icon_dirname_only, f"{file_name}_icon{ext}") # Craft new path
logo_content = test.logo.read()
original_logo = Image.open(io.BytesIO(logo_content))
# Resize the image to a smaller size for logo_icon
width, height = original_logo.size
new_width = 100 # Specify the desired width for the logo_icon
new_height = int((new_width / width) * height)
resized_logo = original_logo.resize((new_width, new_height))
# Create a BytesIO object to save the resized image
icon_content = io.BytesIO()
resized_logo.save(icon_content, format='PNG')
test.logo_icon.save(new_path, ContentFile(icon_content.getvalue()),save=False)
test.save() There should be a
test2 = comps_no_icon_logo[1] Loop over the restimport pdb
for comp in comps_no_icon_logo:
pdb.set_trace(); # use 'c' to cycle through 1 at a time
comp.make_logo_icon()
comp.save() Notes:src/utils/data.py has a |
Note for me: for comp in comps_no_icon_logo:
comp.make_logo_icon()
comp.save() |
@bbearce After deploying this on codabench-test server, I have an issue. EDIT: IT'S ONLY WHEN NOT LOGGED IN I ran the script to create a "logo icon" for all competitions. Now when I load the competition list, I get this error: Some logs: codabench-django-1 | Internal Server Error: /api/competitions/public/
codabench-django-1 | Traceback (most recent call last):
codabench-django-1 | File "/usr/local/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
codabench-django-1 | response = get_response(request)
codabench-django-1 | File "/usr/local/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response
codabench-django-1 | response = self.process_exception_by_middleware(e, request)
codabench-django-1 | File "/usr/local/lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response
codabench-django-1 | response = wrapped_callback(request, *callback_args, **callback_kwargs)
codabench-django-1 | File "/usr/local/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
codabench-django-1 | return view_func(*args, **kwargs)
codabench-django-1 | File "/usr/local/lib/python3.8/site-packages/rest_framework/viewsets.py", line 116, in view
codabench-django-1 | return self.dispatch(request, *args, **kwargs)
codabench-django-1 | File "/usr/local/lib/python3.8/site-packages/rest_framework/views.py", line 495, in dispatch
codabench-django-1 | response = self.handle_exception(exc)
codabench-django-1 | File "/usr/local/lib/python3.8/site-packages/rest_framework/views.py", line 455, in handle_exception
codabench-django-1 | self.raise_uncaught_exception(exc)
codabench-django-1 | File "/usr/local/lib/python3.8/site-packages/rest_framework/views.py", line 492, in dispatch
codabench-django-1 | response = handler(request, *args, **kwargs)
codabench-django-1 | File "/usr/local/lib/python3.8/site-packages/rest_framework_extensions/cache/decorators.py", line 44, in inner
codabench-django-1 | return this.process_cache_response(
codabench-django-1 | File "/usr/local/lib/python3.8/site-packages/rest_framework_extensions/cache/decorators.py", line 68, in process_cache_response
codabench-django-1 | response = view_method(view_instance, request, *args, **kwargs)
codabench-django-1 | File "/app/src/apps/api/views/competitions.py", line 547, in public
codabench-django-1 | return self.get_paginated_response(serializer.data)
codabench-django-1 | File "/usr/local/lib/python3.8/site-packages/rest_framework/serializers.py", line 768, in data
codabench-django-1 | ret = super(ListSerializer, self).data
codabench-django-1 | File "/usr/local/lib/python3.8/site-packages/rest_framework/serializers.py", line 262, in data
codabench-django-1 | self._data = self.to_representation(self.instance)
codabench-django-1 | File "/usr/local/lib/python3.8/site-packages/rest_framework/serializers.py", line 685, in to_representation
codabench-django-1 | return [
codabench-django-1 | File "/usr/local/lib/python3.8/site-packages/rest_framework/serializers.py", line 686, in <listcomp>
codabench-django-1 | self.child.to_representation(item) for item in iterable
codabench-django-1 | File "/usr/local/lib/python3.8/site-packages/rest_framework/serializers.py", line 530, in to_representation
codabench-django-1 | ret[field.field_name] = field.to_representation(attribute)
codabench-django-1 | File "/usr/local/lib/python3.8/site-packages/rest_framework/serializers.py", line 530, in to_representation
codabench-django-1 | ret[field.field_name] = field.to_representation(attribute)
codabench-django-1 | File "/usr/local/lib/python3.8/site-packages/rest_framework/fields.py", line 1893, in to_representation
codabench-django-1 | return method(value)
codabench-django-1 | File "/app/src/apps/api/serializers/queues.py", line 105, in get_competitions
codabench-django-1 | competitions = obj.competitions.filter(
codabench-django-1 | File "/usr/local/lib/python3.8/site-packages/django/db/models/manager.py", line 82, in manager_method
codabench-django-1 | return getattr(self.get_queryset(), name)(*args, **kwargs)
codabench-django-1 | File "/usr/local/lib/python3.8/site-packages/django/db/models/query.py", line 892, in filter
codabench-django-1 | return self._filter_or_exclude(False, *args, **kwargs)
codabench-django-1 | File "/usr/local/lib/python3.8/site-packages/django/db/models/query.py", line 910, in _filter_or_exclude
codabench-django-1 | clone.query.add_q(Q(*args, **kwargs))
codabench-django-1 | File "/usr/local/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1290, in add_q
codabench-django-1 | clause, _ = self._add_q(q_object, self.used_aliases)
codabench-django-1 | File "/usr/local/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1310, in _add_q
codabench-django-1 | child_clause, needed_inner = self._add_q(
codabench-django-1 | File "/usr/local/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1315, in _add_q
codabench-django-1 | child_clause, needed_inner = self.build_filter(
codabench-django-1 | File "/usr/local/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1251, in build_filter
codabench-django-1 | condition = self.build_lookup(lookups, col, value)
codabench-django-1 | File "/usr/local/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1116, in build_lookup
codabench-django-1 | lookup = lookup_class(lhs, rhs)
codabench-django-1 | File "/usr/local/lib/python3.8/site-packages/django/db/models/lookups.py", line 20, in __init__
codabench-django-1 | self.rhs = self.get_prep_lookup()
codabench-django-1 | File "/usr/local/lib/python3.8/site-packages/django/db/models/fields/related_lookups.py", line 115, in get_prep_lookup
codabench-django-1 | self.rhs = target_field.get_prep_value(self.rhs)
codabench-django-1 | File "/usr/local/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 972, in get_prep_value
codabench-django-1 | return int(value)
codabench-django-1 | File "/usr/local/lib/python3.8/site-packages/django/contrib/auth/models.py", line 388, in __int__
codabench-django-1 | raise TypeError('Cannot cast AnonymousUser to int. Are you trying to use it in place of User?')
codabench-django-1 | TypeError: Cannot cast AnonymousUser to int. Are you trying to use it in place of User? |
TypeError: Cannot cast AnonymousUser to int. Are you trying to use it in place of User? This error is raised because somewhere in your code (likely in a query or a model method that involves user filtering), there's an assumption that the user is a registered user with a valid ID. However, since the user is not logged in, Django substitutes a default AnonymousUser object, which does not have an integer ID that can be used in database queries. |
A brief description of the purpose of the changes contained in this PR.
Logos on public challenge page are taking too long to load. In this fix I propose we change:
Issues this PR resolves
A checklist for hand testing
Any relevant files for testing
add_icon_sized_logo.md
Checklist