Add: custom template filters
This commit is contained in:
parent
b27e53f287
commit
4f8d459001
1 changed files with 47 additions and 0 deletions
47
archivist/sites/furaffinity/templatetags/custom_filters.py
Normal file
47
archivist/sites/furaffinity/templatetags/custom_filters.py
Normal 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
|
Loading…
Reference in a new issue