def mail_old_assignee(): # send old assignee an email if old_member and not self.user == old_member: subject = 'You have been signed out of a shift.' body = 'Hi,\n\n{} has signed you off a shift that '\ 'you were previously assigned to.\nThe shift is '\ 'now:\n\n{}\n\nYou can view the shift '\ 'schedule at {}.\n{}'.format( ascii_save(self.user.fullname), shift, schedule_url, q) sendmail(old_member.mem_email, subject, body, folder='shifts')
def test_sendmail(self): to = '*****@*****.**' subject = 'A vokomokum test' body = 'Bla\nBla' mail.sendmail(to, subject, body) # look up mail (only file in folder) mail_folder = '{}/other'.format( get_settings()['vokomokum.mail_folder']) mails = [m for m in os.listdir(mail_folder) if m.endswith('.eml')] f = open('%s/%s' % (mail_folder, mails[0]), 'r') _ = f.readline() self.assertEqual(f.readline(), 'To: %s\n' % to) self.assertEqual(f.readline(), 'Subject: %s\n' % subject) _ = f.readline() self.assertEqual(f.readline(), 'Bla\n') self.assertEqual(f.readline(), 'Bla\n')
def __call__(self): ''' Send members an email about their negative balance. We send these mails after orders, that is why this action is tied to an order number (it also helps us to keep track a little over when we already sent these reminders). ''' o_id = self.request.matchdict['o_id'] session = DBSession() order = session.query(Order).\ filter(Order.id == o_id).first() if not order: return dict(msg='An order with ID {} does not exist.'.format(o_id), order=None) members = [m for m in session.query(Member).all() if m.balance < 0] now = datetime.datetime.now() deadline = now + datetime.timedelta(days = 3) weekdays_en= ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'] weekdays_nl = ['maandag', 'dinsdag', 'woensdag', 'donderdag', 'vrijdag', 'zaterdag', 'zondag'] deadline_en = '{} {}.{}.{}'.format(weekdays_en[deadline.weekday()], deadline.day, deadline.month, deadline.year) deadline_nl = '{} {}.{}.{}'.format(weekdays_nl[deadline.weekday()], deadline.day, deadline.month, deadline.year) for m in members: subject = 'Payment request / Verzoek tot betaling' mail_templ = open('members/templates/order_charge_txt.eml', 'r') mail = mail_templ.read() body = mail.format(mem_id=m.mem_id, label=order.label, amount=round(m.balance, 2), deadline_nl=deadline_nl, deadline_en=deadline_en) sendmail(m.mem_email, subject, body, folder='order-charges/{}'.format(order.id), sender='*****@*****.**') return dict(msg=u'Emails with payment reminders have been sent out '\ 'after order {}'.format(order.label))
def send_pwdreset_request(member, app_url, first=False): ''' Make a key that enables resetting the password. Store it in the member table and then send it per email to the member, in a nice rest URL. :param Member member: member whose password is to be reset :param string app_url: URL of this application :param bool first: True if this email is the first this user gets (on account creation) ''' code = str(base64.urlsafe_b64encode(os.urandom(16)), "utf-8") member.mem_pwd_url = code subject = 'Set a new password for your Vokomokum account.' if first: subject = 'Welcome to Vokomokum. Please set a password.' mail_templ = open('members/templates/pwdreset_txt.eml', 'r') mail = mail_templ.read() body = mail.format(portal_url=app_url, mem_id=member.mem_id, key=code) return sendmail(member.mem_email, subject, body, folder='passwords')
def __call__(self): db_session = DBSession() params = self.request.params wg_id = self.request.matchdict['wg_id'] wg = get_wg(db_session, self.request) if not wg.active: return self.redir_to_shiftlist( wg, int(params['year']), int(params['month']), 'This workgroup is inactive, no new shifts can be added.') if not 'people' in params: people = 1 else: people = int(params['people']) self.added_shifts = [] def add_shift(month=None, year=None): ''' add a shift object to session, several times if people > 1 ''' shift = Shift(wg_id, '', None, None, None, None) shift.workgroup = DBSession().query(Workgroup).get(wg_id) shift = fill_shift_from_request(shift, self.request) shift.validate() db_session.add(shift) self.added_shifts.append(shift) if month: shift.month = month if year: shift.year = year if people > 1: for _ in range(1, people): s = shift.clone() s.workgroup = DBSession().query(Workgroup).get(s.wg_id) s.validate() db_session.add(s) self.added_shifts.append(s) day = params['day'] if not str(day).isdigit(): day = 1 else: day = int(day) sdate = datetime.datetime(int(params['year']), int(params['month']), day) if not 'repeat' in params: repeat = 'once' else: repeat = params['repeat'] if repeat == 'once': add_shift() else: udate = datetime.datetime(int(params['until_year']), int(params['until_month']), day) if repeat in ('monthly', 'bi-monthly-startnow', 'bi-monthly-startnext'): for year in range(sdate.year, udate.year + 1): smonth = 1 if year == sdate.year: smonth = sdate.month umonth = 12 if year == udate.year: umonth = udate.month step = 1 if repeat.startswith('bi-monthly'): step = 2 if repeat == 'bi-monthly-startnext': smonth += 1 for month in range(smonth, umonth + 1, step): add_shift(month, year) if len(self.added_shifts) == 0: return self.redir_to_shiftlist( wg, sdate.year, sdate.month, "Invalid date range: {}/{} to {}/{}".format( sdate.month, sdate.year, udate.month, udate.year)) else: return self.redir_to_shiftlist( wg, sdate.year, sdate.month, 'Could not create shifts. "repeat"-command unknown.') # inform (other) coordinators subject = "Workgroup {}: Shifts were created by {}"\ .format(ascii_save(wg.name), ascii_save(self.user.fullname)) body = "These shifts were added:\n\n{}\n\nBest, Vokomokum"\ .format('\n'.join([str(s) for s in self.added_shifts])) for c in wg.leaders: if c is not self.user: sendmail(c.mem_email, subject, body, folder='shifts') return self.redir_to_shiftlist(wg, sdate.year, sdate.month, 'Succesfully added {} shift(s) for task "{}".'\ .format(len(self.added_shifts), ascii_save(self.request.params['task'])))
def __call__(self): db_session = DBSession() wg = get_wg(db_session, self.request) if not wg: raise Exception( "Don't know which workgroup this is supposed to be.") shift = get_shift(db_session, self.request) if not shift: raise Exception("No shift with id %d" % self.request.matchdict['s_id']) def redir(msg): return self.redir_to_shiftlist(wg, shift.year, shift.month, msg) if not wg.active: return redir( 'Workgroup is inactive, editing shifts therefore not possible.' ) action = self.request.matchdict['action'] if action == "": raise Exception('No action given.') if action == "setmember": if not 'mem_id' in self.request.params: return redir('No member selected.') if not self.user in wg.members and not self.user.mem_admin: return redir( 'You are not allowed to assign shifts in this workgroup.') if self.request.params['mem_id'] == '--': member = None else: member = get_member(db_session, self.request) # prepare some things for mailing schedule_url = '{}/workgroup/{}/shifts/{}/{}'.format( self.request.application_url, shift.workgroup.id, shift.year, shift.month) q = 'This email was automatically generated, so please do not '\ 'directly reply to it. You may direct any questions regarding '\ 'the workgroup to your coordinator(s). Only technical questions '\ 'go to [email protected].'\ '\n\nBest,\nVokomokum' old_member = shift.member def mail_old_assignee(): # send old assignee an email if old_member and not self.user == old_member: subject = 'You have been signed out of a shift.' body = 'Hi,\n\n{} has signed you off a shift that '\ 'you were previously assigned to.\nThe shift is '\ 'now:\n\n{}\n\nYou can view the shift '\ 'schedule at {}.\n{}'.format( ascii_save(self.user.fullname), shift, schedule_url, q) sendmail(old_member.mem_email, subject, body, folder='shifts') if member: shift.member = member shift.state = 'assigned' shift.validate() if not self.user == member: # send new assignee an email subject = 'You have been assigned to a shift.' body = 'Hi,\n\n{} has assigned you to a shift: '\ '\n\n{}\n\nYou can view the shift schedule at {}'\ '\n\n{}'.format(ascii_save(self.user.fullname), str(shift), schedule_url, q) sendmail(member.mem_email, subject, body, folder='shifts') # let coordinator(s) know, as well subject = "Workgroup {}: The shift for {} on day '{}' in "\ "{}/{} is now assigned to {}".format(shift.workgroup, shift.task, shift.day, shift.month, shift.year, ascii_save(shift.member.fullname)) body = "The assignment was done by member {}."\ .format(ascii_save(self.user.fullname)) if old_member: body += " The previous assignee was: {}."\ .format(ascii_save(old_member.fullname)) else: body += " No one was assigned to this shift before." body += "\n\n{}".format(q) for c in wg.leaders: if c is not self.user: sendmail(c.mem_email, subject, body, folder='shifts') # and inform previous assignee mail_old_assignee() name = ascii_save(shift.member.fullname) return redir(u'{} has been signed up for the shift.'\ .format(name)) else: if shift.is_locked and not self.user in wg.leaders\ and not self.user.mem_admin: return redir( 'Shift is already locked. Ask your workgroup admin for help.' ) shift.member = None shift.state = 'open' shift.validate() mail_old_assignee() # let coordinator(s) know, as well subject = "Workgroup {}: Member {} was unassigned from the "\ "shift for {} on day '{}' in {}/{}"\ .format(shift.workgroup, ascii_save(old_member.fullname), shift.task, shift.day, shift.month, shift.year) body = "The un-assignment was done by member {}."\ .format(ascii_save(self.user.fullname)) body += "\n\n{}".format(q) for c in wg.leaders: if c is not self.user: sendmail(c.mem_email, subject, body, folder='shifts') return redir('Shift is now open.') return redir('You are not allowed to do this.') elif action == "settask": if self.user in wg.leaders or self.user.mem_admin: if not 'task' in self.request.params: return dict(msg='No task given.') shift.task = self.request.params['task'] shift.validate() return redir('Changed task of shift.') return redir('You are not allowed to edit the task.') elif action == "setday": if self.user in wg.leaders or self.user.mem_admin: if not 'day' in self.request.params: return dict(msg='No day given.') shift.day = self.request.params['day'] shift.validate() return redir('Changed day of shift to {}.'.format(shift.day)) return redir('You are not allowed to set the day.') elif action == "setstate": if self.user in wg.leaders or self.user.mem_admin: if not 'state' in self.request.params: return redir('No state given.') shift.state = self.request.params['state'] shift.validate() return redir('Changed shift state to {}.'.format(shift.state)) return redir('You are not allowed to set the state.') elif action == 'delete': if self.user in wg.leaders or self.user.mem_admin: db_session.delete(shift) return redir('Deleted shift.') return redir('You are not allowed to delete a shift.')
def __call__(self): ''' Create order charges for a given order in the system ''' o_id = int(self.request.matchdict['o_id']) session = DBSession() order = session.query(Order).\ filter(Order.id == o_id).first() if not order: return dict(msg='An order with ID {} does not exist.'.format(o_id), order=None, action=None) charge_ttype_id = get_ttypeid_by_name('Order Charge') if not 'Finance' in [wg.name for wg in self.user.workgroups]\ and not self.user.mem_admin: return dict(order=order, action=None, msg='Only Finance people can do this.') # all (positive) charges for members who have not been charged for # this order yet charges = [MemberOrder(m, order) for m in session.query(Member).all()] charges = [c for c in charges if c.amount > 0 and not o_id in\ [t.order.id for t in c.member.transactions\ if t.ttype_id == charge_ttype_id]] first_orderers = [] if not 'action' in self.request.params: # show charges that would be made return dict(order=order, action='show', charges=charges) else: # temporary backdoor to test this if self.request.params['action'] == 'gggraph': orders_money_and_people() if self.request.params['action'] == 'charge': # confirmation: make charges for c in charges: t = Transaction(amount = -1 * c.amount, comment = 'Automatically charged.' ) ttype = session.query(TransactionType)\ .get(charge_ttype_id) t.ttype = ttype t.member = c.member t.order = c.order t.validate() session.add(t) # first order of this member? Charge Membership fee if c.is_first_order(): first_orderers.append(c.member) mf = Transaction(\ amount = membership_fee(c.member) * -1, comment = 'Automatically charged (for {}'\ ' people in the household) on first-time'\ ' order ({})'\ .format(c.member.mem_household_size, c.order.label) ) ttype = session.query(TransactionType)\ .get(get_ttypeid_by_name('Membership Fee')) mf.ttype = ttype mf.member = c.member mf.validate() session.add(mf) # use this opportunity to update graph data orders_money_and_people() # inform membership about people who ordered for first time subject = 'First-time orderers' body = 'FYI: Below is a list of people who ordered for the'\ ' first time today. This mail was auto-generated.\n\n' body += '\n'.join(['{} [ID:{}]'.format(ascii_save(m.fullname), m.mem_id)\ for m in first_orderers]) sendmail('*****@*****.**', subject, body, folder='order-charges/{}'.format(o_id), sender='*****@*****.**') return dict(order=order, action='done') return dict(order=order, action='')