8000 Fix/collection thumb by zoek1 · Pull Request #7975 · gitcoinco/web · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix/collection thumb #7975

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

Merged
merged 1 commit into from
Dec 1, 2020
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
16 changes: 12 additions & 4 deletions app/grants/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1613,7 +1613,7 @@ def update_tx_status(self):
# Tx hash should start with `sync-tx:` and have a 64 character hash (no 0x prefix)
raise Exception('Unsupported zkSync transaction hash format')
tx_hash = self.split_tx_id.replace('sync-tx:', '0x') # replace `sync-tx:` prefix with `0x`

# Get transaction data with zkSync's API: https://zksync.io/api/v0.1.html#transaction-details
base_url = 'https://rinkeby-api.zksync.io/api/v0.1' if network == 'rinkeby' else 'https://api.zksync.io/api/v0.1'
r = requests.get(f"{base_url}/transactions_all/{tx_hash}")
Expand Down Expand Up @@ -1932,7 +1932,7 @@ def generate_cache(self):
'count': grants.count(),
'grants': [{
'id': grant.id,
'logo': grant.logo.url if grant.logo and grant.logo.url else f'v2/images/grants/logos/{self.id % 3}.png',
'logo': grant.logo.url if grant.logo and grant.logo.url else f'v2/images/grants/logos/{grant.id % 3}.png',
} for grant in grants]
}

Expand All @@ -1950,7 +1950,7 @@ def generate_cache(self):
self.cache = cache
self.save()

def to_json_dict(self):
def to_json_dict(self, build_absolute_uri):
curators = [{
'url': curator.url,
'handle': curator.handle,
Expand All @@ -1962,14 +1962,22 @@ def to_json_dict(self):
'handle': self.profile.handle,
'avatar_url': self.profile.avatar_url
}

grants = self.cache.get('grants', 0)

if grants:
grants = [{
**grant,
'logo': build_absolute_uri(static(grant['logo'])) if 'v2/images' in grant['logo'] else grant['logo']
} for grant in grants]
return {
'id': self.id,
'owner': owner,
'title': self.title,
'description': self.description,
'cover': self.cover.url if self.cover else '',
'count': self.cache.get('count', 0),
'grants': self.cache.get('grants', 0),
'grants': grants,
'curators': curators + [owner]
}

Expand Down
34 changes: 25 additions & 9 deletions app/grants/utils.py
< 8000 td id="diff-521f1ba1ba5b2f65871f8e704d5f7996a0db04d794d5ae404d27007014704dabR223" data-line-number="223" class="blob-num blob-num-context js-linkable-line-number js-blob-rnum">
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from secrets import token_hex

from app import settings
from app.settings import BASE_DIR
from avatar.utils import convert_img
from economy.utils import ConversionRateNotFoundError, convert_amount
from gas.utils import eth_usd_conv_rate
Expand Down Expand Up @@ -193,8 +194,13 @@ def generate_collection_thumbnail(collection, width, heigth):
if grant.logo:
if len(logos) > DISPLAY_GRANTS_LIMIT:
break
logos.append(grant.logo.open())
else:
logo = open(f'{BASE_DIR}/assets/v2/images/grants/logos/{grant.id % 3}.png', 'rb')
logos.append(logo)

logos.append(grant.logo)
for logo in range(len(logos), 4):
logos.append(None)

thumbail = Image.new('RGBA', IMAGE_BOX, color=BG)
avatar_url = f'{settings.BASE_URL[:-1]}{collection.profile.avatar_url}'
Expand All @@ -205,7 +211,7 @@ def generate_collection_thumbnail(collection, width, heigth):
draw = ImageDraw.Draw(mask)
draw.ellipse((0, 0) + PROFILE_BOX, fill=0)
profile_thumbnail = Image.open(fd)
profile_thumbnail.thumbnail(PROFILE_BOX, Image.ANTIALIAS)
# profile_thumbnail.thumbnail(PROFILE_BOX, Image.ANTIALIAS)
profile_circle = ImageOps.fit(profile_thumbnail, mask.size, centering=(0.5, 0.5))


Expand All @@ -216,22 +222,32 @@ def generate_collection_thumbnail(collection, width, heigth):
[width - GRANT_WIDTH - MARGIN, heigth - GRANT_HEIGHT - MARGIN] # bottom right grant
]

for index in range(len(logos)):
for index in range(4):
if logos[index] is None:
grant_bg = Image.new('RGBA', GRANT_BOX, color='white')
thumbail.paste(grant_bg, CORNERS[index], grant_bg)
continue

if re.match(r'.*\.svg', logos[index].name):
grant_img = convert_img(logos[index].open())
grant_img = convert_img(logos[index])
grant_thumbail = Image.open(grant_img)
else:
grant_thumbail = Image.open(logos[index].open())
try:
grant_thumbail = Image.open(logos[index])
except ValueError:
grant_thumbail = Image.open(logos[index]).convert("RGBA")

grant_thumbail.thumbnail(GRANT_BOX, Image.ANTIALIAS)

grant_bg = Image.new('RGBA', GRANT_BOX, color='white')
if grant_thumbail.mode in ('RGBA'):

try:
grant_bg.paste(grant_thumbail, (int(GRANT_WIDTH / 2 - grant_thumbail.size[0] / 2),
int(GRANT_HEIGHT / 2 - grant_thumbail.size[1] / 2)), grant_thumbail)
else:
except ValueError:
grant_bg.paste(grant_thumbail, (int(GRANT_WIDTH / 2 - grant_thumbail.size[0] / 2),
int(GRANT_HEIGHT / 2 - grant_thumbail.size[1] / 2)))

thumbail.paste(grant_bg, CORNERS[index], grant_bg)

draw_on_thumbnail = ImageDraw.Draw(thumbail)
Expand All @@ -240,10 +256,10 @@ def generate_collection_thumbnail(collection, width, heigth):
(int(width / 2 + PROFILE_WIDTH / 2), int(heigth / 2 + PROFILE_HEIGHT / 2))
], fill="#0D013B")

if profile_circle.mode in ('P', 'RGBA'):
try:
thumbail.paste(profile_circle, (int(width / 2 - PROFILE_WIDTH / 2) + HALF_LOGO_SIZE_DIFF, int(heigth / 2 - PROFILE_HEIGHT / 2) + HALF_LOGO_SIZE_DIFF),
profile_circle)
else:
except ValueError:
thumbail.paste(profile_circle, (int(width / 2 - PROFILE_WIDTH / 2) + HALF_LOGO_SIZE_DIFF, int(heigth / 2 - PROFILE_HEIGHT / 2) + HALF_LOGO_SIZE_DIFF))

return thumbail
Expand Down
2 changes: 1 addition & 1 deletion app/grants/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ def get_grants(request):
'current_type': grant_type,
'category': category,
'grants': grants_array,
'collections': [collection.to_json_dict() for collection in collections],
'collections': [collection.to_json_dict(request.build_absolute_uri) for collection in collections],
'credentials': {
'is_staff': request.user.is_staff,
'is_authenticated': request.user.is_authenticated
Expand Down
0