def saleschannels(type, stores): """ Type is either new or update """ if type == "new": query = dbq.insert_sales_channels get_query = dbq.get_new_instance_notes date_field = "insert_date" elif type == "update": query = dbq.update_sales_channels get_query = dbq.get_updated_instance_notes date_field = "update_date" db_instance_notes = dbq.exec_db_query_dict(get_query, qty="all") for inst_idx in range(len(db_instance_notes)): channels = {} notes = list(eval(db_instance_notes[inst_idx]['notes'])) for notes_idx in range(len(notes)): store = dict_switcher(notes[notes_idx]['field_id'], stores) if store != None: notes[notes_idx]['value'] channels[store] = notes[notes_idx]['value'] query_data = { 'instance_id': db_instance_notes[inst_idx]['instance_id'], 'sales_channels': str(channels), date_field: datetime.now().strftime('%Y-%m-%d %H:%M:%S') } dbq.exec_db_query(query, query_data, query_type='insert')
def getcustomfields(): """ Grab the discogs custom fields and insert/update the db """ for idx in range(len(user.collection_fields)): query = None # Check field table for field db_instance = dbq.exec_db_query_dict(dbq.get_field, user.collection_fields[idx].id) if db_instance == None: query = dbq.custom_field_insert query_data = { 'field_id': user.collection_fields[idx].id, 'field_name': user.collection_fields[idx].name, 'insert_date': datetime.now().strftime('%Y-%m-%d %H:%M:%S') } elif db_instance['field_name'] != user.collection_fields[idx].name: query = dbq.custom_field_update query_data = { 'field_id': user.collection_fields[idx].id, 'field_name': user.collection_fields[idx].name, 'update_date': datetime.now().strftime('%Y-%m-%d %H:%M:%S') } if query != None: dbq.exec_db_query(query, query_data, query_type='insert')
def check_db_version(): try: test = dbq.exec_db_query_dict(dbq.get_db_version, db_version) except NameError: print ('Check your config.py file for db_version') sys.exit(10) else: if test is None: print("Update the database to version: " + str(db_version)) sys.exit(10)
def process_products(type, db_products): """ Take list of new instance id's, create product page and update instance table with Woo ID """ if type == "new": query = dbq.insert_woo_instance_product date_field = "insert_date" elif type == "update": query = dbq.update_woo_instance_product date_field = "update_date" for idx in range(len(db_products)): # Get instance data from db instance_data = dbq.exec_db_query_dict(dbq.get_instance_info, db_products[idx]['instance_id']) dov_sales_status = eval(dbq.exec_db_query_dict(dbq.get_sales_channels, db_products[idx]['instance_id'])['sales_channels'])['Sell on DoV'] # Get release data from Discogs # release_data = dbq.exec_db_query_dict(dbq.get_release_info, instance_data['release_id']) release_data = discogs.release(db_products[idx]['release_id']) # Send data to formater product_data = formatproduct(instance_data, release_data, dov_sales_status, type) # Create Woo Product if type == "new": product_endpoint = "products" elif type == "update": product_endpoint = ("products/" + str(db_products[idx]['woo_id'])) try: woo_product = wcapi.post(product_endpoint, product_data).json() except: print("Instance ID: " + str(db_products[idx]['instance_id']) + " threw an error during a " + type + " product operation talking to Woo. ") continue # Update DB with Woo Product ID query_data = {"woo_id": woo_product['id'], "instance_id": db_products[idx]['instance_id'], date_field: datetime.now().strftime('%Y-%m-%d %H:%M:%S')} dbq.exec_db_query(query, query_data, query_type='insert')
def process_woo(): check_db_version() run_id = dblog.startup(process_name) """ TODO: Remove this section Attribute terms can be created on the fly and will match existing # Manually create attribute name # Get woo attributes, name to id mapping woo_attributes_list = get_woo_attributes_list() # Genres to attribute terms update_attrib_term_list('genres', woo_attributes_list['Genres']) # Styles to attribute terms update_attrib_term_list('styles', woo_attributes_list['Styles']) """ # Get category ids global categories categories = get_woo_categories() # Get attrib id dict. global attrib_ids attrib_ids = get_attrib_ids() # create new products db_new_products = dbq.exec_db_query_dict(dbq.get_new_woo_instances, qty="all") process_products("new", db_new_products) # Update existing products db_update_products = dbq.exec_db_query_dict(dbq.get_update_woo_instances, qty="all") process_products("update", db_update_products) # TODO: group multiple products? SELECT release_id, title, COUNT(*) copies FROM dov_discogs_instances GROUP BY release_id HAVING copies > 1; # TODO: Calculate and set pricing # TODO: update / reactivate existing products # TODO: Cross/Up sell items # TODO: Group items # TODO: deactivate removed products # TODO: Sold products Woo -> Discogs dblog.finished(run_id)
def update_attrib_term_list(attrib_name, attrib_id): """ Passed the Woo attribute type and woo id, query attribute table for new terms and populate Woo """ query_data = attrib_name query = dbq.woo_get_new_attribs new_terms = dbq.exec_db_query_dict(query, query_data, qty="all") for idx in range(len(new_terms)): data = {"name": new_terms[idx]['attrib_term']} created = wcapi.post("products/attributes/" + str(attrib_id) + "/terms", data).json() query_data = {'woo_attrib_id': created["id"], 'update_date': datetime.now().strftime('%Y-%m-%d %H:%M:%S'), 'id': new_terms[idx]['id'] } query = dbq.update_attribs_woo_id dbq.exec_db_query(query, query_data, query_type='insert')
def get_stores(): stores = {} store_fields = dbq.exec_db_query_dict(dbq.get_store_fields, qty="all") for idx in range(len(store_fields)): stores[store_fields[idx]['field_id']] = store_fields[idx]['field_name'] return stores
def discogsImport(discogs_folder): """ Imports discogs collections to table """ # Set collection collection = user.collection_folders # Populate import table for album in collection[discogs_folder].releases: query = None # Concatenate notes hashing_note = None if album.notes != None: for idx in range(len(album.notes)): hashing_note = str(hashing_note) + str( album.notes[idx]['field_id']) + str( album.notes[idx]['value']) # Hash the notes notes_chksum = hashNotes(hashing_note) # Query instance table for instance db_instance = dbq.exec_db_query_dict(dbq.get_instance_info, album.instance_id) # New items if db_instance == None: # Build insert data query_data = { 'instance_id': album.instance_id, 'rating': album.rating, 'title': album.release.title, 'folder_id': album.folder_id, 'discogs_date_added': album.date_added, 'notes': str(album.notes), 'notes_chksum': notes_chksum.hexdigest(), 'release_id': album.id, 'insert_date': datetime.now().strftime('%Y-%m-%d %H:%M:%S') } query = dbq.add_instance dbq.exec_db_query(query, query_data, query_type='insert') # Test for existing and changed elif db_instance['instance_id'] == album.instance_id and \ (db_instance['notes_chksum'] != notes_chksum.hexdigest() or db_instance['folder_id'] != album.folder_id or db_instance['release_id'] != album.id ): # Update notes if hash is different if db_instance['notes_chksum'] != notes_chksum.hexdigest(): query_data = { 'notes': str(album.notes), 'notes_chksum': notes_chksum.hexdigest(), 'update_date': datetime.now().strftime('%Y-%m-%d %H:%M:%S'), 'instance_id': album.instance_id, 'release_id': album.id } query = dbq.update_instance_notes_chksum dbq.exec_db_query(query, query_data, query_type='insert') # Update folder id if db_instance['folder_id'] != album.folder_id: query_data = { 'folder_id': album.folder_id, 'update_date': datetime.now().strftime('%Y-%m-%d %H:%M:%S'), 'instance_id': album.instance_id } query = dbq.update_instance_folder_id dbq.exec_db_query(query, query_data, query_type='insert') if db_instance['release_id'] != album.id: query_data = { 'release_id': album.id, 'instance_id': album.instance_id } query = dbq.update_instance_release_id dbq.exec_db_query(query, query_data, query_type='insert')