def simulate_startup(): conn = mysql.connect() cursor = conn.cursor() t = Transaction(cursor, conn) ec = EmployeeCard(cursor, conn) for dept, e_list in client.department_employees.items(): dept_bal = client.retrieve_balance( dept).gpa.available_balance * (random.randint(1, 19) / 100) employees = random.sample(e_list, 5) for e in employees: transfer = client.transfer( dept_bal, dept, e, dest_token_is_user=True) t.insert(dept, e, Transaction.current_time( transfer.created_time), dept_bal) card = client.client_sdk.cards.list_for_user(e)[0].token mid_identifer = random.choice(MIDS) employee_transaction = simulate( card, amount=dept_bal * (random.randint(1, 19) / 100), mid=mid_identifer) t.insert(card, mid_identifer, Transaction.current_time( employee_transaction.created_time), employee_transaction.amount, is_card=True) ec.insert(e, card) conn.close()
def save_all_mutations(self): mutations = self.mutations update = Update.get_or_none(Update.id == 1) last_asked_transaction_timestamp = update.last_transaction if update is not None else 0 print(last_asked_transaction_timestamp) last_timestamp = last_asked_transaction_timestamp for mutation in mutations['mutationsList']['mutations'][::-1]: mutation = mutation['mutation'] # Trim the mutation time so we won't bother with microsecond comparisons mutation_time = datetime.strptime( mutation['transactionTimestamp'], '%Y%m%d%H%M%S%f').replace(microsecond=0) timestamp = mutation_time.timestamp() if timestamp > last_asked_transaction_timestamp: last_timestamp = last_timestamp if last_timestamp > timestamp else timestamp is_debit = mutation['amount'] < 0 amount = "{0:.2f}".format( mutation['amount'] * -1 if is_debit else mutation['amount']) Transaction.insert( amount=amount, is_debit=is_debit, description=mutation['counterAccountName'], time=mutation_time).on_conflict('ignore').execute() if update is not None: update.update(last_transaction=last_timestamp).execute() else: Update.create(last_transaction=last_timestamp)
def dept_to_emp(plan_id): conn = mysql.connect() cursor = conn.cursor() query = 'SELECT * FROM plan WHERE id = %s' cursor.execute(query, plan_id) records = cursor.fetchall() funding_amount = records[0][2] source_fund_FK = records[0][7] query = 'SELECT token FROM department_lookup WHERE id = %s' cursor.execute(query, source_fund_FK) source_token = cursor.fetchall()[0][0] query = 'SELECT * FROM employee_plan WHERE ep_plan_FK = %s' cursor.execute(query, plan_id) records = cursor.fetchall() t = Transaction(cursor, conn) for row in records: query = 'SELECT token FROM employee WHERE id = %s' cursor.execute(query, row[0]) dest_token = cursor.fetchall()[0][0] transfer = client.transfer(funding_amount, source_token, dest_token, dest_token_is_user=True) t.insert(source_token, dest_token, Transaction.current_time(transfer.created_time), funding_amount) conn.commit() conn.close() complete_employee_plan(plan_id) simulate_employee_plan(plan_id)
def transaction_create(payload): body = request.get_json() new_drink_id = body.get('drink_id') new_quantity = body.get('quantity') new_created_at = body.get('created_at') # check input if new_drink_id is None or new_quantity is None or new_created_at is None: abort(400) drink = Drink.query.get(new_drink_id) if drink is None or drink.quantity < new_quantity: abort(400) t = datetime.datetime.strptime(new_created_at, "%Y-%m-%d") trans = Transaction(new_drink_id, new_quantity, t) trans.insert() drink.quantity -= new_quantity drink.update() return jsonify({ 'success': True, 'transaction': trans.format(), 'drink': drink.name })
def add_transaction(): body = request.get_json() item = body.get('item', None) price = body.get('price', None) buyer_id = body.get('buyer_id', None) borrower_id = body.get('borrower_id', None) group_id = body.get('group_id', None) # try: new_transaction = Transaction(item=item, price=price, buyer_id=buyer_id, borrower_id=borrower_id, group_id=group_id) new_transaction.insert() # except: # abort(422) buyer = User.query.filter_by(id=buyer_id).one() #Updates the user debts if not group_id: price = (price / 2) update_individual_transaction(buyer_id, borrower_id, price) else: update_group_transaction(buyer_id, group_id, price) return jsonify({ 'success': True, 'transaction': new_transaction.format() })
def load_values(): conn = mysql.connect() cursor = conn.cursor() print('\n\nINITIALIZING DB\n\n') query1 = """ INSERT INTO department_lookup (token, department) VALUES (%s,%s)""" for i, dept in enumerate(client.departments): cursor.execute(query1, (dept.token, client.DEPARTMENT_LIST[i])) print(dept.token + client.DEPARTMENT_LIST[i] + ' has been inserted.', file=stderr) emp = Employee(cursor, conn, immediate_commit=False) for e in client.employees: emp.insert(e.token, e.first_name, e.last_name, e.parent_token) print(e.token + 'h has been inserted.', file=stderr) desc = "My primary role is managing different banking sectors involved in asset management, sanctioning loans, " \ "mortgages, investments, and account operations. I oversee the efficient day to day processes as well as " \ "preparing forecasts and reports to drive the overall success of our clients and the department. " man = Manager(cursor, conn, immediate_commit=False) for dept in client.DEPARTMENT_LIST: man.insert(client.MANAGERS[dept]['email'], client.MANAGERS[dept]['pass'], client.MANAGERS[dept]['first_name'], client.MANAGERS[dept]['last_name'], 'Sr. Division Manager', desc, client.MANAGERS[dept]['manager_dept_FK'], client.MANAGERS[dept]['gender']) trans = Transaction(cursor, conn, immediate_commit=False) for t in client.transactions: if t.recipient_user_token is None: trans.insert( t.sender_business_token, t.recipient_business_token, Transaction.current_time(t.created_time), t.amount, ) else: trans.insert( t.sender_business_token, t.recipient_user_token, Transaction.current_time(t.created_time), t.amount, ) conn.commit() conn.close() simulate_startup() session['db_init'] = True create_background_scheduler()
def dept_to_dept(plan_id): """ for department to department transfers, gathers info from plan in db and enters it into Marqet API param: plan_id - the id of the plan in db to submit returns: 1 on success 0 on faliure """ conn = mysql.connect() cursor = conn.cursor() query = 'SELECT * FROM plan WHERE id = %s' cursor.execute(query, plan_id) records = cursor.fetchall() funding_amount = records[0][2] source_fund_FK = records[0][7] dest_fund_FK = records[0][8] query = 'SELECT token FROM department_lookup WHERE id = %s' cursor.execute(query, source_fund_FK) source_token = cursor.fetchall()[0][0] query = 'SELECT token FROM department_lookup WHERE id = %s' cursor.execute(query, dest_fund_FK) dest_token = cursor.fetchall()[0][0] transfer = client.transfer(funding_amount, source_token, dest_token, dest_token_is_user=False) t = Transaction(cursor, conn=conn) t.insert(source_token, dest_token, Transaction.current_time(transfer.created_time), funding_amount) query = 'UPDATE plan SET complete = 1 WHERE id = %s' cursor.execute(query, plan_id) conn.commit() conn.close()
def insert_transaction(payload): print(payload) body = request.get_json() time = body.get('time_of_transaction') amount = body.get('amount') game_id = body.get('game_id') customer_id = body.get('customer_id') if (time is None) or (amount is None) or (game_id is None) or ( customer_id is None): abort(422) review = body.get('review', None) game = Game.query.filter(Game.id == game_id).one_or_none() customer = Customer.query.filter( Customer.id == customer_id).one_or_none() if (game is None) or (customer is None): abort(422) transaction = Transaction( time_of_transaction=time, amount=amount, review=review, game=game, customer=customer) try: transaction.insert() return retrieve_transactions() except Exception: abort(422)
def simulate_employee_plan(plan_id): conn = mysql.connect() cursor = conn.cursor() query = ''' SELECT e.token, ep_card_token FROM employee_plan ep JOIN employee e ON ep.ep_employee_FK = e.id WHERE ep_plan_FK = %s''' t = Transaction(cursor, conn=conn) # THIS WILL RETURN ALL EMPLOYEES AND THEIR ASSOCIATED CARDS WITH AN ACCORDING PLAN cursor.execute(query, (plan_id)) for employee_card_pair in cursor.fetchall(): mid_identifer = random.choice(MIDS) employee_token = employee_card_pair[0] card_token = employee_card_pair[1] # NEED TO FIND BALANCE OF THE USER AND TIMES THAT BY SOME PERCENTAGE e_balance = client.retrieve_balance( employee_token).gpa.available_balance * .1 employee_transaction = simulate( card_token, amount=e_balance, mid=mid_identifer) t.insert(card_token, mid_identifer, Transaction.current_time( employee_transaction.created_time), employee_transaction.amount, is_card=True) conn.close()
def get_and_save_transactions(self): for is_debit in [True, False]: result = self.service.spreadsheets().values().get( spreadsheetId=self.config['id'], range=self.config['expenses_range'] if is_debit else self.config['income_range']).execute() for transaction in result['values']: time = datetime.strptime(transaction[0], self.config['date_format']) description = transaction[1] amount = float(transaction[3].replace(',', '.') if self. config['use_comma'] else transaction[3]) Transaction.insert( time=time, description=description, amount=amount, is_debit=is_debit, asked=True, in_drive=True).on_conflict('replace').execute( ) # Using insert as we do not need the model
def setUp(self): """Define test variables and initialize app.""" self.app = create_app() self.client = self.app.test_client self.database_name = "lending_test" self.database_path = 'postgres://davidhunter@localhost:5432/lending_test' #"postgres://{}/{}".format('localhost:5432', self.database_name) setup_db(self.app) self.new_user1 = { 'name': 'Jedediah' } self.new_user2 = { 'name': 'Barbara' } self.new_group = { 'name': 'house', 'users': [1,2] } self.new_transaction = { 'item':'milk', 'price': 3, 'buyer_id': 1, 'group_id': 1 } user_1 = User(name="Jedidiah", total_owed=0, outstanding=json.dumps({})) user_2 = User(name="Barbara", total_owed=0, outstanding=json.dumps({})) user_1.insert() user_2.insert() group_1 = Group(name="house") group_1.insert() group_1.people.append(user_1) group_1.people.append(user_2) group_1.update() transaction_1 = Transaction(item="milk", price=3, buyer_id=1, group_id=1) transaction_2 = Transaction(item="milk", price=3, buyer_id=1, group_id=1) transaction_3 = Transaction(item="milk", price=3, buyer_id=1, group_id=1) transaction_1.insert() transaction_2.insert() transaction_3.insert() with self.app.app_context(): self.db = SQLAlchemy() self.db.init_app(self.app) self.db.create_all()