Add: custom template filters

This commit is contained in:
Aroy-Art 2023-09-27 15:11:06 +02:00
parent b27e53f287
commit 4f8d459001
Signed by: Aroy
GPG key ID: 583642324A1D2070

View file

@ -0,0 +1,47 @@
# custom_filters.py
from django import template
import nh3
from bs4 import BeautifulSoup
register = template.Library()
@register.filter
def is_image(file_url):
image_extensions = ['.png', '.jpg', '.jpeg', '.gif', '.webp']
return file_url.lower().endswith(tuple(image_extensions))
@register.filter
def is_flash(file_url):
flash_extensions = ['.swf']
return file_url.lower().endswith(tuple(flash_extensions))
@register.filter
def is_pdf(file_url):
pdf_extensions = ['.pdf']
return file_url.lower().endswith(tuple(pdf_extensions))
@register.filter
def descriptionHtml2Text(description):
if description.startswith("<div"):
cleanHtml = nh3.clean(description)
soup = BeautifulSoup(cleanHtml, "html.parser")
descriptionText = soup.get_text()
else:
descriptionText = description
return descriptionText
@register.filter
def stylizeDescription(description):
if description.startswith("<div"):
stylizedDescription = nh3.clean(description)
else:
stylizedDescription = description
return stylizedDescription