def setUp(self): testuser = User(username='******', email='*****@*****.**') testuser.save() self.testuser = testuser User(username='******', email='*****@*****.**', admin=True).save() testpayment = Transaction(diff=1000, user=testuser) testpayment.save() testexpense = Transaction(diff=-500) testexpense.save() testcons = Consumption(units=1, price_per_unit=50, user=testuser) testcons.save() self.app = coffee.app.test_client()
def administrate_consumption(): cform = ConsumptionForm() cform.uid.choices = User.get_uids() if not cform.validate_on_submit(): return 'Form not valid' uid = cform.uid.data user = User.objects.get(id=uid) user.active = True user.save() # Check if there was any useful input if True not in [x and x > 0 for x in cform.units.data]: flash('No updates given.') return redirect(url_for('coffee.admin')) for u, c in zip(cform.units.data, app.config['COFFEE_PRICES']): if (u): consumption = Consumption(units=u, price_per_unit=c[0], user=user) consumption.save() flash('Consumptions for user %s added.' % user) balance = user.balance if balance < app.config['BUDGET_WARN_BELOW']: if user.email: msg = EmailMessage() msg['Subject'] = f'[Kaffeeministerium] Geringes Guthaben!' msg['From'] = app.config['MAIL_DEFAULT_SENDER'] msg['To'] = user.email msg.set_content( render_template( 'mail/lowbudget', balance=euros(balance), minister_name=app.config['MAIL_MINISTER_NAME'])) if not app.config['DEBUG']: s = getMailServer() s.send_message(msg) else: print(u'Sending mail \n{}'.format(msg.as_string())) flash('Warning mail sent. Balance is {}'.format(euros(balance))) else: flash(f'Balance is {euros(balance)}. User {user.name} could not be' ' notified, no mail address available.') return redirect(url_for('coffee.admin'))
def administrate_consumption(): cform = ConsumptionForm() cform.uid.choices = User.get_uids() if not cform.validate_on_submit(): return 'Form not valid' uid = cform.uid.data user = User.objects.get(id=uid) user.active = True user.save() # Check if there was any useful input if True not in [x and x > 0 for x in cform.units.data]: flash('No updates given.') return redirect(url_for('coffee.admin')) for u, c in zip(cform.units.data, app.config['COFFEE_PRICES']): if (u): consumption = Consumption(units=u, price_per_unit=c[0], user=user) consumption.save() flash('Consumptions for user %s added.' % user) balance = user.balance if balance < app.config['BUDGET_WARN_BELOW']: if user.email: msg = Message(u"[Kaffeeministerium] Geringes Guthaben!") msg.charset = 'utf-8' msg.add_recipient(user.email) msg.body = render_template('mail/lowbudget', balance=euros(balance)) if not app.config['DEBUG']: mail.send(msg) else: print(u'Sending mail \n{}'.format(msg.as_string())) flash('Warning mail sent. Balance is {}'.format(euros(balance))) else: flash('Balance is {}. User could not be notified.'.format( euros(balance))) return redirect(url_for('coffee.admin'))