source: main/waeup.kofa/branches/uli-py3/src/waeup/kofa/mandates/mandate.py @ 17932

Last change on this file since 17932 was 15609, checked in by Henrik Bettermann, 5 years ago

Finalize parents access.

  • Property svn:keywords set to Id
File size: 4.3 KB
Line 
1## $Id: mandate.py 15609 2019-09-26 11:38:36Z 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    REDIRECT_WITH_MANDATE_ID = False
38
39    def __init__(self, days=1, mandate_id=None):
40        super(Mandate, self).__init__()
41        self.creation_date = datetime.utcnow() # offset-naive datetime
42        delta = timedelta(days=days)
43        self.expires = datetime.utcnow() + delta
44        if mandate_id is None:
45            mandate_id = os.urandom(20)
46            mandate_id = hashlib.md5(mandate_id).hexdigest()
47        self.mandate_id = mandate_id
48        self.params = {}
49        return
50
51    def execute(self):
52        return _('Nothing to do.')
53
54class PasswordMandate(Mandate):
55    """This is a mandate which can set a password.
56    """
57
58    def _setPassword(self):
59        user = self.params.get('user', None)
60        pwd = self.params.get('password', None)
61        if not None in (user, pwd):
62            try:
63                IUserAccount(user).setPassword(pwd)
64                return True
65            except:
66                return False
67        return False
68
69    def execute(self):
70        msg = _('Wrong mandate parameters.')
71        redirect_path = ''
72        if self.expires < datetime.utcnow():
73            msg = _('Mandate expired.')
74        elif self._setPassword():
75            msg = _('Password has been successfully set. '
76                    'Login with your new password.')
77            username = IUserAccount(self.params['user']).name
78            grok.getSite().logger.info(
79                'PasswordMandate used: %s ' % username)
80            redirect_path = '/login'
81        del self.__parent__[self.mandate_id]
82        return msg, redirect_path
83
84class ParentsPasswordMandate(Mandate):
85
86    def _setPassword(self):
87        student = self.params.get('student', None)
88        pwd = self.params.get('password', None)
89        if not None in (student, pwd):
90            try:
91                student.setParentsPassword(pwd)
92                return True
93            except:
94                return False
95        return False
96
97    def execute(self):
98        msg = _('Wrong mandate parameters.')
99        redirect_path = ''
100        if self.expires < datetime.utcnow():
101            msg = _('Mandate expired.')
102        elif self._setPassword():
103            msg = _('Parents password has been successfully set. '
104                    'Login with your new parents password.')
105            grok.getSite().logger.info(
106                'ParentsPasswordMandate used: %s '
107                % self.params['student'].student_id)
108            redirect_path = '/login'
109        del self.__parent__[self.mandate_id]
110        return msg, redirect_path
111
112class RefereeReportMandate(Mandate):
113    """This is a mandate which can unlock a `RefereeReportAddFormPage`.
114    The mandate is not automatically deleted. This has to be done
115    by the submit method of the add form page.
116    """
117
118    REDIRECT_WITH_MANDATE_ID = True
119
120    def execute(self):
121        if self.expires < datetime.utcnow():
122            return _('Mandate expired.'), ''
123        redirect_path = self.params.get('redirect_path', None)
124        name = self.params.get('name', None)
125        email = self.params.get('email', None)
126        if None in (redirect_path, name, email):
127            return _('Wrong mandate parameters.'), ''
128        return None, redirect_path
Note: See TracBrowser for help on using the repository browser.