Skip to content

Commit 62ba054

Browse files
authored
Merge pull request jazzband#81 from jazzband/python3
Port to Python 3 and get rid of legacy code
2 parents 279fba2 + e4acf6e commit 62ba054

29 files changed

+242
-491
lines changed

.coveragerc

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[run]
2+
source = dbtemplates
3+
branch = 1
4+
5+
[report]
6+
omit = *tests*,*migrations*

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ MANIFEST
44
build
55
dist
66
*.egg-info
7-
example/example.db
87
docs/_build
98
.tox/
109
*.egg/

.travis.yml

+27-29
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,32 @@
1+
language: python
2+
python: 3.5
13
sudo: false
24
cache: pip
3-
language: python
4-
python:
5-
- 2.6
6-
- 2.7
7-
install:
8-
- pip install -e .
9-
- pip install -r requirements/tests.txt Django==$DJANGO
10-
before_script:
11-
- flake8 dbtemplates --ignore=E501 --exclude=migrations
12-
script:
13-
- coverage run --branch --source=dbtemplates `which django-admin.py` test dbtemplates
14-
- coverage report --omit="dbtemplates/test*,dbtemplates/migrations*"
155
env:
16-
global:
17-
- DJANGO_SETTINGS_MODULE=dbtemplates.test_settings
18-
matrix:
19-
- DJANGO="1.4.5 importlib"
20-
- DJANGO="1.5.1 importlib"
21-
- DJANGO=1.7.8
22-
- DJANGO=1.8.11
23-
- DJANGO=1.9.4
24-
matrix:
25-
exclude:
26-
- python: 2.6
27-
env: DJANGO=1.7.8
28-
- python: 2.6
29-
env: DJANGO=1.8.11
30-
- python: 2.6
31-
env: DJANGO=1.9.4
6+
- TOXENV=flake8-py27
7+
- TOXENV=flake8-py35
8+
- TOXENV=readme-py27
9+
- TOXENV=py27-dj18
10+
- TOXENV=py27-dj19
11+
- TOXENV=py27-dj110
12+
- TOXENV=py27-djmaster
13+
- TOXENV=py34-dj18
14+
- TOXENV=py34-dj19
15+
- TOXENV=py34-dj110
16+
- TOXENV=py34-djmaster
17+
- TOXENV=py35-dj18
18+
- TOXENV=py35-dj19
19+
- TOXENV=py35-dj110
20+
- TOXENV=py35-djmaster
21+
- TOXENV=pypy-dj18
22+
- TOXENV=pypy-dj19
23+
- TOXENV=pypy-dj110
24+
- TOXENV=pypy-djmaster
25+
install:
26+
- pip install tox codecov
27+
script: tox -v
28+
after_success:
29+
- codecov
3230
deploy:
3331
provider: pypi
3432
user: jazzband
@@ -39,4 +37,4 @@ deploy:
3937
tags: true
4038
repo: jazzband/django-dbtemplates
4139
python: "2.7"
42-
condition: "$DJANGO = 1.8.11"
40+
condition: "$TOXENV = py27-dj110"

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2007-2012, Jannis Leidel and contributors
1+
Copyright (c) 2007-2016, Jannis Leidel and contributors
22
All rights reserved.
33

44
Redistribution and use in source and binary forms, with or without

README.rst

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
django-dbtemplates
22
==================
33

4-
.. image:: https://secure.travis-ci.org/jazzband/django-dbtemplates.png
5-
:alt: Build Status
6-
:target: http://travis-ci.org/jazzband/django-dbtemplates
7-
84
.. image:: https://jazzband.co/static/img/badge.svg
95
:alt: Jazzband
106
:target: https://jazzband.co/
117

8+
.. image:: https://travis-ci.org/jazzband/django-dbtemplates.svg?branch=master
9+
:alt: Build Status
10+
:target: http://travis-ci.org/jazzband/django-dbtemplates
11+
12+
.. image:: https://codecov.io/github/jazzband/django-dbtemplates/coverage.svg?branch=master
13+
:alt: Codecov
14+
:target: https://codecov.io/github/jazzband/django-dbtemplates?branch=master
15+
1216
``dbtemplates`` is a Django app that consists of two parts:
1317

1418
1. It allows you to store templates in your database
@@ -24,10 +28,4 @@ The source code and issue tracker can be found on Github:
2428

2529
https://github.com/jazzband/django-dbtemplates
2630

27-
Compatibility Roadmap
28-
---------------------
29-
30-
- 1.3.2 ``dbtemplates`` dropped support for Django < 1.4
31-
- 1.4 will be supported only Django >= 1.7, please freeze your requirements on specific version of ``dbtemplates`` !
32-
3331
.. _template loader: http://docs.djangoproject.com/en/dev/ref/templates/api/#loader-types

dbtemplates/__init__.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
# following PEP 386
2-
__version__ = "1.3.2"
1+
__version__ = "2.0"

dbtemplates/loader.py

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
1-
import django
21
from django.contrib.sites.models import Site
32
from django.db import router
43
from django.template import TemplateDoesNotExist
4+
from django.template.loaders.base import Loader as BaseLoader
55

66
from dbtemplates.models import Template
77
from dbtemplates.utils.cache import (cache, get_cache_key,
88
set_and_return, get_cache_notfound_key)
99

10-
if django.get_version() >= '1.8':
11-
from django.template.loaders.base import Loader as tLoaderCls
12-
else:
13-
from django.template.loader import BaseLoader as tLoaderCls # noqa
1410

15-
16-
class Loader(tLoaderCls):
11+
class Loader(BaseLoader):
1712
"""
1813
A custom template loader to load templates from the database.
1914
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
from django.core.management.base import CommandError, NoArgsCommand
1+
from django.core.management.base import CommandError, BaseCommand
22

33
from dbtemplates.models import Template
44
from dbtemplates.utils.template import check_template_syntax
55

66

7-
class Command(NoArgsCommand):
7+
class Command(BaseCommand):
88
help = "Ensures templates stored in the database don't have syntax errors."
99

10-
def handle_noargs(self, **options):
10+
def handle(self, **options):
1111
errors = []
1212
for template in Template.objects.all():
1313
valid, error = check_template_syntax(template)
@@ -16,6 +16,4 @@ def handle_noargs(self, **options):
1616
if errors:
1717
raise CommandError(
1818
'Some templates contained errors\n%s' % '\n'.join(errors))
19-
# NOTE: printing instead of using self.stdout.write to maintain
20-
# Django 1.2 compatibility
21-
print('OK')
19+
self.stdout.write('OK')

dbtemplates/management/commands/create_error_templates.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import sys
2-
from optparse import make_option
3-
4-
from django.core.management.base import CommandError, NoArgsCommand
2+
from django.core.management.base import CommandError, BaseCommand
53
from django.contrib.sites.models import Site
64

75
from dbtemplates.models import Template
@@ -26,14 +24,15 @@
2624
}
2725

2826

29-
class Command(NoArgsCommand):
27+
class Command(BaseCommand):
3028
help = "Creates the default error templates as database template objects."
31-
option_list = NoArgsCommand.option_list + (
32-
make_option("-f", "--force", action="store_true", dest="force",
33-
default=False,
34-
help="overwrite existing database templates"),)
3529

36-
def handle_noargs(self, **options):
30+
def add_arguments(self, parser):
31+
parser.add_argument(
32+
"-f", "--force", action="store_true", dest="force",
33+
default=False, help="overwrite existing database templates")
34+
35+
def handle(self, **options):
3736
force = options.get('force')
3837
try:
3938
site = Site.objects.get_current()
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,54 @@
1+
import io
12
import os
2-
import codecs
3-
from optparse import make_option
4-
from django import VERSION
53
from django.contrib.sites.models import Site
6-
from django.core.management.base import CommandError, NoArgsCommand
4+
from django.core.management.base import CommandError, BaseCommand
5+
from django.template.utils import get_app_template_dirs
6+
from django.template.loader import _engine_list
77
try:
88
from django.utils.six import input as raw_input
99
except ImportError:
1010
pass
1111

12-
from dbtemplates.conf import settings
1312
from dbtemplates.models import Template
1413

1514
ALWAYS_ASK, FILES_TO_DATABASE, DATABASE_TO_FILES = ('0', '1', '2')
1615

1716
DIRS = []
17+
for engine in _engine_list():
18+
DIRS.extend(engine.dirs)
19+
app_template_dirs = get_app_template_dirs('templates')
1820

19-
if VERSION[:2] < (1, 8):
20-
from django.template.loaders.app_directories import app_template_dirs
21-
DIRS = settings.TEMPLATE_DIRS
22-
else:
23-
from django.template.utils import get_app_template_dirs
24-
from django.template.loader import _engine_list
25-
for engine in _engine_list():
26-
DIRS.extend(engine.dirs)
27-
app_template_dirs = get_app_template_dirs('templates')
2821

29-
30-
class Command(NoArgsCommand):
22+
class Command(BaseCommand):
3123
help = "Syncs file system templates with the database bidirectionally."
32-
option_list = NoArgsCommand.option_list + (
33-
make_option("-e", "--ext",
34-
dest="ext", action="store", default="html",
35-
help="extension of the files you want to "
36-
"sync with the database [default: %default]"),
37-
make_option("-f", "--force",
38-
action="store_true", dest="force", default=False,
39-
help="overwrite existing database templates"),
40-
make_option("-o", "--overwrite",
41-
action="store", dest="overwrite", default='0',
42-
help="'0' - ask always, '1' - overwrite database "
43-
"templates from template files, '2' - overwrite "
44-
"template files from database templates"),
45-
make_option("-a", "--app-first",
46-
action="store_true", dest="app_first", default=False,
47-
help="look for templates in applications "
48-
"directories before project templates"),
49-
make_option("-d", "--delete",
50-
action="store_true", dest="delete", default=False,
51-
help="Delete templates after syncing"))
5224

53-
def handle_noargs(self, **options):
25+
def add_arguments(self, parser):
26+
parser.add_argument(
27+
"-e", "--ext",
28+
dest="ext", action="store", default="html",
29+
help="extension of the files you want to "
30+
"sync with the database [default: %default]")
31+
parser.add_argument(
32+
"-f", "--force",
33+
action="store_true", dest="force", default=False,
34+
help="overwrite existing database templates")
35+
parser.add_argument(
36+
"-o", "--overwrite",
37+
action="store", dest="overwrite", default='0',
38+
help="'0' - ask always, '1' - overwrite database "
39+
"templates from template files, '2' - overwrite "
40+
"template files from database templates")
41+
parser.add_argument(
42+
"-a", "--app-first",
43+
action="store_true", dest="app_first", default=False,
44+
help="look for templates in applications "
45+
"directories before project templates")
46+
parser.add_argument(
47+
"-d", "--delete",
48+
action="store_true", dest="delete", default=False,
49+
help="Delete templates after syncing")
50+
51+
def handle(self, **options):
5452
extension = options.get('ext')
5553
force = options.get('force')
5654
overwrite = options.get('overwrite')
@@ -66,10 +64,6 @@ def handle_noargs(self, **options):
6664
raise CommandError("Please make sure to have the sites contrib "
6765
"app installed and setup with a site object")
6866

69-
if not type(settings.TEMPLATE_DIRS) in (tuple, list):
70-
raise CommandError("Please make sure settings.TEMPLATE_DIRS is a "
71-
"list or tuple.")
72-
7367
if app_first:
7468
tpl_dirs = app_template_dirs + DIRS
7569
else:
@@ -93,8 +87,8 @@ def handle_noargs(self, **options):
9387
"database.\nCreate it with '%s'?"
9488
" (y/[n]): """ % (name, path))
9589
if force or confirm.lower().startswith('y'):
96-
t = Template(name=name,
97-
content=codecs.open(path, "r").read())
90+
with io.open(path, encoding='utf-8') as f:
91+
t = Template(name=name, content=f.read())
9892
t.save()
9993
t.sites.add(site)
10094
else:
@@ -111,21 +105,19 @@ def handle_noargs(self, **options):
111105
if confirm in ('', FILES_TO_DATABASE,
112106
DATABASE_TO_FILES):
113107
if confirm == FILES_TO_DATABASE:
114-
t.content = codecs.open(path, 'r').read()
115-
t.save()
116-
t.sites.add(site)
108+
with io.open(path, encoding='utf-8') as f:
109+
t.content = f.read()
110+
t.save()
111+
t.sites.add(site)
117112
if delete:
118113
try:
119114
os.remove(path)
120115
except OSError:
121116
raise CommandError(
122117
u"Couldn't delete %s" % path)
123118
elif confirm == DATABASE_TO_FILES:
124-
f = codecs.open(path, 'w', 'utf-8')
125-
try:
119+
with io.open(path, 'w', encoding='utf-8') as f:
126120
f.write(t.content)
127-
finally:
128-
f.close()
129121
if delete:
130122
t.delete()
131123
break

0 commit comments

Comments
 (0)