Merge lp:~stevanr/linaro-license-protection/add-setsuperuser-command into lp:~linaro-automation/linaro-license-protection/trunk

Proposed by Stevan Radaković
Status: Merged
Approved by: Данило Шеган
Approved revision: 111
Merged at revision: 111
Proposed branch: lp:~stevanr/linaro-license-protection/add-setsuperuser-command
Merge into: lp:~linaro-automation/linaro-license-protection/trunk
Diff against target: 81 lines (+52/-0)
3 files modified
license_protected_downloads/management/commands/setsuperuser.py (+23/-0)
license_protected_downloads/tests/__init__.py (+3/-0)
license_protected_downloads/tests/test_custom_commands.py (+26/-0)
To merge this branch: bzr merge lp:~stevanr/linaro-license-protection/add-setsuperuser-command
Reviewer Review Type Date Requested Status
Данило Шеган (community) Approve
Review via email: mp+120879@code.launchpad.net

Description of the change

Add setsuperuser custom command for license_protected_downloads application.

To post a comment you must log in.
Revision history for this message
Данило Шеган (danilo) wrote :

I hate that we are already starting to duplicate this code. Let's thing tomorrow how we can start producing a shared django base for Linaro.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added directory 'license_protected_downloads/management'
2=== added file 'license_protected_downloads/management/__init__.py'
3=== added directory 'license_protected_downloads/management/commands'
4=== added file 'license_protected_downloads/management/commands/__init__.py'
5=== added file 'license_protected_downloads/management/commands/setsuperuser.py'
6--- license_protected_downloads/management/commands/setsuperuser.py 1970-01-01 00:00:00 +0000
7+++ license_protected_downloads/management/commands/setsuperuser.py 2012-08-22 21:52:18 +0000
8@@ -0,0 +1,23 @@
9+from django.core.management.base import BaseCommand, CommandError
10+from django.contrib.auth.models import User
11+
12+
13+class Command(BaseCommand):
14+ args = '<username username ...>'
15+ help = 'Sets specified user as superuser of the ' + \
16+ 'license_protected_downloads admin app'
17+
18+ def handle(self, *args, **options):
19+ for username in args:
20+ self.find_and_update_user(username)
21+ self.stdout.write('Successfully updated user "%s" \n' % username)
22+
23+ def find_and_update_user(self, username):
24+ try:
25+ user = User.objects.get(username=username)
26+ except User.DoesNotExist:
27+ raise CommandError('User "%s" does not exist' % username)
28+
29+ user.is_staff = True
30+ user.is_superuser = True
31+ user.save()
32
33=== modified file 'license_protected_downloads/tests/__init__.py'
34--- license_protected_downloads/tests/__init__.py 2012-07-11 13:08:55 +0000
35+++ license_protected_downloads/tests/__init__.py 2012-08-22 21:52:18 +0000
36@@ -4,6 +4,8 @@
37 from license_protected_downloads.tests.test_pyflakes import TestPyflakes
38 from license_protected_downloads.tests.test_views import ViewTests
39 from license_protected_downloads.tests.test_openid_auth import TestOpenIDAuth
40+from license_protected_downloads.tests.test_custom_commands \
41+ import SetsuperuserCommandTest
42
43 #starts the test suite
44 __test__ = {
45@@ -13,4 +15,5 @@
46 'TestPep8': TestPep8,
47 'TestPyflakes': TestPyflakes,
48 'TestOpenIDAuth': TestOpenIDAuth,
49+ 'SetsuperuserCommandTest': SetsuperuserCommandTest,
50 }
51
52=== added file 'license_protected_downloads/tests/test_custom_commands.py'
53--- license_protected_downloads/tests/test_custom_commands.py 1970-01-01 00:00:00 +0000
54+++ license_protected_downloads/tests/test_custom_commands.py 2012-08-22 21:52:18 +0000
55@@ -0,0 +1,26 @@
56+#!/usr/bin/env python
57+
58+import license_protected_downloads.management.commands.setsuperuser \
59+ as setsuperuser
60+from django.contrib.auth.models import User
61+from django.core.management.base import CommandError
62+from django.test import TestCase
63+
64+
65+class SetsuperuserCommandTest(TestCase):
66+
67+ def setUp(self):
68+ self.command = setsuperuser.Command()
69+
70+ def test_find_and_update_user_non_existing(self):
71+ self.assertRaises(CommandError,
72+ self.command.find_and_update_user,
73+ ("non_existing_user"))
74+
75+ def test_find_and_update_user(self):
76+ user = User(username="existing_user")
77+ user.save()
78+ self.command.find_and_update_user("existing_user")
79+ user = User.objects.get(username="existing_user")
80+ self.assertEquals(user.is_staff, True)
81+ self.assertEquals(user.is_superuser, True)

Subscribers

People subscribed via source and target branches