def save_category_options(): try: data = request.form # Setting category limit limit_attributes = { 'user_id': g.user.user_id, 'category_id': int(data['cat']), 'value': float(data.get('limit', 0) or 0), } limit = Limit.get(user_id=g.user.user_id, category_id=limit_attributes['category_id']) if data.get('setLimit') and limit_attributes['value'] > 0: if limit is None: add_to_db(Limit(), **limit_attributes) else: limit.value = limit_attributes['value'] db_session.commit() else: if limit is not None: db_session.delete(limit) db_session.commit() return HttpResponse('Options saved!', status=200) except BadRequestKeyError as e: return HttpErrorResponse(e, 'Unable to save the options.', status=400)
def dummy_google_user_info(request): user_info = { 'at_hash': 'KW9YqqKkO214I35VTdGQZw', 'aud': '567386675874-5fqmolj2dt7cu9g2dam7cheq7k923p8q.apps.googleusercontent.com', 'azp': '567386675874-5fqmolj2dt7cu9g2dam7cheq7k923p8q.apps.googleusercontent.com', 'iss': 'accounts.google.com', 'iat': 1485265757, 'exp': 1485269357, 'sub': '107161240925642157925', 'picture': 'https://lh4.googleusercontent.com/-zm0SY3XlTgY/AAAAAAAAAAI/AAAAAAAACyM/QqXcAAfeajU/s96-c/photo.jpg', 'email': '*****@*****.**', 'email_verified': True, 'locale': 'en-CA', 'given_name': 'Test', 'family_name': 'Dummy', 'name': 'Test Dummy' } user = User.get(google_id=user_info['sub']) if user is not None: db_session.delete(user) db_session.commit() def teardown(): user = User.get(google_id=user_info['sub']) db_session.delete(user) db_session.commit() request.addfinalizer(teardown) return user_info
def link_keyword_to_category(): keyword = Keyword.get(keyword_id=request.values['keywordId']) category_id = request.values['categoryId'] keyword.categories = [Category.get( category_id=category_id)] if category_id != '-1' else [] db_session.commit() return HttpResponse('Keyword {0} assigned to category {1}'.format( keyword.keyword_id, category_id))
def delete_keywords(): ids = request.values['keywordIds'].split(',') keywords = Keyword.get_all(Keyword.keyword_id.in_(ids)) for keyword in keywords: keyword.categories = [] db_session.delete(keyword) db_session.commit() return HttpResponse('Keywords deleted.')
def get_current_user(): g.user = getattr(g, 'user', User.get(email=session['email']) if session.get('email') else None) if g.user is not None: g.user.heartbeat = datetime.now() db_session.commit() if '127.0.0.1' in request.host: # In order for Google Sign in to works return redirect(request.url.replace('127.0.0.1', 'localhost'))
def set_keywords(): data = request.get_json(silent=True) for key, values in data['keywords'].items(): keyword = Keyword.get(keyword_id=key) description = keyword.description for i in range(len(values)): keyword.value = values[i] db_session.commit() if i != len(values) - 1: # Creating new keyword for multiple values keyword = set_attributes(Keyword(), description=description) db_session.add(keyword) return HttpResponse('Keywords set!')
def transactions_fetcher(): truncate = lambda data, max: (data[:max - 3] + '...') if len( data) > max - 3 else data transactions_count = 0 transactions = json.loads(request.form['transactions']) for transaction in transactions: try: # Getting fields attributes = { 'user': g.user, 'bank': Bank.get(name=transaction.get('bank')), 'description': truncate(transaction.get('desc', ''), 256), 'amount': float(transaction.get('amount', '0').replace(',', '.') or 0), 'date': datetime.strptime(transaction.get('date'), '%d-%m-%Y') if transaction.get('date') else datetime.today() } # Fields validation if not attributes['description'] or not attributes['bank']: continue if attributes['date'] > datetime.today(): attributes['date'] = datetime.today() # Trying to find a category category = Categorizer.find_category(attributes['description']) # Creating transaction db_transaction = Transaction.get(**attributes) if db_transaction is None: db_transaction = add_to_db(Transaction(), **attributes) transactions_count += 1 # Finding or updating category if category is not None: db_transaction.category = category db_session.commit() except Exception as e: pass # Skip this transaction return HttpResponse('Transactions fetched.', {'count': transactions_count}, status=200)
def update_user(google_user_info): """Update user using Google id token. Create a new user if it does not exist.""" attributes = { 'first_name': google_user_info['given_name'], 'last_name': google_user_info['family_name'], 'picture': google_user_info['picture'], 'google_id': google_user_info['sub'] } user = User.get(google_id=attributes['google_id']) if user is None: user = User.get(email=google_user_info['email']) if user is None: attributes['email'] = google_user_info['email'] user = User() db_session.add(user) user.heartbeat = datetime.now() set_attributes(user, **attributes) db_session.commit() return user
def teardown(): user = User.get(google_id=user_info['sub']) db_session.delete(user) db_session.commit()