~hardware-certification/zope3/certify-staging-2.5

« back to all changes in this revision

Viewing changes to src/zope/app/homefolder/.svn/text-base/browser.py.svn-base

  • Committer: Marc Tardif
  • Date: 2008-04-26 19:03:34 UTC
  • Revision ID: cr3@lime-20080426190334-u16xo4llz56vliqf
Initial import.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
##############################################################################
 
2
#
 
3
# Copyright (c) 2004 Zope Corporation and Contributors.
 
4
# All Rights Reserved.
 
5
#
 
6
# This software is subject to the provisions of the Zope Public License,
 
7
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
 
8
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
 
9
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 
10
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
 
11
# FOR A PARTICULAR PURPOSE.
 
12
#
 
13
##############################################################################
 
14
"""Home Folder related views.
 
15
 
 
16
$Id$
 
17
"""
 
18
__docformat__ = "reStructuredText"
 
19
import zope.schema
 
20
from zope.schema.vocabulary import SimpleVocabulary
 
21
from zope.security.proxy import removeSecurityProxy
 
22
from zope.traversing.interfaces import TraversalError
 
23
from zope.traversing.api import getPath, getRoot, traverse
 
24
from zope.dottedname.resolve import resolve
 
25
 
 
26
from zope.app.form.browser import TextWidget, MultiSelectWidget
 
27
from zope.app.form.utility import setUpWidget
 
28
from zope.app.form.interfaces import IInputWidget
 
29
from zope.app.form.interfaces import ConversionError
 
30
from zope.app.homefolder.i18n import _
 
31
 
 
32
 
 
33
from zope.app.security.vocabulary import PrincipalSource
 
34
 
 
35
class PathWidget(TextWidget):
 
36
 
 
37
    def _toFieldValue(self, input):
 
38
        path = super(PathWidget, self)._toFieldValue(input)
 
39
        root = getRoot(self.context.context)
 
40
        try:
 
41
            proxy = traverse(root, path)
 
42
        except TraversalError, e:
 
43
            raise ConversionError(_('path is not correct !'), e)
 
44
        else:
 
45
            return removeSecurityProxy(proxy)
 
46
 
 
47
    def _toFormValue(self, value):
 
48
        if value is None:
 
49
            return ''
 
50
        return getPath(value)
 
51
 
 
52
class DottedNameWidget(TextWidget):
 
53
    """ Checks if the input is a resolvable class. """
 
54
    def _toFieldValue(self, input):
 
55
        try:
 
56
            objectToCreate = resolve(input)
 
57
        except ImportError, e:
 
58
            raise  ConversionError(_('dotted name is not correct !'), e)
 
59
        else:
 
60
            return input
 
61
 
 
62
class AssignHomeFolder(object):
 
63
 
 
64
    def setupWidgets(self):
 
65
        self.principal_field = zope.schema.Choice(
 
66
            __name__ = 'principal',
 
67
            title=u'Principal Id',
 
68
            source=PrincipalSource(),
 
69
            required=False)
 
70
 
 
71
        self.folderName_field = zope.schema.TextLine(
 
72
            __name__ = 'folderName',
 
73
            title=u'Folder Name',
 
74
            required=False)
 
75
 
 
76
        self.selectedPrincipals_field = zope.schema.List(
 
77
            __name__ = 'selectedPrincipals',
 
78
            title=u'Existing Assignments',
 
79
            value_type=zope.schema.Choice(
 
80
                vocabulary=SimpleVocabulary.fromItems(
 
81
                    [('%s (%s)' %(key, value), key)
 
82
                     for key, value in self.context.assignments.items()]
 
83
                    )),
 
84
            required=False)
 
85
 
 
86
        setUpWidget(self, 'principal', self.principal_field, IInputWidget)
 
87
        setUpWidget(self, 'folderName', self.folderName_field, IInputWidget)
 
88
        self.selectedPrincipals_widget = MultiSelectWidget(
 
89
            self.selectedPrincipals_field.bind(self),
 
90
            self.selectedPrincipals_field.value_type.vocabulary,
 
91
            self.request)
 
92
        self.selectedPrincipals_widget.setRenderedValue([])
 
93
 
 
94
    def update(self):
 
95
        self.setupWidgets()
 
96
 
 
97
        if 'SUBMIT_ASSIGN' in self.request:
 
98
            if not self.principal_widget.hasInput():
 
99
                return u''
 
100
 
 
101
            principal = self.principal_widget.getInputValue()
 
102
            name = self.folderName_widget.getInputValue()
 
103
 
 
104
            self.context.assignHomeFolder(principal, name)
 
105
            self.setupWidgets()
 
106
            return u'Home Folder assignment was successful.'
 
107
 
 
108
        if 'SUBMIT_UNASSIGN' in self.request:
 
109
            if not self.selectedPrincipals_widget.hasInput():
 
110
                return u''
 
111
 
 
112
            for id in self.selectedPrincipals_widget.getInputValue():
 
113
                self.context.unassignHomeFolder(id)
 
114
 
 
115
            self.setupWidgets()
 
116
            return u'Principals were successfully unassigned.'
 
117
 
 
118
        return u''