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

Last change on this file since 14843 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
RevLine 
[8846]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
[8847]22import hashlib
[8849]23import os
[8846]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
[8849]30class 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]54class 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
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
[13989]90    REDIRECT_WITH_MANDATE_ID = True
91
[13988]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.