def add_or_update_scheduled_transaction(): try: item = json.loads(request.data) # import ipdb; ipdb.set_trace() frequency = int(item.get('frequency', 1)) number_replications = int(item.get('number_replications', 1)) if frequency < 1 or number_replications < 1: return view.response(status=500, messages=['The frequency and the number of replications have to be bigger than zero.']) unit_replications = item.get('unit_replications') transaction_date = utils.to_date(item.get('transaction_date', utils.from_date(date.today()))) for i in range(frequency): if i > 0: if unit_replications == 'weeks': transaction_date = transaction_date + relativedelta(weeks=+number_replications) elif unit_replications == 'months': transaction_date = transaction_date + relativedelta(months=+number_replications) elif unit_replications == 'years': transaction_date = transaction_date + relativedelta(years=+number_replications) item.update({'transaction_date': transaction_date}) item.update({'user_id': current_user.id}) view.save_item(models.ScheduledTransaction, item) return view.response(fields=item, total=frequency) except Exception as e: return view.response(status=500, messages=[e.message])
def register(): try: item = json.loads(request.data) username = item.get('username', '') if username == '': raise Exception(u'The username is blank') if models.User.query.filter_by(username=username).count() > 0: raise Exception(u'The username already exists') password = item.get('password', '') if len(password) < 5 or len(password) > 16: raise Exception(u'Password must be 6-16 characters') user = models.User(username='', password='') user.from_dict(item) user.get_session.commit() return view.response(total=1, fields={ 'username': user.username, 'token': user.token }) except Exception as e: models.session.rollback() return view.response(status=500, messages=[e.message])
def account_summary(): try: start_date = utils.to_date(request.args.get('start')) end_date = utils.to_date(request.args.get('end')) account_id = request.args.get('account') # account = models.Account.query.filter_by(id=account_id).first() qry = 'user_id=%s and account_id=%s and transaction_date >= "%s" and transaction_date <= "%s"' % (current_user.id, account_id, start_date, end_date) items = models.Transaction.query.filter(qry).order_by('transaction_date').all() fields = [] data = {'date': None, 'balance': 0} year = None amount = 0 for item in items: year = item.transaction_date.year month = item.transaction_date.month dt = '%d/%d' % (month, year) if data.get('date') == dt: amount = data.get('balance') + item.amount data.update({ 'balance': amount }) else: data = {'date': dt, 'balance': item.amount + amount} fields.append(data) #status = 404 if len(fields) == 0 else 200 return view.response(fields=fields) except Exception, e: return view.response(status=500, messages=[e.message])
def account_summary(): try: start_date = utils.to_date(request.args.get('start')) end_date = utils.to_date(request.args.get('end')) account_id = request.args.get('account') # account = models.Account.query.filter_by(id=account_id).first() qry = 'user_id=%s and account_id=%s and transaction_date >= "%s" and transaction_date <= "%s"' % ( current_user.id, account_id, start_date, end_date) items = models.Transaction.query.filter(qry).order_by( 'transaction_date').all() fields = [] data = {'date': None, 'balance': 0} year = None amount = 0 for item in items: year = item.transaction_date.year month = item.transaction_date.month dt = '%d/%d' % (month, year) if data.get('date') == dt: amount = data.get('balance') + item.amount data.update({'balance': amount}) else: data = {'date': dt, 'balance': item.amount + amount} fields.append(data) #status = 404 if len(fields) == 0 else 200 return view.response(fields=fields) except Exception, e: return view.response(status=500, messages=[e.message])
def delete_scheduled_transactions(item_key): items = models.ScheduledTransaction.query.filter_by(user_id=current_user.id, key=item_key).all() if len(items) == 0: return view.response(status=404) for item in items: item.delete() item.get_session.commit() return view.response(success=True)
def delete_scheduled_transactions(item_key): items = models.ScheduledTransaction.query.filter_by( user_id=current_user.id, key=item_key).all() if len(items) == 0: return view.response(status=404) for item in items: item.delete() item.get_session.commit() return view.response(success=True)
def homebank_importing(): try: f = request.files['filename'] temp_dir = current_user.token xml = os.path.join(tempfile.gettempdir(), temp_dir) f.save(xml) homebank.create(file=xml, user=current_user) return view.response() except Exception as e: return view.response(status=500, messages=[e.message])
def calculate(): try: item = json.loads(request.data) today = utils.from_date(date.today()) future_date = item.get('date', today) account_id = int(item.get('account', 0)) account = models.Account.query.filter_by(id=account_id).first() if not account: return view.response(status=404, messages=['Account not found.']) qry = 'user_id="%s" and account_id="%s" and transaction_date<="%s"' % ( current_user.id, account_id, future_date) transactions = models.Transaction.query.filter(qry).all() balance = 0 for transaction in transactions: balance = balance + transaction.amount qry = 'user_id="%s" and account_id="%s" and transaction_date<="%s"' % ( current_user.id, account_id, future_date) scheduled_transactions = models.ScheduledTransaction.query.filter( qry).all() income = 0 expense = 0 for scheduled in scheduled_transactions: if scheduled.amount > 0: income = income + scheduled.amount else: expense = expense + scheduled.amount other_transactions = item.get('transactions', []) other_amount = 0 for other in other_transactions: try: raw_date = other.get('transaction_date') dt = utils.to_date(raw_date) if dt <= utils.to_date(future_date): other_amount += Decimal(other.get('amount', 0)) except: pass amount = balance + income + expense + other_amount result = { 'balance': balance, 'income': income, 'expense': expense, 'transactions': other_amount, 'amount': amount } return view.response(fields=result, total=1) except Exception as e: return view.response(status=500, messages=[e.message])
def login(): try: item = json.loads(request.data) username = item.get('username', '') password = models.User.encrypt(item.get('password', '')) user = models.User.query.filter_by(username=username, password=password).first() login_user(user) return view.response() except Exception as e: models.session.rollback() return view.response(status=401, messages=[e.message])
def calculate(): try: item = json.loads(request.data) today = utils.from_date(date.today()) future_date = item.get('date', today) account_id = int(item.get('account', 0)) account = models.Account.query.filter_by(id=account_id).first() if not account: return view.response(status=404, messages=['Account not found.']) qry = 'user_id="%s" and account_id="%s" and transaction_date<="%s"' % (current_user.id, account_id, future_date) transactions = models.Transaction.query.filter(qry).all() balance = 0 for transaction in transactions: balance = balance + transaction.amount qry = 'user_id="%s" and account_id="%s" and transaction_date<="%s"' % (current_user.id, account_id, future_date) scheduled_transactions = models.ScheduledTransaction.query.filter(qry).all() income = 0 expense = 0 for scheduled in scheduled_transactions: if scheduled.amount > 0: income = income + scheduled.amount else: expense = expense + scheduled.amount other_transactions = item.get('transactions', []) other_amount = 0 for other in other_transactions: try: raw_date = other.get('transaction_date') dt = utils.to_date(raw_date) if dt <= utils.to_date(future_date): other_amount += Decimal(other.get('amount', 0)) except: pass amount = balance + income + expense + other_amount result = { 'balance': balance, 'income': income, 'expense': expense, 'transactions': other_amount, 'amount': amount } return view.response(fields=result, total=1) except Exception as e: return view.response(status=500, messages=[e.message])
def csv_importing(): try: f = request.files['filename'] temp_dir = current_user.token csv_file = os.path.join(tempfile.gettempdir(), temp_dir) f.save(csv_file) template = request.values.get('template') account_id = request.values.get('account') account = models.Account.query.filter_by(id=account_id).first() data = csv.create(file=csv_file, user=current_user, template=template, account=account) status = 500 if len(data) == 0 else 200 return view.response(status=status, fields=data) except Exception as e: return view.response(status=500, messages=[e.message])
def add_or_update_transaction(): try: items = json.loads(request.data) messages = [] for item in items: item.update({'user_id': current_user.id}) amount = Decimal(item.get('amount', '0')) item.update({'amount': amount}) transaction_date = utils.to_date(item.get('transaction_date')) item.update({'transaction_date': transaction_date}) view.save_item(models.Transaction, item) except Exception as e: messages.append(e.message) if len(messages) > 0: return view.response(status=500, messages=[e.message]) return view.response()
def list_scheduled_transactions(upcoming=False): try: qry = models.ScheduledTransaction.query.order_by('transaction_date').filter_by(user_id=current_user.id) if upcoming: today = date.today() next_month = today + relativedelta(months=+1) where_clausule = 'transaction_date >= "%s" and transaction_date <= "%s"' % (today, next_month) qry = qry.filter(where_clausule) items = qry.group_by('description').all() fields = [] if len(items) > 0: for item in items: fields.append(item.to_dict()) return view.response(fields=fields) except Exception, e: return view.response(status=500, messages=[e.message])
def list_scheduled_transactions(upcoming=False): try: qry = models.ScheduledTransaction.query.order_by( 'transaction_date').filter_by(user_id=current_user.id) if upcoming: today = date.today() next_month = today + relativedelta(months=+1) where_clausule = 'transaction_date >= "%s" and transaction_date <= "%s"' % ( today, next_month) qry = qry.filter(where_clausule) items = qry.group_by('description').all() fields = [] if len(items) > 0: for item in items: fields.append(item.to_dict()) return view.response(fields=fields) except Exception, e: return view.response(status=500, messages=[e.message])
def finish_scheduled_transaction(): try: item = json.loads(request.data) scheduled = models.ScheduledTransaction.query.filter_by(id=item.get('id'), user_id=current_user.id, key=item.get('key')).first() transaction = models.Transaction() transaction.description = item.get('description', scheduled.description) transaction.amount = Decimal(item.get('amount', scheduled.amount)) transaction.transaction_date = date.today() transaction.account_id = scheduled.account_id transaction.category_id = scheduled.category_id transaction.payee_id = scheduled.payee_id transaction.get_session.commit() scheduled.delete() return view.response(fields=[transaction.to_dict()]) except Exception, e: models.session.rollback() return view.response(status=500, messages=[e.message])
def register(): try: item = json.loads(request.data) username = item.get('username', '') if username == '': raise Exception(u'The username is blank') if models.User.query.filter_by(username=username).count() > 0: raise Exception(u'The username already exists') password = item.get('password', '') if len(password) < 5 or len(password) > 16: raise Exception(u'Password must be 6-16 characters') user = models.User(username='', password='') user.from_dict(item) user.get_session.commit() return view.response(total=1, fields={'username': user.username, 'token': user.token}) except Exception as e: models.session.rollback() return view.response(status=500, messages=[e.message])
def list_transactions(): qry = models.Transaction.query.order_by('transaction_date', 'description') if 'desc' == request.args.get('ordering', 'asc'): qry = models.Transaction.query.order_by(desc('transaction_date')) items = qry.filter_by(user_id=current_user.id).all() fields = [] if len(items) > 0: for item in items: fields.append(item.to_dict()) return view.response(fields=fields)
def finish_scheduled_transaction(): try: item = json.loads(request.data) scheduled = models.ScheduledTransaction.query.filter_by( id=item.get('id'), user_id=current_user.id, key=item.get('key')).first() transaction = models.Transaction() transaction.description = item.get('description', scheduled.description) transaction.amount = Decimal(item.get('amount', scheduled.amount)) transaction.transaction_date = date.today() transaction.account_id = scheduled.account_id transaction.category_id = scheduled.category_id transaction.payee_id = scheduled.payee_id transaction.get_session.commit() scheduled.delete() return view.response(fields=[transaction.to_dict()]) except Exception, e: models.session.rollback() return view.response(status=500, messages=[e.message])
def add_or_update_scheduled_transaction(): try: item = json.loads(request.data) # import ipdb; ipdb.set_trace() frequency = int(item.get('frequency', 1)) number_replications = int(item.get('number_replications', 1)) if frequency < 1 or number_replications < 1: return view.response( status=500, messages=[ 'The frequency and the number of replications have to be bigger than zero.' ]) unit_replications = item.get('unit_replications') transaction_date = utils.to_date( item.get('transaction_date', utils.from_date(date.today()))) for i in range(frequency): if i > 0: if unit_replications == 'weeks': transaction_date = transaction_date + relativedelta( weeks=+number_replications) elif unit_replications == 'months': transaction_date = transaction_date + relativedelta( months=+number_replications) elif unit_replications == 'years': transaction_date = transaction_date + relativedelta( years=+number_replications) item.update({'transaction_date': transaction_date}) item.update({'user_id': current_user.id}) view.save_item(models.ScheduledTransaction, item) return view.response(fields=item, total=frequency) except Exception as e: return view.response(status=500, messages=[e.message])
def logout(): logout_user() return view.response()
def is_authenticated(): return view.response()
def unauthorized(): from api.helpers import view return view.response(status=401, messages=['Access denied'])