Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
Django Favicon Plus Reloaded
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
AlekSIS®
Libraries
Django Favicon Plus Reloaded
Commits
a3bc5119
Verified
Commit
a3bc5119
authored
3 years ago
by
Nik | Klampfradler
Browse files
Options
Downloads
Patches
Plain Diff
Use bulk queries to find icons
parent
cdcfc7bb
No related branches found
No related tags found
1 merge request
!1
Resolve "Optimise query count"
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
favicon/models.py
+39
-13
39 additions, 13 deletions
favicon/models.py
favicon/templatetags/favtags.py
+2
-4
2 additions, 4 deletions
favicon/templatetags/favtags.py
with
41 additions
and
17 deletions
favicon/models.py
+
39
−
13
View file @
a3bc5119
...
...
@@ -6,7 +6,7 @@ from django.conf import settings
from
django.core.files.storage
import
default_storage
as
storage
from
django.core.files.uploadedfile
import
InMemoryUploadedFile
from
django.db
import
models
from
django.db.models
import
signals
from
django.db.models
import
Q
,
signals
use_sites
=
hasattr
(
settings
,
"
SITE_ID
"
)
...
...
@@ -53,12 +53,35 @@ class Favicon(models.Model):
verbose_name
=
'
Favicon
'
verbose_name_plural
=
'
Favicons
'
def
get_favicons
(
self
):
favicons
=
[]
def
get_favicons
(
self
,
update
=
False
):
# Get all combinations of favicon rels and sizes from config
query
=
Q
()
for
rel
in
config
:
for
size
in
config
[
rel
]:
favicons
.
append
(
self
.
get_favicon
(
size
,
rel
))
return
favicons
query
|=
Q
(
rel
=
rel
,
size
=
size
)
# Get all existing favicons
favicons
=
FaviconImg
.
objects
.
filter
(
faviconFK
=
self
).
filter
(
query
)
# Delete all favicon images to update all
if
update
:
favicons
.
delete
()
favicons
=
FaviconImg
.
objects
.
empty
()
else
:
favicons
=
favicons
.
all
()
# Check whether favicons are missing
new_favicons
=
[]
for
rel
in
config
:
for
size
in
config
[
rel
]:
if
not
favicons
.
filter
(
rel
=
rel
,
size
=
size
):
fav
=
FaviconImg
(
faviconFK
=
self
,
size
=
size
,
rel
=
rel
)
fav
.
generate_image
()
new_favicons
.
append
(
fav
)
if
new_favicons
:
FaviconImg
.
objects
.
bulk_create
(
new_favicons
)
return
list
(
favicons
)
+
new_favicons
def
__str__
(
self
):
return
self
.
faviconImage
.
name
...
...
@@ -80,14 +103,7 @@ class Favicon(models.Model):
if
update
and
fav
.
faviconImage
:
fav
.
del_image
()
if
self
.
faviconImage
and
not
fav
.
faviconImage
:
tmp
=
Image
.
open
(
storage
.
open
(
self
.
faviconImage
.
name
))
tmp
.
thumbnail
((
size
,
size
),
Image
.
ANTIALIAS
)
tmp_io
=
BytesIO
()
tmp
.
save
(
tmp_io
,
format
=
'
PNG
'
)
tmp_file
=
InMemoryUploadedFile
(
tmp_io
,
None
,
f
"
fav-
{
size
}
s.png
"
,
'
image/png
'
,
sys
.
getsizeof
(
tmp_io
),
None
)
fav
.
faviconImage
=
tmp_file
fav
.
generate_image
()
fav
.
save
()
return
fav
...
...
@@ -116,6 +132,16 @@ class FaviconImg(models.Model):
rel
=
models
.
CharField
(
max_length
=
250
,
null
=
True
)
faviconImage
=
models
.
ImageField
(
upload_to
=
image_path
)
def
generate_image
(
self
):
tmp
=
Image
.
open
(
storage
.
open
(
self
.
faviconFK
.
faviconImage
.
name
))
tmp
.
thumbnail
((
self
.
size
,
self
.
size
),
Image
.
ANTIALIAS
)
tmp_io
=
BytesIO
()
tmp
.
save
(
tmp_io
,
format
=
'
PNG
'
)
tmp_file
=
InMemoryUploadedFile
(
tmp_io
,
None
,
f
"
fav-
{
self
.
size
}
s.png
"
,
'
image/png
'
,
sys
.
getsizeof
(
tmp_io
),
None
)
self
.
faviconImage
=
tmp_file
def
del_image
(
self
):
self
.
faviconImage
.
delete
()
...
...
This diff is collapsed.
Click to expand it.
favicon/templatetags/favtags.py
+
2
−
4
View file @
a3bc5119
...
...
@@ -20,10 +20,8 @@ def place_favicon(context):
if
not
fav
:
return
mark_safe
(
'
<!-- no favicon -->
'
)
html
=
''
for
rel
in
config
:
for
size
in
sorted
(
config
[
rel
],
reverse
=
True
):
n
=
fav
.
get_favicon
(
size
=
size
,
rel
=
rel
)
html
+=
f
'
<link rel=
"
{
n
.
rel
}
"
sizes=
"
{
n
.
size
}
x
{
n
.
size
}
"
href=
"
{
n
.
faviconImage
.
url
}
"
/>
'
for
n
in
fav
.
get_favicons
():
html
+=
f
'
<link rel=
"
{
n
.
rel
}
"
sizes=
"
{
n
.
size
}
x
{
n
.
size
}
"
href=
"
{
n
.
faviconImage
.
url
}
"
/>
'
default_fav
=
fav
.
get_favicon
(
size
=
32
,
rel
=
'
shortcut icon
'
)
html
+=
f
'
<link rel=
"
{
default_fav
.
rel
}
"
sizes=
"
{
default_fav
.
size
}
x
{
default_fav
.
size
}
"
\
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment