Skip to content

Commit

Permalink
Merge f98307a into ff10072
Browse files Browse the repository at this point in the history
  • Loading branch information
kdmccormick committed Jul 17, 2019
2 parents ff10072 + f98307a commit ca22883
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
2 changes: 1 addition & 1 deletion organizations/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""
edx-organizations app initialization module
"""
__version__ = '2.0.3' # pragma: no cover
__version__ = '2.1.0' # pragma: no cover
38 changes: 38 additions & 0 deletions organizations/management/commands/add_organization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""
Add an organization through manage.py.
"""
import logging

from django.core.management import BaseCommand, CommandError

from organizations import api


logger = logging.getLogger(__name__)


class Command(BaseCommand):
"""Management command used to add an organization.
Example: ./manage.py add_organization hogwarts "Hogwarts School of Witchcraft and Wizardry"
"""

def add_arguments(self, parser):
parser.add_argument('short_name')
parser.add_argument('name')

def handle(self, *args, **options):
if len(options['short_name']) > len(options['name']):
fmt = (
"The provided short_name ({short_name}) "
"is longer than the provided name ({name}). "
"You probably want to switch the order of the arguments."
)
raise CommandError(fmt.format(**options))

created_org = api.add_organization(options)
log_fmt = (
"Created or activated organization: id={id}, "
"short_name={short_name}, name='{name}'"
)
logger.info(log_fmt.format(**created_org))
45 changes: 45 additions & 0 deletions organizations/management/commands/tests/test_add_organization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""
Tests for organization-adding management command.
"""
from unittest import TestCase

from django.core.management import call_command, CommandError

from organizations.models import Organization


class TestAddOrganizationCommand(TestCase):
""" Tests for add_organization.Command. """

ORG_SHORT_NAME = "msw"
ORG_NAME = "Ministry of Silly Walks"

def assert_one_active_organization(self, short_name, name):
"""
Assert that there is exactly one organization with the given short_name,
and if so, that it has the specified name and is active.
"""
# Fails with MultipleObjectsReturned if >1 orgs matching `short_name`
org = Organization.objects.get(short_name=short_name)
self.assertEqual(org.name, name)
self.assertTrue(org.active)

def test_add_org(self):
call_command('add_organization', self.ORG_SHORT_NAME, self.ORG_NAME)
self.assert_one_active_organization(self.ORG_SHORT_NAME, self.ORG_NAME)

def test_add_same_org_twice(self):
call_command('add_organization', self.ORG_SHORT_NAME, self.ORG_NAME)
call_command('add_organization', self.ORG_SHORT_NAME, self.ORG_NAME)
self.assert_one_active_organization(self.ORG_SHORT_NAME, self.ORG_NAME)

def test_add_existing_org(self):
Organization.objects.create(
short_name=self.ORG_SHORT_NAME, name=self.ORG_NAME, active=False
)
call_command('add_organization', self.ORG_SHORT_NAME, self.ORG_NAME)
self.assert_one_active_organization(self.ORG_SHORT_NAME, self.ORG_NAME)

def test_bad_argument_order(self):
with self.assertRaises(CommandError):
call_command('add_organization', self.ORG_NAME, self.ORG_SHORT_NAME)

0 comments on commit ca22883

Please sign in to comment.