Commit 983eab0a by Saleem Latif

Create Management Command for adding Site and SiteThemes

parent d5863143
......@@ -130,6 +130,50 @@ Disabling a Theme
-----------------
Theme can be disabled by removing its corresponding ``Site Theme`` entry using django admin.
---------------------------------------
Creating or Updating Site and SiteTheme
---------------------------------------
If you have already setup ``COMPREHENSIVE_THEME_DIRS`` then you can use management command for adding
``Site`` and ``SiteTheme`` directly from the terminal.
.. code-block:: Bash
python manage.py create_or_update_site_theme --site-domain=localhost:8002 --site-name=localhost:8002 --site-theme=my-theme
``create_or_update_site_theme`` accepts the following optional arguments
:--settings: settings file to use, ``default: ecommerce.settings.devstack``
.. code-block:: Bash
python manage.py create_or_update_site_theme --settings=ecommerce.settings.production
:--site-id: id of the site that you want to update
.. code-block:: Bash
# update domain of the site with id 1 and add a new theme ``my-theme`` for this site
python manage.py create_or_update_site_theme --site-id=1 --site-domain=my-theme.localhost:8002 --site-name=my-theme.localhost:8002 --site-theme=my-theme
:--site-domain: domain of the site to be created,
.. code-block:: Bash
python manage.py create_or_update_site_theme --site-domain=localhost:8002 --site-theme=my-theme
:--site-name: Name of the site to be created, ``default: ''``
.. code-block:: Bash
python manage.py create_or_update_site_theme --site-domain=localhost:8002 --site-name=localhost:8002 --site-theme=my-theme
:--site-theme: theme dir for the new theme,
.. code-block:: Bash
python manage.py create_or_update_site_theme --site-domain=localhost:8002 --site-name=localhost:8002 --site-theme=my-theme
--------------------
Compiling Theme Sass
--------------------
......
"""
Creates or updates a Site and Site Theme.
"""
from __future__ import unicode_literals
import logging
from django.contrib.sites.models import Site
from django.core.management import BaseCommand
from ecommerce.theming.models import SiteTheme
logger = logging.getLogger(__name__)
class Command(BaseCommand):
help = 'Create or update Site and SiteTheme'
def add_arguments(self, parser):
parser.add_argument('--site-id',
action='store',
dest='site_id',
type=int,
help='ID of the Site to update.')
parser.add_argument('--site-domain',
action='store',
dest='site_domain',
type=str,
required=True,
help='Domain of the Site to create or update.')
parser.add_argument('--site-name',
action='store',
dest='site_name',
type=str,
default='',
help='Name of the Site to create or update.')
parser.add_argument('--site-theme',
action='store',
dest='site_theme',
type=str,
required=True,
help='Name of the theme to apply to the site.')
def handle(self, *args, **options):
site_id = options.get('site_id')
site_domain = options.get('site_domain')
site_name = options.get('site_name')
site_theme = options.get('site_theme')
try:
site = Site.objects.get(id=site_id)
except Site.DoesNotExist:
site, site_created = Site.objects.get_or_create(domain=site_domain)
if site_created:
logger.info('Site created with domain %s', site_domain)
site.domain = site_domain
if site_name:
site.name = site_name
site.save()
_, created = SiteTheme.objects.update_or_create(
site=site,
defaults={
'theme_dir_name': site_theme,
}
)
logger.info('Site Theme %s with theme "%s"', "created" if created else "updated", site_theme)
"""
Tests for management command for creating or updating site themes.
"""
from django.test import TestCase
from django.core.management import call_command, CommandError
from django.contrib.sites.models import Site
from ecommerce.theming.models import SiteTheme
class TestCreateUpdateSiteTheme(TestCase):
"""
Test django management command for creating or updating site themes.
"""
def test_errors_for_invalid_arguments(self):
"""
Test Error in case of invalid arguments.
"""
# make sure error is raised if no argument is given
with self.assertRaises(CommandError):
call_command("create_or_update_site_theme")
# make sure error is raised if --site-theme is not given
with self.assertRaises(CommandError):
call_command("create_or_update_site_theme", site_domain="test.localhost")
def test_create_site_theme(self):
"""
Test that site theme is created properly.
"""
call_command(
"create_or_update_site_theme",
"--site-domain=test.localhost",
"--site-name=Site Name",
'--site-theme=test',
)
# Verify updated site name
site = Site.objects.get(domain="test.localhost")
site_theme = SiteTheme.objects.get(site=site)
self.assertEqual(site.name, "Site Name")
self.assertEqual(site_theme.theme_dir_name, "test")
def test_update_site(self):
"""
Test that site is updated properly if site-id belongs to an existing site.
"""
# Create a site to update
site = Site.objects.create(domain="test.localhost", name="Test Site")
call_command(
"create_or_update_site_theme",
"--site-id={}".format(site.id),
"--site-name=updated name",
"--site-domain=test.localhost",
'--site-theme=test',
)
# Verify updated site name
site = Site.objects.get(id=site.id)
self.assertEqual(site.name, "updated name")
def test_update_site_theme(self):
"""
Test that site theme is updated properly when site and site theme already exist.
"""
# Create a site and site theme to update
site = Site.objects.create(domain="test.localhost", name="Test Site")
site_theme = SiteTheme.objects.create(site=site, theme_dir_name="site_theme_1")
call_command(
"create_or_update_site_theme",
"--site-domain=test.localhost",
'--site-theme=site_theme_2',
)
# Verify updated site name
site_theme = SiteTheme.objects.get(id=site_theme.id)
self.assertEqual(site_theme.theme_dir_name, "site_theme_2")
......@@ -16,7 +16,7 @@ from ecommerce.theming.management.commands.update_assets import (
class TestUpdateAssets(TestCase):
"""
Test comprehensive theming helper functions.
Test management command for updating/compiling themed assets.
"""
def setUp(self):
super(TestUpdateAssets, self).setUp()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment