~ubuntu-branches/ubuntu/raring/maas/raring-updates

« back to all changes in this revision

Viewing changes to src/maasserver/models/userprofile.py

  • Committer: Package Import Robot
  • Author(s): Andres Rodriguez
  • Date: 2012-07-03 17:42:37 UTC
  • mfrom: (1.1.13)
  • Revision ID: package-import@ubuntu.com-20120703174237-p8l0keuuznfg721k
Tags: 0.1+bzr709+dfsg-0ubuntu1
* New Upstream release
* debian/control:
  - Depends on python-celery, python-tempita, libjs-yui3-{full,min},
    libjs-raphael
* debian/maas.install:
  - Install apiclient, celeryconfig.py, maas-import-pxe-files, preseeds_v2.
  - Update to install various files from chroot, rather tha manually copy
    them from the source.
* debian/maas.links: symlink celeryconfig.py
* debian/maas.maas-celery.upstart: Add job.
* debian/rules:
  - Install celery upstart job.
  - Do not install jslibs as packages are now used.
  - Drop copying of maas_local_settings_sample.py as source now ships
    a maas_local_settings.py
* debian/patches:
  - 04-maas-http-fix.patch: Drop. Merged upstream.
  - 01-fix-database-settings.patch: Refreshed.
  - 99_enums_js.patch: Added until creation of enum.js / build process
    is fixed.
* debian/maas.postinst: Update bzr version to correctly handle upgrades.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2012 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 
 
4
"""UserProfile model."""
 
5
 
 
6
from __future__ import (
 
7
    absolute_import,
 
8
    print_function,
 
9
    unicode_literals,
 
10
    )
 
11
 
 
12
__metaclass__ = type
 
13
__all__ = [
 
14
    'UserProfile',
 
15
    ]
 
16
 
 
17
 
 
18
from django.contrib.auth.models import User
 
19
from django.db.models import (
 
20
    Manager,
 
21
    Model,
 
22
    OneToOneField,
 
23
    )
 
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
 
29
 
 
30
 
 
31
class UserProfileManager(Manager):
 
32
    """A utility to manage the collection of UserProfile (or User).
 
33
 
 
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).
 
37
    """
 
38
 
 
39
    def all_users(self):
 
40
        """Returns all the "real" users (the users which are not system users
 
41
        and thus have a UserProfile object attached to them).
 
42
 
 
43
        :return: A QuerySet of the users.
 
44
        :rtype: django.db.models.query.QuerySet_
 
45
 
 
46
        .. _django.db.models.query.QuerySet: https://docs.djangoproject.com/
 
47
           en/dev/ref/models/querysets/
 
48
 
 
49
        """
 
50
        user_ids = UserProfile.objects.all().values_list('user', flat=True)
 
51
        return User.objects.filter(id__in=user_ids)
 
52
 
 
53
 
 
54
class UserProfile(CleanSave, Model):
 
55
    """A User profile to store MAAS specific methods and fields.
 
56
 
 
57
    :ivar user: The related User_.
 
58
 
 
59
    .. _UserProfile: https://docs.djangoproject.com/
 
60
       en/dev/topics/auth/
 
61
       #storing-additional-information-about-users
 
62
 
 
63
    """
 
64
 
 
65
    class Meta(DefaultMeta):
 
66
        """Needed for South to recognize this model."""
 
67
 
 
68
    objects = UserProfileManager()
 
69
    user = OneToOneField(User)
 
70
 
 
71
    def delete(self):
 
72
        if self.user.node_set.exists():
 
73
            nb_nodes = self.user.node_set.count()
 
74
            msg = (
 
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()
 
79
        self.user.delete()
 
80
        super(UserProfile, self).delete()
 
81
 
 
82
    def get_authorisation_tokens(self):
 
83
        """Fetches all the user's OAuth tokens.
 
84
 
 
85
        :return: A QuerySet of the tokens.
 
86
        :rtype: django.db.models.query.QuerySet_
 
87
 
 
88
        .. _django.db.models.query.QuerySet: https://docs.djangoproject.com/
 
89
           en/dev/ref/models/querysets/
 
90
 
 
91
        """
 
92
        # Avoid circular imports.
 
93
        from maasserver.models.user import get_auth_tokens
 
94
 
 
95
        return get_auth_tokens(self.user)
 
96
 
 
97
    def create_authorisation_token(self):
 
98
        """Create a new Token and its related Consumer (OAuth authorisation).
 
99
 
 
100
        :return: A tuple containing the Consumer and the Token that were
 
101
            created.
 
102
        :rtype: tuple
 
103
 
 
104
        """
 
105
        # Avoid circular imports.
 
106
        from maasserver.models.user import create_auth_token
 
107
 
 
108
        token = create_auth_token(self.user)
 
109
        return token.consumer, token
 
110
 
 
111
    def delete_authorisation_token(self, token_key):
 
112
        """Delete the user's OAuth token wich key token_key.
 
113
 
 
114
        :param token_key: The key of the token to be deleted.
 
115
        :type token_key: str
 
116
        :raises: django.http.Http404_
 
117
 
 
118
        """
 
119
        token = get_object_or_404(
 
120
            Token, user=self.user, token_type=Token.ACCESS, key=token_key)
 
121
        token.consumer.delete()
 
122
        token.delete()
 
123
 
 
124
    def __unicode__(self):
 
125
        return self.user.username