favicon base
This commit is contained in:
parent
d8b46f76ed
commit
9c5d78ac2c
|
@ -15,7 +15,7 @@ import os
|
|||
|
||||
# default django
|
||||
from pathlib import Path
|
||||
from django.conf.global_settings import MEDIA_ROOT
|
||||
from django.conf.global_settings import FILE_UPLOAD_TEMP_DIR, MEDIA_ROOT
|
||||
from django.utils.log import ServerFormatter
|
||||
|
||||
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
||||
|
@ -296,3 +296,4 @@ LOGGING = {
|
|||
#MEDIA_ROOT = "/home/plex/Documents/code/python/gawa/media"
|
||||
MEDIA_ROOT = "media/"
|
||||
MEDIA_URL = "media/"
|
||||
FILE_UPLOAD_TEMP_DIR = "/tmp/gawa/upload"
|
||||
|
|
|
@ -41,6 +41,6 @@ class LinkAdmin(admin.ModelAdmin):
|
|||
"""
|
||||
Admin Interface for Links
|
||||
"""
|
||||
list_display = ["title_en", "title_de", "url", "suburl"]
|
||||
list_display = ["title_en", "title_de", "url", "suburl", "favicon"]
|
||||
ordering = ['title_de', 'title_en']
|
||||
actions = [regenerate]
|
||||
|
|
|
@ -1,11 +1,17 @@
|
|||
from enum import unique
|
||||
from django.db import models
|
||||
from django.db.models.options import override
|
||||
from django.utils.translation import gettext as _
|
||||
from django.core.files import File
|
||||
|
||||
from django.conf import settings
|
||||
|
||||
import logging
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
import requests
|
||||
import favicon
|
||||
import random
|
||||
|
||||
class Keyword(models.Model):
|
||||
"""
|
||||
this is the model that should contain searchable keywords
|
||||
|
@ -103,6 +109,24 @@ class Link(Searchable):
|
|||
"""
|
||||
logger.info(f"regenerating {self.__class__.__name__} object: {self}")
|
||||
self.suburl = f"/links#{self.title_en}"
|
||||
logger.info(f"getting favicon for link object {self}: '{self.url}'")
|
||||
icons = favicon.get(self.url, timeout=2)
|
||||
if icons[0] is None:
|
||||
# just keep whatever was stored if we cant get a new favicon
|
||||
return
|
||||
|
||||
response = requests.get(icons[0].url, stream=True)
|
||||
filename: str = f"favicon-{random.randint(0x1000, 0xffff)}.{icons[0].format}"
|
||||
logger.debug(response)
|
||||
logger.debug(filename)
|
||||
with open(f"{settings.FILE_UPLOAD_TEMP_DIR}/{filename}", 'wb') as image:
|
||||
for chunk in response.iter_content(1024):
|
||||
image.write(chunk)
|
||||
logger.debug(image)
|
||||
with open(f"{settings.FILE_UPLOAD_TEMP_DIR}/{filename}", 'rb') as image:
|
||||
self.favicon.save(filename, File(image))
|
||||
|
||||
#self.favicon.save(f"favicon-{self.title_en}", favicon.get(self.url, timeout=2))
|
||||
self.save()
|
||||
|
||||
class Meta:
|
||||
|
|
|
@ -1,3 +0,0 @@
|
|||
Django>=3.0,<4.0
|
||||
psycopg2>=2.8
|
||||
mysqlclient>=1.4.3
|
|
@ -5,3 +5,4 @@ django_compressor>=2.2
|
|||
django-libsass>=0.7
|
||||
pillow>=9.0.0
|
||||
colorlog>=6.7.0
|
||||
favicon>=0.7.0
|
||||
|
|
Loading…
Reference in New Issue