def get(self, entry_id): """Return single entry or a list.""" if entry_id is None: d = request.args.get('month') if not d: d = date.today() else: d = datetime.strptime(d, '%Y-%m') resp = {'entries': [e.to_json() for e in Entry.query_month(d)]} if request.args.get('total') == '1': resp['totalAmount'] = '%.2f' % (Entry.month_total(d) or 0) return jsonify(**resp) else: entry = Entry.query.filter_by(id=entry_id).one() return jsonify(**entry.to_json())
def index(year=None, month=None): if year is None and month is None: d = date.today().replace(day=1) else: try: d = date(year, month, 1) except ValueError: abort(400) entries = [e.to_json() for e in Entry.query_month(d)] total = '%.2f' % (Entry.month_total(d) or 0) tags = [t.to_json() for t in Tag.query.order_by(Tag.name)] return render_template('main.jinja', date=str(d), entries=entries, totalAmount=total, tags=tags)
def post(self, **kwargs): """Create entry.""" data = AddEntrySchema().deserialize(request.json) entry = Entry() entry.amount = data['amount'] entry.date = data['date'] entry.setoptattr('note', data['note']) if data['tags']: entry.tags = Tag.query.filter(Tag.id.in_(data['tags'])).all() db_session.add(entry) db_session.commit() resp = {'id': entry.id} d = data['totalForMonth'] if d is not None: resp['totalAmount'] = '%.2f' % (Entry.month_total(d) or 0) return jsonify(**resp)
def _return_result_or_empty(self, data): d = data.get('totalForMonth') if d is not None: total = '%.2f' % (Entry.month_total(d) or 0) return jsonify(totalAmount=total) return '', 204