## $Id: mandate.py 8848 2012-06-29 11:26:59Z henrik $ ## ## Copyright (C) 2012 Uli Fouquet & Henrik Bettermann ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; if not, write to the Free Software ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ## """ These are the mandates. """ import grok import hashlib from datetime import datetime, timedelta from time import time from grok import index from waeup.kofa.interfaces import IUserAccount from waeup.kofa.interfaces import MessageFactory as _ from waeup.kofa.mandates.interfaces import IMandate class Mandate(grok.Container): """This is a mandate which can set a student password. """ grok.implements(IMandate) grok.provides(IMandate) def __init__(self, days=1, category=None, mandate_id=None): super(Mandate, self).__init__() self.creation_date = datetime.utcnow() # offset-naive datetime delta = timedelta(days=days) self.expires = datetime.utcnow() + delta if mandate_id is None: timestamp = "%d" % int(time()*1000) mandate_id = hashlib.md5(timestamp).hexdigest() self.mandate_id = mandate_id self.category = category self.params = {} return def _setStudentPassword(self): student_id = self.params.get('student_id', None) pwd = self.params.get('password', None) student = grok.getSite()['students'].get(student_id, None) if student and pwd: IUserAccount(student).setPassword(pwd) return True return False def execute(self): msg = _('Wrong mandate parameters.') if self.expires < datetime.utcnow(): msg = _('Mandate expired.') elif self.category == 'student_password': if self._setStudentPassword(): msg = _('Password has been successfully set.') del self.__parent__[self.mandate_id] return msg