black on utils.py

o
This commit is contained in:
Yohan Boniface 2023-02-28 17:29:11 +01:00
parent 90607c7581
commit d2161a3d09

View file

@ -6,7 +6,7 @@ from django.urls import URLPattern, URLResolver
def get_uri_template(urlname, args=None, prefix=""): def get_uri_template(urlname, args=None, prefix=""):
''' """
Utility function to return an URI Template from a named URL in django Utility function to return an URI Template from a named URL in django
Copied from django-digitalpaper. Copied from django-digitalpaper.
@ -18,21 +18,22 @@ def get_uri_template(urlname, args=None, prefix=""):
non-capturing parenthesis in them) by trying to find a pattern non-capturing parenthesis in them) by trying to find a pattern
whose optional parameters match those you specified (a parameter whose optional parameters match those you specified (a parameter
is considered optional if it doesn't appear in every pattern possibility) is considered optional if it doesn't appear in every pattern possibility)
''' """
def _convert(template, args=None): def _convert(template, args=None):
"""URI template converter""" """URI template converter"""
if not args: if not args:
args = [] args = []
paths = template % dict([p, "{%s}" % p] for p in args) paths = template % dict([p, "{%s}" % p] for p in args)
return u'%s/%s' % (prefix, paths) return "%s/%s" % (prefix, paths)
resolver = get_resolver(None) resolver = get_resolver(None)
parts = urlname.split(':') parts = urlname.split(":")
if len(parts) > 1 and parts[0] in resolver.namespace_dict: if len(parts) > 1 and parts[0] in resolver.namespace_dict:
namespace = parts[0] namespace = parts[0]
urlname = parts[1] urlname = parts[1]
nprefix, resolver = resolver.namespace_dict[namespace] nprefix, resolver = resolver.namespace_dict[namespace]
prefix = prefix + '/' + nprefix.rstrip('/') prefix = prefix + "/" + nprefix.rstrip("/")
possibilities = resolver.reverse_dict.getlist(urlname) possibilities = resolver.reverse_dict.getlist(urlname)
for tmp in possibilities: for tmp in possibilities:
possibility, pattern = tmp[:2] possibility, pattern = tmp[:2]
@ -61,7 +62,6 @@ def get_uri_template(urlname, args=None, prefix=""):
class DecoratedURLPattern(URLPattern): class DecoratedURLPattern(URLPattern):
def resolve(self, *args, **kwargs): def resolve(self, *args, **kwargs):
result = URLPattern.resolve(self, *args, **kwargs) result = URLPattern.resolve(self, *args, **kwargs)
if result: if result:
@ -97,6 +97,7 @@ def decorated_patterns(func, *urls):
if not hasattr(pp, "_decorate_with"): if not hasattr(pp, "_decorate_with"):
setattr(pp, "_decorate_with", []) setattr(pp, "_decorate_with", [])
pp._decorate_with.append(func) pp._decorate_with.append(func)
if func: if func:
if not isinstance(func, (list, tuple)): if not isinstance(func, (list, tuple)):
func = [func] func = [func]
@ -108,11 +109,11 @@ def decorated_patterns(func, *urls):
def gzip_file(from_path, to_path): def gzip_file(from_path, to_path):
stat = os.stat(from_path) stat = os.stat(from_path)
with open(from_path, 'rb') as f_in: with open(from_path, "rb") as f_in:
with gzip.open(to_path, 'wb') as f_out: with gzip.open(to_path, "wb") as f_out:
f_out.writelines(f_in) f_out.writelines(f_in)
os.utime(to_path, (stat.st_mtime, stat.st_mtime)) os.utime(to_path, (stat.st_mtime, stat.st_mtime))
def is_ajax(request): def is_ajax(request):
return request.headers.get('x-requested-with') == 'XMLHttpRequest' return request.headers.get("x-requested-with") == "XMLHttpRequest"