1 | ## $Id: mandate.py 8848 2012-06-29 11:26:59Z 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 | import hashlib |
---|
23 | from datetime import datetime, timedelta |
---|
24 | from time import time |
---|
25 | from grok import index |
---|
26 | from waeup.kofa.interfaces import IUserAccount |
---|
27 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
28 | from waeup.kofa.mandates.interfaces import IMandate |
---|
29 | |
---|
30 | class Mandate(grok.Container): |
---|
31 | """This is a mandate which can set a student password. |
---|
32 | """ |
---|
33 | grok.implements(IMandate) |
---|
34 | grok.provides(IMandate) |
---|
35 | |
---|
36 | def __init__(self, days=1, category=None, mandate_id=None): |
---|
37 | super(Mandate, self).__init__() |
---|
38 | self.creation_date = datetime.utcnow() # offset-naive datetime |
---|
39 | delta = timedelta(days=days) |
---|
40 | self.expires = datetime.utcnow() + delta |
---|
41 | if mandate_id is None: |
---|
42 | timestamp = "%d" % int(time()*1000) |
---|
43 | mandate_id = hashlib.md5(timestamp).hexdigest() |
---|
44 | self.mandate_id = mandate_id |
---|
45 | self.category = category |
---|
46 | self.params = {} |
---|
47 | return |
---|
48 | |
---|
49 | def _setStudentPassword(self): |
---|
50 | student_id = self.params.get('student_id', None) |
---|
51 | pwd = self.params.get('password', None) |
---|
52 | student = grok.getSite()['students'].get(student_id, None) |
---|
53 | if student and pwd: |
---|
54 | IUserAccount(student).setPassword(pwd) |
---|
55 | return True |
---|
56 | return False |
---|
57 | |
---|
58 | def execute(self): |
---|
59 | msg = _('Wrong mandate parameters.') |
---|
60 | if self.expires < datetime.utcnow(): |
---|
61 | msg = _('Mandate expired.') |
---|
62 | elif self.category == 'student_password': |
---|
63 | if self._setStudentPassword(): |
---|
64 | msg = _('Password has been successfully set.') |
---|
65 | del self.__parent__[self.mandate_id] |
---|
66 | return msg |
---|