favicon base

This commit is contained in:
Christoph J. Scherr 2023-06-05 20:28:56 +02:00
parent d8b46f76ed
commit 9c5d78ac2c
Signed by: PlexSheep
GPG Key ID: 25B4ACF7D88186CC
5 changed files with 29 additions and 6 deletions

View File

@ -15,7 +15,7 @@ import os
# default django # default django
from pathlib import Path 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 from django.utils.log import ServerFormatter
# Build paths inside the project like this: BASE_DIR / 'subdir'. # 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 = "/home/plex/Documents/code/python/gawa/media"
MEDIA_ROOT = "media/" MEDIA_ROOT = "media/"
MEDIA_URL = "media/" MEDIA_URL = "media/"
FILE_UPLOAD_TEMP_DIR = "/tmp/gawa/upload"

View File

@ -41,6 +41,6 @@ class LinkAdmin(admin.ModelAdmin):
""" """
Admin Interface for Links 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'] ordering = ['title_de', 'title_en']
actions = [regenerate] actions = [regenerate]

View File

@ -1,11 +1,17 @@
from enum import unique
from django.db import models from django.db import models
from django.db.models.options import override from django.db.models.options import override
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
from django.core.files import File
from django.conf import settings
import logging import logging
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
import requests
import favicon
import random
class Keyword(models.Model): class Keyword(models.Model):
""" """
this is the model that should contain searchable keywords 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}") logger.info(f"regenerating {self.__class__.__name__} object: {self}")
self.suburl = f"/links#{self.title_en}" 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() self.save()
class Meta: class Meta:

View File

@ -1,3 +0,0 @@
Django>=3.0,<4.0
psycopg2>=2.8
mysqlclient>=1.4.3

View File

@ -5,3 +5,4 @@ django_compressor>=2.2
django-libsass>=0.7 django-libsass>=0.7
pillow>=9.0.0 pillow>=9.0.0
colorlog>=6.7.0 colorlog>=6.7.0
favicon>=0.7.0