source: main/waeup.kofa/trunk/src/waeup/kofa/mandates/mandate.py @ 8853

Last change on this file since 8853 was 8853, checked in by Henrik Bettermann, 12 years ago

StudentRequestPasswordPage?: Create mandate and send link by email instead of setting password directly.

Show link to StudentRequestPasswordPage? on login page.

  • Property svn:keywords set to Id
File size: 2.5 KB
Line 
1## $Id: mandate.py 8853 2012-06-29 17:28:06Z 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"""
19These are the mandates.
20"""
21import grok
22import hashlib
23import os
24from datetime import datetime, timedelta
25from grok import index
26from waeup.kofa.interfaces import IUserAccount
27from waeup.kofa.interfaces import MessageFactory as _
28from waeup.kofa.mandates.interfaces import IMandate
29
30class Mandate(grok.Model):
31    """This is a mandate.
32    """
33    grok.implements(IMandate)
34    grok.provides(IMandate)
35    grok.baseclass()
36
37    def __init__(self, days=1, mandate_id=None):
38        super(Mandate, self).__init__()
39        self.creation_date = datetime.utcnow() # offset-naive datetime
40        delta = timedelta(days=days)
41        self.expires = datetime.utcnow() + delta
42        if mandate_id is None:
43            mandate_id = os.urandom(20)
44            mandate_id = hashlib.md5(mandate_id).hexdigest()
45        self.mandate_id = mandate_id
46        self.params = {}
47        return
48
49    def execute(self):
50        return _('Nothing to do.')
51
52class StudentPasswordMandate(Mandate):
53    """This is a mandate which can set a student password.
54    """
55
56    def _setStudentPassword(self):
57        student_id = self.params.get('student_id', None)
58        pwd = self.params.get('password', None)
59        student = grok.getSite()['students'].get(student_id, None)
60        if student and pwd:
61            IUserAccount(student).setPassword(pwd)
62            return True
63        return False
64
65    def execute(self):
66        msg = _('Wrong mandate parameters.')
67        if self.expires < datetime.utcnow():
68            msg = _('Mandate expired.')
69        if self._setStudentPassword():
70            msg = _('Password has been successfully set. '
71                    'Proceed to the login page and enter your credentials.')
72        del self.__parent__[self.mandate_id]
73        return msg
Note: See TracBrowser for help on using the repository browser.