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

Logo Icons too big fix (Issue: 1290) #1306

Merged
merged 4 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/apps/api/serializers/competitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ class CompetitionCreateSerializer(CompetitionSerializer):

class CompetitionDetailSerializer(serializers.ModelSerializer):
created_by = serializers.CharField(source='created_by.username', read_only=True)
logo_icon = NamedBase64ImageField(allow_null=True)
pages = PageSerializer(many=True)
phases = PhaseDetailSerializer(many=True)
leaderboards = serializers.SerializerMethodField()
Expand All @@ -347,6 +348,7 @@ class Meta:
'created_by',
'created_when',
'logo',
'logo_icon',
'terms',
'pages',
'phases',
Expand Down
19 changes: 19 additions & 0 deletions src/apps/competitions/migrations/0045_auto_20240129_2314.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 2.2.17 on 2024-01-29 23:14

from django.db import migrations, models
import utils.data


class Migration(migrations.Migration):

dependencies = [
('competitions', '0044_merge_20231221_1416'),
]

operations = [
migrations.AddField(
model_name='competition',
name='logo_icon',
field=models.ImageField(blank=True, null=True, upload_to=utils.data.PathWrapper('logos', manual_override=True)),
),
]
34 changes: 34 additions & 0 deletions src/apps/competitions/models.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import logging
import uuid
import os
import io

from django.conf import settings
from django.contrib.sites.models import Site
from django.contrib.postgres.fields import JSONField
from django.core.files.base import ContentFile
from django.db import models
from django.db.models import Q
from django.urls import reverse
Expand All @@ -15,6 +18,7 @@
from profiles.models import User, Organization
from utils.data import PathWrapper
from utils.storage import BundleStorage
from PIL import Image

from tasks.models import Task

Expand All @@ -32,6 +36,7 @@ class Competition(ChaHubSaveMixin, models.Model):

title = models.CharField(max_length=256)
logo = models.ImageField(upload_to=PathWrapper('logos'), null=True, blank=True)
logo_icon = models.ImageField(upload_to=PathWrapper('logos', manual_override=True), null=True, blank=True)
created_by = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True, on_delete=models.SET_NULL,
related_name="competitions")
created_when = models.DateTimeField(default=now)
Expand Down Expand Up @@ -214,8 +219,37 @@ def get_chahub_data(self):

return self.clean_private_data(data)

def make_logo_icon(self):
if self.logo:
# Read the content of the logo file
self.logo.name
self.logo_icon
icon_dirname_only = os.path.dirname(self.logo.name) # Get just the path
icon_basename_only = os.path.basename(self.logo.name) # Get just the filename
file_name = os.path.splitext(icon_basename_only)[0]
ext = os.path.splitext(icon_basename_only)[1]
new_path = os.path.join(icon_dirname_only, f"{file_name}_icon{ext}")
logo_content = self.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')
# Save the resized logo as logo_icon
self.logo_icon.save(new_path, ContentFile(icon_content.getvalue()), save=False)

def save(self, *args, **kwargs):
super().save(*args, **kwargs)
if not self.logo:
pass
elif not self.logo_icon:
self.make_logo_icon()
elif os.path.dirname(self.logo.name) != os.path.dirname(self.logo_icon.name):
self.make_logo_icon()
to_create = User.objects.filter(
Q(id=self.created_by_id) | Q(id__in=self.collaborators.all().values_list('id', flat=True))
).exclude(id__in=self.participants.values_list('user_id', flat=True)).distinct()
Expand Down
2 changes: 1 addition & 1 deletion src/static/riot/competitions/public-list.tag
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<div each="{competition in competitions.results}">
<div class="tile-wrapper">
<div class="ui square tiny bordered image img-wrapper">
<img src="{competition.logo}" loading="lazy">
<img src="{competition.logo_icon ? competition.logo_icon : competition.logo}" loading="lazy">
</div>
<a class="link-no-deco" href="../{competition.id}">
<div class="comp-info">
Expand Down
30 changes: 19 additions & 11 deletions src/utils/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,28 @@
class PathWrapper(object):
"""Helper to generate UUID's in file names while maintaining their extension"""

def __init__(self, base_directory):
def __init__(self, base_directory, manual_override=False):
self.path = base_directory
self.manual_override = manual_override

def __call__(self, instance, filename):
name, extension = os.path.splitext(filename)
truncated_uuid = uuid.uuid4().hex[0:12]
truncated_name = name[0:35]

return os.path.join(
self.path,
now().strftime('%Y-%m-%d-%s'),
truncated_uuid,
"{0}{1}".format(truncated_name, extension)
)
if not self.manual_override:
name, extension = os.path.splitext(filename)
truncated_uuid = uuid.uuid4().hex[0:12]
truncated_name = name[0:35]

path = os.path.join(
self.path,
now().strftime('%Y-%m-%d-%s'),
truncated_uuid,
"{0}{1}".format(truncated_name, extension)
)
else:
path = os.path.join(
filename
)

return path


def make_url_sassy(path, permission='r', duration=60 * 60 * 24, content_type='application/zip'):
Expand Down