launchpad-reviewers team mailing list archive
-
launchpad-reviewers team
-
Mailing list archive
-
Message #08809
[Merge] lp:~jtv/maas/migrate-user-helpers into lp:maas
Jeroen T. Vermeulen has proposed merging lp:~jtv/maas/migrate-user-helpers into lp:maas.
Requested reviews:
Launchpad code reviewers (launchpad-reviewers)
For more details, see:
https://code.launchpad.net/~jtv/maas/migrate-user-helpers/+merge/110245
As planned, but a day late (sorry!) this branch moves some MAAS-specific helpers and details related to User into a module of their own.
Most of the diff is for changes in imports: I'm not re-exporting these helpers directly in the maasserver.models package, because they're not really models. So they now need to be imported from the new module they're in.
Jeroen
--
https://code.launchpad.net/~jtv/maas/migrate-user-helpers/+merge/110245
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~jtv/maas/migrate-user-helpers into lp:maas.
=== modified file 'src/maasserver/models/__init__.py'
--- src/maasserver/models/__init__.py 2012-06-14 05:52:58 +0000
+++ src/maasserver/models/__init__.py 2012-06-14 06:58:19 +0000
@@ -74,9 +74,9 @@
from maasserver.models.nodegroup import NodeGroup
from maasserver.models.sshkey import SSHKey
from maasserver.models.timestampedmodel import TimestampedModel
+from maasserver.models.user import create_user
from maasserver.models.userprofile import UserProfile
from maasserver.utils import ignore_unused
-from metadataserver import nodeinituser
from piston.models import (
Consumer,
Token,
@@ -88,17 +88,9 @@
from provisioningserver.tasks import power_on
# Scheduled for model migration on 2012-06-15
-# Suppress warning about NodeGroup being imported, but only used for
+# Suppress warning about symbols being imported, but only used for
# export in __all__.
-ignore_unused(NodeGroup)
-
-
-# Scheduled for model migration on 2012-06-13
-# Special users internal to MAAS.
-SYSTEM_USERS = [
- # For nodes' access to the metadata API:
- nodeinituser.user_name,
- ]
+ignore_unused(NodeGroup, UserProfile)
# Scheduled for model migration on 2012-06-15
@@ -639,61 +631,6 @@
self.save()
-# Scheduled for model migration on 2012-06-13
-GENERIC_CONSUMER = 'MAAS consumer'
-
-
-# Scheduled for model migration on 2012-06-13
-def create_auth_token(user):
- """Create new Token and Consumer (OAuth authorisation) for `user`.
-
- :param user: The user to create a token for.
- :type user: User
- :return: The created Token.
- :rtype: piston.models.Token
-
- """
- consumer = Consumer.objects.create(
- user=user, name=GENERIC_CONSUMER, status='accepted')
- consumer.generate_random_codes()
- # This is a 'generic' consumer aimed to service many clients, hence
- # we don't authenticate the consumer with key/secret key.
- consumer.secret = ''
- consumer.save()
- token = Token.objects.create(
- user=user, token_type=Token.ACCESS, consumer=consumer,
- is_approved=True)
- token.generate_random_codes()
- return token
-
-
-# Scheduled for model migration on 2012-06-13
-def get_auth_tokens(user):
- """Fetches all the user's OAuth tokens.
-
- :return: A QuerySet of the tokens.
- :rtype: django.db.models.query.QuerySet_
-
- .. _django.db.models.query.QuerySet: https://docs.djangoproject.com/
- en/dev/ref/models/querysets/
-
- """
- return Token.objects.select_related().filter(
- user=user, token_type=Token.ACCESS, is_approved=True).order_by('id')
-
-
-# Scheduled for model migration on 2012-06-13
-# When a user is created: create the related profile and the default
-# consumer/token.
-def create_user(sender, instance, created, **kwargs):
- # System users do not have profiles.
- if created and instance.username not in SYSTEM_USERS:
- # Create related UserProfile.
- profile = UserProfile.objects.create(user=instance)
-
- # Create initial authorisation token.
- profile.create_authorisation_token()
-
# Scheduled for model migration on 2012-06-15
# Connect the 'create_user' method to the post save signal of User.
post_save.connect(create_user, sender=User)
=== added file 'src/maasserver/models/user.py'
--- src/maasserver/models/user.py 1970-01-01 00:00:00 +0000
+++ src/maasserver/models/user.py 2012-06-14 06:58:19 +0000
@@ -0,0 +1,83 @@
+# Copyright 2012 Canonical Ltd. This software is licensed under the
+# GNU Affero General Public License version 3 (see the file LICENSE).
+
+"""MAAS-specific helpers for :class:`User`."""
+
+from __future__ import (
+ absolute_import,
+ print_function,
+ unicode_literals,
+ )
+
+__metaclass__ = type
+__all__ = [
+ 'create_auth_token',
+ 'create_user',
+ 'get_auth_tokens',
+ ]
+
+from metadataserver import nodeinituser
+from piston.models import (
+ Consumer,
+ Token,
+ )
+
+# Special users internal to MAAS.
+SYSTEM_USERS = [
+ # For nodes' access to the metadata API:
+ nodeinituser.user_name,
+ ]
+
+GENERIC_CONSUMER = 'MAAS consumer'
+
+
+def create_auth_token(user):
+ """Create new Token and Consumer (OAuth authorisation) for `user`.
+
+ :param user: The user to create a token for.
+ :type user: User
+ :return: The created Token.
+ :rtype: piston.models.Token
+
+ """
+ consumer = Consumer.objects.create(
+ user=user, name=GENERIC_CONSUMER, status='accepted')
+ consumer.generate_random_codes()
+ # This is a 'generic' consumer aimed to service many clients, hence
+ # we don't authenticate the consumer with key/secret key.
+ consumer.secret = ''
+ consumer.save()
+ token = Token.objects.create(
+ user=user, token_type=Token.ACCESS, consumer=consumer,
+ is_approved=True)
+ token.generate_random_codes()
+ return token
+
+
+def get_auth_tokens(user):
+ """Fetches all the user's OAuth tokens.
+
+ :return: A QuerySet of the tokens.
+ :rtype: django.db.models.query.QuerySet_
+
+ .. _django.db.models.query.QuerySet: https://docs.djangoproject.com/
+ en/dev/ref/models/querysets/
+
+ """
+ return Token.objects.select_related().filter(
+ user=user, token_type=Token.ACCESS, is_approved=True).order_by('id')
+
+
+# When a user is created: create the related profile and the default
+# consumer/token.
+def create_user(sender, instance, created, **kwargs):
+ # Avoid circular imports.
+ from maasserver.models.userprofile import UserProfile
+
+ # System users do not have profiles.
+ if created and instance.username not in SYSTEM_USERS:
+ # Create related UserProfile.
+ profile = UserProfile.objects.create(user=instance)
+
+ # Create initial authorisation token.
+ profile.create_authorisation_token()
=== modified file 'src/maasserver/models/userprofile.py'
--- src/maasserver/models/userprofile.py 2012-05-31 07:21:13 +0000
+++ src/maasserver/models/userprofile.py 2012-06-14 06:58:19 +0000
@@ -90,7 +90,7 @@
"""
# Avoid circular imports.
- from maasserver.models import get_auth_tokens
+ from maasserver.models.user import get_auth_tokens
return get_auth_tokens(self.user)
@@ -103,7 +103,7 @@
"""
# Avoid circular imports.
- from maasserver.models import create_auth_token
+ from maasserver.models.user import create_auth_token
token = create_auth_token(self.user)
return token.consumer, token
=== modified file 'src/maasserver/testing/factory.py'
--- src/maasserver/testing/factory.py 2012-06-05 08:08:15 +0000
+++ src/maasserver/testing/factory.py 2012-06-14 06:58:19 +0000
@@ -24,7 +24,6 @@
NODE_STATUS,
)
from maasserver.models import (
- create_auth_token,
FileStorage,
MACAddress,
Node,
@@ -32,6 +31,7 @@
NodeGroup,
SSHKey,
)
+from maasserver.models.user import create_auth_token
from maasserver.testing import (
get_data,
reload_object,
=== modified file 'src/maasserver/testing/oauthclient.py'
--- src/maasserver/testing/oauthclient.py 2012-04-16 10:00:51 +0000
+++ src/maasserver/testing/oauthclient.py 2012-06-14 06:58:19 +0000
@@ -17,7 +17,7 @@
from time import time
from django.test.client import Client
-from maasserver.models import get_auth_tokens
+from maasserver.models.user import get_auth_tokens
from oauth.oauth import (
generate_nonce,
OAuthConsumer,
=== modified file 'src/maasserver/tests/test_api.py'
--- src/maasserver/tests/test_api.py 2012-06-11 08:46:01 +0000
+++ src/maasserver/tests/test_api.py 2012-06-14 06:58:19 +0000
@@ -50,10 +50,12 @@
from maasserver.exceptions import Unauthorized
from maasserver.models import (
Config,
+ MACAddress,
+ Node,
+ )
+from maasserver.models.user import (
create_auth_token,
get_auth_tokens,
- MACAddress,
- Node,
)
from maasserver.testing import (
reload_object,
=== modified file 'src/maasserver/tests/test_models.py'
--- src/maasserver/tests/test_models.py 2012-06-07 11:44:14 +0000
+++ src/maasserver/tests/test_models.py 2012-06-14 06:58:19 +0000
@@ -32,14 +32,16 @@
from maasserver.exceptions import NodeStateViolation
from maasserver.models import (
Config,
- create_auth_token,
- get_auth_tokens,
get_db_state,
MACAddress,
Node,
NODE_TRANSITIONS,
now,
)
+from maasserver.models.user import (
+ create_auth_token,
+ get_auth_tokens,
+ )
from maasserver.provisioning import get_provisioning_api_proxy
from maasserver.testing import reload_object
from maasserver.testing.factory import factory
=== modified file 'src/maasserver/tests/test_userprofile.py'
--- src/maasserver/tests/test_userprofile.py 2012-06-04 23:20:26 +0000
+++ src/maasserver/tests/test_userprofile.py 2012-06-14 06:58:19 +0000
@@ -14,11 +14,11 @@
from django.contrib.auth.models import User
from maasserver.exceptions import CannotDeleteUserException
-from maasserver.models import (
+from maasserver.models import UserProfile
+from maasserver.models.user import (
GENERIC_CONSUMER,
get_auth_tokens,
SYSTEM_USERS,
- UserProfile,
)
from maasserver.testing.factory import factory
from maasserver.testing.testcase import TestCase
=== modified file 'src/metadataserver/models/__init__.py'
--- src/metadataserver/models/__init__.py 2012-05-08 02:57:56 +0000
+++ src/metadataserver/models/__init__.py 2012-06-14 06:58:19 +0000
@@ -26,11 +26,9 @@
Model,
)
from django.shortcuts import get_object_or_404
-from maasserver.models import (
- create_auth_token,
- Node,
- )
+from maasserver.models import Node
from maasserver.models.cleansave import CleanSave
+from maasserver.models.user import create_auth_token
from metadataserver.fields import (
Bin,
BinaryField,