[8846] | 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 | """ |
---|
| 19 | These are the mandates. |
---|
| 20 | """ |
---|
| 21 | import grok |
---|
[8847] | 22 | import hashlib |
---|
[8849] | 23 | import os |
---|
[8846] | 24 | from datetime import datetime, timedelta |
---|
| 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 | |
---|
[8849] | 30 | class Mandate(grok.Model): |
---|
| 31 | """This is a mandate. |
---|
[8846] | 32 | """ |
---|
| 33 | grok.implements(IMandate) |
---|
| 34 | grok.provides(IMandate) |
---|
[8849] | 35 | grok.baseclass() |
---|
[8846] | 36 | |
---|
[13989] | 37 | REDIRECT_WITH_MANDATE_ID = False |
---|
| 38 | |
---|
[8849] | 39 | def __init__(self, days=1, mandate_id=None): |
---|
[8846] | 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 |
---|
[8847] | 44 | if mandate_id is None: |
---|
[8849] | 45 | mandate_id = os.urandom(20) |
---|
| 46 | mandate_id = hashlib.md5(mandate_id).hexdigest() |
---|
[8847] | 47 | self.mandate_id = mandate_id |
---|
[8846] | 48 | self.params = {} |
---|
| 49 | return |
---|
| 50 | |
---|
[8849] | 51 | def execute(self): |
---|
| 52 | return _('Nothing to do.') |
---|
| 53 | |
---|
[8857] | 54 | class PasswordMandate(Mandate): |
---|
[8860] | 55 | """This is a mandate which can set a password. |
---|
[8849] | 56 | """ |
---|
| 57 | |
---|
[8857] | 58 | def _setPassword(self): |
---|
[8858] | 59 | user = self.params.get('user', None) |
---|
[8846] | 60 | pwd = self.params.get('password', None) |
---|
[8859] | 61 | if not None in (user, pwd): |
---|
[8858] | 62 | try: |
---|
| 63 | IUserAccount(user).setPassword(pwd) |
---|
| 64 | return True |
---|
| 65 | except: |
---|
| 66 | return False |
---|
[8846] | 67 | return False |
---|
| 68 | |
---|
| 69 | def execute(self): |
---|
| 70 | msg = _('Wrong mandate parameters.') |
---|
[13986] | 71 | redirect_path = '' |
---|
[8846] | 72 | if self.expires < datetime.utcnow(): |
---|
| 73 | msg = _('Mandate expired.') |
---|
[13987] | 74 | elif self._setPassword(): |
---|
[11681] | 75 | msg = _('Password has been successfully set. ' |
---|
| 76 | 'Login with your new password.') |
---|
[8860] | 77 | username = IUserAccount(self.params['user']).name |
---|
| 78 | grok.getSite().logger.info( |
---|
| 79 | 'PasswordMandate used: %s ' % username) |
---|
[13990] | 80 | redirect_path = '/login' |
---|
[8846] | 81 | del self.__parent__[self.mandate_id] |
---|
[13986] | 82 | return msg, redirect_path |
---|
[13988] | 83 | |
---|
[15607] | 84 | class 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. ' |
---|
[15609] | 104 | 'Login with your new parents password.') |
---|
[15607] | 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 | |
---|
[13988] | 112 | class 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 | |
---|
[13989] | 118 | REDIRECT_WITH_MANDATE_ID = True |
---|
| 119 | |
---|
[13988] | 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 |
---|