forked from 00dani/lemoncurry
Switch from django-favicon-plus to django-super-favicon, it performs better and doesn't require a DB table
This commit is contained in:
parent
025910029e
commit
1654ceecf3
6 changed files with 74 additions and 28 deletions
45
wellknowns/favicons.py
Normal file
45
wellknowns/favicons.py
Normal file
|
@ -0,0 +1,45 @@
|
|||
from itertools import chain
|
||||
from django.core.files.storage import default_storage
|
||||
|
||||
|
||||
class Favicon:
|
||||
def __init__(self, size, rel='icon', mime='image/png'):
|
||||
self.rel = rel
|
||||
self.mime = mime
|
||||
if not isinstance(size, tuple):
|
||||
size = (size, size)
|
||||
self.size = size
|
||||
|
||||
@property
|
||||
def url(self):
|
||||
return default_storage.url('favicon/' + self.filename)
|
||||
|
||||
@property
|
||||
def filename(self):
|
||||
return 'favicon-{0}.png'.format(*self.size)
|
||||
|
||||
@property
|
||||
def sizes(self):
|
||||
return 'x'.join(str(s) for s in self.size)
|
||||
|
||||
|
||||
tile_sizes = {'small': 128, 'medium': 270, 'wide': (558, 270), 'large': 558}
|
||||
|
||||
|
||||
class Tile(Favicon):
|
||||
def __init__(self, size_name):
|
||||
super().__init__(tile_sizes[size_name])
|
||||
self.size_name = size_name
|
||||
|
||||
@property
|
||||
def filename(self):
|
||||
return '{0}tile.png'.format(self.size_name)
|
||||
|
||||
|
||||
sizes = (32, 57, 76, 96, 120, 128, 144, 180, 195, 228)
|
||||
icons = tuple(chain(
|
||||
(Favicon(s) for s in sizes),
|
||||
(Tile(s) for s in tile_sizes.keys()),
|
||||
(Favicon(152, rel='apple-touch-icon-precomposed'),
|
||||
Favicon(196, rel='shortcut icon'))
|
||||
))
|
|
@ -1,12 +1,16 @@
|
|||
from django.http import JsonResponse
|
||||
from django.urls import reverse
|
||||
from favicon.models import FaviconImg
|
||||
from ..favicons import icons
|
||||
from lemoncurry import utils
|
||||
from lemoncurry.theme import color
|
||||
from urllib.parse import urljoin
|
||||
from textwrap import shorten
|
||||
|
||||
|
||||
def manifest_icons(base):
|
||||
return [{'src': i.url, 'type': i.mime, 'sizes': i.sizes} for i in sorted(icons, key=lambda i: i.size)]
|
||||
|
||||
|
||||
def manifest(request):
|
||||
base = utils.origin(request)
|
||||
start_url = reverse('home:index') + '?utm_source=homescreen'
|
||||
|
@ -14,6 +18,7 @@ def manifest(request):
|
|||
app = {
|
||||
'name': request.site.name,
|
||||
'short_name': shorten(request.site.name, width=20, placeholder=''),
|
||||
'icons': manifest_icons(base),
|
||||
|
||||
'display': 'browser',
|
||||
'start_url': urljoin(base, start_url),
|
||||
|
@ -22,15 +27,14 @@ def manifest(request):
|
|||
'theme_color': color(2),
|
||||
}
|
||||
|
||||
rels = ('shortcut icon', 'apple-touch-icon')
|
||||
icons = FaviconImg.objects.filter(
|
||||
faviconFK__isFavicon=True,
|
||||
rel__in=rels,
|
||||
).order_by('size')
|
||||
app['icons'] = [{
|
||||
'type': 'image/png',
|
||||
'sizes': '{0}x{0}'.format(icon.size),
|
||||
'src': urljoin(base, icon.faviconImage.url),
|
||||
} for icon in icons]
|
||||
# icons = FaviconImg.objects.filter(
|
||||
# faviconFK__isFavicon=True,
|
||||
# rel__in=rels,
|
||||
# ).order_by('size')
|
||||
# app['icons'] = [{
|
||||
# 'type': 'image/png',
|
||||
# 'sizes': '{0}x{0}'.format(icon.size),
|
||||
# 'src': urljoin(base, icon.faviconImage.url),
|
||||
# } for icon in icons]
|
||||
|
||||
return JsonResponse(app, content_type='application/manifest+json')
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue