Commit 9ff495fc by Hasnain Committed by Hasnain Naveed

WL-1325 | Management command for creating Sites, SiteThemes, SiteConfigurations and Partners.

parent 949c4b40
""" This command creates Sites, SiteThemes, SiteConfigurations and partners."""
from __future__ import unicode_literals
import fnmatch
import json
import logging
import os
from django.contrib.sites.models import Site
from django.core.management import BaseCommand
from oscar.core.loading import get_model
from ecommerce.core.models import SiteConfiguration
from ecommerce.theming.models import SiteTheme
logger = logging.getLogger(__name__)
Partner = get_model('partner', 'Partner')
class Command(BaseCommand):
"""Creates Sites, SiteThemes, SiteConfigurations and partners."""
help = 'Creates Sites, SiteThemes, SiteConfigurations and partners.'
dns_name = None
theme_path = None
def add_arguments(self, parser):
parser.add_argument(
"--dns-name",
type=str,
help="Enter DNS name of sandbox.",
required=True
)
parser.add_argument(
"--theme-path",
type=str,
help="Enter theme directory path",
required=True
)
def _create_sites(self, site_domain, theme_dir_name, site_configuration, partner_code):
"""
Create Sites, SiteThemes and SiteConfigurations
"""
site, _ = Site.objects.get_or_create(
domain=site_domain,
defaults={"name": theme_dir_name}
)
logger.info('Creating %s SiteTheme', site_domain)
SiteTheme.objects.get_or_create(
site=site,
theme_dir_name=theme_dir_name
)
logger.info('Creating %s Partner', site_domain)
partner, _ = Partner.objects.get_or_create(
short_code=partner_code,
defaults={
"name": partner_code
}
)
logger.info('Creating %s SiteConfiguration', site_domain)
SiteConfiguration.objects.get_or_create(
site=site,
partner=partner,
defaults=site_configuration
)
def find(self, pattern, path):
"""
Matched the given pattern in given path and returns the list of matching files
"""
result = []
for root, dirs, files in os.walk(path): # pylint: disable=unused-variable
for name in files:
if fnmatch.fnmatch(name, pattern):
result.append(os.path.join(root, name))
return result
def _get_site_partner_data(self):
"""
Reads the json files from theme directory and returns the site partner data in JSON format.
"""
site_data = {}
for config_file in self.find('sandbox_configuration.json', self.theme_path):
logger.info('Reading file from %s', config_file)
configuration_data = json.loads(
json.dumps(
json.load(
open(config_file)
)
).replace("{dns_name}", self.dns_name)
)['ecommerce_configuration']
site_data[configuration_data['site_partner']] = {
"partner_code": configuration_data['site_partner'],
"site_domain": configuration_data['site_domain'],
"theme_dir_name": configuration_data['theme_dir_name'],
"configuration": configuration_data['configuration']
}
return site_data
def handle(self, *args, **options):
self.dns_name = options['dns_name']
self.theme_path = options['theme_path']
logger.info('DNS name: %s', self.dns_name)
logger.info('Theme path: %s', self.theme_path)
all_sites = self._get_site_partner_data()
for site_name, site_data in all_sites.items():
logger.info('Creating %s Site', site_name)
self._create_sites(
site_data['site_domain'],
site_data['theme_dir_name'],
site_data['configuration'],
site_data['partner_code']
)
{
"ecommerce_configuration":{
"site_partner": "dummy",
"theme_dir_name": "dummy.dir",
"site_domain": "ecommerce-dummy-{dns_name}.example.com",
"configuration": {
"lms_url_root": "https://dummy-{dns_name}.example.com",
"payment_processors": "dummy-method",
"client_side_payment_processor": "dummy-method",
"enable_enrollment_codes": true,
"oauth_settings": {
"dummy-key": "dummy-value"
},
"payment_support_email": "dummy@example.com",
"payment_support_url": "https://dummy-{dns_name}.example.com/contact",
"discovery_api_url": "https://discovery-dummy-{dns_name}.example.com/api/v1/"
}
}
}
"""
Tests for management command for creating Sites, SiteThemes, SiteConfigurations and Partners.
"""
import logging
import os
from django.contrib.sites.models import Site
from django.core.management import CommandError, call_command
from django.test import TestCase
from oscar.core.loading import get_model
from ecommerce.core.models import SiteConfiguration
from ecommerce.theming.models import SiteTheme
logger = logging.getLogger(__name__)
Partner = get_model('partner', 'Partner')
SITES = ["dummy"]
class TestCreateSitesAndPartners(TestCase):
"""
Test django management command for creating Sites, SiteThemes, SiteConfigurations and Partners.
"""
def setUp(self):
super(TestCreateSitesAndPartners, self).setUp()
self.dns_name = "dummy-dns"
self.theme_path = os.path.dirname(__file__)
def _assert_sites_data_is_valid(self):
sites = Site.objects.all()
partners = Partner.objects.all()
# There is an extra default site.
self.assertEqual(len(sites), len(SITES) + 1)
self.assertEqual(len(partners), len(SITES))
for site in sites:
if site.name in SITES:
site_name = site.name
self.assertEqual(
site.domain,
"ecommerce-{site}-{dns_name}.example.com".format(site=site_name, dns_name=self.dns_name)
)
site_theme = SiteTheme.objects.get(site=site)
self.assertEqual(
site_theme.theme_dir_name,
"dummy.dir"
)
site_config = SiteConfiguration.objects.get(site=site)
self.assertEqual(
site_config.partner.short_code,
"dummy"
)
self.assertEqual(
site_config.lms_url_root,
"ecommerce-dummy-{dns_name}.example.com".format(dns_name=self.dns_name)
)
self.assertTrue(site_config.enable_enrollment_codes)
self.assertEqual(
site_config.payment_support_email,
"dummy@example.com"
)
self.assertEqual(
site_config.payment_support_url,
"https://dummy-{dns_name}.example.com/contact".format(dns_name=self.dns_name),
)
self.assertEqual(
site_config.discovery_api_url,
"https://discovery-dummy-{dns_name}.example.com/api/v1/".format(dns_name=self.dns_name)
)
self.assertEqual(
site_config.client_side_payment_processor,
"dummy-method"
)
self.assertDictEqual(
site_config.oauth_settings,
{
"dummy-key": "dummy-value"
}
)
def test_missing_required_arguments(self):
"""
Verify CommandError is raised when required arguments are missing.
"""
# If a required argument is not specified the system should raise a CommandError
with self.assertRaises(CommandError):
call_command(
"create_sites_and_partners",
"--dns-name", self.dns_name,
)
with self.assertRaises(CommandError):
call_command(
"create_sites_and_partners",
"--theme-path", self.theme_path,
)
def test_create_site_and_partner(self):
"""
Verify that command creates sites and Partners
"""
call_command(
"create_sites_and_partners",
"--dns-name", self.dns_name,
"--theme-path", self.theme_path
)
self._assert_sites_data_is_valid()
call_command(
"create_sites_and_partners",
"--dns-name", self.dns_name,
"--theme-path", self.theme_path
)
# if we run command with same dns then it will not duplicates the sites and partners.
self._assert_sites_data_is_valid()
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