main
Estelle Poulin 2 years ago
parent b845aa6c19
commit d5488a2fda

@ -1,23 +1,24 @@
from setuptools import find_packages, setup from setuptools import find_packages, setup
with open('README.md') as readme: with open("README.md") as readme:
setup( setup(
name='subjugate', name="subjugate",
version=__import__("subjugate").__version__, # version=__import__("subjugate.__version__").__version__,
url='https://github.com/estheruary/subjugate', version="1.0.0",
license='LGPLv3', url="https://github.com/estheruary/subjugate",
description='Write templates in Django with Dominate', license="LGPLv3",
description="Write templates in Django with Dominate",
long_description=readme.read(), long_description=readme.read(),
long_description_content_type='text/markdown', long_description_content_type="text/markdown",
author='Estelle Poulin', author="Estelle Poulin",
author_email='dev@inspiredby.es', author_email="dev@inspiredby.es",
packages=find_packages(exclude=['tests']), packages=find_packages(exclude=["tests"]),
include_package_data=True, include_package_data=True,
install_requires=["django>=2.2"], install_requires=["Django>=2.2"],
python_requires=">=3.6", python_requires=">=3.6",
zip_safe=False, zip_safe=False,
project_urls={ project_urls={
'Source': 'https://github.com/estheruary/subjugate', "Source": "https://github.com/estheruary/subjugate",
'Changelog': 'https://github.com/estheruary/subjugate/blob/main/CHANGELOG.md', "Changelog": "https://github.com/estheruary/subjugate/blob/main/CHANGELOG.md",
}, },
) )

@ -22,6 +22,9 @@ class SubjugateTemplate(ABC):
def csrf_token(self): def csrf_token(self):
return self.context.get("csrf_token", "") if self.context else "" return self.context.get("csrf_token", "") if self.context else ""
def csrf_input(self):
return str(self.context.get("csrf_input", "")) if self.context else ""
def url(self, path): def url(self, path):
return reverse(path) return reverse(path)
@ -51,4 +54,4 @@ class SubjugateTemplate(ABC):
@abstractmethod @abstractmethod
def render(**kwargs): def render(**kwargs):
... pass

@ -0,0 +1 @@
__version__ = "0.0.1"

@ -1,6 +1,7 @@
from importlib.util import module_from_spec from importlib.util import module_from_spec
from django.template.context import Context, make_context from django.template.context import make_context
from django.template.exceptions import TemplateSyntaxError from django.template.exceptions import TemplateSyntaxError
from django.template.backends.utils import csrf_input_lazy, csrf_token_lazy
from subjugate import SubjugateTemplate as SubjugateUserTemplate from subjugate import SubjugateTemplate as SubjugateUserTemplate
@ -12,9 +13,12 @@ import importlib.machinery
class SubjugateTemplate: class SubjugateTemplate:
def find_template_class(self, module, base_class): def find_template_class(self, module, base_class):
userclass = next( userclass = next(
(cls for _, cls in inspect.getmembers(module, inspect.isclass) (
if cls.__module__ == module.__name__ and issubclass(cls, base_class)), cls
None for _, cls in inspect.getmembers(module, inspect.isclass)
if cls.__module__ == module.__name__ and issubclass(cls, base_class)
),
None,
) )
if not userclass: if not userclass:
@ -24,13 +28,10 @@ class SubjugateTemplate:
def code_to_module(self, code, template_name): def code_to_module(self, code, template_name):
module_name = self.modulify(template_name) module_name = self.modulify(template_name)
spec = importlib.machinery.ModuleSpec( spec = importlib.machinery.ModuleSpec(name=module_name, loader=None, origin=self.origin)
name=module_name,
loader=None,
origin=self.origin
)
module = module_from_spec(spec) module = module_from_spec(spec)
exec(code, module.__dict__) exec(code, module.__dict__)
return module return module
def modulify(self, template_name: str): def modulify(self, template_name: str):
@ -60,7 +61,7 @@ class SubjugateTemplate:
return f"SubjugateTemplate{base}" return f"SubjugateTemplate{base}"
def __init__(self, contents, origin, template_name, engine): def __init__(self, contents, origin, template_name, engine):
self.contents= contents self.contents = contents
self.origin = origin self.origin = origin
self.template_name = template_name self.template_name = template_name
self.engine = engine self.engine = engine
@ -71,4 +72,9 @@ class SubjugateTemplate:
if isinstance(context, dict) or context is None: if isinstance(context, dict) or context is None:
context = make_context(context, request, autoescape=self.engine.autoescape) context = make_context(context, request, autoescape=self.engine.autoescape)
if request is not None:
context["request"] = request
context["csrf_input"] = csrf_input_lazy(request)
context["csrf_token"] = csrf_token_lazy(request)
return self.userclass(self.engine, context, request).render(**kwargs) return self.userclass(self.engine, context, request).render(**kwargs)

@ -72,10 +72,6 @@ class GenericEngine(Engine, DjangoTemplates):
def __repr__(self): def __repr__(self):
return f"<{self.__class__.__qualname__}>" return f"<{self.__class__.__qualname__}>"
@cached_property
def template_context_processors(self):
return tuple([import_string(path) for path in self.context_processors])
def find_template_loader(self, loader): def find_template_loader(self, loader):
if isinstance(loader, (tuple, list)): if isinstance(loader, (tuple, list)):
loader, *args = loader loader, *args = loader

Loading…
Cancel
Save