diff --git a/setup.py b/setup.py index 75ad74e..d6a0b78 100644 --- a/setup.py +++ b/setup.py @@ -1,23 +1,24 @@ from setuptools import find_packages, setup -with open('README.md') as readme: +with open("README.md") as readme: setup( - name='subjugate', - version=__import__("subjugate").__version__, - url='https://github.com/estheruary/subjugate', - license='LGPLv3', - description='Write templates in Django with Dominate', + name="subjugate", + # version=__import__("subjugate.__version__").__version__, + version="1.0.0", + url="https://github.com/estheruary/subjugate", + license="LGPLv3", + description="Write templates in Django with Dominate", long_description=readme.read(), - long_description_content_type='text/markdown', - author='Estelle Poulin', - author_email='dev@inspiredby.es', - packages=find_packages(exclude=['tests']), + long_description_content_type="text/markdown", + author="Estelle Poulin", + author_email="dev@inspiredby.es", + packages=find_packages(exclude=["tests"]), include_package_data=True, - install_requires=["django>=2.2"], + install_requires=["Django>=2.2"], python_requires=">=3.6", zip_safe=False, project_urls={ - 'Source': 'https://github.com/estheruary/subjugate', - 'Changelog': 'https://github.com/estheruary/subjugate/blob/main/CHANGELOG.md', + "Source": "https://github.com/estheruary/subjugate", + "Changelog": "https://github.com/estheruary/subjugate/blob/main/CHANGELOG.md", }, ) diff --git a/subjugate/__init__.py b/subjugate/__init__.py index 3e4d95b..e5046c9 100644 --- a/subjugate/__init__.py +++ b/subjugate/__init__.py @@ -22,6 +22,9 @@ class SubjugateTemplate(ABC): def csrf_token(self): 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): return reverse(path) @@ -51,4 +54,4 @@ class SubjugateTemplate(ABC): @abstractmethod def render(**kwargs): - ... + pass diff --git a/subjugate/__version__.py b/subjugate/__version__.py new file mode 100644 index 0000000..f102a9c --- /dev/null +++ b/subjugate/__version__.py @@ -0,0 +1 @@ +__version__ = "0.0.1" diff --git a/subjugate/template/__init__.py b/subjugate/template/__init__.py index 0c33d3d..d0458a0 100644 --- a/subjugate/template/__init__.py +++ b/subjugate/template/__init__.py @@ -1,6 +1,7 @@ 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.backends.utils import csrf_input_lazy, csrf_token_lazy from subjugate import SubjugateTemplate as SubjugateUserTemplate @@ -12,9 +13,12 @@ import importlib.machinery class SubjugateTemplate: def find_template_class(self, module, base_class): userclass = next( - (cls for _, cls in inspect.getmembers(module, inspect.isclass) - if cls.__module__ == module.__name__ and issubclass(cls, base_class)), - None + ( + cls + for _, cls in inspect.getmembers(module, inspect.isclass) + if cls.__module__ == module.__name__ and issubclass(cls, base_class) + ), + None, ) if not userclass: @@ -24,13 +28,10 @@ class SubjugateTemplate: def code_to_module(self, code, template_name): module_name = self.modulify(template_name) - spec = importlib.machinery.ModuleSpec( - name=module_name, - loader=None, - origin=self.origin - ) + spec = importlib.machinery.ModuleSpec(name=module_name, loader=None, origin=self.origin) module = module_from_spec(spec) exec(code, module.__dict__) + return module def modulify(self, template_name: str): @@ -60,7 +61,7 @@ class SubjugateTemplate: return f"SubjugateTemplate{base}" def __init__(self, contents, origin, template_name, engine): - self.contents= contents + self.contents = contents self.origin = origin self.template_name = template_name self.engine = engine @@ -71,4 +72,9 @@ class SubjugateTemplate: if isinstance(context, dict) or context is None: 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) diff --git a/subjugate/template/engine.py b/subjugate/template/engine.py index 5ab3f5b..0170323 100644 --- a/subjugate/template/engine.py +++ b/subjugate/template/engine.py @@ -72,10 +72,6 @@ class GenericEngine(Engine, DjangoTemplates): def __repr__(self): 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): if isinstance(loader, (tuple, list)): loader, *args = loader