1 | """Users (principals) for the WAeUP portal. |
---|
2 | """ |
---|
3 | import grok |
---|
4 | from hurry import yui |
---|
5 | from zope.exceptions import DuplicationError |
---|
6 | from waeup.authentication import Account |
---|
7 | from waeup.interfaces import IUserContainer, IUserAccount |
---|
8 | from waeup.viewlets import LeftSidebar, MainArea, Index, Add, FormWrapMixin |
---|
9 | |
---|
10 | class UserContainer(grok.Container): |
---|
11 | """A container for principals. |
---|
12 | |
---|
13 | See interfaces.py and users.txt for extensive description. |
---|
14 | """ |
---|
15 | grok.implements(IUserContainer) |
---|
16 | grok.require('waeup.manageUsers') |
---|
17 | |
---|
18 | def addUser(self, name, password, title=None, description=None): |
---|
19 | if title is None: |
---|
20 | title = name |
---|
21 | if description is None: |
---|
22 | description = title |
---|
23 | self[name] = Account(name, password, title, description) |
---|
24 | |
---|
25 | def addAccount(self, account): |
---|
26 | self[account.name] = account |
---|
27 | |
---|
28 | def delUser(self, name): |
---|
29 | if name in self.keys(): |
---|
30 | del self[name] |
---|
31 | |
---|
32 | class UserContainerView(Index): |
---|
33 | grok.require('waeup.manageUsers') |
---|
34 | grok.context(IUserContainer) |
---|
35 | grok.name('index') |
---|
36 | |
---|
37 | def update(self, userid=None, adduser=None, edit=None, delete=None): |
---|
38 | yui.sam.need() |
---|
39 | yui.datatable.need() |
---|
40 | |
---|
41 | if edit is not None and userid is not None: |
---|
42 | self.redirect(self.url(userid)) |
---|
43 | |
---|
44 | def render(self, *args, **kw): |
---|
45 | return super(UserContainerView, self).render(*args, **kw) |
---|
46 | |
---|
47 | class AddUserForm(grok.AddForm): |
---|
48 | grok.require('waeup.manageUsers') |
---|
49 | grok.context(UserContainer) |
---|
50 | form_fields = grok.AutoFields(IUserAccount) |
---|
51 | label = 'Add user' |
---|
52 | |
---|
53 | @grok.action('Add user') |
---|
54 | def addFaculty(self, **data): |
---|
55 | #user = Account(**data) |
---|
56 | name = data['name'] |
---|
57 | title = data['title'] |
---|
58 | description = data['description'] |
---|
59 | password = data['password'] |
---|
60 | try: |
---|
61 | self.context.addUser(name, password, title=title, |
---|
62 | description=description) |
---|
63 | except DuplicationError: |
---|
64 | self.status = Invalid('The userid chosen already exists ' |
---|
65 | 'in the database') |
---|
66 | return |
---|
67 | self.redirect(self.url(self.context)) |
---|
68 | |
---|
69 | class AddUser(FormWrapMixin, grok.Viewlet): |
---|
70 | """A viewlet that wraps the `AddUserForm`. |
---|
71 | """ |
---|
72 | grok.viewletmanager(MainArea) |
---|
73 | grok.context(UserContainer) |
---|
74 | grok.view(Add) |
---|
75 | grok.require('waeup.manageUsers') |
---|
76 | |
---|
77 | formview_name = 'adduserform' # The name of the formview we want |
---|
78 | # to be rendered in this viewlet. |
---|
79 | |
---|
80 | class UserContainerMain(grok.Viewlet): |
---|
81 | grok.require('waeup.manageUsers') |
---|
82 | grok.viewletmanager(MainArea) |
---|
83 | grok.template('usermain') |
---|
84 | grok.view(UserContainerView) |
---|
85 | |
---|
86 | |
---|
87 | class UserEditForm(grok.EditForm): |
---|
88 | grok.context(IUserAccount) |
---|
89 | form_fields = grok.AutoFields(IUserAccount) |
---|
90 | grok.require('waeup.manageUsers') |
---|
91 | |
---|
92 | @grok.action('Save') |
---|
93 | def save(self, **data): |
---|
94 | self.applyData(self.context, **data) |
---|
95 | self.flash('User was modified.') |
---|
96 | return |
---|
97 | |
---|
98 | @grok.action('Save and return') |
---|
99 | def saveAndReturn(self, **data): |
---|
100 | self.applyData(self.context, **data) |
---|
101 | self.flash('User was modified.') |
---|
102 | self.redirect(self.url(self.context.__parent__)) |
---|
103 | return |
---|
104 | |
---|
105 | @grok.action('Cancel') |
---|
106 | def cancel(self, **data): |
---|
107 | self.redirect(self.url(self.context.__parent__)) |
---|
108 | return |
---|
109 | |
---|
110 | |
---|
111 | class UserEditMain(FormWrapMixin, grok.Viewlet): |
---|
112 | """A viewlet that renders the `UserEditForm`. |
---|
113 | """ |
---|
114 | grok.viewletmanager(MainArea) |
---|
115 | grok.context(IUserAccount) |
---|
116 | grok.view(Index) |
---|
117 | grok.require('waeup.manageUsers') |
---|
118 | |
---|
119 | formview_name = 'usereditform' # The name of the formview we want to |
---|
120 | # be rendered in this viewlet. |
---|