Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from database_stuff import DB, MTG, mtgcursor, sqliteconnection
- from database_stuff import sqlitecursor
- from mkm import mkm_api
- from tricks import tech
- import copy
- import sys
- def gather_all_orders():
- def test_build_sqlite_table(cls, table, eachorder, eachkey):
- try:
- getattr(cls, eachkey)
- tmp_key = db_sqlite(table, eachkey)
- return tmp_key
- except AttributeError:
- if eachorder[eachkey] == None or eachorder[eachkey] == "":
- return False
- elif type(eachorder[eachkey]) == float:
- tmp_key = db_sqlite(table, eachkey, 'FLOAT')
- elif type(eachorder[eachkey]) == int or type(eachorder[eachkey]) == bool:
- tmp_key = db_sqlite(table, eachkey, 'INTEGER')
- elif type(eachorder[eachkey]) == str:
- tmp_key = db_sqlite(table, eachkey, 'TEXT')
- else:
- return False
- setattr(cls, eachkey, tmp_key)
- print('Created new column in order_head ->', eachkey, type(eachorder[eachkey]))
- return tmp_key
- def value_filler(head_values_alpha, eachorder, eachkey, table):
- """
- takes a list with values and add new values into that list before returning it, also: DB.class is updated
- :param head_values_alpha: this list comes in and goes back
- :param eachorder: MKM dictionary
- :param eachkey: specified key for MKM dictionary
- :param table: sqlite table
- :return: head_values_alpha
- """
- typelist = [str, int, float, bool]
- if type(eachorder[eachkey]) not in typelist:
- return head_values_alpha
- rv_key = tech.test_build_sqlite_table(DB.order_head, table, eachorder, eachkey)
- if rv_key == False:
- return head_values_alpha
- else:
- _, head_values = tech.empty_insert_query(sqlitecursor, table)
- head_values[rv_key] = eachorder[eachkey]
- for head_count in range(len(head_values_alpha)):
- if type(head_values_alpha[head_count]) in typelist and head_values_alpha[head_count] != "":
- head_values[head_count] = head_values_alpha[head_count]
- return head_values
- def create_values_input(**kwargs):
- """
- takes all keys from order.json at each level and sets those keys via value_filler
- if root present i kwargs tree is scanned for keys, then list of branches and leafs
- :return: appends lists into list and return list-list
- """
- rvlist = []
- order = kwargs['order']
- branch = kwargs['branch']
- leaf = kwargs['leaf']
- table = kwargs['table']
- c = {'tree': order, 'bransch': branch, 'leaf': leaf}
- if 'root' in kwargs:
- _, head_values_alpha = tech.empty_insert_query(sqlitecursor, table)
- allkeys = order.keys()
- for key in allkeys:
- head_values_alpha = value_filler(head_values_alpha, order, key, table)
- rvlist.append(head_values_alpha)
- for level in c['bransch']:
- _, head_values_alpha = tech.empty_insert_query(sqlitecursor, table)
- allkeys = c['tree'][level].keys()
- for key in allkeys:
- head_values_alpha = value_filler(head_values_alpha, c['tree'][level], key, table)
- for leaf in c['leaf']:
- allkeys = c['tree'][level][leaf].keys()
- for key in allkeys:
- head_values_alpha = value_filler(head_values_alpha, c['tree'][level][leaf], key, table)
- rvlist.append(head_values_alpha)
- return rvlist
- def extract_user(eachorder):
- """
- takes both seller and buyer from order.json and store
- their user details into a mkm_user database
- :param eachorder: order.json dictionary
- """
- rvlist = create_values_input(order=eachorder, branch=['seller', 'buyer'], leaf=['name', 'address'], table='mkm_user')
- for head_values_alpha in rvlist:
- query, _ = tech.empty_insert_query(sqlitecursor, 'mkm_user')
- sqlitecursor.execute('select * from mkm_user where idUser = (?)', (head_values_alpha[DB.mkm_user.idUser],))
- data = sqlitecursor.fetchone()
- if data != None:
- with sqliteconnection:
- sqlitecursor.execute('delete from mkm_user where idUser = (?)', (head_values_alpha[DB.mkm_user.idUser],))
- data = list(data)
- for column in range(len(head_values_alpha)):
- if head_values_alpha[column] != None:
- data[column] = head_values_alpha[column]
- head_values_alpha = data
- with sqliteconnection:
- sqlitecursor.execute(query, head_values_alpha)
- def make_order_head(eachorder):
- """
- makes an order header from order.json
- :param eachorder: order.json dictionary
- """
- try:
- eachorder['shippingMethod']['shippingMethod'] = eachorder['shippingMethod']['name']
- eachorder['shippingMethod'].pop('name')
- except KeyError:
- pass
- rvlist = create_values_input(order=eachorder, branch=['state', 'shippingMethod', 'shippingAddress'], leaf=[], table='order_head', root=True)
- head_values_alpha = rvlist[-1]
- for count in range(0, len(rvlist)-1):
- for count_count in range(len(rvlist[count])):
- if rvlist[count][count_count] != None:
- head_values_alpha[count_count] = rvlist[count][count_count]
- head_values_alpha[DB.order_head.seller.idUser_seller] = eachorder['seller']['idUser']
- head_values_alpha[DB.order_head.buyer.idUser_buyer] = eachorder['buyer']['idUser']
- with sqliteconnection:
- head_query_alpha, _ = tech.empty_insert_query(sqlitecursor, 'order_head')
- sqlitecursor.execute(head_query_alpha, head_values_alpha)
- def make_orders(rv):
- query, values = tech.empty_insert_query(sqlitecursor, 'orders')
- executemanylist = []
- cycle = {
- DB.orders.idProduct: ['idProduct', int],
- DB.orders.language: ['language', 'languageName', str],
- DB.orders.comments: ['comments', str],
- DB.orders.price: ['price', float],
- DB.orders.condition: ['condition', str],
- DB.orders.foil: ['isFoil', bool],
- DB.orders.amount: ['count', int],
- }
- if 'order' not in rv:
- print("Error: found no orders in JSON!")
- return
- for eachorder in rv['order']:
- if 'article' in eachorder and 'idOrder' in eachorder:
- sqlitecursor.execute('select * from order_head where idOrder = (?)', (eachorder['idOrder'],))
- data = sqlitecursor.fetchone()
- if data != None:
- continue
- else:
- continue
- make_order_head(eachorder)
- extract_user(eachorder)
- values[DB.orders.idOrder] = eachorder['idOrder']
- for eacharticle in eachorder['article']:
- _values = copy.copy(values)
- for dbkey, v in cycle.items():
- vvv = copy.copy(eacharticle)
- for checkcount in range(0, len(v)-1):
- vv = v[checkcount]
- try: vvv = vvv[vv]
- except KeyError: pass
- if type(vvv) == v[-1] and vvv != "":
- _values[dbkey] = vvv
- if 'isPlayset' in eacharticle:
- if eacharticle['isPlayset'] == True and type(_values[DB.orders.amount]) == int:
- _values[DB.orders.amount] = _values[DB.orders.amount] * 4
- if type(_values[DB.orders.condition]) == str:
- for grade in range(7):
- if DB.grade(grade)[0] == _values[DB.orders.condition]:
- _values[DB.orders.condition] = grade
- if _values[DB.orders.foil] == True:
- _values[DB.orders.foil] = 1
- else:
- _values[DB.orders.foil] = 0
- if _values[DB.orders.idProduct] != None:
- mtgcursor.execute('select * from cards where mcmId = (?)', (_values[DB.orders.idProduct],))
- mtgjsondata = mtgcursor.fetchone()
- if mtgjsondata != None:
- _values[DB.orders.uuid] = mtgjsondata[MTG.cards.uuid]
- executemanylist.append(tuple(_values))
- if len(executemanylist) > 0:
- with sqliteconnection:
- sqlitecursor.executemany(query, executemanylist)
- catchrange = 1
- rv = mkm_api('orders/buyer/received/' + str(catchrange))
- if rv != False:
- make_orders(rv)
Advertisement
Add Comment
Please, Sign In to add comment