Skip to content

Commit

Permalink
Merge pull request #188 from level12/168-template-names
Browse files Browse the repository at this point in the history
use dash-named templates by default
  • Loading branch information
guruofgentoo committed Oct 26, 2022
2 parents a2e0c0a + 6eb7765 commit 32e426a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 6 deletions.
40 changes: 35 additions & 5 deletions keg/web.py
Expand Up @@ -2,15 +2,17 @@

import inspect
import sys
from warnings import warn

from blazeutils.strings import case_cw2us, case_cw2dash
import flask
from flask import request
from flask import current_app, request
from flask.views import MethodView, http_method_funcs
try:
from flask.views import MethodViewType
except ImportError:
MethodViewType = None
from jinja2 import TemplateNotFound
import six
from werkzeug.datastructures import MultiDict

Expand Down Expand Up @@ -216,16 +218,44 @@ def process_auto_assign(self):
for key in self.auto_assign:
self.assign(key, getattr(self, key))

def calc_class_fname(self):
return case_cw2us(self.__class__.__name__)
def calc_class_fname(self, use_us=False):
if use_us:
return case_cw2us(self.__class__.__name__)
return case_cw2dash(self.__class__.__name__)

def calc_template_name(self):
def calc_template_name(self, use_us=False):
if self.template_name is not None:
return self.template_name
template_path = '{}.html'.format(self.calc_class_fname())
template_path = '{}.html'.format(self.calc_class_fname(use_us=use_us))
blueprint_name = request.blueprint
if blueprint_name:
template_path = '{}/{}'.format(blueprint_name, template_path)

# This block and `use_us` usage can be removed when we drop
# template names generated with underscores.
jinja_env = current_app.jinja_env
try:
jinja_env.get_template(template_path)
except TemplateNotFound:
raise_original_exception = False
if not use_us:
# Fall back to underscore-named templates for now, but warn if
# that succeeds
try:
template_path = self.calc_template_name(use_us=True)
warn(
'Templates named by underscore-notated class names are '
'deprecated and will not be supported. Rename the template '
'files using dashes.',
DeprecationWarning,
stacklevel=2
)
except TemplateNotFound:
# Neither template exists. Raise the exception from the dashed name
raise_original_exception = True
if use_us or raise_original_exception:
raise

return template_path

def assign(self, key, value):
Expand Down
2 changes: 1 addition & 1 deletion keg_apps/web/views/other.py
Expand Up @@ -22,7 +22,7 @@ def get(self):

class AutoAssignWithResponse(BaseView):
auto_assign = ('bar',)
template_name = 'other/auto_assign.html'
template_name = 'other/auto-assign.html'

def get(self):
self.bar = 'bar'
Expand Down

0 comments on commit 32e426a

Please sign in to comment.