Commit 014a29d3 authored by Jens Langhammer's avatar Jens Langhammer

pyazo: cleanup template tags

parent 0e99f974
{% extends "_admin/base.html" %}
{% load i18n %}
{% load setting %}
{% load pyazo %}
{% block content %}
<div class="row">
......
{% load fieldtype %}
{% load pyazo %}
{% csrf_token %}
{% for field in form %}
......
{% load fieldtype %}
{% load pyazo %}
{% csrf_token %}
{% for field in form %}
......
......@@ -2,8 +2,6 @@
{% load i18n %}
{% load static %}
{% load is_active %}
{% load is_active_app %}
{% load gravatar %}
{% load pyazo %}
......
{% extends "core/base.html" %}
{% load i18n %}
{% load back %}
{% load pyazo %}
{% block title %}
{{ title }}
......
{% extends "core/base.html" %}
{% load fieldtype %}
{% load i18n %}
{% load pyazo %}
{% block content %}
<div class="clr-wizard">
......
"""
pyazo Core back Templatetag
"""
from urllib.parse import urlparse
from django import template
register = template.Library()
def is_absolute(url):
"""
Check if domain is absolute to
prevent user from being redirect somehwere else
"""
return bool(urlparse(url).netloc)
@register.simple_tag(takes_context=True)
def back(context):
"""
Return whether a back link is active or not.
"""
req = context['request']
if 'back' in req.GET:
if not is_absolute(req.GET.get('back')):
return req.GET.get('back')
if 'HTTP_REFERER' in req.META:
if not is_absolute(req.META.get('HTTP_REFERER')):
return req.META.get('HTTP_REFERER')
return ''
"""
pyazo Core Fieldtype filter
"""
from django import template
register = template.Library()
@register.filter('fieldtype')
def fieldtype(field):
"""
Return classname
"""
return field.__class__.__name__
"""pyazo Core navbar Templatetag"""
from django import template
register = template.Library()
@register.simple_tag(takes_context=True)
def is_active(context, *args):
"""Return whether a navbar link is active or not."""
req = context['request']
if not req.resolver_match:
return ''
for url in args:
short_url = url.split(':')[1] if ':' in url else url
if req.resolver_match.url_name.startswith(url) or \
req.resolver_match.url_name.startswith(short_url):
return 'active'
return ''
"""
pyazo Core navbar Templatetag
"""
from django import template
register = template.Library()
@register.simple_tag(takes_context=True)
def is_active_app(context, *args):
"""
Return True if current link is from app
"""
req = context['request']
if not req.resolver_match:
return ''
for app in args:
if app in req.resolver_match.app_names:
return 'active'
return ''
"""pyazo sidebar collection Templatetag"""
from urllib.parse import urlparse
from django import template
from pyazo.models import Collection
......@@ -12,3 +14,54 @@ def collections(context):
"""Return User's Collections"""
request = context['request']
return Collection.objects.filter(owner=request.user)
@register.filter('fieldtype')
def fieldtype(field):
"""Return classname"""
return field.__class__.__name__
def is_absolute(url):
"""Check if domain is absolute to
prevent user from being redirect somewhere else"""
return bool(urlparse(url).netloc)
@register.simple_tag(takes_context=True)
def back(context):
"""Return whether a back link is active or not."""
request = context.get('request')
if 'back' in request.GET:
if not is_absolute(request.GET.get('back')):
return request.GET.get('back')
if 'HTTP_REFERER' in request.META:
if not is_absolute(request.META.get('HTTP_REFERER')):
return request.META.get('HTTP_REFERER')
return ''
@register.simple_tag(takes_context=True)
def is_active(context, *args):
"""Return whether a navbar link is active or not."""
request = context.get('request')
if not request.resolver_match:
return ''
for url in args:
short_url = url.split(':')[1] if ':' in url else url
if request.resolver_match.url_name.startswith(url) or \
request.resolver_match.url_name.startswith(short_url):
return 'active'
return ''
@register.simple_tag(takes_context=True)
def is_active_app(context, *args):
"""Return True if current link is from app"""
request = context.get('request')
if not request.resolver_match:
return ''
for app in args:
if app in request.resolver_match.app_names:
return 'active'
return ''
"""
pyazo Core templte_wildcard Templatetag
"""
import glob
import os
from django import template
from django.conf import settings
register = template.Library()
@register.simple_tag
def template_wildcard(*args):
"""
Return a list of all templates in dir
"""
templates = []
for tmpl_dir in args:
for app in settings.INSTALLED_APPS:
if app.startswith('pyazo'):
app_dir = '/'.join(app.split('.')[1:-2])
prefix = os.path.abspath(os.path.join(settings.BASE_DIR, app_dir, 'templates/'))
path = os.path.join(prefix, tmpl_dir)
if os.path.isdir(path):
files = glob.glob(path+'**')
for file in files:
templates.append(os.path.relpath(file, start=prefix))
return templates
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment