mirror of
https://github.com/jazzband/django-dbtemplates.git
synced 2026-03-16 22:20:28 +00:00
Compare commits
14 commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
10e7de2eda | ||
|
|
930ce5c65c | ||
|
|
8e284b54d8 | ||
|
|
05f1ee1193 | ||
|
|
303bd0cabe | ||
|
|
64d112cc4f | ||
|
|
7f1c6701c1 | ||
|
|
873c90b777 | ||
|
|
e64a457281 | ||
|
|
a7f4e0bbe8 | ||
|
|
218b28b7aa | ||
|
|
602717af95 | ||
|
|
8769e29057 | ||
|
|
ac740e06f3 |
16 changed files with 164 additions and 113 deletions
|
|
@ -3,4 +3,4 @@ source = dbtemplates
|
|||
branch = 1
|
||||
|
||||
[report]
|
||||
omit = *tests*,*migrations*
|
||||
omit = *tests*,*/migrations/*,test_*
|
||||
|
|
|
|||
2
.github/workflows/release.yml
vendored
2
.github/workflows/release.yml
vendored
|
|
@ -33,7 +33,7 @@ jobs:
|
|||
|
||||
- name: Upload packages to Jazzband
|
||||
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
|
||||
uses: pypa/gh-action-pypi-publish@master
|
||||
uses: pypa/gh-action-pypi-publish@release/v1
|
||||
with:
|
||||
user: jazzband
|
||||
password: ${{ secrets.JAZZBAND_RELEASE_KEY }}
|
||||
|
|
|
|||
2
.github/workflows/test.yml
vendored
2
.github/workflows/test.yml
vendored
|
|
@ -9,7 +9,7 @@ jobs:
|
|||
fail-fast: false
|
||||
max-parallel: 5
|
||||
matrix:
|
||||
python-version: ['3.7', '3.8', '3.9', '3.10', '3.11-dev']
|
||||
python-version: ['3.8', '3.9', '3.10', '3.11', '3.13']
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
|
|
|||
|
|
@ -1,10 +1,3 @@
|
|||
from pkg_resources import get_distribution, DistributionNotFound
|
||||
import importlib.metadata
|
||||
|
||||
try:
|
||||
__version__ = get_distribution("django-dbtemplates").version
|
||||
except DistributionNotFound:
|
||||
# package is not installed
|
||||
__version__ = None
|
||||
|
||||
|
||||
default_app_config = 'dbtemplates.apps.DBTemplatesConfig'
|
||||
__version__ = importlib.metadata.version("django-dbtemplates")
|
||||
|
|
|
|||
|
|
@ -2,15 +2,8 @@ import posixpath
|
|||
from django import forms
|
||||
from django.contrib import admin
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
try:
|
||||
# Django 4.0
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from django.utils.translation import ngettext
|
||||
except ImportError:
|
||||
# Before Django 4.0
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.utils.translation import ungettext as ngettext
|
||||
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from django.utils.translation import ngettext
|
||||
from django.utils.safestring import mark_safe
|
||||
|
||||
from dbtemplates.conf import settings
|
||||
|
|
@ -104,6 +97,7 @@ class TemplateAdminForm(forms.ModelForm):
|
|||
|
||||
class TemplateAdmin(TemplateModelAdmin):
|
||||
form = TemplateAdminForm
|
||||
readonly_fields = ['creation_date', 'last_changed']
|
||||
fieldsets = (
|
||||
(None, {
|
||||
'fields': ('name', 'content'),
|
||||
|
|
|
|||
|
|
@ -1,10 +1,9 @@
|
|||
from django.apps import AppConfig
|
||||
try:
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
except ImportError:
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
|
||||
class DBTemplatesConfig(AppConfig):
|
||||
name = 'dbtemplates'
|
||||
verbose_name = _('Database templates')
|
||||
|
||||
default_auto_field = 'django.db.models.AutoField'
|
||||
|
|
|
|||
|
|
@ -0,0 +1,23 @@
|
|||
# Generated by Django 5.1 on 2025-05-26 19:59
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('dbtemplates', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='template',
|
||||
name='creation_date',
|
||||
field=models.DateTimeField(auto_now_add=True, verbose_name='creation date'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='template',
|
||||
name='last_changed',
|
||||
field=models.DateTimeField(auto_now=True, verbose_name='last changed'),
|
||||
),
|
||||
]
|
||||
|
|
@ -1,20 +1,16 @@
|
|||
from dbtemplates.conf import settings
|
||||
from dbtemplates.utils.cache import (add_template_to_cache,
|
||||
remove_cached_template)
|
||||
from dbtemplates.utils.cache import (
|
||||
add_template_to_cache,
|
||||
remove_cached_template,
|
||||
)
|
||||
from dbtemplates.utils.template import get_template_source
|
||||
|
||||
from django.contrib.sites.managers import CurrentSiteManager
|
||||
from django.contrib.sites.models import Site
|
||||
from django.db import models
|
||||
from django.db.models import signals
|
||||
from django.template import TemplateDoesNotExist
|
||||
try:
|
||||
# Django >= 4.0
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
except ImportError:
|
||||
# Django 3.2
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from django.utils.timezone import now
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
|
||||
class Template(models.Model):
|
||||
|
|
@ -22,15 +18,15 @@ class Template(models.Model):
|
|||
Defines a template model for use with the database template loader.
|
||||
The field ``name`` is the equivalent to the filename of a static template.
|
||||
"""
|
||||
id = models.AutoField(primary_key=True, verbose_name=_('ID'),
|
||||
serialize=False, auto_created=True)
|
||||
name = models.CharField(_('name'), max_length=100,
|
||||
help_text=_("Example: 'flatpages/default.html'"))
|
||||
content = models.TextField(_('content'), blank=True)
|
||||
sites = models.ManyToManyField(Site, verbose_name=_('sites'),
|
||||
blank=True)
|
||||
creation_date = models.DateTimeField(_('creation date'),
|
||||
default=now)
|
||||
last_changed = models.DateTimeField(_('last changed'),
|
||||
default=now)
|
||||
creation_date = models.DateTimeField(_('creation date'), auto_now_add=True)
|
||||
last_changed = models.DateTimeField(_('last changed'), auto_now=True)
|
||||
|
||||
objects = models.Manager()
|
||||
on_site = CurrentSiteManager('sites')
|
||||
|
|
@ -59,7 +55,6 @@ class Template(models.Model):
|
|||
pass
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
self.last_changed = now()
|
||||
# If content is empty look for a template with the given name and
|
||||
# populate the template instance with its content.
|
||||
if settings.DBTEMPLATES_AUTO_POPULATE_CONTENT and not self.content:
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import os
|
||||
import shutil
|
||||
import tempfile
|
||||
from unittest import mock
|
||||
|
||||
from django.conf import settings as django_settings
|
||||
from django.core.cache.backends.base import BaseCache
|
||||
|
|
@ -151,3 +152,24 @@ class DbTemplatesTestCase(TestCase):
|
|||
def test_get_cache_name(self):
|
||||
self.assertEqual(get_cache_key('name with spaces'),
|
||||
'dbtemplates::name-with-spaces::1')
|
||||
|
||||
def test_cache_invalidation(self):
|
||||
# Add t1 into the cache of site2
|
||||
self.t1.sites.add(self.site2)
|
||||
with mock.patch('django.contrib.sites.models.SiteManager.get_current',
|
||||
return_value=self.site2):
|
||||
result = loader.get_template("base.html").render()
|
||||
self.assertEqual(result, 'base')
|
||||
|
||||
# Update content
|
||||
self.t1.content = 'new content'
|
||||
self.t1.save()
|
||||
result = loader.get_template("base.html").render()
|
||||
self.assertEqual(result, 'new content')
|
||||
|
||||
# Cache invalidation should work across sites.
|
||||
# Site2 should see the new content.
|
||||
with mock.patch('django.contrib.sites.models.SiteManager.get_current',
|
||||
return_value=self.site2):
|
||||
result = loader.get_template("base.html").render()
|
||||
self.assertEqual(result, 'new content')
|
||||
|
|
|
|||
|
|
@ -21,9 +21,10 @@ def get_cache_backend():
|
|||
cache = get_cache_backend()
|
||||
|
||||
|
||||
def get_cache_key(name):
|
||||
current_site = Site.objects.get_current()
|
||||
return f"dbtemplates::{slugify(name)}::{current_site.pk}"
|
||||
def get_cache_key(name, site=None):
|
||||
if site is None:
|
||||
site = Site.objects.get_current()
|
||||
return f"dbtemplates::{slugify(name)}::{site.pk}"
|
||||
|
||||
|
||||
def get_cache_notfound_key(name):
|
||||
|
|
@ -57,4 +58,5 @@ def remove_cached_template(instance, **kwargs):
|
|||
Called via Django's signals to remove cached templates, if the template
|
||||
in the database was changed or deleted.
|
||||
"""
|
||||
cache.delete(get_cache_key(instance.name))
|
||||
for site in instance.sites.all():
|
||||
cache.delete(get_cache_key(instance.name, site=site))
|
||||
|
|
|
|||
|
|
@ -1,7 +1,20 @@
|
|||
Changelog
|
||||
=========
|
||||
|
||||
v4.0 (2022-08-29)
|
||||
v5.0 (unreleased)
|
||||
-----------------
|
||||
|
||||
.. warning::
|
||||
|
||||
This is a backwards-incompatible release!
|
||||
|
||||
* Dropped support for Python 3.7 and Django < 4.2.
|
||||
|
||||
* Added support for Python 3.11, 3.12, 3.13.
|
||||
|
||||
* Django 5.x support
|
||||
|
||||
v4.0 (2022-09-3)
|
||||
-----------------
|
||||
|
||||
.. warning::
|
||||
|
|
|
|||
|
|
@ -3,8 +3,7 @@ Setup
|
|||
|
||||
1. Get the source from the `Git repository`_ or install it from the
|
||||
Python Package Index by running ``pip install django-dbtemplates``.
|
||||
2. Follow the instructions in the INSTALL file
|
||||
3. Edit the settings.py of your Django site:
|
||||
2. Edit the settings.py of your Django site:
|
||||
|
||||
* Add ``dbtemplates`` to the ``INSTALLED_APPS`` setting
|
||||
|
||||
|
|
@ -61,8 +60,8 @@ Setup
|
|||
from the database to be used to override templates in other locations,
|
||||
put ``dbtemplates.loader.Loader`` at the beginning of ``loaders``.
|
||||
|
||||
4. Sync your database ``python manage.py migrate``
|
||||
5. Restart your Django server
|
||||
3. Sync your database ``python manage.py migrate``
|
||||
4. Restart your Django server
|
||||
|
||||
.. _Git repository: https://github.com/jazzband/django-dbtemplates/
|
||||
|
||||
|
|
|
|||
51
pyproject.toml
Normal file
51
pyproject.toml
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
[build-system]
|
||||
requires = [
|
||||
"setuptools>=61.2",
|
||||
"setuptools_scm",
|
||||
]
|
||||
build-backend = "setuptools.build_meta"
|
||||
|
||||
[project]
|
||||
name = "django-dbtemplates"
|
||||
authors = [{name = "Jannis Leidel", email = "jannis@leidel.info"}]
|
||||
description = "Template loader for templates stored in the database"
|
||||
readme = "README.rst"
|
||||
classifiers = [
|
||||
"Development Status :: 5 - Production/Stable",
|
||||
"Environment :: Web Environment",
|
||||
"Intended Audience :: Developers",
|
||||
"License :: OSI Approved :: BSD License",
|
||||
"Operating System :: OS Independent",
|
||||
"Programming Language :: Python",
|
||||
"Programming Language :: Python :: 3",
|
||||
"Programming Language :: Python :: 3 :: Only",
|
||||
"Programming Language :: Python :: 3.8",
|
||||
"Programming Language :: Python :: 3.9",
|
||||
"Programming Language :: Python :: 3.10",
|
||||
"Programming Language :: Python :: 3.11",
|
||||
"Programming Language :: Python :: 3.12",
|
||||
"Programming Language :: Python :: 3.13",
|
||||
"Framework :: Django",
|
||||
]
|
||||
requires-python = ">=3.8"
|
||||
dependencies = ["django-appconf >= 0.4"]
|
||||
dynamic = ["version"]
|
||||
|
||||
[project.urls]
|
||||
Documentation = "https://django-dbtemplates.readthedocs.io/"
|
||||
Changelog = "https://django-dbtemplates.readthedocs.io/en/latest/changelog.html"
|
||||
Source = "https://github.com/jazzband/django-dbtemplates"
|
||||
|
||||
[tool.setuptools]
|
||||
zip-safe = false
|
||||
include-package-data = false
|
||||
|
||||
[tool.setuptools.packages]
|
||||
find = {namespaces = false}
|
||||
|
||||
[tool.setuptools.package-data]
|
||||
dbtemplates = [
|
||||
"locale/*/LC_MESSAGES/*",
|
||||
"static/dbtemplates/css/*.css",
|
||||
"static/dbtemplates/js/*.js",
|
||||
]
|
||||
|
|
@ -1,4 +0,0 @@
|
|||
[build_sphinx]
|
||||
source-dir = docs/
|
||||
build-dir = docs/_build
|
||||
all_files = 1
|
||||
48
setup.py
48
setup.py
|
|
@ -1,48 +0,0 @@
|
|||
import os
|
||||
import io
|
||||
from setuptools import setup, find_packages
|
||||
|
||||
|
||||
def read(*parts):
|
||||
filename = os.path.join(os.path.dirname(__file__), *parts)
|
||||
with open(filename, encoding="utf-8") as fp:
|
||||
return fp.read()
|
||||
|
||||
|
||||
setup(
|
||||
name="django-dbtemplates",
|
||||
use_scm_version={"version_scheme": "post-release"},
|
||||
setup_requires=["setuptools_scm"],
|
||||
description="Template loader for templates stored in the database",
|
||||
long_description=read("README.rst"),
|
||||
author="Jannis Leidel",
|
||||
author_email="jannis@leidel.info",
|
||||
url="https://django-dbtemplates.readthedocs.io/",
|
||||
packages=find_packages(),
|
||||
zip_safe=False,
|
||||
package_data={
|
||||
"dbtemplates": [
|
||||
"locale/*/LC_MESSAGES/*",
|
||||
"static/dbtemplates/css/*.css",
|
||||
"static/dbtemplates/js/*.js",
|
||||
],
|
||||
},
|
||||
classifiers=[
|
||||
"Development Status :: 5 - Production/Stable",
|
||||
"Environment :: Web Environment",
|
||||
"Intended Audience :: Developers",
|
||||
"License :: OSI Approved :: BSD License",
|
||||
"Operating System :: OS Independent",
|
||||
"Programming Language :: Python",
|
||||
"Programming Language :: Python :: 3",
|
||||
"Programming Language :: Python :: 3 :: Only",
|
||||
"Programming Language :: Python :: 3.7",
|
||||
"Programming Language :: Python :: 3.8",
|
||||
"Programming Language :: Python :: 3.9",
|
||||
"Programming Language :: Python :: 3.10",
|
||||
"Framework :: Django",
|
||||
],
|
||||
python_requires=">=3.7",
|
||||
install_requires=["django-appconf >= 0.4"],
|
||||
)
|
||||
|
||||
36
tox.ini
36
tox.ini
|
|
@ -1,41 +1,53 @@
|
|||
[tox]
|
||||
skipsdist = True
|
||||
usedevelop = True
|
||||
minversion = 1.8
|
||||
minversion = 4.0
|
||||
envlist =
|
||||
flake8
|
||||
py3{7,8,9,10,11}-dj32
|
||||
py3{8,9,10,11}-dj{40,41,main}
|
||||
py3{8,9,10,11,12}-dj42
|
||||
py3{10,11,12}-dj{50}
|
||||
py3{10,11,12,13}-dj{51,52}
|
||||
py3{12,13}-dj{main}
|
||||
coverage
|
||||
|
||||
[gh-actions]
|
||||
python =
|
||||
3.7: py37
|
||||
3.8: py38
|
||||
3.9: py39
|
||||
3.10: py310
|
||||
3.10: py310, flake8
|
||||
3.11: py311
|
||||
3.12: py312
|
||||
3.13: py313
|
||||
|
||||
[testenv]
|
||||
skipsdist = true
|
||||
package = editable
|
||||
basepython =
|
||||
py37: python3.7
|
||||
py38: python3.8
|
||||
py39: python3.9
|
||||
py310: python3.10
|
||||
py311: python3.11
|
||||
usedevelop = true
|
||||
py312: python3.12
|
||||
py313: python3.13
|
||||
setenv =
|
||||
DJANGO_SETTINGS_MODULE = dbtemplates.test_settings
|
||||
deps =
|
||||
-r requirements/tests.txt
|
||||
dj32: Django<3.3
|
||||
dj40: Django<4.1
|
||||
dj41: Django<4.2
|
||||
dj42: Django>=4.2,<4.3
|
||||
dj50: Django>=5.0,<5.1
|
||||
dj51: Django>=5.1,<5.2
|
||||
dj52: Django>=5.2,<5.3
|
||||
djmain: https://github.com/django/django/archive/main.tar.gz#egg=django
|
||||
|
||||
commands =
|
||||
python --version
|
||||
coverage run {envbindir}/django-admin test -v2 {posargs:dbtemplates}
|
||||
python -c "import django ; print(django.VERSION)"
|
||||
coverage run --branch --parallel-mode {envbindir}/django-admin test -v2 {posargs:dbtemplates}
|
||||
|
||||
[testenv:coverage]
|
||||
basepython = python3.10
|
||||
deps = coverage
|
||||
commands =
|
||||
coverage combine
|
||||
coverage report
|
||||
coverage xml
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue