def showSummary(): entries = [] sumIncome = 0 expensePerCategory = {} for category in Category.objects().all(): expensePerCategory.update({category.name : 0}) userId = ObjectId(session.get('user')['_id']['$oid']) for entry in Entry.objects(owner = userId).all(): e = {'_id' : entry.id, 'amount' : entry.amount, 'description' : entry.description } # Check if the entry has a category. if entry.category is not None: e.update({'category' : Category.objects(id = entry.category.id).first().name}) else: e.update({'category' : '-'}) entries.append(e) if e['amount'] > 0: sumIncome = sumIncome + e['amount'] else: expensePerCategory[e['category']] = \ expensePerCategory[e['category']] - e['amount'] logger.debug('Currency: {0}'.format(session.get('currency'))) return render_template('/budget/summary.html', entries = entries, sumIncome = sumIncome, expensePerCategory = expensePerCategory)
def addEntry(template, asAsset = False): form = AddEntryForm(request.form) # Load the categories from the DB into the SelectField form.loadCategories() logger.debug('addEntry has been called.') if request.method == 'POST' and form.validate(): logger.debug('A form has been submitted to addEntry.') entry = Entry() logger.debug('Trying to populate form.') form.populate_obj(entry) logger.debug('Entry: {0}, {1}, {2}'.format(entry.amount, entry.description, entry.category)) # If this is an expense, multiply the amount by (-1). # And also add a category to it. if not asAsset: entry.amount = entry.amount * (-1) entry.category = Category.objects(id = ObjectId(entry.category)).first() else: entry.category = Category.objects(name = 'None').first() # Insert owner into the ReferenceField. userId = ObjectId(session.get('user')['_id']['$oid']) entry.owner = User.objects(id = userId).first() entry.save() logger.debug('{0} added Income({1}, {2}, {3})'.format( session.get('user')['username'], entry.amount, entry.description, entry.category.name)) flash('Your entry has been added.') return redirect(url_for('budget.default')) return render_template(template, form = form)
def loadCategories(self): self.category.choices = [(c.id, c.name) for c in Category.objects().all()]