~ubuntu-branches/ubuntu/precise/horizon/precise-updates

« back to all changes in this revision

Viewing changes to .pc/git-2012.1~e2~20111201.1077.patch/horizon/horizon/dashboards/syspanel/users/forms.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2011-12-09 16:18:55 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20111209161855-nguyenpghx2o2lqy
Tags: 2012.1~e2~20111209.1104-0ubuntu1
* New upstream release.
* Refreshed patches.
* debian/docs: Removed README

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
 
 
3
 
# Copyright 2011 United States Government as represented by the
4
 
# Administrator of the National Aeronautics and Space Administration.
5
 
# All Rights Reserved.
6
 
#
7
 
# Copyright 2011 Nebula, Inc.
8
 
#
9
 
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
10
 
#    not use this file except in compliance with the License. You may obtain
11
 
#    a copy of the License at
12
 
#
13
 
#         http://www.apache.org/licenses/LICENSE-2.0
14
 
#
15
 
#    Unless required by applicable law or agreed to in writing, software
16
 
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17
 
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18
 
#    License for the specific language governing permissions and limitations
19
 
#    under the License.
20
 
 
21
 
import logging
22
 
 
23
 
from django import shortcuts
24
 
from django.contrib import messages
25
 
from django.utils.translation import ugettext as _
26
 
from openstackx.api import exceptions as api_exceptions
27
 
 
28
 
from horizon import api
29
 
from horizon import forms
30
 
 
31
 
 
32
 
LOG = logging.getLogger(__name__)
33
 
 
34
 
 
35
 
class UserForm(forms.Form):
36
 
    def __init__(self, *args, **kwargs):
37
 
        tenant_list = kwargs.pop('tenant_list', None)
38
 
        super(UserForm, self).__init__(*args, **kwargs)
39
 
        self.fields['tenant_id'].choices = [[tenant.id, tenant.name]
40
 
                for tenant in tenant_list]
41
 
 
42
 
    name = forms.CharField(label=_("Name"))
43
 
    email = forms.CharField(label=_("Email"))
44
 
    password = forms.CharField(label=_("Password"),
45
 
                               widget=forms.PasswordInput(render_value=False),
46
 
                               required=False)
47
 
    tenant_id = forms.ChoiceField(label=_("Primary Tenant"))
48
 
 
49
 
 
50
 
class UserUpdateForm(forms.Form):
51
 
    def __init__(self, *args, **kwargs):
52
 
        tenant_list = kwargs.pop('tenant_list', None)
53
 
        super(UserUpdateForm, self).__init__(*args, **kwargs)
54
 
        self.fields['tenant_id'].choices = [[tenant.id, tenant.name]
55
 
                for tenant in tenant_list]
56
 
 
57
 
    id = forms.CharField(label=_("ID"),
58
 
            widget=forms.TextInput(attrs={'readonly': 'readonly'}))
59
 
    # FIXME: keystone doesn't return the username from a get API call.
60
 
    #name = forms.CharField(label=_("Name"))
61
 
    email = forms.CharField(label=_("Email"))
62
 
    password = forms.CharField(label=_("Password"),
63
 
                               widget=forms.PasswordInput(render_value=False),
64
 
                               required=False)
65
 
    tenant_id = forms.ChoiceField(label=_("Primary Tenant"))
66
 
 
67
 
 
68
 
class UserDeleteForm(forms.SelfHandlingForm):
69
 
    user = forms.CharField(required=True)
70
 
 
71
 
    def handle(self, request, data):
72
 
        user_id = data['user']
73
 
        LOG.info('Deleting user with id "%s"' % user_id)
74
 
        api.user_delete(request, user_id)
75
 
        messages.info(request, _('%(user)s was successfully deleted.')
76
 
                                % {"user": user_id})
77
 
        return shortcuts.redirect(request.build_absolute_uri())
78
 
 
79
 
 
80
 
class UserEnableDisableForm(forms.SelfHandlingForm):
81
 
    id = forms.CharField(label=_("ID (username)"), widget=forms.HiddenInput())
82
 
    enabled = forms.ChoiceField(label=_("enabled"), widget=forms.HiddenInput(),
83
 
                                choices=[[c, c]
84
 
                                         for c in ("disable", "enable")])
85
 
 
86
 
    def handle(self, request, data):
87
 
        user_id = data['id']
88
 
        enabled = data['enabled'] == "enable"
89
 
 
90
 
        try:
91
 
            api.user_update_enabled(request, user_id, enabled)
92
 
            messages.info(request,
93
 
                        _("User %(user)s %(state)s") %
94
 
                        {"user": user_id,
95
 
                        "state": "enabled" if enabled else "disabled"})
96
 
        except api_exceptions.ApiException:
97
 
            messages.error(request,
98
 
                        _("Unable to %(state)s user %(user)s") %
99
 
                        {"state": "enable" if enabled else "disable",
100
 
                        "user": user_id})
101
 
 
102
 
        return shortcuts.redirect(request.build_absolute_uri())