Instantly share code, notes, and snippets.
Created
June 8, 2021 15:50
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save hazho/39cf028edd66044175a7f3fbf7d69f20 to your computer and use it in GitHub Desktop.
simple custom template_tags for Django/Wagtail
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django import template | |
from wagtail.images.models import Image | |
from django.utils.safestring import mark_safe | |
register = template.Library() | |
@register.filter(needs_autoescape=True) | |
def replace(value, arg, autoescape=True): | |
if arg == "[but]": result = value.replace(arg, "<button style='font-size:1.5rem; padding: .5rem; margin: .25rem; text-align: center; box-shadow: 0 0 .5rem blue;'>") | |
if arg == "[s.5]": result = value.replace(arg, "<span style='font-size: .5rem !important;'>") | |
if arg == "[s1]": result = value.replace(arg, "<span style='font-size:1rem !important;'>") | |
elif arg == "[s1.2]": result = value.replace(arg, "<span style='font-size:1.2rem !important;'>") | |
elif arg == "[s1.4]": result = value.replace(arg, "<span style='font-size:1.4rem !important;'>") | |
elif arg == "[s1.6]": result = value.replace(arg, "<span style='font-size:1.6rem !important;'>") | |
elif arg == "[s1.8]": result = value.replace(arg, "<span style='font-size:1.8rem !important;'>") | |
elif arg == "[s2]": result = value.replace(arg, "<span style='font-size:2rem !important;'>") | |
elif arg == "[s2.2]": result = value.replace(arg, "<span style='font-size:2.2rem !important;'>") | |
elif arg == "[s2.4]": result = value.replace(arg, "<span style='font-size:2.4rem !important;'>") | |
elif arg == "[s2.6]": result = value.replace(arg, "<span style='font-size:2.6rem !important;'>") | |
elif arg == "[s2.8]": result = value.replace(arg, "<span style='font-size:2.8rem !important;'>") | |
elif arg == "[s3]": result = value.replace(arg, "<span style='font-size:3rem !important;'>") | |
elif arg == "[s3.2]": result = value.replace(arg, "<span style='font-size:3.2rem !important;'>") | |
elif arg == "[s3.4]": result = value.replace(arg, "<span style='font-size:3.4rem !important;'>") | |
elif arg == "[s3.6]": result = value.replace(arg, "<span style='font-size:3.6rem !important;'>") | |
elif arg == "[s3.8]": result = value.replace(arg, "<span style='font-size:3.8rem !important;'>") | |
elif arg == "[s4]": result = value.replace(arg, "<span style='font-size:4rem !important;'>") | |
elif arg == "[s4.2]": result = value.replace(arg, "<span style='font-size:4.2rem !important;'>") | |
elif arg == "[s4.4]": result = value.replace(arg, "<span style='font-size:4.4rem !important;'>") | |
elif arg == "[s4.6]": result = value.replace(arg, "<span style='font-size:4.6rem !important;'>") | |
elif arg == "[s4.8]": result = value.replace(arg, "<span style='font-size:4.8rem !important;'>") | |
elif arg == "[s5]": result = value.replace(arg, "<span style='font-size:5rem !important;'>") | |
elif arg == "[s5.2]": result = value.replace(arg, "<span style='font-size:5.2rem !important;'>") | |
elif arg == "[s5.4]": result = value.replace(arg, "<span style='font-size:5.4rem !important;'>") | |
elif arg == "[s5.6]": result = value.replace(arg, "<span style='font-size:5.6rem !important;'>") | |
elif arg == "[s5.8]": result = value.replace(arg, "<span style='font-size:5.8rem !important;'>") | |
elif arg == "[s6]": result = value.replace(arg, "<span style='font-size:6rem !important;'>") | |
elif arg == "[s6.2]": result = value.replace(arg, "<span style='font-size:6.2rem !important;'>") | |
elif arg == "[s6.4]": result = value.replace(arg, "<span style='font-size:6.4rem !important;'>") | |
elif arg == "[s6.6]": result = value.replace(arg, "<span style='font-size:6.6rem !important;'>") | |
elif arg == "[s6.8]": result = value.replace(arg, "<span style='font-size:6.8rem !important;'>") | |
elif arg == "[s7]": result = value.replace(arg, "<span style='font-size:7rem !important;'>") | |
elif arg == "[r]": result = value.replace(arg, "<span style='color: red !important;'>") | |
elif arg == "[dr]": result = value.replace(arg, "<span style='color: darkred !important;'>") | |
elif arg == "[g]": result = value.replace(arg, "<span style='color: green !important;'>") | |
elif arg == "[dg]": result = value.replace(arg, "<span style='color: darkgreen !important;'>") | |
elif arg == "[b]": result = value.replace(arg, "<span style='color: blue !important;'>") | |
elif arg == "[db]": result = value.replace(arg, "<span style='color: darkblue !important;'>") | |
elif arg == "[p]": result = value.replace(arg, "<span style='color: purple !important;'>") | |
elif arg == "[dp]": result = value.replace(arg, "<span style='color: darkpurple !important;'>") | |
elif arg == "[y]": result = value.replace(arg, "<span style='color: yellow !important;'>") | |
elif arg == "[dy]": result = value.replace(arg, "<span style='color: darkyellow !important;'>") | |
elif arg == "[]": result = value.replace(arg, "<span style='color: var(--main_color) !important;'>") | |
elif arg == "[sp]": result = value.replace(arg, " ") | |
elif arg == "[nl]": result = value.replace(arg, "<br>") | |
return mark_safe(result) | |
@register.filter() | |
def to_string(value): | |
return str(value) | |
@register.filter() | |
def ends_with(value, args): | |
return value.endswith(args) | |
@register.filter() | |
def has_group(user, group_name=None, ends_with=None, starts_with=None, has=None): | |
if not ends_with and not starts_with and not has: | |
# return user.groups.filter(name=group_name).exists() | |
return group_name in [ gn[0] for gn in user.groups.values_list('name')] | |
elif not ends_with and not starts_with: | |
if has in group_name: | |
grp_name = group_name | |
return user.groups.filter(name=grp_name).exists() | |
elif not ends_with and not has: | |
grp_name = group_name.startswith(starts_with) | |
return user.groups.filter(name=grp_name).exists() | |
elif not has and not starts_with: | |
grp_name = group_name.endswith(ends_with) | |
return user.groups.filter(name=grp_name).exists() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment