1 | ## $Id: mandate.py 8846 2012-06-29 08:19:47Z henrik $ |
---|
2 | ## |
---|
3 | ## Copyright (C) 2012 Uli Fouquet & Henrik Bettermann |
---|
4 | ## This program is free software; you can redistribute it and/or modify |
---|
5 | ## it under the terms of the GNU General Public License as published by |
---|
6 | ## the Free Software Foundation; either version 2 of the License, or |
---|
7 | ## (at your option) any later version. |
---|
8 | ## |
---|
9 | ## This program is distributed in the hope that it will be useful, |
---|
10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | ## GNU General Public License for more details. |
---|
13 | ## |
---|
14 | ## You should have received a copy of the GNU General Public License |
---|
15 | ## along with this program; if not, write to the Free Software |
---|
16 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
17 | ## |
---|
18 | """ |
---|
19 | These are the mandates. |
---|
20 | """ |
---|
21 | import grok |
---|
22 | from datetime import datetime, timedelta |
---|
23 | from grok import index |
---|
24 | from waeup.kofa.interfaces import IUserAccount |
---|
25 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
26 | from waeup.kofa.mandates.interfaces import IMandate |
---|
27 | |
---|
28 | class Mandate(grok.Container): |
---|
29 | """This is a mandate which can set a student password. |
---|
30 | """ |
---|
31 | grok.implements(IMandate) |
---|
32 | grok.provides(IMandate) |
---|
33 | |
---|
34 | def __init__(self, days=1): |
---|
35 | super(Mandate, self).__init__() |
---|
36 | self.creation_date = datetime.utcnow() # offset-naive datetime |
---|
37 | delta = timedelta(days=days) |
---|
38 | self.expires = datetime.utcnow() + delta |
---|
39 | self.mandate_id = None |
---|
40 | self.category = None |
---|
41 | self.params = {} |
---|
42 | return |
---|
43 | |
---|
44 | def _setStudentPassword(self): |
---|
45 | student_id = self.params.get('student_id', None) |
---|
46 | pwd = self.params.get('password', None) |
---|
47 | student = grok.getSite()['students'].get(student_id, None) |
---|
48 | if student and pwd: |
---|
49 | IUserAccount(student).setPassword(pwd) |
---|
50 | return True |
---|
51 | return False |
---|
52 | |
---|
53 | def execute(self): |
---|
54 | msg = _('Wrong mandate parameters.') |
---|
55 | if self.expires < datetime.utcnow(): |
---|
56 | msg = _('Mandate expired.') |
---|
57 | if self.category == 'student_password': |
---|
58 | if self._setStudentPassword(): |
---|
59 | msg = _('Password has been successfully set.') |
---|
60 | del self.__parent__[self.mandate_id] |
---|
61 | return msg |
---|