Browse Source

Switch from django-favicon-plus to django-super-favicon, it performs better and doesn't require a DB table

tags/v1.9.6
Danielle McLean 1 year ago
parent
commit
1654ceecf3
Signed by: Danielle McLean <dani@00dani.me> GPG Key ID: 8EB789DDF3ABD240

+ 1
- 1
Pipfile View File

@@ -13,7 +13,6 @@ gunicorn = "*"
13 13
 "psycopg2" = "*"
14 14
 pillow = "*"
15 15
 python-memcached = "*"
16
-django-favicon-plus = "*"
17 16
 django-meta = "*"
18 17
 django-redis-cache = "*"
19 18
 django-activeurl = "*"
@@ -44,5 +43,6 @@ pytest-django = "*"
44 43
 python-baseconv = "*"
45 44
 django-computed-property = "*"
46 45
 docutils = "*"
46
+django-super-favicon = "*"
47 47
 
48 48
 [dev-packages]

+ 8
- 14
Pipfile.lock View File

@@ -1,7 +1,7 @@
1 1
 {
2 2
     "_meta": {
3 3
         "hash": {
4
-            "sha256": "4df43e91dcd5baca07940a89c9f23c321de025dd3454739f4266fcbe4490da2d"
4
+            "sha256": "ff6bfe755c0aaf4328e61d282a464f6bc86731fdd077f04aad1f0ebabacee6d6"
5 5
         },
6 6
         "pipfile-spec": 6,
7 7
         "requires": {
@@ -188,12 +188,6 @@
188 188
             ],
189 189
             "version": "==0.8.0"
190 190
         },
191
-        "django-compat": {
192
-            "hashes": [
193
-                "sha256:3ac9a3bedc56b9365d9eb241bc5157d0c193769bf995f9a78dc1bc24e7c2331b"
194
-            ],
195
-            "version": "==1.0.15"
196
-        },
197 191
         "django-compressor": {
198 192
             "hashes": [
199 193
                 "sha256:7732676cfb9d58498dfb522b036f75f3f253f72ea1345ac036434fdc418c2e57",
@@ -225,13 +219,6 @@
225 219
             "index": "pypi",
226 220
             "version": "==1.9.1"
227 221
         },
228
-        "django-favicon-plus": {
229
-            "hashes": [
230
-                "sha256:3394a951d8dc611eb1ea027ad1181d7f650ca234506585b27e93d7ed06b981bf"
231
-            ],
232
-            "index": "pypi",
233
-            "version": "==0.0.8"
234
-        },
235 222
         "django-meta": {
236 223
             "hashes": [
237 224
                 "sha256:21fc5d0d5fcacda5d038af0babd08afaa4d5bed1b746edb6522c4d3435da8db6",
@@ -287,6 +274,13 @@
287 274
             "index": "pypi",
288 275
             "version": "==1.1.0"
289 276
         },
277
+        "django-super-favicon": {
278
+            "hashes": [
279
+                "sha256:56cb5268ea73ef3cbde5cb01fef02fea2ec00739cdae0566d3102009f052f683"
280
+            ],
281
+            "index": "pypi",
282
+            "version": "==0.6.1"
283
+        },
290 284
         "docutils": {
291 285
             "hashes": [
292 286
                 "sha256:02aec4bd92ab067f6ff27a38a38a41173bf01bed8f89157768c1573f53e474a6",

+ 3
- 0
lemoncurry/settings/base.py View File

@@ -263,3 +263,6 @@ PUSH_HUB = 'https://00dani.superfeedr.com/'
263 263
 # django-rq
264 264
 # https://github.com/ui/django-rq
265 265
 RQ_QUEUES = {'default': {'USE_REDIS_CACHE': 'default'}}
266
+
267
+# django-super-favicon
268
+FAVICON_STORAGE = 'django.core.files.storage.DefaultStorage'

+ 2
- 2
lemoncurry/templates/lemoncurry/layout.html View File

@@ -1,4 +1,4 @@
1
-{% load analytical compress favtags lemoncurry_tags meta static theme_colour %}<!doctype html>
1
+{% load analytical compress favicon lemoncurry_tags meta static theme_colour %}<!doctype html>
2 2
 <html dir="ltr" lang="en" class="{% block html_class %}{% endblock %}">
3 3
 	<head{% meta_namespaces %}>{% site_name as site_name %}{% request_uri request as uri %}{% request_origin request as origin %}
4 4
 		<meta charset="utf-8" />
@@ -28,7 +28,7 @@
28 28
 		<meta property="og:url" content="{{ uri }}" />
29 29
 		<meta property="og:title" content="{% firstof title site_name %}" />
30 30
 		{% include 'meta/meta.html' %}
31
-		{% placeFavicon %}
31
+		{% get_favicons 'favicon/' %}
32 32
 
33 33
 		<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css"
34 34
 					integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous" />

+ 45
- 0
wellknowns/favicons.py View File

@@ -0,0 +1,45 @@
1
+from itertools import chain
2
+from django.core.files.storage import default_storage
3
+
4
+
5
+class Favicon:
6
+    def __init__(self, size, rel='icon', mime='image/png'):
7
+        self.rel = rel
8
+        self.mime = mime
9
+        if not isinstance(size, tuple):
10
+            size = (size, size)
11
+        self.size = size
12
+
13
+    @property
14
+    def url(self):
15
+        return default_storage.url('favicon/' + self.filename)
16
+
17
+    @property
18
+    def filename(self):
19
+        return 'favicon-{0}.png'.format(*self.size)
20
+
21
+    @property
22
+    def sizes(self):
23
+        return 'x'.join(str(s) for s in self.size)
24
+
25
+
26
+tile_sizes = {'small': 128, 'medium': 270, 'wide': (558, 270), 'large': 558}
27
+
28
+
29
+class Tile(Favicon):
30
+    def __init__(self, size_name):
31
+        super().__init__(tile_sizes[size_name])
32
+        self.size_name = size_name
33
+
34
+    @property
35
+    def filename(self):
36
+        return '{0}tile.png'.format(self.size_name)
37
+
38
+
39
+sizes = (32, 57, 76, 96, 120, 128, 144, 180, 195, 228)
40
+icons = tuple(chain(
41
+    (Favicon(s) for s in sizes),
42
+    (Tile(s) for s in tile_sizes.keys()),
43
+    (Favicon(152, rel='apple-touch-icon-precomposed'),
44
+     Favicon(196, rel='shortcut icon'))
45
+))

+ 15
- 11
wellknowns/views/manifest.py View File

@@ -1,12 +1,16 @@
1 1
 from django.http import JsonResponse
2 2
 from django.urls import reverse
3
-from favicon.models import FaviconImg
3
+from ..favicons import icons
4 4
 from lemoncurry import utils
5 5
 from lemoncurry.theme import color
6 6
 from urllib.parse import urljoin
7 7
 from textwrap import shorten
8 8
 
9 9
 
10
+def manifest_icons(base):
11
+    return [{'src': i.url, 'type': i.mime, 'sizes': i.sizes} for i in sorted(icons, key=lambda i: i.size)]
12
+
13
+
10 14
 def manifest(request):
11 15
     base = utils.origin(request)
12 16
     start_url = reverse('home:index') + '?utm_source=homescreen'
@@ -14,6 +18,7 @@ def manifest(request):
14 18
     app = {
15 19
         'name': request.site.name,
16 20
         'short_name': shorten(request.site.name, width=20, placeholder=''),
21
+        'icons': manifest_icons(base),
17 22
 
18 23
         'display': 'browser',
19 24
         'start_url': urljoin(base, start_url),
@@ -22,15 +27,14 @@ def manifest(request):
22 27
         'theme_color': color(2),
23 28
     }
24 29
 
25
-    rels = ('shortcut icon', 'apple-touch-icon')
26
-    icons = FaviconImg.objects.filter(
27
-        faviconFK__isFavicon=True,
28
-        rel__in=rels,
29
-    ).order_by('size')
30
-    app['icons'] = [{
31
-        'type': 'image/png',
32
-        'sizes': '{0}x{0}'.format(icon.size),
33
-        'src': urljoin(base, icon.faviconImage.url),
34
-    } for icon in icons]
30
+    # icons = FaviconImg.objects.filter(
31
+    #     faviconFK__isFavicon=True,
32
+    #     rel__in=rels,
33
+    # ).order_by('size')
34
+    # app['icons'] = [{
35
+    #     'type': 'image/png',
36
+    #     'sizes': '{0}x{0}'.format(icon.size),
37
+    #     'src': urljoin(base, icon.faviconImage.url),
38
+    # } for icon in icons]
35 39
 
36 40
     return JsonResponse(app, content_type='application/manifest+json')

Loading…
Cancel
Save