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

Last change on this file since 17952 was 16550, checked in by Henrik Bettermann, 3 years ago
  • Property svn:keywords set to Id
File size: 4.4 KB
RevLine 
[8846]1## $Id: mandate.py 16550 2021-07-14 06:40:53Z 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
[8847]22import hashlib
[8849]23import os
[16550]24from urllib import urlencode
[8846]25from datetime import datetime, timedelta
26from grok import index
27from waeup.kofa.interfaces import IUserAccount
28from waeup.kofa.interfaces import MessageFactory as _
29from waeup.kofa.mandates.interfaces import IMandate
30
[8849]31class Mandate(grok.Model):
32    """This is a mandate.
[8846]33    """
34    grok.implements(IMandate)
35    grok.provides(IMandate)
[8849]36    grok.baseclass()
[8846]37
[13989]38    REDIRECT_WITH_MANDATE_ID = False
39
[8849]40    def __init__(self, days=1, mandate_id=None):
[8846]41        super(Mandate, self).__init__()
42        self.creation_date = datetime.utcnow() # offset-naive datetime
43        delta = timedelta(days=days)
44        self.expires = datetime.utcnow() + delta
[8847]45        if mandate_id is None:
[8849]46            mandate_id = os.urandom(20)
47            mandate_id = hashlib.md5(mandate_id).hexdigest()
[8847]48        self.mandate_id = mandate_id
[8846]49        self.params = {}
50        return
51
[8849]52    def execute(self):
53        return _('Nothing to do.')
54
[8857]55class PasswordMandate(Mandate):
[8860]56    """This is a mandate which can set a password.
[8849]57    """
58
[8857]59    def _setPassword(self):
[8858]60        user = self.params.get('user', None)
[8846]61        pwd = self.params.get('password', None)
[8859]62        if not None in (user, pwd):
[8858]63            try:
64                IUserAccount(user).setPassword(pwd)
65                return True
66            except:
67                return False
[8846]68        return False
69
70    def execute(self):
71        msg = _('Wrong mandate parameters.')
[13986]72        redirect_path = ''
[8846]73        if self.expires < datetime.utcnow():
74            msg = _('Mandate expired.')
[13987]75        elif self._setPassword():
[11681]76            msg = _('Password has been successfully set. '
77                    'Login with your new password.')
[8860]78            username = IUserAccount(self.params['user']).name
[16550]79            password = self.params['password']
[8860]80            grok.getSite().logger.info(
81                'PasswordMandate used: %s ' % username)
[16550]82            args = {'login':username, 'password':password}
83            redirect_path = '/login?%s' % urlencode(args)
[8846]84        del self.__parent__[self.mandate_id]
[13986]85        return msg, redirect_path
[13988]86
[15607]87class ParentsPasswordMandate(Mandate):
88
89    def _setPassword(self):
90        student = self.params.get('student', None)
91        pwd = self.params.get('password', None)
92        if not None in (student, pwd):
93            try:
94                student.setParentsPassword(pwd)
95                return True
96            except:
97                return False
98        return False
99
100    def execute(self):
101        msg = _('Wrong mandate parameters.')
102        redirect_path = ''
103        if self.expires < datetime.utcnow():
104            msg = _('Mandate expired.')
105        elif self._setPassword():
106            msg = _('Parents password has been successfully set. '
[15609]107                    'Login with your new parents password.')
[15607]108            grok.getSite().logger.info(
109                'ParentsPasswordMandate used: %s '
110                % self.params['student'].student_id)
111            redirect_path = '/login'
112        del self.__parent__[self.mandate_id]
113        return msg, redirect_path
114
[13988]115class RefereeReportMandate(Mandate):
116    """This is a mandate which can unlock a `RefereeReportAddFormPage`.
[16059]117    The mandate is not automatically deleted.
[13988]118    """
119
[13989]120    REDIRECT_WITH_MANDATE_ID = True
121
[13988]122    def execute(self):
123        if self.expires < datetime.utcnow():
124            return _('Mandate expired.'), ''
125        redirect_path = self.params.get('redirect_path', None)
126        name = self.params.get('name', None)
127        email = self.params.get('email', None)
128        if None in (redirect_path, name, email):
129            return _('Wrong mandate parameters.'), ''
130        return None, redirect_path
Note: See TracBrowser for help on using the repository browser.