← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~jtv/maas/migrate-userprofile into lp:maas

 

Jeroen T. Vermeulen has proposed merging lp:~jtv/maas/migrate-userprofile into lp:maas.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~jtv/maas/migrate-userprofile/+merge/108111

As per the migration plan.

You'll note two import statements inside methods, of a pair of functions that manipulate auth tokens.  This was always foreseen in the migration plan, but we could conceivably fix this by extracting those functions into their own module.  That's not a job for this migration branch though, since the functions operate on User — not on UserProfile which is the class being moved here.


Jeroen
-- 
https://code.launchpad.net/~jtv/maas/migrate-userprofile/+merge/108111
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~jtv/maas/migrate-userprofile into lp:maas.
=== modified file 'src/maasserver/models/__init__.py'
--- src/maasserver/models/__init__.py	2012-05-30 12:24:57 +0000
+++ src/maasserver/models/__init__.py	2012-05-31 07:35:23 +0000
@@ -49,8 +49,6 @@
     ForeignKey,
     IntegerField,
     Manager,
-    Model,
-    OneToOneField,
     Q,
     )
 from django.db.models.signals import post_save
@@ -66,10 +64,7 @@
     NODE_STATUS_CHOICES,
     NODE_STATUS_CHOICES_DICT,
     )
-from maasserver.exceptions import (
-    CannotDeleteUserException,
-    NodeStateViolation,
-    )
+from maasserver.exceptions import NodeStateViolation
 from maasserver.fields import (
     JSONObjectField,
     MACAddressField,
@@ -79,6 +74,7 @@
 from maasserver.models.filestorage import FileStorage
 from maasserver.models.sshkey import SSHKey
 from maasserver.models.timestampedmodel import TimestampedModel
+from maasserver.models.userprofile import UserProfile
 from metadataserver import nodeinituser
 from piston.models import (
     Consumer,
@@ -695,99 +691,6 @@
         user=user, token_type=Token.ACCESS, is_approved=True).order_by('id')
 
 
-# Scheduled for model migration on 2012-06-01
-class UserProfileManager(Manager):
-    """A utility to manage the collection of UserProfile (or User).
-
-    This should be used when dealing with UserProfiles or Users because it
-    returns only users with a profile attached to them (as opposed to system
-    users who don't have a profile).
-    """
-
-    def all_users(self):
-        """Returns all the "real" users (the users which are not system users
-        and thus have a UserProfile object attached to them).
-
-        :return: A QuerySet of the users.
-        :rtype: django.db.models.query.QuerySet_
-
-        .. _django.db.models.query.QuerySet: https://docs.djangoproject.com/
-           en/dev/ref/models/querysets/
-
-        """
-        user_ids = UserProfile.objects.all().values_list('user', flat=True)
-        return User.objects.filter(id__in=user_ids)
-
-
-# Scheduled for model migration on 2012-06-01
-class UserProfile(CleanSave, Model):
-    """A User profile to store MAAS specific methods and fields.
-
-    :ivar user: The related User_.
-
-    .. _UserProfile: https://docs.djangoproject.com/
-       en/dev/topics/auth/
-       #storing-additional-information-about-users
-
-    """
-
-    class Meta(DefaultMeta):
-        """Needed for South to recognize this model."""
-
-    objects = UserProfileManager()
-    user = OneToOneField(User)
-
-    def delete(self):
-        if self.user.node_set.exists():
-            nb_nodes = self.user.node_set.count()
-            msg = (
-                "User %s cannot be deleted: it still has %d node(s) "
-                "deployed." % (self.user.username, nb_nodes))
-            raise CannotDeleteUserException(msg)
-        self.user.consumers.all().delete()
-        self.user.delete()
-        super(UserProfile, self).delete()
-
-    def get_authorisation_tokens(self):
-        """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 get_auth_tokens(self.user)
-
-    def create_authorisation_token(self):
-        """Create a new Token and its related Consumer (OAuth authorisation).
-
-        :return: A tuple containing the Consumer and the Token that were
-            created.
-        :rtype: tuple
-
-        """
-        token = create_auth_token(self.user)
-        return token.consumer, token
-
-    def delete_authorisation_token(self, token_key):
-        """Delete the user's OAuth token wich key token_key.
-
-        :param token_key: The key of the token to be deleted.
-        :type token_key: str
-        :raises: django.http.Http404_
-
-        """
-        token = get_object_or_404(
-            Token, user=self.user, token_type=Token.ACCESS, key=token_key)
-        token.consumer.delete()
-        token.delete()
-
-    def __unicode__(self):
-        return self.user.username
-
-
 # When a user is created: create the related profile and the default
 # consumer/token.
 def create_user(sender, instance, created, **kwargs):

=== added file 'src/maasserver/models/userprofile.py'
--- src/maasserver/models/userprofile.py	1970-01-01 00:00:00 +0000
+++ src/maasserver/models/userprofile.py	2012-05-31 07:35:23 +0000
@@ -0,0 +1,125 @@
+# Copyright 2012 Canonical Ltd.  This software is licensed under the
+# GNU Affero General Public License version 3 (see the file LICENSE).
+
+"""UserProfile model."""
+
+from __future__ import (
+    absolute_import,
+    print_function,
+    unicode_literals,
+    )
+
+__metaclass__ = type
+__all__ = [
+    'UserProfile',
+    ]
+
+
+from django.contrib.auth.models import User
+from django.db.models import (
+    Manager,
+    Model,
+    OneToOneField,
+    )
+from django.shortcuts import get_object_or_404
+from maasserver import DefaultMeta
+from maasserver.exceptions import CannotDeleteUserException
+from maasserver.models.cleansave import CleanSave
+from piston.models import Token
+
+
+class UserProfileManager(Manager):
+    """A utility to manage the collection of UserProfile (or User).
+
+    This should be used when dealing with UserProfiles or Users because it
+    returns only users with a profile attached to them (as opposed to system
+    users who don't have a profile).
+    """
+
+    def all_users(self):
+        """Returns all the "real" users (the users which are not system users
+        and thus have a UserProfile object attached to them).
+
+        :return: A QuerySet of the users.
+        :rtype: django.db.models.query.QuerySet_
+
+        .. _django.db.models.query.QuerySet: https://docs.djangoproject.com/
+           en/dev/ref/models/querysets/
+
+        """
+        user_ids = UserProfile.objects.all().values_list('user', flat=True)
+        return User.objects.filter(id__in=user_ids)
+
+
+class UserProfile(CleanSave, Model):
+    """A User profile to store MAAS specific methods and fields.
+
+    :ivar user: The related User_.
+
+    .. _UserProfile: https://docs.djangoproject.com/
+       en/dev/topics/auth/
+       #storing-additional-information-about-users
+
+    """
+
+    class Meta(DefaultMeta):
+        """Needed for South to recognize this model."""
+
+    objects = UserProfileManager()
+    user = OneToOneField(User)
+
+    def delete(self):
+        if self.user.node_set.exists():
+            nb_nodes = self.user.node_set.count()
+            msg = (
+                "User %s cannot be deleted: it still has %d node(s) "
+                "deployed." % (self.user.username, nb_nodes))
+            raise CannotDeleteUserException(msg)
+        self.user.consumers.all().delete()
+        self.user.delete()
+        super(UserProfile, self).delete()
+
+    def get_authorisation_tokens(self):
+        """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/
+
+        """
+        # Avoid circular imports.
+        from maasserver.models import get_auth_tokens
+
+        return get_auth_tokens(self.user)
+
+    def create_authorisation_token(self):
+        """Create a new Token and its related Consumer (OAuth authorisation).
+
+        :return: A tuple containing the Consumer and the Token that were
+            created.
+        :rtype: tuple
+
+        """
+        # Avoid circular imports.
+        from maasserver.models import create_auth_token
+
+        token = create_auth_token(self.user)
+        return token.consumer, token
+
+    def delete_authorisation_token(self, token_key):
+        """Delete the user's OAuth token wich key token_key.
+
+        :param token_key: The key of the token to be deleted.
+        :type token_key: str
+        :raises: django.http.Http404_
+
+        """
+        token = get_object_or_404(
+            Token, user=self.user, token_type=Token.ACCESS, key=token_key)
+        token.consumer.delete()
+        token.delete()
+
+    def __unicode__(self):
+        return self.user.username