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'))
def api(function): prices = {p[0]: p[1] for p in app.config['COFFEE_PRICES']} products = {p[1]: p[0] for p in app.config['COFFEE_PRICES']} def get_userlist(): # always calculate user list today = pendulum.today(app.config['TZ']) users = [] for user in User.objects(active=True).order_by('-vip', 'name'): # build user dictionary manually user_dict = { 'name': user.name, 'username': user.username, 'id': str(user.id), 'consume': [], # fill in all of todays achievements 'achievements': [{ 'key': a.key, 'date': a.date, } for a in user.achievements if a.date > pendulum.today()] } # perform a query for todays consumptions for each user # @TODO: try to move this to the mongo query... instead of hitting # the DB n + 1 times. for consume in Consumption.objects(user=user, date__gte=today): if consume.price_per_unit in prices: user_dict['consume'].extend( consume.units * [prices.get(consume.price_per_unit)]) users.append(user_dict) return users def get_service(): current_service = Service.current() last_cleaned = (Service.objects( master=True, cleaned=True).order_by('-date').first()) service = { 'uid': str(current_service.user.id), 'last_cleaned': last_cleaned.date if last_cleaned else 'Never', 'upcoming': [ dict(week=s['_id'], user=str(s['user'])) for s in Service.upcoming() ] } if current_service else None return service if function == 'user_list': return jsonify(users=get_userlist(), service=get_service()) if function == 'add_consumption': data = request.get_json() user = User.objects.get(id=data.get('id')) created = Consumption(user=user, price_per_unit=products.get( data['consumption_type']), units=data['cur_consumption'], date=datetime.now()).save() if user.balance < app.config['BUDGET_WARN_BELOW']: alert = { 'text': 'Geringes Guthaben, bitte laden Sie bald wieder auf.', 'type': 'warning', } else: alert = None status = 'success' if created else 'failure' return jsonify(status=status, users=get_userlist(), alert=alert) if function == 'finish_service': data = request.get_json() service = Service.current() service.__setattr__(data.get('service'), True) service.save() return jsonify(service=get_service(), alert={ 'text': 'Service eingetragen', 'type': 'success' }) return abort(404)
def api(function): prices = {p[0]: p[1] for p in app.config['COFFEE_PRICES']} products = {p[1]: p[0] for p in app.config['COFFEE_PRICES']} def get_userlist(): # always calculate user list today = pendulum.today(app.config['TZ']) users = [] for user in User.objects(active=True).order_by('-vip', 'name'): user_dict = { 'name': user.name, 'username': user.username, 'id': str(user.id), 'consume': [] } for consume in Consumption.objects(user=user, date__gte=today): if consume.price_per_unit in prices: user_dict['consume'].extend( consume.units * [prices.get(consume.price_per_unit)]) users.append(user_dict) return users def get_service(): current_service = Service.current() service = { 'uid': str(current_service.user.id), 'cleaned': current_service.cleaned, 'cleaningProgram': current_service.cleaning_program, 'decalcifyProgram': current_service.decalcify_program, } if current_service else None return service if function == 'user_list': return jsonify(users=get_userlist(), service=get_service()) if function == 'add_consumption': data = request.get_json() user = User.objects.get(id=data.get('id')) created = Consumption(user=user, price_per_unit=products.get( data['consumption_type']), units=data['cur_consumption'], date=datetime.now()).save() if user.balance < app.config['BUDGET_WARN_BELOW']: alert = { 'text': 'Geringes Guthaben, bitte laden Sie bald wieder auf.', 'type': 'warning', } else: alert = None status = 'success' if created else 'failure' return jsonify(status=status, users=get_userlist(), alert=alert) if function == 'finish_service': data = request.get_json() service = Service.current() service.__setattr__(data.get('service'), True) service.save() return jsonify(service=get_service(), alert={ 'text': 'Service eingetragen', 'type': 'success' }) return abort(404)