-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
84 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
45
organizations/management/commands/tests/test_add_organization.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |