1
# Copyright 2012 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
4
"""UserProfile model."""
6
from __future__ import (
18
from django.contrib.auth.models import User
19
from django.db.models import (
24
from django.shortcuts import get_object_or_404
25
from maasserver import DefaultMeta
26
from maasserver.exceptions import CannotDeleteUserException
27
from maasserver.models.cleansave import CleanSave
28
from piston.models import Token
31
class UserProfileManager(Manager):
32
"""A utility to manage the collection of UserProfile (or User).
34
This should be used when dealing with UserProfiles or Users because it
35
returns only users with a profile attached to them (as opposed to system
36
users who don't have a profile).
40
"""Returns all the "real" users (the users which are not system users
41
and thus have a UserProfile object attached to them).
43
:return: A QuerySet of the users.
44
:rtype: django.db.models.query.QuerySet_
46
.. _django.db.models.query.QuerySet: https://docs.djangoproject.com/
47
en/dev/ref/models/querysets/
50
user_ids = UserProfile.objects.all().values_list('user', flat=True)
51
return User.objects.filter(id__in=user_ids)
54
class UserProfile(CleanSave, Model):
55
"""A User profile to store MAAS specific methods and fields.
57
:ivar user: The related User_.
59
.. _UserProfile: https://docs.djangoproject.com/
61
#storing-additional-information-about-users
65
class Meta(DefaultMeta):
66
"""Needed for South to recognize this model."""
68
objects = UserProfileManager()
69
user = OneToOneField(User)
72
if self.user.node_set.exists():
73
nb_nodes = self.user.node_set.count()
75
"User %s cannot be deleted: it still has %d node(s) "
76
"deployed." % (self.user.username, nb_nodes))
77
raise CannotDeleteUserException(msg)
78
self.user.consumers.all().delete()
80
super(UserProfile, self).delete()
82
def get_authorisation_tokens(self):
83
"""Fetches all the user's OAuth tokens.
85
:return: A QuerySet of the tokens.
86
:rtype: django.db.models.query.QuerySet_
88
.. _django.db.models.query.QuerySet: https://docs.djangoproject.com/
89
en/dev/ref/models/querysets/
92
# Avoid circular imports.
93
from maasserver.models.user import get_auth_tokens
95
return get_auth_tokens(self.user)
97
def create_authorisation_token(self):
98
"""Create a new Token and its related Consumer (OAuth authorisation).
100
:return: A tuple containing the Consumer and the Token that were
105
# Avoid circular imports.
106
from maasserver.models.user import create_auth_token
108
token = create_auth_token(self.user)
109
return token.consumer, token
111
def delete_authorisation_token(self, token_key):
112
"""Delete the user's OAuth token wich key token_key.
114
:param token_key: The key of the token to be deleted.
116
:raises: django.http.Http404_
119
token = get_object_or_404(
120
Token, user=self.user, token_type=Token.ACCESS, key=token_key)
121
token.consumer.delete()
124
def __unicode__(self):
125
return self.user.username