Skip to content

Commit

Permalink
Add tests (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
robmoorman authored Nov 30, 2016
1 parent 52ee84e commit 0cbf567
Show file tree
Hide file tree
Showing 25 changed files with 334 additions and 17 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
__pycache__/
.cache/
.coverage
coverage.xml
.DS_Store
*.sqlite3
*.egg-info/
Expand Down
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
sudo: false
language: python
python:
- "2.7"
- "3.5"
install:
- pip uninstall pytest -y
- pip install coveralls
- pip install -e .[test]
script:
- py.test --cov=wagtailthemes --cov-report=xml
- flake8 src/
after_success:
- coveralls
18 changes: 15 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
all: clean install
all: install clean test lint

clean:
find . -name '*.pyc' | xargs rm
find . -type f -name "*.py[co]" -delete
find . -type d -name "__pycache__" -delete

flake8:
flake8 src/

install:
pip install -e .
pip install -e .[test]

isort:
isort --check-only --diff --recursive src/

lint: flake8 isort

test:
py.test
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

Site specific theme loader for Django Wagtail.

[![Build Status](https://travis-ci.org/moorinteractive/wagtail-themes.svg?branch=master)](https://travis-ci.org/moorinteractive/wagtail-themes)
[![Coverage Status](https://coveralls.io/repos/github/moorinteractive/wagtail-themes/badge.svg?branch=master)](https://coveralls.io/github/moorinteractive/wagtail-themes?branch=master)

* Issues: [https://github.com/moorinteractive/wagtail-themes/issues](https://github.com/moorinteractive/wagtail-themes/issues)
* Testing: [https://travis-ci.org/moorinteractive/wagtail-themes](https://travis-ci.org/moorinteractive/wagtail-themes)
* Coverage: [https://coveralls.io/github/moorinteractive/wagtail-themes](https://coveralls.io/github/moorinteractive/wagtail-themes)

![ThemeSettings](docs/screenshot.jpg)

## Example app
Expand Down Expand Up @@ -99,13 +106,17 @@ TEMPLATES = [
]
```

Now select where your themes are stored with `WAGTAIL_THEME_PREFIX` which has
a default value of `themes`.
Now select where your themes are stored with the `WAGTAIL_THEME_PATH` settings
which has a default value of `None`.

```python
THEME_DIR = 'mythemes'
WAGTAIL_THEME_PATH = 'themes'
```

Note that the setting `WAGTAIL_THEME_PATH` is optional. We strongly recommend
using this if you have a large set of themes to keep your template directory
maintainable.

Finally define your to be used themes in the setting `WAGTAIL_THEMES`

```python
Expand All @@ -123,7 +134,7 @@ The `ThemeLoader` class searches for files in your (see settings above) defined
In this case templates files will be found in the following order (for this
example code we have set `brand` as theme in our CMS)

1. /myapp/templates/mythemes/brand/
1. /myapp/templates/themes/brand/
2. /myapp/templates/

Its wise to build your templates as you are used to and only override the
Expand Down
13 changes: 13 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,2 +1,15 @@
[coverage:run]
omit=src/wagtailthemes/tests/*,src/wagtailthemes/migrations/*

[isort]
line_length=80

[flake8]
exclude=src/wagtailthemes/migrations/
max-line-length=80

[tool:pytest]
DJANGO_SETTINGS_MODULE=wagtailthemes.tests.settings

[wheel]
universal=1
14 changes: 12 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
from setuptools import find_packages, setup


Expand All @@ -7,6 +6,15 @@
'wagtail>=1.2'
]

test_require = [
'flake8',
'isort',
'pytest',
'pytest-cov',
'pytest-django',
'wagtail',
]

setup(
name='wagtail-themes',
version='0.1.3',
Expand All @@ -16,11 +24,13 @@
url='https://github.com/moorinteractive/wagtail-themes',
license='MIT',
install_requires=install_requires,
extras_require={
'test': test_require,
},
package_dir={'': 'src'},
packages=find_packages('src'),
include_package_data=True,
classifiers=[
'Development Status :: 4 - Beta',
'Environment :: Web Environment',
'Framework :: Django',
'Operating System :: Unix',
Expand Down
15 changes: 12 additions & 3 deletions src/wagtailthemes/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,25 @@

from wagtailthemes.thread import get_theme

THEME_DIR = getattr(settings, 'THEME_DIR', 'themes')


class ThemeLoader(BaseLoader):
"""
Theme template Loader class for serving optional themes per Wagtail site.
"""
def get_dirs(self):
dirs = super(ThemeLoader, self).get_dirs()
theme = get_theme()
theme_path = getattr(settings, 'WAGTAIL_THEME_PATH', None)

if theme:
theme_dirs = [os.path.join(dir, THEME_DIR, theme) for dir in dirs]
if theme_path:
# Prepend theme path if WAGTAIL_THEME_PATH is set
theme_dirs = [
os.path.join(dir, theme_path, theme) for dir in dirs]
else:
# Append theme for each directory in the DIRS option of the
# TEMPLATES setting
theme_dirs = [os.path.join(dir, theme) for dir in dirs]
return theme_dirs

return dirs
9 changes: 7 additions & 2 deletions src/wagtailthemes/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@

class ThemeMiddleware(MiddlewareMixin):
def process_request(self, request):
if not request.site:
try:
site = request.site
except:
site = None

if not site:
raise ImproperlyConfigured(
"ThemeMiddleware must be added after SiteMiddleware")

theme_settings = ThemeSettings.for_site(request.site)
theme_settings = ThemeSettings.for_site(site)
theme = theme_settings.theme

if theme:
Expand Down
2 changes: 1 addition & 1 deletion src/wagtailthemes/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
# Generated by Django 1.10 on 2016-09-09 13:50
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):
Expand Down
1 change: 0 additions & 1 deletion src/wagtailthemes/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from wagtail.contrib.settings.models import BaseSetting, register_setting
from wagtail.wagtailadmin.edit_handlers import FieldPanel


__ALL__ = ['ThemeSettings']


Expand Down
Empty file.
1 change: 1 addition & 0 deletions src/wagtailthemes/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytest_plugins = 'wagtailthemes.tests.fixtures'
29 changes: 29 additions & 0 deletions src/wagtailthemes/tests/fixtures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import pytest
from django.test import Client
from wagtail.wagtailcore.models import Page, Site

from wagtailthemes.models import ThemeSettings


@pytest.fixture
def page():
page = Page.objects.get(slug='home')
return page


@pytest.fixture
def site():
site = Site.objects.get(is_default_site=True)
return site


@pytest.fixture
def client():
client = Client()
return client


@pytest.fixture
def settings(site):
settings = ThemeSettings.for_site(site)
return settings
Empty file.
29 changes: 29 additions & 0 deletions src/wagtailthemes/tests/integration/test_middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import pytest
from django.core.exceptions import ImproperlyConfigured
from django.test.utils import override_settings

from wagtailthemes.thread import get_theme, set_theme


@pytest.mark.django_db
def test_middleware_not_configured(client):
with override_settings(MIDDLEWARE_CLASSES=[
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'wagtailthemes.middleware.ThemeMiddleware',
'wagtail.wagtailcore.middleware.SiteMiddleware',
]):
with pytest.raises(ImproperlyConfigured):
client.get('/')


@pytest.mark.django_db
def test_middleware_set_theme_for_site(client, site, settings):
settings.theme = 'personal'
settings.save()

set_theme('wagtail')
assert get_theme() == 'wagtail'

client.get('/')
assert get_theme() == 'personal'
123 changes: 123 additions & 0 deletions src/wagtailthemes/tests/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

ALLOWED_HOSTS = [
'testserver'
]

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}

DEBUG = True

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',

'wagtail.wagtailforms',
'wagtail.wagtailredirects',
'wagtail.wagtailembeds',
'wagtail.wagtailsites',
'wagtail.wagtailusers',
'wagtail.wagtailsnippets',
'wagtail.wagtaildocs',
'wagtail.wagtailimages',
'wagtail.wagtailsearch',
'wagtail.wagtailadmin',
'wagtail.wagtailcore',

'modelcluster',
'taggit',

'wagtailthemes',
]

LANGUAGES = [
('en', 'English'),
('nl', 'Dutch'),
]

LANGUAGE_CODE = 'en-us'

LOCALE_PATHS = [
os.path.join(BASE_DIR, 'locale'),
]

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'public/media')

MIDDLEWARE_CLASSES = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'wagtail.wagtailcore.middleware.SiteMiddleware',
'wagtail.wagtailredirects.middleware.RedirectMiddleware',
'wagtailthemes.middleware.ThemeMiddleware',
]

PASSWORD_HASHERS = (
'django.contrib.auth.hashers.MD5PasswordHasher',
)

ROOT_URLCONF = 'wagtailthemes.tests.urls'

SECRET_KEY = '7b&ova34-9b(dj$gevm65$lc!m3#^#g1z*v#gv-g8k0wlo7#l8'

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'public/static')
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'tests', 'templates'),
],
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
'loaders': [
'wagtailthemes.loaders.ThemeLoader',
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
]
},
},
]

TIME_ZONE = 'UTC'

USE_I18N = True
USE_L10N = True
USE_TZ = True

WAGTAIL_SITE_NAME = 'Wagtail Themes'

# Wagtail themes

WAGTAIL_THEME_PATH = None
WAGTAIL_THEMES = [
('brand', 'Brand site'),
('personal', 'Personal site')
]
1 change: 1 addition & 0 deletions src/wagtailthemes/tests/templates/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% block title %}{% endblock %}{% block appendix %}Base{% endblock %}
Loading

0 comments on commit 0cbf567

Please sign in to comment.