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

Last change on this file since 14466 was 13990, checked in by Henrik Bettermann, 8 years ago

Use url method properly.

  • Property svn:keywords set to Id
File size: 3.4 KB
Line 
1## $Id: mandate.py 13990 2016-06-25 05:00:54Z 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 RefereeReportMandate(Mandate):
85    """This is a mandate which can unlock a `RefereeReportAddFormPage`.
86    The mandate is not automatically deleted. This has to be done
87    by the submit method of the add form page.
88    """
89
90    REDIRECT_WITH_MANDATE_ID = True
91
92    def execute(self):
93        if self.expires < datetime.utcnow():
94            return _('Mandate expired.'), ''
95        redirect_path = self.params.get('redirect_path', None)
96        name = self.params.get('name', None)
97        email = self.params.get('email', None)
98        if None in (redirect_path, name, email):
99            return _('Wrong mandate parameters.'), ''
100        return None, redirect_path
Note: See TracBrowser for help on using the repository browser.